Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
61 changes: 61 additions & 0 deletions spec/development/workload/PageIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { assert } from "chai";
import { Event } from "microsoft-graph";

import { PageIterator, PageIteratorCallback } from "../../../src/tasks/PageIterator";
import { getClient } from "../test-helper";

const client = getClient();
describe("PageIterator", function() {
describe("sameHeadersPassedWithPageIterator", () => {
const pstHeader = { Prefer: 'outlook.timezone= "pacific standard time"' };
const utc = "UTC";
const pst = "Pacific Standard Time";

it("verify same headers", async () => {
const response = await client
.api(`/me/events?$top=2`)
.headers(pstHeader)
.select("id,start,end")
.get();

const callback: PageIteratorCallback = (eventResponse) => {
const event = eventResponse as Event;
assert.equal(event.start.timeZone, pst);
return true;
};
var requestOptions = { headers: pstHeader };
if (response["@odata.nextLink"]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there isn't a next link then we'll just assume things have passed but haven't actually tested the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a before hook to make sure that certain number of events are present/created.

const pageIterator = new PageIterator(client, response, callback, requestOptions);
await pageIterator.iterate();
assert.isTrue(pageIterator.isComplete());
}
});

it("differentHeadersPassedWithPageIterator", async () => {
const response = await client
.api(`/me/events?$top=4`)
.headers({ Prefer: `outlook.timezone= "${utc}"` })
.select("id,start,end")
.get();

let counter = 0;
const callback: PageIteratorCallback = (eventResponse) => {
const event = eventResponse as Event;
if (counter < 4) {
assert.equal(event.start.timeZone, utc);
counter++;
} else {
assert.equal(event.start.timeZone, pst);
}
return true;
};

var requestOptions = { headers: pstHeader };
if (response["@odata.nextLink"]) {
const pageIterator = new PageIterator(client, response, callback, requestOptions);
await pageIterator.iterate();
assert.isTrue(pageIterator.isComplete());
}
});
});
});
8 changes: 3 additions & 5 deletions src/GraphRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export class GraphRequest {
* @private
* A member to hold custom header options for a request
*/
private _headers: {
[key: string]: string;
};
private _headers: HeadersInit;

/**
* @private
Expand Down Expand Up @@ -428,10 +426,10 @@ export class GraphRequest {
/**
* @public
* Sets the custom headers for a request
* @param {KeyValuePairObjectStringNumber} headers - The headers key value pair object
* @param {KeyValuePairObjectStringNumber | HeadersInit} headers - The request headers
* @returns The same GraphRequest instance that is being called with
*/
public headers(headers: KeyValuePairObjectStringNumber): GraphRequest {
public headers(headers: KeyValuePairObjectStringNumber | HeadersInit): GraphRequest {
for (const key in headers) {
if (headers.hasOwnProperty(key)) {
this._headers[key] = headers[key] as string;
Expand Down
45 changes: 43 additions & 2 deletions src/tasks/PageIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
* @module PageIterator
*/

import { FetchOptions } from "../IFetchOptions";
import { Client } from "../index";
import { MiddlewareOptions } from "../middleware/options/IMiddlewareOptions";
import { ResponseType } from "../ResponseType";

/**
* Signature representing PageCollection
Expand All @@ -25,6 +28,21 @@ export interface PageCollection {
[Key: string]: any;
}

/**
* Signature to define the request options to be sent during request.
* The values of the RequestOptions properties are passed to the Graph Request object.
* @property {HeadersInit} headers - the header options for the request
* @property {MiddlewareOptions[]} middlewareoptions - The middleware options for the request
* @property {FetchOptions} options - The fetch options for the request
* @property {ResponseType} responseType - The response type expected
*/
export interface RequestOptions {
headers?: HeadersInit;
middlewareOptions?: MiddlewareOptions[];
options?: FetchOptions;
responseType?: ResponseType;
}

/**
* Signature representing callback for page iterator
* @property {Function} callback - The callback function which should return boolean to continue the continue/stop the iteration.
Expand Down Expand Up @@ -73,22 +91,29 @@ export class PageIterator {
*/
private complete: boolean;

/**
* Information to be added to the request
*/
private requestOptions: RequestOptions;

/**
* @public
* @constructor
* Creates new instance for PageIterator
* @param {Client} client - The graph client instance
* @param {PageCollection} pageCollection - The page collection object
* @param {PageIteratorCallback} callBack - The callback function
* @param {RequestOptions} requestOptions - The request options
* @returns An instance of a PageIterator
*/
public constructor(client: Client, pageCollection: PageCollection, callback: PageIteratorCallback) {
public constructor(client: Client, pageCollection: PageCollection, callback: PageIteratorCallback, requestOptions?: RequestOptions) {
this.client = client;
this.collection = pageCollection.value;
this.nextLink = pageCollection["@odata.nextLink"];
this.deltaLink = pageCollection["@odata.deltaLink"];
this.callback = callback;
this.complete = false;
this.requestOptions = requestOptions;
}

/**
Expand Down Expand Up @@ -116,7 +141,23 @@ export class PageIterator {
*/
private async fetchAndUpdateNextPageData(): Promise<any> {
try {
const response: PageCollection = await this.client.api(this.nextLink).get();
let graphRequest = this.client.api(this.nextLink);
if (this.requestOptions) {
if (this.requestOptions.headers) {
graphRequest = graphRequest.headers(this.requestOptions.headers);
}
if (this.requestOptions.middlewareOptions) {
graphRequest = graphRequest.middlewareOptions(this.requestOptions.middlewareOptions);
}
if (this.requestOptions.responseType) {
graphRequest = graphRequest.responseType(this.requestOptions.responseType);
}
if (this.requestOptions.options) {
graphRequest = graphRequest.options(this.requestOptions.options);
}
}

const response: PageCollection = await graphRequest.get();
this.collection = response.value;
this.nextLink = response["@odata.nextLink"];
this.deltaLink = response["@odata.deltaLink"];
Expand Down