@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
183 lines • 6.16 kB
JavaScript
import { combine, isUrlAbsolute, isArray, objectDefinedNotNull, stringIsNullOrEmpty } from "@pnp/core";
import { Queryable, queryableFactory } from "@pnp/queryable";
import { spPostDelete, spPostDeleteETag } from "./operations.js";
export const spInvokableFactory = (f) => {
return queryableFactory(f);
};
/**
* SharePointQueryable Base Class
*
*/
export class _SPQueryable extends Queryable {
/**
* Creates a new instance of the SharePointQueryable class
*
* @constructor
* @param base A string or SharePointQueryable that should form the base part of the url
*
*/
constructor(base, path) {
if (typeof base === "string") {
let url = "";
let parentUrl = "";
// we need to do some extra parsing to get the parent url correct if we are
// being created from just a string.
if (isUrlAbsolute(base) || base.lastIndexOf("/") < 0) {
parentUrl = base;
url = combine(base, path);
}
else if (base.lastIndexOf("/") > base.lastIndexOf("(")) {
// .../items(19)/fields
const index = base.lastIndexOf("/");
parentUrl = base.slice(0, index);
path = combine(base.slice(index), path);
url = combine(parentUrl, path);
}
else {
// .../items(19)
const index = base.lastIndexOf("(");
parentUrl = base.slice(0, index);
url = combine(base, path);
}
// init base with corrected string value
super(url);
this.parentUrl = parentUrl;
}
else {
super(base, path);
const q = isArray(base) ? base[0] : base;
this.parentUrl = isArray(base) ? base[1] : q.toUrl();
const target = q.query.get("@target");
if (objectDefinedNotNull(target)) {
this.query.set("@target", target);
}
}
}
/**
* Gets the full url with query information
*/
toRequestUrl() {
const aliasedParams = new URLSearchParams(this.query);
// this regex is designed to locate aliased parameters within url paths. These may have the form:
// /something(!@p1::value)
// /something(!@p1::value, param=value)
// /something(param=value,!@p1::value)
// /something(param=value,!@p1::value,param=value)
// /something(param=!@p1::value)
// there could be spaces or not around the boundaries
let url = this.toUrl().replace(/([( *| *, *| *= *])'!(@.*?)::(.*?)'([ *)| *, *])/ig, (match, frontBoundary, labelName, value, endBoundary) => {
this.log(`Rewriting aliased parameter from match ${match} to label: ${labelName} value: ${value}`, 0);
aliasedParams.set(labelName, `'${value}'`);
return `${frontBoundary}${labelName}${endBoundary}`;
});
const query = aliasedParams.toString();
if (!stringIsNullOrEmpty(query)) {
url += `${url.indexOf("?") > -1 ? "&" : "?"}${query}`;
}
return url;
}
/**
* Choose which fields to return
*
* @param selects One or more fields to return
*/
select(...selects) {
if (selects.length > 0) {
this.query.set("$select", selects.join(","));
}
return this;
}
/**
* Expands fields such as lookups to get additional data
*
* @param expands The Fields for which to expand the values
*/
expand(...expands) {
if (expands.length > 0) {
this.query.set("$expand", expands.join(","));
}
return this;
}
/**
* Gets a parent for this instance as specified
*
* @param factory The contructor for the class to create
*/
getParent(factory, path, base = this.parentUrl) {
const parent = factory([this, base], path);
const t = "@target";
if (this.query.has(t)) {
parent.query.set(t, this.query.get(t));
}
return parent;
}
}
export const SPQueryable = spInvokableFactory(_SPQueryable);
/**
* Represents a REST collection which can be filtered, paged, and selected
*
*/
export class _SPCollection extends _SPQueryable {
/**
* Filters the returned collection (https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#bk_supported)
*
* @param filter The string representing the filter query
*/
filter(filter) {
this.query.set("$filter", filter);
return this;
}
/**
* Orders based on the supplied fields
*
* @param orderby The name of the field on which to sort
* @param ascending If false DESC is appended, otherwise ASC (default)
*/
orderBy(orderBy, ascending = true) {
const o = "$orderby";
const query = this.query.has(o) ? this.query.get(o).split(",") : [];
query.push(`${orderBy} ${ascending ? "asc" : "desc"}`);
this.query.set(o, query.join(","));
return this;
}
/**
* Skips the specified number of items
*
* @param skip The number of items to skip
*/
skip(skip) {
this.query.set("$skip", skip.toString());
return this;
}
/**
* Limits the query to only return the specified number of items
*
* @param top The query row limit
*/
top(top) {
this.query.set("$top", top.toString());
return this;
}
}
export const SPCollection = spInvokableFactory(_SPCollection);
/**
* Represents an instance that can be selected
*
*/
export class _SPInstance extends _SPQueryable {
}
export const SPInstance = spInvokableFactory(_SPInstance);
/**
* Adds the a delete method to the tagged class taking no parameters and calling spPostDelete
*/
export function deleteable() {
return function () {
return spPostDelete(this);
};
}
export function deleteableWithETag() {
return function (eTag = "*") {
return spPostDeleteETag(this, {}, eTag);
};
}
//# sourceMappingURL=spqueryable.js.map