@microsoft/msgraph-sdk-core
Version:
Core functionalities for the Microsoft Graph JavaScript SDK
104 lines • 3.91 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @class
* Class that handles BatchResponseContent
*/
export class BatchResponseContent {
/**
* @public
* @constructor
* Creates the BatchResponseContent instance
* @param {BatchResponseBody} response - The response body returned for batch request from server
* @returns An instance of a BatchResponseContent
*/
constructor(response) {
this.responses = new Map();
this.update(response);
}
/**
* @private
* Updates the Batch response content instance with given responses.
* @param {BatchResponseBody} response - The response json representing batch response message
* @returns Nothing
*/
update(response) {
const responses = response.responses;
for (let i = 0, l = responses.length; i < l; i++) {
this.responses.set(responses[i].id, responses[i]);
}
}
/**
* @public
* To get the response of a request for a given request id
* @param {string} requestId - The request id value
* @returns The Response object instance for the particular request
*/
getResponseById(requestId) {
return this.responses.get(requestId);
}
/**
* Retrieves a parsable response by request ID.
* @template T - The type of the parsable response.
* @param {string} requestId - The request ID value.
* @param parseNodeFactoryRegistry - The registry to create parse nodes.
* @param {ParsableFactory<T>} factory - The factory to create the parsable response.
* @returns {T | undefined} The parsable response object instance for the particular request, or undefined if not found.
*/
getParsableResponseById(requestId, parseNodeFactoryRegistry, factory) {
const res = this.responses.get(requestId);
if (res === null || res === void 0 ? void 0 : res.body) {
const result = parseNodeFactoryRegistry.deserializeFromJson(res.body, factory);
return result;
}
return undefined;
}
/**
* @public
* To get all the responses of the batch request
* @returns The Map object containing the response objects
*/
getResponses() {
return this.responses;
}
/**
* @public
* To get the iterator for the responses
* @returns The Iterable generator for the response objects
*/
*getResponsesIterator() {
const iterator = this.responses.entries();
let cur = iterator.next();
while (!cur.done) {
yield cur.value;
cur = iterator.next();
}
}
/**
* Retrieves the status codes of all responses in the batch request.
* @returns {Promise<Map<string, number>>} A promise that resolves to a map of request IDs to their status codes.
* @throws {Error} If a status code is not found for a request ID.
*/
getResponsesStatusCodes() {
return new Promise((resolve, reject) => {
const statusCodes = new Map();
const iterator = this.responses.entries();
let cur = iterator.next();
while (!cur.done) {
const [key, value] = cur.value;
if (value.status === undefined) {
reject(new Error(`Status code not found for request ID: ${key}`));
return;
}
statusCodes.set(key, value.status);
cur = iterator.next();
}
resolve(statusCodes);
});
}
}
//# sourceMappingURL=BatchResponseContent.js.map