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