UNPKG

10.3 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { isUrlAbsolute, combine, noInherit } from "@pnp/core";
3import { body, cancelableScope } from "@pnp/queryable";
4import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPInstance, } from "../spqueryable.js";
5import { odataUrlFrom } from "../utils/odata-url-from.js";
6import { Item } from "../items/types.js";
7import { defaultPath } from "../decorators.js";
8import { spPost, spPostMerge } from "../operations.js";
9import { extractWebUrl } from "../utils/extract-web-url.js";
10import { toResourcePath } from "../utils/to-resource-path.js";
11import { encodePath } from "../utils/encode-path-str.js";
12import "../context-info/index.js";
13import { BatchNever } from "../batching.js";
14let _Folders = class _Folders extends _SPCollection {
15 /**
16 * Gets a folder by it's name
17 *
18 * @param name Folder's name
19 */
20 getByUrl(name) {
21 return Folder(this).concat(`('${encodePath(name)}')`);
22 }
23 /**
24 * Adds a new folder by path and should be prefered over add
25 *
26 * @param serverRelativeUrl The server relative url of the new folder to create
27 * @param overwrite True to overwrite an existing folder, default false
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);
40export { _Folders };
41export const Folders = spInvokableFactory(_Folders);
42export class _Folder extends _SPInstance {
43 constructor() {
44 super(...arguments);
45 this.delete = deleteableWithETag();
46 }
47 /**
48 * Gets this folder's sub folders
49 *
50 */
51 get folders() {
52 return Folders(this);
53 }
54 /**
55 * Gets this folder's list item field values
56 *
57 */
58 get listItemAllFields() {
59 return SPInstance(this, "listItemAllFields");
60 }
61 /**
62 * Gets the parent folder, if available
63 *
64 */
65 get parentFolder() {
66 return Folder(this, "parentFolder");
67 }
68 /**
69 * Gets this folder's properties
70 *
71 */
72 get properties() {
73 return SPInstance(this, "properties");
74 }
75 /**
76 * Gets this folder's storage metrics information
77 *
78 */
79 get storageMetrics() {
80 return SPInstance(this, "storagemetrics");
81 }
82 /**
83 * Updates folder's properties
84 * @param props Folder's properties to update
85 */
86 async update(props) {
87 const data = await spPostMerge(this, body(props));
88 return {
89 data,
90 folder: this,
91 };
92 }
93 /**
94 * Moves the folder to the Recycle Bin and returns the identifier of the new Recycle Bin item.
95 */
96 recycle() {
97 return spPost(Folder(this, "recycle"));
98 }
99 /**
100 * Gets the associated list item for this folder, loading the default properties
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 * Deletes the folder object with options.
144 *
145 * @param parameters Specifies the options to use when deleting a folder.
146 */
147 async deleteWithParams(parameters) {
148 return spPost(Folder(this, "DeleteWithParameters"), body({ parameters }));
149 }
150 /**
151 * Create the subfolder inside the current folder, as specified by the leafPath
152 *
153 * @param leafPath leafName of the new folder
154 */
155 async addSubFolderUsingPath(leafPath) {
156 await spPost(Folder(this, "AddSubFolderUsingPath"), body({ leafPath: toResourcePath(leafPath) }));
157 return this.folders.getByUrl(leafPath);
158 }
159 /**
160 * Gets the parent information for this folder's list and web
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 * Implementation of folder move/copy
184 *
185 * @param destUrl The server relative path to which the folder will be copied/moved
186 * @param options Any options
187 * @param methodName The method to call
188 * @returns An IFolder representing the moved or copied folder
189 */
190 moveCopyImpl(destUrl, options, methodName) {
191 // create a timeline we will manipulate for this request
192 const poster = Folder(this);
193 // add our pre-request actions, this fixes issues with batching hanging #2668
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);
214export const Folder = spInvokableFactory(_Folder);
215/**
216 * Creates an IFolder instance given a base object and a server relative path
217 *
218 * @param base Valid SPQueryable from which the observers will be used and the web url extracted
219 * @param serverRelativePath The server relative url to the folder (ex: '/sites/dev/documents/folder3')
220 * @returns IFolder instance referencing the folder described by the supplied parameters
221 */
222export function folderFromServerRelativePath(base, serverRelativePath) {
223 return Folder([base, extractWebUrl(base.toUrl())], `_api/web/getFolderByServerRelativePath(decodedUrl='${encodePath(serverRelativePath)}')`);
224}
225/**
226 * Creates an IFolder instance given a base object and an absolute path
227 *
228 * @param base Valid SPQueryable from which the observers will be used
229 * @param serverRelativePath The absolute url to the folder (ex: 'https://tenant.sharepoint.com/sites/dev/documents/folder/')
230 * @returns IFolder instance referencing the folder described by the supplied parameters
231 */
232export 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 * Creates an IFolder intance given a base object and either an absolute or server relative path to a folder
239 *
240 * @param base Valid SPQueryable from which the observers will be used
241 * @param serverRelativePath server relative or absolute url to the file (ex: 'https://tenant.sharepoint.com/sites/dev/documents/folder' or '/sites/dev/documents/folder')
242 * @returns IFile instance referencing the file described by the supplied parameters
243 */
244export async function folderFromPath(base, path) {
245 return (isUrlAbsolute(path) ? folderFromAbsolutePath : folderFromServerRelativePath)(base, path);
246}
247//# sourceMappingURL=types.js.map
\No newline at end of file