UNPKG

6.16 kBJavaScriptView Raw
1import { combine, isUrlAbsolute, isArray, objectDefinedNotNull, stringIsNullOrEmpty } from "@pnp/core";
2import { Queryable, queryableFactory } from "@pnp/queryable";
3import { spPostDelete, spPostDeleteETag } from "./operations.js";
4export const spInvokableFactory = (f) => {
5 return queryableFactory(f);
6};
7/**
8 * SharePointQueryable Base Class
9 *
10 */
11export class _SPQueryable extends Queryable {
12 /**
13 * Creates a new instance of the SharePointQueryable class
14 *
15 * @constructor
16 * @param base A string or SharePointQueryable that should form the base part of the url
17 *
18 */
19 constructor(base, path) {
20 if (typeof base === "string") {
21 let url = "";
22 let parentUrl = "";
23 // we need to do some extra parsing to get the parent url correct if we are
24 // being created from just a string.
25 if (isUrlAbsolute(base) || base.lastIndexOf("/") < 0) {
26 parentUrl = base;
27 url = combine(base, path);
28 }
29 else if (base.lastIndexOf("/") > base.lastIndexOf("(")) {
30 // .../items(19)/fields
31 const index = base.lastIndexOf("/");
32 parentUrl = base.slice(0, index);
33 path = combine(base.slice(index), path);
34 url = combine(parentUrl, path);
35 }
36 else {
37 // .../items(19)
38 const index = base.lastIndexOf("(");
39 parentUrl = base.slice(0, index);
40 url = combine(base, path);
41 }
42 // init base with corrected string value
43 super(url);
44 this.parentUrl = parentUrl;
45 }
46 else {
47 super(base, path);
48 const q = isArray(base) ? base[0] : base;
49 this.parentUrl = isArray(base) ? base[1] : q.toUrl();
50 const target = q.query.get("@target");
51 if (objectDefinedNotNull(target)) {
52 this.query.set("@target", target);
53 }
54 }
55 }
56 /**
57 * Gets the full url with query information
58 */
59 toRequestUrl() {
60 const aliasedParams = new URLSearchParams(this.query);
61 // this regex is designed to locate aliased parameters within url paths. These may have the form:
62 // /something(!@p1::value)
63 // /something(!@p1::value, param=value)
64 // /something(param=value,!@p1::value)
65 // /something(param=value,!@p1::value,param=value)
66 // /something(param=!@p1::value)
67 // there could be spaces or not around the boundaries
68 let url = this.toUrl().replace(/([( *| *, *| *= *])'!(@.*?)::(.*?)'([ *)| *, *])/ig, (match, frontBoundary, labelName, value, endBoundary) => {
69 this.log(`Rewriting aliased parameter from match ${match} to label: ${labelName} value: ${value}`, 0);
70 aliasedParams.set(labelName, `'${value}'`);
71 return `${frontBoundary}${labelName}${endBoundary}`;
72 });
73 const query = aliasedParams.toString();
74 if (!stringIsNullOrEmpty(query)) {
75 url += `${url.indexOf("?") > -1 ? "&" : "?"}${query}`;
76 }
77 return url;
78 }
79 /**
80 * Choose which fields to return
81 *
82 * @param selects One or more fields to return
83 */
84 select(...selects) {
85 if (selects.length > 0) {
86 this.query.set("$select", selects.join(","));
87 }
88 return this;
89 }
90 /**
91 * Expands fields such as lookups to get additional data
92 *
93 * @param expands The Fields for which to expand the values
94 */
95 expand(...expands) {
96 if (expands.length > 0) {
97 this.query.set("$expand", expands.join(","));
98 }
99 return this;
100 }
101 /**
102 * Gets a parent for this instance as specified
103 *
104 * @param factory The contructor for the class to create
105 */
106 getParent(factory, path, base = this.parentUrl) {
107 const parent = factory([this, base], path);
108 const t = "@target";
109 if (this.query.has(t)) {
110 parent.query.set(t, this.query.get(t));
111 }
112 return parent;
113 }
114}
115export const SPQueryable = spInvokableFactory(_SPQueryable);
116/**
117 * Represents a REST collection which can be filtered, paged, and selected
118 *
119 */
120export class _SPCollection extends _SPQueryable {
121 /**
122 * Filters the returned collection (https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#bk_supported)
123 *
124 * @param filter The string representing the filter query
125 */
126 filter(filter) {
127 this.query.set("$filter", filter);
128 return this;
129 }
130 /**
131 * Orders based on the supplied fields
132 *
133 * @param orderby The name of the field on which to sort
134 * @param ascending If false DESC is appended, otherwise ASC (default)
135 */
136 orderBy(orderBy, ascending = true) {
137 const o = "$orderby";
138 const query = this.query.has(o) ? this.query.get(o).split(",") : [];
139 query.push(`${orderBy} ${ascending ? "asc" : "desc"}`);
140 this.query.set(o, query.join(","));
141 return this;
142 }
143 /**
144 * Skips the specified number of items
145 *
146 * @param skip The number of items to skip
147 */
148 skip(skip) {
149 this.query.set("$skip", skip.toString());
150 return this;
151 }
152 /**
153 * Limits the query to only return the specified number of items
154 *
155 * @param top The query row limit
156 */
157 top(top) {
158 this.query.set("$top", top.toString());
159 return this;
160 }
161}
162export const SPCollection = spInvokableFactory(_SPCollection);
163/**
164 * Represents an instance that can be selected
165 *
166 */
167export class _SPInstance extends _SPQueryable {
168}
169export const SPInstance = spInvokableFactory(_SPInstance);
170/**
171 * Adds the a delete method to the tagged class taking no parameters and calling spPostDelete
172 */
173export function deleteable() {
174 return function () {
175 return spPostDelete(this);
176 };
177}
178export function deleteableWithETag() {
179 return function (eTag = "*") {
180 return spPostDeleteETag(this, {}, eTag);
181 };
182}
183//# sourceMappingURL=spqueryable.js.map
\No newline at end of file