UNPKG

5.83 kBJavaScriptView Raw
1import { combine, isUrlAbsolute, isArray } 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 = q.toUrl();
50 const target = q.query.get("@target");
51 if (target !== undefined) {
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 Map(this.query);
61 // '!(@.*?)::(.*?)(?<!')'(?!')/ig <- Lookback query breaks Safari browser https://caniuse.com/js-regexp-lookbehind
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 * Choose which fields to return
75 *
76 * @param selects One or more fields to return
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 * Expands fields such as lookups to get additional data
86 *
87 * @param expands The Fields for which to expand the values
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 * Gets a parent for this instance as specified
97 *
98 * @param factory The contructor for the class to create
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}
109export const SPQueryable = spInvokableFactory(_SPQueryable);
110/**
111 * Represents a REST collection which can be filtered, paged, and selected
112 *
113 */
114export class _SPCollection extends _SPQueryable {
115 /**
116 * Filters the returned collection (https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#bk_supported)
117 *
118 * @param filter The string representing the filter query
119 */
120 filter(filter) {
121 this.query.set("$filter", encodeURIComponent(filter));
122 return this;
123 }
124 /**
125 * Orders based on the supplied fields
126 *
127 * @param orderby The name of the field on which to sort
128 * @param ascending If false DESC is appended, otherwise ASC (default)
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 * Skips the specified number of items
139 *
140 * @param skip The number of items to skip
141 */
142 skip(skip) {
143 this.query.set("$skip", skip.toString());
144 return this;
145 }
146 /**
147 * Limits the query to only return the specified number of items
148 *
149 * @param top The query row limit
150 */
151 top(top) {
152 this.query.set("$top", top.toString());
153 return this;
154 }
155}
156export const SPCollection = spInvokableFactory(_SPCollection);
157/**
158 * Represents an instance that can be selected
159 *
160 */
161export class _SPInstance extends _SPQueryable {
162}
163export const SPInstance = spInvokableFactory(_SPInstance);
164/**
165 * Adds the a delete method to the tagged class taking no parameters and calling spPostDelete
166 */
167export function deleteable() {
168 return function () {
169 return spPostDelete(this);
170 };
171}
172export function deleteableWithETag() {
173 return function (eTag = "*") {
174 return spPostDeleteETag(this, {}, eTag);
175 };
176}
177//# sourceMappingURL=spqueryable.js.map
\No newline at end of file