Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workbook package #3019

Draft
wants to merge 10 commits into
base: version-4
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions packages/graph/behaviors/prefer-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TimelinePipe, delay } from "@pnp/core";
import { errorCheck, InjectHeaders } from "@pnp/queryable";
import { GraphQueryable, _GraphQueryable, graphGet } from "../graphqueryable.js";
import { RichLongRunningOperation } from "@microsoft/microsoft-graph-types";

export function PreferAsync(pollIntervalMs: number = 25000, maxPolls: number = 4): TimelinePipe {
return (instance: _GraphQueryable) => {
instance.using(InjectHeaders({ "Prefer": "respond-async" }));
instance.on.parse(errorCheck);
instance.on.parse(async function (url: URL, response: Response, result: any) {
if (response.status === 202) {
ChapC marked this conversation as resolved.
Show resolved Hide resolved
const opLocation = response.headers.get("Location");
const opId = opLocation.split("/").at(-1);

const statusQuery = GraphQueryable(instance, `operations/${opId}`);
for (let i = 0; i < maxPolls; i++) {
await delay(pollIntervalMs);

const status = await statusQuery<RichLongRunningOperation>();
if (status.status === 'succeeded') {
let resultEndpoint = status.resourceLocation.split("/").at(-1);
result = await graphGet(GraphQueryable(instance, resultEndpoint));
} else if (status.status === 'failed') {
throw status.error;
}
}
throw new Error(`Timed out waiting for async operation after ${pollIntervalMs * maxPolls}ms`);
}

return [url, response, result];
});

return instance;
}
}
18 changes: 18 additions & 0 deletions packages/graph/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ export interface IGetById<R = any, T = string> {
getById(id: T): R;
}

export function getItemAt<R>(factory: (...args: any[]) => R) {
return function <T extends { new(...args: any[]): {} }>(target: T) {
// eslint-disable-next-line @typescript-eslint/ban-types
return class extends target {
public getItemAt(this: IGraphQueryable, index: number): R {
return factory(this, `itemAt(index=${index})`);
}
};
};
}
export interface IGetItemAt<R = any, T = number> {
/**
* Get an item based on its position in the collection.
* @param index Index of the item to be retrieved. Zero-indexed.
*/
getItemAt(index: T): R;
}

/**
* Adds the getByName method to a collection
*/
Expand Down
24 changes: 24 additions & 0 deletions packages/graph/workbooks/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IGraphQueryable } from "../graphqueryable";
import { IRange, Range } from "./types";

/**
* Adds the getRange method to the tagged class
*/
export function getRange() {
// eslint-disable-next-line @typescript-eslint/ban-types
return function <T extends { new(...args: any[]): {} }>(target: T) {

return class extends target {
public getRange(this: IGraphQueryable): IRange {
return Range(this, "range");
}
};
};
}

export interface IGetRange {
/**
* Get the range of cells contained by this element.
*/
getRange(): IRange;
}
32 changes: 32 additions & 0 deletions packages/graph/workbooks/driveitem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { addProp, body, InjectHeaders } from "@pnp/queryable";
import { _DriveItem } from "../files/types.js";
import { IWorkbook, IWorkbookWithSession, Workbook, WorkbookWithSession } from "./types.js";
import { graphPost, GraphQueryable } from "../graphqueryable.js";
import { WorkbookSessionInfo } from "@microsoft/microsoft-graph-types";

declare module "../files/types.js" {
interface _DriveItem {
readonly workbook: IWorkbook;
getWorkbookSession(persistChanges: boolean): Promise<IWorkbookWithSession>;
}
interface DriveItem {
readonly workbook: IWorkbook;
getWorkbookSession(persistChanges: boolean): Promise<IWorkbookWithSession>;
}
}

addProp(_DriveItem, "workbook", Workbook);
_DriveItem.prototype.getWorkbookSession = getWorkbookSession;

export async function getWorkbookSession(this: _DriveItem, persistChanges: boolean): Promise<IWorkbookWithSession> {
const workbook = WorkbookWithSession(this);
const sessionResult = await graphPost<WorkbookSessionInfo>(
GraphQueryable(workbook, 'createSession'), body({
persistChanges
}));

if (!sessionResult.id) throw new Error("createSession did not respond with a session ID");

workbook.using(InjectHeaders({ "workbook-session-id": sessionResult.id }));
return workbook;
}
20 changes: 20 additions & 0 deletions packages/graph/workbooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import "./driveitem.js";

export {
IWorkbook,
IWorksheet,
IWorksheets,
IRange,
ITable,
ITables,
ITableRow,
ITableRows,
ITableColumn,
ITableColumns,
IRangeFormat,
IRangeBorder,
IRangeBorders,
IRangeFill,
IRangeFont,
IRangeFormatProtection
} from "./types.js";
Loading