diff --git a/packages/graph/directory-objects/types.ts b/packages/graph/directory-objects/types.ts index 9783e294d..820b7c36f 100644 --- a/packages/graph/directory-objects/types.ts +++ b/packages/graph/directory-objects/types.ts @@ -10,32 +10,32 @@ import { graphPost } from "../operations"; @deleteable() export class _DirectoryObject extends _GraphQueryableInstance { - /** - * Returns all the groups and directory roles that the specified Directory Object is a member of. The check is transitive - * - * @param securityEnabledOnly - */ - public getMemberObjects(securityEnabledOnly = false): Promise<{ value: string[] }> { - return graphPost(this.clone(DirectoryObject, "getMemberObjects"), body({ securityEnabledOnly })); - } + /** + * Returns all the groups and directory roles that the specified Directory Object is a member of. The check is transitive + * + * @param securityEnabledOnly + */ + public getMemberObjects(securityEnabledOnly = false): Promise { + return graphPost(this.clone(DirectoryObject, "getMemberObjects"), body({ securityEnabledOnly })); + } - /** - * Returns all the groups that the specified Directory Object is a member of. The check is transitive - * - * @param securityEnabledOnly - */ - public getMemberGroups(securityEnabledOnly = false): Promise<{ value: string[] }> { - return graphPost(this.clone(DirectoryObject, "getMemberGroups"), body({ securityEnabledOnly })); - } + /** + * Returns all the groups that the specified Directory Object is a member of. The check is transitive + * + * @param securityEnabledOnly + */ + public getMemberGroups(securityEnabledOnly = false): Promise { + return graphPost(this.clone(DirectoryObject, "getMemberGroups"), body({ securityEnabledOnly })); + } - /** - * Check for membership in a specified list of groups, and returns from that list those groups of which the specified user, group, or directory object is a member. - * This function is transitive. - * @param groupIds A collection that contains the object IDs of the groups in which to check membership. Up to 20 groups may be specified. - */ - public checkMemberGroups(groupIds: String[]): Promise<{ value: string[] }> { - return graphPost(this.clone(DirectoryObject, "checkMemberGroups"), body({ groupIds })); - } + /** + * Check for membership in a specified list of groups, and returns from that list those groups of which the specified user, group, or directory object is a member. + * This function is transitive. + * @param groupIds A collection that contains the object IDs of the groups in which to check membership. Up to 20 groups may be specified. + */ + public checkMemberGroups(groupIds: String[]): Promise { + return graphPost(this.clone(DirectoryObject, "checkMemberGroups"), body({ groupIds })); + } } export interface IDirectoryObject extends _DirectoryObject, IDeleteable { } @@ -48,16 +48,16 @@ export const DirectoryObject = graphInvokableFactory(_Director @defaultPath("directoryObjects") @getById(DirectoryObject) export class _DirectoryObjects extends _GraphQueryableCollection { - /** - * Returns the directory objects specified in a list of ids. NOTE: The directory objects returned are the full objects containing all their properties. - * The $select query option is not available for this operation. - * - * @param ids A collection of ids for which to return objects. You can specify up to 1000 ids. - * @param type A collection of resource types that specifies the set of resource collections to search. Default is directoryObject. - */ - public getByIds(ids: string[], type: DirectoryObjectTypes = DirectoryObjectTypes.directoryObject): Promise { - return graphPost(this.clone(DirectoryObjects, "getByIds"), body({ ids, type })); - } + /** + * Returns the directory objects specified in a list of ids. NOTE: The directory objects returned are the full objects containing all their properties. + * The $select query option is not available for this operation. + * + * @param ids A collection of ids for which to return objects. You can specify up to 1000 ids. + * @param type A collection of resource types that specifies the set of resource collections to search. Default is directoryObject. + */ + public getByIds(ids: string[], type: DirectoryObjectTypes = DirectoryObjectTypes.directoryObject): Promise { + return graphPost(this.clone(DirectoryObjects, "getByIds"), body({ ids, type })); + } } export interface IDirectoryObjects extends _DirectoryObjects, IGetById { } export const DirectoryObjects = graphInvokableFactory(_DirectoryObjects); @@ -66,20 +66,20 @@ export const DirectoryObjects = graphInvokableFactory(_Direct * DirectoryObjectTypes */ export enum DirectoryObjectTypes { - /** - * Directory Objects - */ - directoryObject, - /** - * User - */ - user, - /** - * Group - */ - group, - /** - * Device - */ - device, + /** + * Directory Objects + */ + directoryObject, + /** + * User + */ + user, + /** + * Group + */ + group, + /** + * Device + */ + device, } diff --git a/packages/odata/queryable.ts b/packages/odata/queryable.ts index ac36e3bf7..9979944a9 100644 --- a/packages/odata/queryable.ts +++ b/packages/odata/queryable.ts @@ -1,11 +1,11 @@ import { - combine, - RuntimeConfig, - IFetchOptions, - IConfigOptions, - mergeOptions, - objectDefinedNotNull, - IRequestClient, + combine, + RuntimeConfig, + IFetchOptions, + IConfigOptions, + mergeOptions, + objectDefinedNotNull, + IRequestClient, } from "@pnp/common"; import { ICachingOptions } from "./caching"; import { Batch } from "./batch"; @@ -14,293 +14,293 @@ import { IODataParser, ODataParser } from "./parsers"; export function cloneQueryableData(source: Partial): Partial { - const s = JSON.stringify(source, (key: string, value: any) => { - - switch (key) { - case "query": - return JSON.stringify([...(>value)]); - case "batch": - return "-"; - case "batchDependency": - return "-"; - case "cachingOptions": - return "-"; - case "clientFactory": - return "-"; - case "parser": - return "-"; - default: - return value; - } - }, 0); - - const parsed = JSON.parse(s, (key: any, value: any) => { - switch (key) { - case "query": - return new Map(JSON.parse(value)); - case "batch": - return source.batch; - case "batchDependency": - return source.batchDependency; - case "cachingOptions": - return source.cachingOptions; - case "clientFactory": - return source.clientFactory; - case "parser": - return source.parser; - default: - return value; - } - }); - - // this handles bodies that cannot be JSON encoded (Blob, etc) - if (source.options && source.options.body) { - parsed.options.body = source.options.body; + const s = JSON.stringify(source, (key: string, value: any) => { + + switch (key) { + case "query": + return JSON.stringify([...(>value)]); + case "batch": + return "-"; + case "batchDependency": + return "-"; + case "cachingOptions": + return "-"; + case "clientFactory": + return "-"; + case "parser": + return "-"; + default: + return value; } + }, 0); + + const parsed = JSON.parse(s, (key: any, value: any) => { + switch (key) { + case "query": + return new Map(JSON.parse(value)); + case "batch": + return source.batch; + case "batchDependency": + return source.batchDependency; + case "cachingOptions": + return source.cachingOptions; + case "clientFactory": + return source.clientFactory; + case "parser": + return source.parser; + default: + return value; + } + }); + + // this handles bodies that cannot be JSON encoded (Blob, etc) + if (source.options && source.options.body) { + parsed.options.body = source.options.body; + } - return parsed; + return parsed; } export interface IQueryableData { - batch: Batch | null; - batchIndex: number; - batchDependency: () => void | null; - cachingOptions: ICachingOptions | null; - cloneParentCacheOptions: ICachingOptions | null; - cloneParentWasCaching: boolean; - query: Map; - options: IFetchOptions | null; - url: string; - parentUrl: string; - useCaching: boolean; - pipes?: PipelineMethod[]; - parser?: IODataParser; - clientFactory?: () => IRequestClient; - method?: string; + batch: Batch | null; + batchIndex: number; + batchDependency: () => void | null; + cachingOptions: ICachingOptions | null; + cloneParentCacheOptions: ICachingOptions | null; + cloneParentWasCaching: boolean; + query: Map; + options: IFetchOptions | null; + url: string; + parentUrl: string; + useCaching: boolean; + pipes?: PipelineMethod[]; + parser?: IODataParser; + clientFactory?: () => IRequestClient; + method?: string; } export interface IQueryable { - data: Partial>; - query: Map; - append(pathPart: string): void; - inBatch(batch: Batch): this; - addBatchDependency(): () => void; - toUrlAndQuery(): string; - toUrl(): string; - concat(pathPart: string): this; - configure(options: IConfigOptions): this; - configureFrom(o: IQueryable): this; - usingCaching(options?: ICachingOptions): this; - usingParser(parser: IODataParser): this; - withPipeline(pipeline: PipelineMethod[]): this; - defaultAction(options?: IFetchOptions): Promise; + data: Partial>; + query: Map; + append(pathPart: string): void; + inBatch(batch: Batch): this; + addBatchDependency(): () => void; + toUrlAndQuery(): string; + toUrl(): string; + concat(pathPart: string): this; + configure(options: IConfigOptions): this; + configureFrom(o: IQueryable): this; + usingCaching(options?: ICachingOptions): this; + usingParser(parser: IODataParser): this; + withPipeline(pipeline: PipelineMethod[]): this; + defaultAction(options?: IFetchOptions): Promise; } export abstract class Queryable implements IQueryable { - private _data: Partial>; - - constructor(dataSeed: Partial> = {}) { - - this._data = Object.assign({}, { - cloneParentWasCaching: false, - options: {}, - parentUrl: "", - parser: new ODataParser(), - query: new Map(), - url: "", - useCaching: false, - }, cloneQueryableData(dataSeed)); + private _data: Partial>; + + constructor(dataSeed: Partial> = {}) { + + this._data = Object.assign({}, { + cloneParentWasCaching: false, + options: {}, + parentUrl: "", + parser: new ODataParser(), + query: new Map(), + url: "", + useCaching: false, + }, cloneQueryableData(dataSeed)); + } + + public get data(): Partial> { + return this._data; + } + + public set data(value: Partial>) { + this._data = Object.assign({}, cloneQueryableData(this.data), cloneQueryableData(value)); + } + + /** + * Gets the full url with query information + * + */ + public abstract toUrlAndQuery(): string; + + /** + * The default action for this + */ + public abstract defaultAction(options?: IFetchOptions): Promise; + + /** + * Gets the currentl url + * + */ + public toUrl(): string { + 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 + */ + public concat(pathPart: string): this { + this.data.url += pathPart; + return this; + } + + /** + * Provides access to the query builder for this url + * + */ + public get query(): Map { + return this.data.query; + } + + /** + * Sets custom options for current object and all derived objects accessible via chaining + * + * @param options custom options + */ + public configure(options: IConfigOptions): this { + 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 + */ + public configureFrom(o: IQueryable): this { + mergeOptions(this.data.options, o.data.options); + return this; + } + + /** + * Enables caching for this request + * + * @param options Defines the options used when caching this request + */ + public usingCaching(options?: ICachingOptions): this { + if (!RuntimeConfig.globalCacheDisable) { + this.data.useCaching = true; + if (options !== undefined) { + this.data.cachingOptions = options; + } } - - public get data(): Partial> { - return this._data; + return this; + } + + public usingParser(parser: IODataParser): this { + 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 + */ + public withPipeline(pipeline: PipelineMethod[]): this { + this.data.pipes = pipeline.slice(0); + return this; + } + + /** + * Appends the given string and normalizes "/" chars + * + * @param pathPart The string to append + */ + public append(pathPart: string): void { + 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(...) + * ``` + */ + public inBatch(batch: Batch): this { + + if (this.hasBatch) { + throw Error("This query is already part of a batch."); } - public set data(value: Partial>) { - this._data = Object.assign({}, cloneQueryableData(this.data), cloneQueryableData(value)); + if (objectDefinedNotNull(batch)) { + batch.track(this); } - /** - * Gets the full url with query information - * - */ - public abstract toUrlAndQuery(): string; - - /** - * The default action for this - */ - public abstract defaultAction(options?: IFetchOptions): Promise; - - /** - * Gets the currentl url - * - */ - public toUrl(): string { - return this.data.url; - } - - /** - * Directly concatonates the supplied string to the current url, not normalizing "/" chars - * - * @param pathPart The string to concatonate to the url - */ - public concat(pathPart: string): this { - this.data.url += pathPart; - return this; - } + return this; + } - /** - * Provides access to the query builder for this url - * - */ - public get query(): Map { - return this.data.query; + /** + * Blocks a batch call from occuring, MUST be cleared by calling the returned function + */ + public addBatchDependency(): () => void { + if (objectDefinedNotNull(this.data.batch)) { + return this.data.batch.addDependency(); } - /** - * Sets custom options for current object and all derived objects accessible via chaining - * - * @param options custom options - */ - public configure(options: IConfigOptions): this { - mergeOptions(this.data.options, options); - return this; + return () => null; + } + + /** + * Indicates if the current query has a batch associated + * + */ + protected get hasBatch(): boolean { + return objectDefinedNotNull(this.data.batch); + } + + /** + * The batch currently associated with this query or null + * + */ + protected get batch(): Batch | null { + return this.hasBatch ? this.data.batch : null; + } + + /** + * Gets the parent url used when creating this instance + * + */ + protected get parentUrl(): string { + return this.data.parentUrl; + } + + /** + * Clones this instance's data to target + * + * @param target Instance to which data is written + * @param settings [Optional] Settings controlling how clone is applied + */ + protected cloneTo>(target: T, settings: { includeBatch: boolean } = { includeBatch: true }): T { + + 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); } - /** - * Configures this instance from the configure options of the supplied instance - * - * @param o Instance from which options should be taken - */ - public configureFrom(o: IQueryable): this { - mergeOptions(this.data.options, o.data.options); - return this; + if (this.data.useCaching) { + target.data.cloneParentWasCaching = true; + target.data.cloneParentCacheOptions = this.data.cachingOptions; } - /** - * Enables caching for this request - * - * @param options Defines the options used when caching this request - */ - public usingCaching(options?: ICachingOptions): this { - if (!RuntimeConfig.globalCacheDisable) { - this.data.useCaching = true; - if (options !== undefined) { - this.data.cachingOptions = options; - } - } - return this; - } - - public usingParser(parser: IODataParser): this { - 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 - */ - public withPipeline(pipeline: PipelineMethod[]): this { - this.data.pipes = pipeline.slice(0); - return this; - } - - /** - * Appends the given string and normalizes "/" chars - * - * @param pathPart The string to append - */ - public append(pathPart: string): void { - 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(...) - * ``` - */ - public inBatch(batch: Batch): this { - - 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 - */ - public addBatchDependency(): () => void { - if (objectDefinedNotNull(this.data.batch)) { - return this.data.batch.addDependency(); - } - - return () => null; - } - - /** - * Indicates if the current query has a batch associated - * - */ - protected get hasBatch(): boolean { - return objectDefinedNotNull(this.data.batch); - } - - /** - * The batch currently associated with this query or null - * - */ - protected get batch(): Batch | null { - return this.hasBatch ? this.data.batch : null; - } - - /** - * Gets the parent url used when creating this instance - * - */ - protected get parentUrl(): string { - return this.data.parentUrl; - } - - /** - * Clones this instance's data to target - * - * @param target Instance to which data is written - * @param settings [Optional] Settings controlling how clone is applied - */ - protected cloneTo>(target: T, settings: { includeBatch: boolean } = { includeBatch: true }): T { - - 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 (this.data.useCaching) { - target.data.cloneParentWasCaching = true; - target.data.cloneParentCacheOptions = this.data.cachingOptions; - } - - return target; - } + return target; + } }