Skip to content

Commit

Permalink
v2 collections will now have their methods applied to the results.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjandatta committed Feb 16, 2024
1 parent 0bd811e commit 87f6c2f
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 66 deletions.
4 changes: 2 additions & 2 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export { Lib }
/**
* V2 Library Components
*/
import * as LibV2 from "./v2";
export { LibV2 }
import * as v2 from "./v2";
export { v2 }

/**
* $REST Global Variable
Expand Down
6 changes: 3 additions & 3 deletions @types/rest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ export interface IREST {
/**
* The default library for a site.
*/
Drive: LibV2Types.IDrive;
drive: LibV2Types.Idrive;

/**
* The libraries for a site.
*/
Drives: LibV2Types.IDrives;
drives: LibV2Types.Idrives;

/**
* Use this api to get the web url from a page url.
Expand Down Expand Up @@ -168,7 +168,7 @@ export interface IREST {
/**
* The graph sites endpoint.
*/
Sites: LibV2Types.ISites;
sites: LibV2Types.Isites;

/**
* Use this api to get the url of a site, by its id.
Expand Down
4 changes: 2 additions & 2 deletions @types/v2/drive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { ITargetInfoProps } from "../utils";
* });
* ```
*/
export const Drive: IDrive;
export const drive: Idrive;

