UNPKG

9.78 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { isUrlAbsolute, combine } 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";
13let _Folders = class _Folders extends _SPCollection {
14 /**
15 * Gets a folder by it's name
16 *
17 * @param name Folder's name
18 */
19 getByUrl(name) {
20 return Folder(this).concat(`('${encodePath(name)}')`);
21 }
22 /**
23 * Adds a new folder by path and should be prefered over add
24 *
25 * @param serverRelativeUrl The server relative url of the new folder to create
26 * @param overwrite True to overwrite an existing folder, default false
27 */
28 async addUsingPath(serverRelativeUrl, overwrite = false) {
29 const data = await spPost(Folders(this, `addUsingPath(DecodedUrl='${encodePath(serverRelativeUrl)}',overwrite=${overwrite})`));
30 return {
31 data,
32 folder: folderFromServerRelativePath(this, data.ServerRelativeUrl),
33 };
34 }
35};
36_Folders = __decorate([
37 defaultPath("folders")
38], _Folders);
39export { _Folders };
40export const Folders = spInvokableFactory(_Folders);
41export class _Folder extends _SPInstance {
42 constructor() {
43 super(...arguments);
44 this.delete = deleteableWithETag();
45 }
46 /**
47 * Gets this folder's sub folders
48 *
49 */
50 get folders() {
51 return Folders(this);
52 }
53 /**
54 * Gets this folder's list item field values
55 *
56 */
57 get listItemAllFields() {
58 return SPInstance(this, "listItemAllFields");
59 }
60 /**
61 * Gets the parent folder, if available
62 *
63 */
64 get parentFolder() {
65 return Folder(this, "parentFolder");
66 }
67 /**
68 * Gets this folder's properties
69 *
70 */
71 get properties() {
72 return SPInstance(this, "properties");
73 }
74 /**
75 * Gets this folder's storage metrics information
76 *
77 */
78 get storageMetrics() {
79 return SPInstance(this, "storagemetrics");
80 }
81 /**
82 * Updates folder's properties
83 * @param props Folder's properties to update
84 */
85 async update(props) {
86 const data = await spPostMerge(this, body(props));
87 return {
88 data,
89 folder: this,
90 };
91 }
92 /**
93 * Moves the folder to the Recycle Bin and returns the identifier of the new Recycle Bin item.
94 */
95 recycle() {
96 return spPost(Folder(this, "recycle"));
97 }
98 /**
99 * Gets the associated list item for this folder, loading the default properties
100 */
101 async getItem(...selects) {
102 const q = this.listItemAllFields;
103 const d = await q.select(...selects)();
104 if (d["odata.null"]) {
105 throw Error("No associated item was found for this folder. It may be the root folder, which does not have an item.");
106 }
107 return Object.assign(Item([this, odataUrlFrom(d)]), d);
108 }
109 async moveByPath(destUrl, ...rest) {
110 let options = {
111 KeepBoth: false,
112 ShouldBypassSharedLocks: true,
113 RetainEditorAndModifiedOnMove: false,
114 };
115 if (rest.length === 1) {
116 if (typeof rest[0] === "boolean") {
117 options.KeepBoth = rest[0];
118 }
119 else if (typeof rest[0] === "object") {
120 options = { ...options, ...rest[0] };
121 }
122 }
123 const urlInfo = await this.getParentInfos();
124 const uri = new URL(urlInfo.ParentWeb.Url);
125 await spPost(Folder([this, urlInfo.ParentWeb.Url], "/_api/SP.MoveCopyUtil.MoveFolderByPath()"), body({
126 destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : combine(uri.origin, destUrl)),
127 options,
128 srcPath: toResourcePath(combine(uri.origin, urlInfo.Folder.ServerRelativeUrl)),
129 }));
130 return folderFromPath(this, destUrl);
131 }
132 async copyByPath(destUrl, ...rest) {
133 let options = {
134 ShouldBypassSharedLocks: true,
135 ResetAuthorAndCreatedOnCopy: true,
136 KeepBoth: false,
137 };
138 if (rest.length === 1) {
139 if (typeof rest[0] === "boolean") {
140 options.KeepBoth = rest[0];
141 }
142 else if (typeof rest[0] === "object") {
143 options = { ...options, ...rest[0] };
144 }
145 }
146 const urlInfo = await this.getParentInfos();
147 const uri = new URL(urlInfo.ParentWeb.Url);
148 await spPost(Folder([this, urlInfo.ParentWeb.Url], "/_api/SP.MoveCopyUtil.CopyFolderByPath()"), body({
149 destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : combine(uri.origin, destUrl)),
150 options,
151 srcPath: toResourcePath(combine(uri.origin, urlInfo.Folder.ServerRelativeUrl)),
152 }));
153 return folderFromPath(this, destUrl);
154 }
155 /**
156 * Deletes the folder object with options.
157 *
158 * @param parameters Specifies the options to use when deleting a folder.
159 */
160 async deleteWithParams(parameters) {
161 return spPost(Folder(this, "DeleteWithParameters"), body({ parameters }));
162 }
163 /**
164 * Create the subfolder inside the current folder, as specified by the leafPath
165 *
166 * @param leafPath leafName of the new folder
167 */
168 async addSubFolderUsingPath(leafPath) {
169 await spPost(Folder(this, "AddSubFolderUsingPath"), body({ leafPath: toResourcePath(leafPath) }));
170 return this.folders.getByUrl(leafPath);
171 }
172 /**
173 * Gets the parent information for this folder's list and web
174 */
175 async getParentInfos() {
176 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")();
177 return {
178 Folder: {
179 ServerRelativeUrl: urlInfo.ServerRelativeUrl,
180 },
181 ParentList: {
182 Id: urlInfo.ListItemAllFields.ParentList.Id,
183 RootFolderServerRelativePath: urlInfo.ListItemAllFields.ParentList.RootFolder.ServerRelativePath,
184 RootFolderServerRelativeUrl: urlInfo.ListItemAllFields.ParentList.RootFolder.ServerRelativeUrl,
185 RootFolderUniqueId: urlInfo.ListItemAllFields.ParentList.RootFolder.UniqueId,
186 },
187 ParentWeb: {
188 Id: urlInfo.ListItemAllFields.ParentList.ParentWeb.Id,
189 ServerRelativePath: urlInfo.ListItemAllFields.ParentList.ParentWeb.ServerRelativePath,
190 ServerRelativeUrl: urlInfo.ListItemAllFields.ParentList.ParentWeb.ServerRelativeUrl,
191 Url: urlInfo.ListItemAllFields.ParentList.ParentWeb.Url,
192 },
193 };
194 }
195}
196__decorate([
197 cancelableScope
198], _Folder.prototype, "moveByPath", null);
199__decorate([
200 cancelableScope
201], _Folder.prototype, "copyByPath", null);
202export const Folder = spInvokableFactory(_Folder);
203/**
204 * Creates an IFolder instance given a base object and a server relative path
205 *
206 * @param base Valid SPQueryable from which the observers will be used and the web url extracted
207 * @param serverRelativePath The server relative url to the folder (ex: '/sites/dev/documents/folder3')
208 * @returns IFolder instance referencing the folder described by the supplied parameters
209 */
210export function folderFromServerRelativePath(base, serverRelativePath) {
211 return Folder([base, extractWebUrl(base.toUrl())], `_api/web/getFolderByServerRelativePath(decodedUrl='${encodePath(serverRelativePath)}')`);
212}
213/**
214 * Creates an IFolder instance given a base object and an absolute path
215 *
216 * @param base Valid SPQueryable from which the observers will be used
217 * @param serverRelativePath The absolute url to the folder (ex: 'https://tenant.sharepoint.com/sites/dev/documents/folder/')
218 * @returns IFolder instance referencing the folder described by the supplied parameters
219 */
220export async function folderFromAbsolutePath(base, absoluteFolderPath) {
221 const { WebFullUrl } = await base.getContextInfo(absoluteFolderPath);
222 const { pathname } = new URL(absoluteFolderPath);
223 return folderFromServerRelativePath(Folder([base, combine(WebFullUrl, "_api/web")]), decodeURIComponent(pathname));
224}
225/**
226 * Creates an IFolder intance given a base object and either an absolute or server relative path to a folder
227 *
228 * @param base Valid SPQueryable from which the observers will be used
229 * @param serverRelativePath server relative or absolute url to the file (ex: 'https://tenant.sharepoint.com/sites/dev/documents/folder' or '/sites/dev/documents/folder')
230 * @returns IFile instance referencing the file described by the supplied parameters
231 */
232export async function folderFromPath(base, path) {
233 return (isUrlAbsolute(path) ? folderFromAbsolutePath : folderFromServerRelativePath)(base, path);
234}
235//# sourceMappingURL=types.js.map
\No newline at end of file