UNPKG

5.2 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { _SPCollection, spInvokableFactory, _SPInstance, SPCollection, } from "../spqueryable.js";
3import { spPost } from "../operations.js";
4import { odataUrlFrom } from "../utils/odata-url-from.js";
5import { extractWebUrl } from "../utils/extract-web-url.js";
6import { File } from "../files/types.js";
7import { combine } from "@pnp/core";
8import { defaultPath } from "../decorators.js";
9function getAppCatalogPath(base, path) {
10 const paths = ["_api/web/tenantappcatalog/", "_api/web/sitecollectionappcatalog/"];
11 for (let i = 0; i < paths.length; i++) {
12 const index = base.indexOf(paths[i]);
13 if (index > -1) {
14 return combine(base.substring(index, index + paths[i].length), path);
15 }
16 }
17 return combine(base, path);
18}
19let _AppCatalog = class _AppCatalog extends _SPCollection {
20 constructor(base, path) {
21 super(base, null);
22 this._url = combine(extractWebUrl(this._url), path);
23 }
24 /**
25 * Get details of specific app from the app catalog
26 * @param id - Specify the guid of the app
27 */
28 getAppById(id) {
29 return App(this, `getById('${id}')`);
30 }
31 /**
32 * Synchronize a solution to the Microsoft Teams App Catalog
33 * @param id - Specify the guid of the app
34 * @param useSharePointItemId (optional) - By default this REST call requires the SP Item id of the app, not the app id.
35 * PnPjs will try to fetch the item id, you can still use this parameter to pass your own item id in the first parameter
36 */
37 async syncSolutionToTeams(id, useSharePointItemId = false) {
38 // This REST call requires that you refer the list item id of the solution in the app catalog site.
39 let appId = null;
40 const webUrl = combine(extractWebUrl(this.toUrl()), "_api/web");
41 if (useSharePointItemId) {
42 appId = id;
43 }
44 else {
45 const listId = (await SPCollection([this, webUrl], "lists").select("Id").filter("EntityTypeName eq 'AppCatalog'")())[0].Id;
46 const listItems = await SPCollection([this, webUrl], `lists/getById('${listId}')/items`).select("Id").filter(`AppProductID eq '${id}'`).top(1)();
47 if (listItems && listItems.length > 0) {
48 appId = listItems[0].Id;
49 }
50 else {
51 throw Error(`Did not find the app with id ${id} in the appcatalog.`);
52 }
53 }
54 return spPost(AppCatalog(this, getAppCatalogPath(this.toUrl(), `SyncSolutionToTeams(id=${appId})`)));
55 }
56 /**
57 * Uploads an app package. Not supported for batching
58 *
59 * @param filename Filename to create.
60 * @param content app package data (eg: the .app or .sppkg file).
61 * @param shouldOverWrite Should an app with the same name in the same location be overwritten? (default: true)
62 * @returns Promise<IAppAddResult>
63 */
64 async add(filename, content, shouldOverWrite = true) {
65 // you don't add to the availableapps collection
66 const adder = AppCatalog(this, getAppCatalogPath(this.toUrl(), `add(overwrite=${shouldOverWrite},url='${filename}')`));
67 const r = await spPost(adder, {
68 body: content, headers: {
69 "binaryStringRequestBody": "true",
70 },
71 });
72 return {
73 data: r,
74 file: File([this, odataUrlFrom(r)]),
75 };
76 }
77};
78_AppCatalog = __decorate([
79 defaultPath("_api/web/tenantappcatalog/AvailableApps")
80], _AppCatalog);
81export { _AppCatalog };
82export const AppCatalog = spInvokableFactory(_AppCatalog);
83export class _App extends _SPInstance {
84 /**
85 * This method deploys an app on the app catalog. It must be called in the context
86 * of the tenant app catalog web or it will fail.
87 *
88 * @param skipFeatureDeployment Deploy the app to the entire tenant
89 */
90 deploy(skipFeatureDeployment = false) {
91 return this.do(`Deploy(${skipFeatureDeployment})`);
92 }
93 /**
94 * This method retracts a deployed app on the app catalog. It must be called in the context
95 * of the tenant app catalog web or it will fail.
96 */
97 retract() {
98 return this.do("Retract");
99 }
100 /**
101 * This method allows an app which is already deployed to be installed on a web
102 */
103 install() {
104 return this.do("Install");
105 }
106 /**
107 * This method allows an app which is already installed to be uninstalled on a web
108 * Note: when you use the REST API to uninstall a solution package from the site, it is not relocated to the recycle bin
109 */
110 uninstall() {
111 return this.do("Uninstall");
112 }
113 /**
114 * This method allows an app which is already installed to be upgraded on a web
115 */
116 upgrade() {
117 return this.do("Upgrade");
118 }
119 /**
120 * This method removes an app from the app catalog. It must be called in the context
121 * of the tenant app catalog web or it will fail.
122 */
123 remove() {
124 return this.do("Remove");
125 }
126 do(path) {
127 return spPost(App(this, path));
128 }
129}
130export const App = spInvokableFactory(_App);
131//# sourceMappingURL=types.js.map
\No newline at end of file