UNPKG

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