/**
* Drive
* The v2.0 REST endpoint.
* @category Drive
*/
export interface IDrive {
export interface Idrive {
/**
* Get the default library for a site.
* @param id - (Optional) The site id to target, current by default.
Expand Down
4 changes: 2 additions & 2 deletions @types/v2/drives.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { ITargetInfoProps } from "../utils";
* });
* ```
*/
export const Drives: IDrives;
export const drives: Idrives;

/**
* Drives
* The v2.0 REST endpoint.
* @category Drives
*/
export interface IDrives {
export interface Idrives {
/**
* Get the libraries for a site.
* @param id - (Optional) The site id to target, current by default.
Expand Down
12 changes: 9 additions & 3 deletions @types/v2/sites.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IBaseExecution } from "gd-sprest-def/lib/base";
import { sites } from "gd-sprest-def/lib/Microsoft/Graph/api";
import { siteMethods } from "gd-sprest-def/lib/Microsoft/Graph/entityTypes";
import { list, listMethods, siteMethods } from "gd-sprest-def/lib/Microsoft/Graph/entityTypes";
import { ITargetInfoProps } from "../utils";

/**
Expand All @@ -17,18 +17,24 @@ import { ITargetInfoProps } from "../utils";
* });
* ```
*/
export const Sites: ISites;
export const sites: Isites;

/**
* Sites
* The v2.0 REST endpoint.
* @category Sites
*/
export interface ISites {
export interface Isites {
/**
* Creates an instance of the site library.
* @param id - (Optional) The site id to target, current by default.
* @param targetInfo - (Optional) The target information.
*/
(id?: string, targetInfo?: ITargetInfoProps): siteMethods & sites;

/** Returns the current web. */
static getCurrentWeb(): IBaseExecution<sites> & siteMethods;

/** Returns a list from the current web. */
static getList(title: string): IBaseExecution<list> & listMethods;
}
30 changes: 18 additions & 12 deletions dist/gd-sprest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ declare module 'gd-sprest' {
/**
* V2 Library Components
*/
import * as LibV2 from "gd-sprest/v2";
export { LibV2 }
import * as v2 from "gd-sprest/v2";
export { v2 }

/**
* $REST Global Variable
Expand Down Expand Up @@ -225,12 +225,12 @@ declare module 'gd-sprest/rest' {
/**
* The default library for a site.
*/
Drive: LibV2Types.IDrive;
drive: LibV2Types.Idrive;

/**
* The libraries for a site.
*/
Drives: LibV2Types.IDrives;
drives: LibV2Types.Idrives;

/**
* Use this api to get the web url from a page url.
Expand Down Expand Up @@ -350,7 +350,7 @@ declare module 'gd-sprest/rest' {
/**
* The graph sites endpoint.
*/
Sites: LibV2Types.ISites;
sites: LibV2Types.Isites;

/**
* Use this api to get the url of a site, by its id.
Expand Down Expand Up @@ -4414,14 +4414,14 @@ declare module 'gd-sprest/v2/drive' {
* });
* ```
*/
export const Drive: IDrive;
export const drive: Idrive;

/**
* Drive
* The v2.0 REST endpoint.
* @category Drive
*/
export interface IDrive {
export interface Idrive {
/**
* Get the default library for a site.
* @param id - (Optional) The site id to target, current by default.
Expand Down Expand Up @@ -4450,14 +4450,14 @@ declare module 'gd-sprest/v2/drives' {
* });
* ```
*/
export const Drives: IDrives;
export const drives: Idrives;

/**
* Drives
* The v2.0 REST endpoint.
* @category Drives
*/
export interface IDrives {
export interface Idrives {
/**
* Get the libraries for a site.
* @param id - (Optional) The site id to target, current by default.
Expand All @@ -4470,7 +4470,7 @@ declare module 'gd-sprest/v2/drives' {
declare module 'gd-sprest/v2/sites' {
import { IBaseExecution } from "gd-sprest-def/lib/base";
import { sites } from "gd-sprest-def/lib/Microsoft/Graph/api";
import { siteMethods } from "gd-sprest-def/lib/Microsoft/Graph/entityTypes";
import { list, listMethods, siteMethods } from "gd-sprest-def/lib/Microsoft/Graph/entityTypes";
import { ITargetInfoProps } from "gd-sprest/utils";

/**
Expand All @@ -4487,20 +4487,26 @@ declare module 'gd-sprest/v2/sites' {
* });
* ```
*/
export const Sites: ISites;
export const sites: Isites;

/**
* Sites
* The v2.0 REST endpoint.
* @category Sites
*/
export interface ISites {
export interface Isites {
/**
* Creates an instance of the site library.
* @param id - (Optional) The site id to target, current by default.
* @param targetInfo - (Optional) The target information.
*/
(id?: string, targetInfo?: ITargetInfoProps): siteMethods & sites;

/** Returns the current web. */
static getCurrentWeb(): IBaseExecution<sites> & siteMethods;

/** Returns a list from the current web. */
static getList(title: string): IBaseExecution<list> & listMethods;
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/gd-sprest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gd-sprest.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gd-sprest",
"version": "7.8.1",
"version": "7.8.2",
"description": "An easy way to develop against the SharePoint REST API.",
"author": "Gunjan Datta <me@dattabase.com> (https://gunjandatta.github.io)",
"license": "MIT",
Expand All @@ -24,7 +24,7 @@
},
"homepage": "https://dattabase.com",
"dependencies": {
"gd-sprest-def": "^1.4.1"
"gd-sprest-def": "^1.4.3"
},
"devDependencies": {
"@babel/core": "^7.18.10",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ import * as Helper from "./helper"
export * from "./lib";
export * from "./rest";
export * from "./sptypes";
import * as V2 from "./v2";
export { Helper, V2 }
import * as v2 from "./v2";
export { Helper, v2 }
24 changes: 16 additions & 8 deletions src/mapper/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ export const MapperV2 = {
requestType: RequestType.Get
}
},
listItems: {
add: {
args: ["values"],
name: "",
requestType: RequestType.PostBodyNoArgs
},
query: { argNames: ["oData"], requestType: RequestType.OData },
},
list: {
activities: {
requestType: RequestType.Get
Expand Down Expand Up @@ -166,10 +158,26 @@ export const MapperV2 = {
requestType: RequestType.Get,
returnType: "fields"
},
query: { argNames: ["oData"], requestType: RequestType.OData },
permissions: {
requestType: RequestType.Get
},
update: {
args: ["values"],
name: "",
requestType: RequestType.PostBodyNoArgs
},
versions: {
requestType: RequestType.GetWithArgsValueOnly,
returnType: "versions"
},
},
listItems: {
add: {
args: ["values"],
name: "",
requestType: RequestType.PostBodyNoArgs
},
query: { argNames: ["oData"], requestType: RequestType.OData },
},
listItemVersion: {
Expand Down
10 changes: 5 additions & 5 deletions src/rest.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as Helper from "./helper";
import * as Lib from "./lib";
import { SPTypes } from "./sptypes";
import * as LibV2 from "./v2";
import { SPTypes } from "./sptypes";
import { IREST } from "../@types";

/**
* SharePoint REST Library
*/
export const $REST: IREST = {
__ver: 7.81,
__ver: 7.82,
AppContext: (siteUrl: string) => { return Lib.Site.getAppContext(siteUrl); },
Apps: Lib.Apps,
ContextInfo: Lib.ContextInfo,
DefaultRequestToHostFl: false,
Drive: LibV2.Drive,
Drives: LibV2.Drives,
drive: LibV2.drive,
drives: LibV2.drives,
GetWebUrlFromPageUrl: Lib.Web.getWebUrlFromPageUrl,
Graph: Lib.Graph,
GroupService: Lib.GroupService,
Expand All @@ -36,7 +36,7 @@ export const $REST: IREST = {
SiteManager: Lib.SiteManager,
SitePages: Lib.SitePages,
SiteExists: (url) => { return Lib.Site.exists(url); },
Sites: LibV2.Sites,
sites: LibV2.sites,
SiteUrl: (id: string) => { return Lib.Site.getUrlById(id); },
SPTypes: SPTypes as any,
SocialFeed: Lib.SocialFeed,
Expand Down
19 changes: 18 additions & 1 deletion src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ export const Helper: IBaseHelper = {
if (obj["results"].length > 0) {
let results = obj["results"];

// See if this is a v2 request, and set the default object type
let objType = null;
if (obj["@odata.context"]) {
// Get the object type
let metadataType = (obj["@odata.context"] || objType);
let values = metadataType.split("_api/v2.0/$metadata#");
if (values.length > 1) {
objType = values[1];
} else {
values = metadataType.split("/");
objType = values[values.length - 1].split("?")[0];
}

// Ensure its not the collection type
objType = objType.replace(/s$/, '');
}

// Parse the results
for (let result of results) {
// Add the base methods
Expand All @@ -291,7 +308,7 @@ export const Helper: IBaseHelper = {
Helper.updateMetadata(obj, result);

// Add the methods
Request.addMethods(result, result);
Request.addMethods(result, result, objType);
}
}
}
Expand Down
27 changes: 20 additions & 7 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { XHRRequest } from "./xhrRequest";
*/
export const Request = {
// Method to add the methods to base object
addMethods: (base: IBase, data, graphType?: string) => {
addMethods: (base: IBase, data, resultsObjType?: string) => {
let obj = base;
let isCollection = data.results && data.results.length > 0;
let methods = null;
Expand All @@ -27,8 +27,21 @@ export const Request = {
let isV2 = obj?.parent?.targetInfo?.endpoint.startsWith("_api/v2.0/") ||
obj["@odata.context"] || objType.startsWith("@odata.context") ? true : false;
if (isV2) {
let values = (obj["@odata.context"] || objType).split("_api/v2.0/$metadata#");
objType = values[values.length > 1 ? 1 : 0];
// See if we are overriding the object type
if (resultsObjType) {
// Set the object type
objType = resultsObjType;
} else {
// Get the object type from the context
let metadataType = (obj["@odata.context"] || objType);
let values = metadataType.split("_api/v2.0/$metadata#");
if (values.length > 1) {
objType = values[1];
} else {
values = metadataType.split("/");
objType = values[values.length - 1].split("?")[0];
}
}

// Get the methods for this object type
methods = MapperV2[objType];
Expand All @@ -37,10 +50,10 @@ export const Request = {
// Else, get the methods from the default mapper, otherwise get it from the custom mapper
else if ((methods = Mapper[objType + (isCollection ? ".Collection" : "")]) == null) {
// Determine the object type
objType = objType.split('/');
objType = (objType[objType.length - 1]);
objType = objType.split('.');
objType = (objType[objType.length - 1]).toLowerCase();
let values = objType.split('/');
objType = values[values.length - 1];
values = objType.split('.');
objType = (values[values.length - 1]).toLowerCase();
objType += isCollection ? "s" : "";

// See if this is a graph request
Expand Down
Loading

0 comments on commit 87f6c2f

Please sign in to comment.