1 | import { __decorate } from "tslib";
|
2 | import { isUrlAbsolute, combine, noInherit } from "@pnp/core";
|
3 | import { body, cancelableScope } from "@pnp/queryable";
|
4 | import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPInstance, } from "../spqueryable.js";
|
5 | import { odataUrlFrom } from "../utils/odata-url-from.js";
|
6 | import { Item } from "../items/types.js";
|
7 | import { defaultPath } from "../decorators.js";
|
8 | import { spPost, spPostMerge } from "../operations.js";
|
9 | import { extractWebUrl } from "../utils/extract-web-url.js";
|
10 | import { toResourcePath } from "../utils/to-resource-path.js";
|
11 | import { encodePath } from "../utils/encode-path-str.js";
|
12 | import "../context-info/index.js";
|
13 | import { BatchNever } from "../batching.js";
|
14 | let _Folders = class _Folders extends _SPCollection {
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 | getByUrl(name) {
|
21 | return Folder(this).concat(`('${encodePath(name)}')`);
|
22 | }
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | async addUsingPath(serverRelativeUrl, overwrite = false) {
|
30 | const data = await spPost(Folders(this, `addUsingPath(DecodedUrl='${encodePath(serverRelativeUrl)}',overwrite=${overwrite})`));
|
31 | return {
|
32 | data,
|
33 | folder: folderFromServerRelativePath(this, data.ServerRelativeUrl),
|
34 | };
|
35 | }
|
36 | };
|
37 | _Folders = __decorate([
|
38 | defaultPath("folders")
|
39 | ], _Folders);
|
40 | export { _Folders };
|
41 | export const Folders = spInvokableFactory(_Folders);
|
42 | export class _Folder extends _SPInstance {
|
43 | constructor() {
|
44 | super(...arguments);
|
45 | this.delete = deleteableWithETag();
|
46 | }
|
47 | |
48 |
|
49 |
|
50 |
|
51 | get folders() {
|
52 | return Folders(this);
|
53 | }
|
54 | |
55 |
|
56 |
|
57 |
|
58 | get listItemAllFields() {
|
59 | return SPInstance(this, "listItemAllFields");
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 | get parentFolder() {
|
66 | return Folder(this, "parentFolder");
|
67 | }
|
68 | |
69 |
|
70 |
|
71 |
|
72 | get properties() {
|
73 | return SPInstance(this, "properties");
|
74 | }
|
75 | |
76 |
|
77 |
|
78 |
|
79 | get storageMetrics() {
|
80 | return SPInstance(this, "storagemetrics");
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 | async update(props) {
|
87 | const data = await spPostMerge(this, body(props));
|
88 | return {
|
89 | data,
|
90 | folder: this,
|
91 | };
|
92 | }
|
93 | |
94 |
|
95 |
|
96 | recycle() {
|
97 | return spPost(Folder(this, "recycle"));
|
98 | }
|
99 | |
100 |
|
101 |
|
102 | async getItem(...selects) {
|
103 | const q = this.listItemAllFields;
|
104 | const d = await q.select(...selects)();
|
105 | if (d["odata.null"]) {
|
106 | throw Error("No associated item was found for this folder. It may be the root folder, which does not have an item.");
|
107 | }
|
108 | return Object.assign(Item([this, odataUrlFrom(d)]), d);
|
109 | }
|
110 | async moveByPath(destUrl, ...rest) {
|
111 | let options = {
|
112 | KeepBoth: false,
|
113 | ShouldBypassSharedLocks: true,
|
114 | RetainEditorAndModifiedOnMove: false,
|
115 | };
|
116 | if (rest.length === 1) {
|
117 | if (typeof rest[0] === "boolean") {
|
118 | options.KeepBoth = rest[0];
|
119 | }
|
120 | else if (typeof rest[0] === "object") {
|
121 | options = { ...options, ...rest[0] };
|
122 | }
|
123 | }
|
124 | return this.moveCopyImpl(destUrl, options, "MoveFolderByPath");
|
125 | }
|
126 | async copyByPath(destUrl, ...rest) {
|
127 | let options = {
|
128 | ShouldBypassSharedLocks: true,
|
129 | ResetAuthorAndCreatedOnCopy: true,
|
130 | KeepBoth: false,
|
131 | };
|
132 | if (rest.length === 1) {
|
133 | if (typeof rest[0] === "boolean") {
|
134 | options.KeepBoth = rest[0];
|
135 | }
|
136 | else if (typeof rest[0] === "object") {
|
137 | options = { ...options, ...rest[0] };
|
138 | }
|
139 | }
|
140 | return this.moveCopyImpl(destUrl, options, "CopyFolderByPath");
|
141 | }
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | async deleteWithParams(parameters) {
|
148 | return spPost(Folder(this, "DeleteWithParameters"), body({ parameters }));
|
149 | }
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 | async addSubFolderUsingPath(leafPath) {
|
156 | await spPost(Folder(this, "AddSubFolderUsingPath"), body({ leafPath: toResourcePath(leafPath) }));
|
157 | return this.folders.getByUrl(leafPath);
|
158 | }
|
159 | |
160 |
|
161 |
|
162 | async getParentInfos() {
|
163 | const urlInfo = await this.select("ServerRelativeUrl", "ListItemAllFields/ParentList/Id", "ListItemAllFields/ParentList/RootFolder/UniqueId", "ListItemAllFields/ParentList/RootFolder/ServerRelativeUrl", "ListItemAllFields/ParentList/RootFolder/ServerRelativePath", "ListItemAllFields/ParentList/ParentWeb/Id", "ListItemAllFields/ParentList/ParentWeb/Url", "ListItemAllFields/ParentList/ParentWeb/ServerRelativeUrl", "ListItemAllFields/ParentList/ParentWeb/ServerRelativePath").expand("ListItemAllFields/ParentList", "ListItemAllFields/ParentList/RootFolder", "ListItemAllFields/ParentList/ParentWeb")();
|
164 | return {
|
165 | Folder: {
|
166 | ServerRelativeUrl: urlInfo.ServerRelativeUrl,
|
167 | },
|
168 | ParentList: {
|
169 | Id: urlInfo.ListItemAllFields.ParentList.Id,
|
170 | RootFolderServerRelativePath: urlInfo.ListItemAllFields.ParentList.RootFolder.ServerRelativePath,
|
171 | RootFolderServerRelativeUrl: urlInfo.ListItemAllFields.ParentList.RootFolder.ServerRelativeUrl,
|
172 | RootFolderUniqueId: urlInfo.ListItemAllFields.ParentList.RootFolder.UniqueId,
|
173 | },
|
174 | ParentWeb: {
|
175 | Id: urlInfo.ListItemAllFields.ParentList.ParentWeb.Id,
|
176 | ServerRelativePath: urlInfo.ListItemAllFields.ParentList.ParentWeb.ServerRelativePath,
|
177 | ServerRelativeUrl: urlInfo.ListItemAllFields.ParentList.ParentWeb.ServerRelativeUrl,
|
178 | Url: urlInfo.ListItemAllFields.ParentList.ParentWeb.Url,
|
179 | },
|
180 | };
|
181 | }
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | moveCopyImpl(destUrl, options, methodName) {
|
191 |
|
192 | const poster = Folder(this);
|
193 |
|
194 | poster.on.pre(noInherit(async (url, init, result) => {
|
195 | const urlInfo = await Folder(this).using(BatchNever()).getParentInfos();
|
196 | const uri = new URL(urlInfo.ParentWeb.Url);
|
197 | url = combine(urlInfo.ParentWeb.Url, `/_api/SP.MoveCopyUtil.${methodName}()`);
|
198 | init = body({
|
199 | destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : combine(uri.origin, destUrl)),
|
200 | options,
|
201 | srcPath: toResourcePath(combine(uri.origin, urlInfo.Folder.ServerRelativeUrl)),
|
202 | }, init);
|
203 | return [url, init, result];
|
204 | }));
|
205 | return spPost(poster).then(() => folderFromPath(this, destUrl));
|
206 | }
|
207 | }
|
208 | __decorate([
|
209 | cancelableScope
|
210 | ], _Folder.prototype, "moveByPath", null);
|
211 | __decorate([
|
212 | cancelableScope
|
213 | ], _Folder.prototype, "copyByPath", null);
|
214 | export const Folder = spInvokableFactory(_Folder);
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | export function folderFromServerRelativePath(base, serverRelativePath) {
|
223 | return Folder([base, extractWebUrl(base.toUrl())], `_api/web/getFolderByServerRelativePath(decodedUrl='${encodePath(serverRelativePath)}')`);
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | export async function folderFromAbsolutePath(base, absoluteFolderPath) {
|
233 | const { WebFullUrl } = await Folder(this).using(BatchNever()).getContextInfo(absoluteFolderPath);
|
234 | const { pathname } = new URL(absoluteFolderPath);
|
235 | return folderFromServerRelativePath(Folder([base, combine(WebFullUrl, "_api/web")]), decodeURIComponent(pathname));
|
236 | }
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | export async function folderFromPath(base, path) {
|
245 | return (isUrlAbsolute(path) ? folderFromAbsolutePath : folderFromServerRelativePath)(base, path);
|
246 | }
|
247 |
|
\ | No newline at end of file |