UNPKG

@microsoft/mgt

Version:
90 lines 3.72 kB
/** * ------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. * See License in the project root for license information. * ------------------------------------------------------------------------------------------- */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { BatchRequestContent } from '@microsoft/microsoft-graph-client'; import { BatchRequest } from './BatchRequest'; import { prepScopes } from './GraphHelpers'; /** * Method to reduce repetitive requests to the Graph * * @export * @class Batch */ export class Batch { constructor(client) { this.requests = new Map(); this.scopes = []; this.client = client; } /** * sets new request and scopes * * @param {string} id * @param {string} resource * @param {string[]} [scopes] * @memberof Batch */ get(id, resource, scopes) { const request = new BatchRequest(resource, 'GET'); this.requests.set(id, request); if (scopes) { this.scopes = this.scopes.concat(scopes); } } /** * Promise to handle Graph request response * * @returns {Promise<any>} * @memberof Batch */ execute() { return __awaiter(this, void 0, void 0, function* () { const responses = {}; if (!this.requests.size) { return responses; } const batchRequestContent = new BatchRequestContent(); for (const request of this.requests) { batchRequestContent.addRequest({ id: request[0], request: new Request(Batch.baseUrl + request[1].resource, { method: request[1].method }) }); } let batchRequest = this.client.api('$batch').version('beta'); if (this.scopes.length) { batchRequest = batchRequest.middlewareOptions(prepScopes(...this.scopes)); } const batchResponse = yield batchRequest.post(yield batchRequestContent.getContent()); for (const response of batchResponse.responses) { if (response.status !== 200) { response[response.id] = null; } else if (response.headers['Content-Type'].includes('image/jpeg')) { responses[response.id] = 'data:image/jpeg;base64,' + response.body; } else { responses[response.id] = response.body; } } return responses; }); } } // this doesn't really mater what it is as long as it's a root base url // otherwise a Request assumes the current path and that could change the relative path Batch.baseUrl = 'https://graph.microsoft.com'; //# sourceMappingURL=Batch.js.map