@pnp/odata
Version:
pnp - provides shared odata functionality and base classes
284 lines • 9.21 kB
JavaScript
import { __read, __spreadArray } from "tslib";
import { combine, mergeOptions, objectDefinedNotNull, assign, Runtime, DefaultRuntime, dateAdd, stringIsNullOrEmpty, } from "@pnp/common";
import { ODataParser } from "./parsers.js";
export function cloneQueryableData(source) {
var body;
// this handles bodies that cannot be JSON encoded (Blob, etc)
// Note however, even bodies that can be serialized will not be cloned.
if (source.options && source.options.body) {
body = source.options.body;
source.options.body = "-";
}
var s = JSON.stringify(source, function (key, value) {
switch (key) {
case "query":
return JSON.stringify(__spreadArray([], __read(value), false));
case "batch":
case "batchDependency":
case "cachingOptions":
case "clientFactory":
case "parser":
return "-";
default:
return value;
}
}, 0);
var parsed = JSON.parse(s, function (key, value) {
switch (key) {
case "query":
return new Map(JSON.parse(value));
case "batch":
case "batchDependency":
case "cachingOptions":
case "clientFactory":
case "parser":
return source[key];
default:
return value;
}
});
if (body) {
parsed.options.body = body;
source.options.body = body;
}
return parsed;
}
var Queryable = /** @class */ (function () {
function Queryable(dataSeed) {
if (dataSeed === void 0) { dataSeed = {}; }
this._data = Object.assign({}, {
cloneParentWasCaching: false,
options: {},
parentUrl: "",
parser: new ODataParser(),
query: new Map(),
url: "",
useCaching: false,
}, cloneQueryableData(dataSeed));
this._runtime = null;
}
Object.defineProperty(Queryable.prototype, "data", {
get: function () {
return this._data;
},
set: function (value) {
this._data = Object.assign({}, this.data, cloneQueryableData(value));
},
enumerable: false,
configurable: true
});
Queryable.prototype.getRuntime = function () {
if (this._runtime === null) {
return DefaultRuntime;
}
return this._runtime;
};
Queryable.prototype.setRuntime = function () {
// need to wait for ts update in spfx: [runtime: Runtime] | [cloneGlobal: boolean, additionalConfig?: ITypedHash<any>]
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (args[0] instanceof Runtime) {
this._runtime = args[0];
}
else {
this._runtime = args[0] ? new Runtime(DefaultRuntime.export()) : new Runtime();
if (args.length > 1 && objectDefinedNotNull(args[1])) {
this._runtime.assign(args[1]);
}
}
return this;
};
/**
* Gets the current url
*
*/
Queryable.prototype.toUrl = function () {
return this.data.url;
};
/**
* Directly concatenates the supplied string to the current url, not normalizing "/" chars
*
* @param pathPart The string to concatenate to the url
*/
Queryable.prototype.concat = function (pathPart) {
this.data.url += pathPart;
return this;
};
Object.defineProperty(Queryable.prototype, "query", {
/**
* Provides access to the query builder for this url
*
*/
get: function () {
return this.data.query;
},
enumerable: false,
configurable: true
});
/**
* Sets custom options for current object and all derived objects accessible via chaining
*
* @param options custom options
*/
Queryable.prototype.configure = function (options) {
mergeOptions(this.data.options, options);
return this;
};
/**
* Configures this instance from the configure options of the supplied instance
*
* @param o Instance from which options should be taken
*/
Queryable.prototype.configureFrom = function (o) {
mergeOptions(this.data.options, o.data.options);
var sourceRuntime = o.getRuntime();
if (!sourceRuntime.get("__isDefault__")) {
this.setRuntime(sourceRuntime);
}
return this;
};
/**
* Enables caching for this request
*
* @param options Defines the options used when caching this request
*/
Queryable.prototype.usingCaching = function (options) {
var runtime = this.getRuntime();
if (!runtime.get("globalCacheDisable")) {
this.data.useCaching = true;
// handle getting just the key
if (typeof options === "string") {
if (stringIsNullOrEmpty(options)) {
throw Error("Cache key cannot be empty.");
}
options = { key: options };
}
// this uses our local options if they are defined as defaults
var defaultOpts = {
expiration: dateAdd(new Date(), "second", runtime.get("defaultCachingTimeoutSeconds")),
storeName: runtime.get("defaultCachingStore"),
};
this.data.cachingOptions = assign(defaultOpts, options);
}
return this;
};
Queryable.prototype.usingParser = function (parser) {
this.data.parser = parser;
return this;
};
/**
* Allows you to set a request specific processing pipeline
*
* @param pipeline The set of methods, in order, to execute a given request
*/
Queryable.prototype.withPipeline = function (pipeline) {
this.data.pipes = pipeline.slice(0);
return this;
};
/**
* Appends the given string and normalizes "/" chars
*
* @param pathPart The string to append
*/
Queryable.prototype.append = function (pathPart) {
this.data.url = combine(this.data.url, pathPart);
};
/**
* Adds this query to the supplied batch
*
* @example
* ```
*
* let b = pnp.sp.createBatch();
* pnp.sp.web.inBatch(b).get().then(...);
* b.execute().then(...)
* ```
*/
Queryable.prototype.inBatch = function (batch) {
if (this.hasBatch) {
throw Error("This query is already part of a batch.");
}
if (objectDefinedNotNull(batch)) {
batch.track(this);
}
return this;
};
/**
* Blocks a batch call from occuring, MUST be cleared by calling the returned function
*/
Queryable.prototype.addBatchDependency = function () {
if (objectDefinedNotNull(this.data.batch)) {
return this.data.batch.addDependency();
}
return function () { return null; };
};
Object.defineProperty(Queryable.prototype, "hasBatch", {
/**
* Indicates if the current query has a batch associated
*
*/
get: function () {
return objectDefinedNotNull(this.data.batch);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Queryable.prototype, "batch", {
/**
* The batch currently associated with this query or null
*
*/
get: function () {
return this.hasBatch ? this.data.batch : null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Queryable.prototype, "parentUrl", {
/**
* Gets the parent url used when creating this instance
*
*/
get: function () {
return this.data.parentUrl;
},
enumerable: false,
configurable: true
});
/**
* Clones this instance's data to target
*
* @param target Instance to which data is written
* @param settings [Optional] Settings controlling how clone is applied
*/
Queryable.prototype.cloneTo = function (target, settings) {
if (settings === void 0) { settings = {}; }
// default values for settings
settings = assign({
includeBatch: true,
includeQuery: false,
}, settings);
target.data = Object.assign({}, cloneQueryableData(this.data), {
batch: null,
cloneParentCacheOptions: null,
cloneParentWasCaching: false,
}, cloneQueryableData(target.data));
target.configureFrom(this);
if (settings.includeBatch) {
target.inBatch(this.batch);
}
if (settings.includeQuery && this.query.size > 0) {
this.query.forEach(function (v, k) { return target.query.set(k, v); });
}
if (this.data.useCaching) {
target.data.cloneParentWasCaching = true;
target.data.cloneParentCacheOptions = this.data.cachingOptions;
}
return target;
};
return Queryable;
}());
export { Queryable };
//# sourceMappingURL=queryable.js.map