1 | import { combine, isUrlAbsolute, isArray } 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 = q.toUrl();
|
50 | const target = q.query.get("@target");
|
51 | if (target !== undefined) {
|
52 | this.query.set("@target", target);
|
53 | }
|
54 | }
|
55 | }
|
56 | |
57 |
|
58 |
|
59 | toRequestUrl() {
|
60 | const aliasedParams = new Map(this.query);
|
61 |
|
62 | let url = this.toUrl().replace(/'!(@.*?)::(.*?)'/ig, (match, labelName, value) => {
|
63 | this.log(`Rewriting aliased parameter from match ${match} to label: ${labelName} value: ${value}`, 0);
|
64 | aliasedParams.set(labelName, `'${value}'`);
|
65 | return labelName;
|
66 | });
|
67 | if (aliasedParams.size > 0) {
|
68 | const char = url.indexOf("?") > -1 ? "&" : "?";
|
69 | url += `${char}${Array.from(aliasedParams).map((v) => v[0] + "=" + v[1]).join("&")}`;
|
70 | }
|
71 | return url;
|
72 | }
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | select(...selects) {
|
79 | if (selects.length > 0) {
|
80 | this.query.set("$select", selects.map(encodeURIComponent).join(","));
|
81 | }
|
82 | return this;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 | expand(...expands) {
|
90 | if (expands.length > 0) {
|
91 | this.query.set("$expand", expands.map(encodeURIComponent).join(","));
|
92 | }
|
93 | return this;
|
94 | }
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | getParent(factory, path, base = this.parentUrl) {
|
101 | const parent = factory([this, base], path);
|
102 | const t = "@target";
|
103 | if (this.query.has(t)) {
|
104 | parent.query.set(t, this.query.get(t));
|
105 | }
|
106 | return parent;
|
107 | }
|
108 | }
|
109 | export const SPQueryable = spInvokableFactory(_SPQueryable);
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | export class _SPCollection extends _SPQueryable {
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 | filter(filter) {
|
121 | this.query.set("$filter", encodeURIComponent(filter));
|
122 | return this;
|
123 | }
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | orderBy(orderBy, ascending = true) {
|
131 | const o = "$orderby";
|
132 | const query = this.query.has(o) ? this.query.get(o).split(",") : [];
|
133 | query.push(`${encodeURIComponent(orderBy)} ${ascending ? "asc" : "desc"}`);
|
134 | this.query.set(o, query.join(","));
|
135 | return this;
|
136 | }
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | skip(skip) {
|
143 | this.query.set("$skip", skip.toString());
|
144 | return this;
|
145 | }
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 | top(top) {
|
152 | this.query.set("$top", top.toString());
|
153 | return this;
|
154 | }
|
155 | }
|
156 | export const SPCollection = spInvokableFactory(_SPCollection);
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | export class _SPInstance extends _SPQueryable {
|
162 | }
|
163 | export const SPInstance = spInvokableFactory(_SPInstance);
|
164 |
|
165 |
|
166 |
|
167 | export function deleteable() {
|
168 | return function () {
|
169 | return spPostDelete(this);
|
170 | };
|
171 | }
|
172 | export function deleteableWithETag() {
|
173 | return function (eTag = "*") {
|
174 | return spPostDeleteETag(this, {}, eTag);
|
175 | };
|
176 | }
|
177 |
|
\ | No newline at end of file |