UNPKG

@microsoft/msgraph-sdk-core

Version:
67 lines 3.16 kB
import { HttpMethod, RequestInformation } from "@microsoft/kiota-abstractions"; import { createBatchResponseContentFromDiscriminatorValue, serializeBatchRequestBody, } from "./BatchRequestStep.js"; import { BatchResponseContent } from "./BatchResponseContent.js"; import { BatchResponseContentCollection } from "./BatchResponseContentCollection.js"; export class BatchRequestBuilder { /** * Creates an instance of BatchRequestContent. * @param {RequestAdapter} requestAdapter - The request adapter to be used for executing the requests. * @param {ErrorMappings} errorMappings - The error mappings to be used while deserializing the response. * @throws {Error} If the request adapter is undefined. * @throws {Error} If the error mappings are undefined. */ constructor(requestAdapter, errorMappings) { if (!requestAdapter) { const error = new Error("Request adapter is undefined, Please provide a valid request adapter"); error.name = "Invalid Request Adapter Error"; throw error; } this.requestAdapter = requestAdapter; if (!errorMappings) { const error = new Error("Error mappings are undefined, Please provide a valid error mappings"); error.name = "Invalid Error Mappings Error"; throw error; } this.errorMappings = errorMappings; } /** * @public * @async * Executes the batch request */ async postBatchResponseContentAsync(batchRequestContent) { const requestInformation = new RequestInformation(); requestInformation.httpMethod = HttpMethod.POST; requestInformation.urlTemplate = "{+baseurl}/$batch"; const content = batchRequestContent.getContent(); requestInformation.setContentFromParsable(this.requestAdapter, "application/json", content, serializeBatchRequestBody); requestInformation.headers.add("Content-Type", "application/json"); const result = await this.requestAdapter.send(requestInformation, createBatchResponseContentFromDiscriminatorValue, this.errorMappings); if (result === undefined) { return undefined; } else { return new BatchResponseContent(result); } } /** * Executes the batch requests asynchronously. * * @returns {Promise<BatchResponseContent | undefined>} A promise that resolves to the batch response content or undefined. * @throws {Error} If the batch limit is exceeded. */ async postBatchRequestContentCollectionAsync(collection) { // chuck the batch requests into smaller batches const batches = collection.getBatchResponseContents(); // loop over batches and create batch request body const batchResponseBody = []; for (const requestContent of batches) { const response = await requestContent.postAsync(); if (response) { batchResponseBody.push(response); } } return new BatchResponseContentCollection(batchResponseBody); } } //# sourceMappingURL=BatchRequestBuilder.js.map