1 | import { combine, isUrlAbsolute, isArray, objectDefinedNotNull, stringIsNullOrEmpty } from "@pnp/core";
|
2 | import { Queryable, queryableFactory } from "@pnp/queryable";
|
3 | import { spPostDelete, spPostDeleteETag } from "./operations.js";
|
4 | export const spInvokableFactory = (f) => {
|
5 | return queryableFactory(f);
|
6 | };
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export class _SPQueryable extends Queryable {
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | constructor(base, path) {
|
20 | if (typeof base === "string") {
|
21 | let url = "";
|
22 | let parentUrl = "";
|
23 |
|
24 |
|
25 | if (isUrlAbsolute(base) || base.lastIndexOf("/") < 0) {
|
26 | parentUrl = base;
|
27 | url = combine(base, path);
|
28 | }
|
29 | else if (base.lastIndexOf("/") > base.lastIndexOf("(")) {
|
30 |
|
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 |
|
38 | const index = base.lastIndexOf("(");
|
39 | parentUrl = base.slice(0, index);
|
40 | url = combine(base, path);
|
41 | }
|
42 |
|
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 |
|
58 |
|
59 | toRequestUrl() {
|
60 | const aliasedParams = new URLSearchParams(this.query);
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
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 |
|
81 |
|
82 |
|
83 |
|
84 | select(...selects) {
|
85 | if (selects.length > 0) {
|
86 | this.query.set("$select", selects.join(","));
|
87 | }
|
88 | return this;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 | expand(...expands) {
|
96 | if (expands.length > 0) {
|
97 | this.query.set("$expand", expands.join(","));
|
98 | }
|
99 | return this;
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
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 | }
|
115 | export const SPQueryable = spInvokableFactory(_SPQueryable);
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | export class _SPCollection extends _SPQueryable {
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | filter(filter) {
|
127 | this.query.set("$filter", filter);
|
128 | return this;
|
129 | }
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
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 |
|
145 |
|
146 |
|
147 |
|
148 | skip(skip) {
|
149 | this.query.set("$skip", skip.toString());
|
150 | return this;
|
151 | }
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 | top(top) {
|
158 | this.query.set("$top", top.toString());
|
159 | return this;
|
160 | }
|
161 | }
|
162 | export const SPCollection = spInvokableFactory(_SPCollection);
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | export class _SPInstance extends _SPQueryable {
|
168 | }
|
169 | export const SPInstance = spInvokableFactory(_SPInstance);
|
170 |
|
171 |
|
172 |
|
173 | export function deleteable() {
|
174 | return function () {
|
175 | return spPostDelete(this);
|
176 | };
|
177 | }
|
178 | export function deleteableWithETag() {
|
179 | return function (eTag = "*") {
|
180 | return spPostDeleteETag(this, {}, eTag);
|
181 | };
|
182 | }
|
183 |
|
\ | No newline at end of file |