UNPKG

@microsoft/msgraph-sdk-core

Version:
130 lines 4.83 kB
/** * ------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. * See License in the project root for license information. * ------------------------------------------------------------------------------------------- */ /** * @module BatchItem */ /** * @interface * Signature represents key value pair object */ import { createUntypedNodeFromDiscriminatorValue, createGuid, } from "@microsoft/kiota-abstractions"; import { defaultUrlReplacementPairs } from "../utils/Constants.js"; /** * Serializes the batch request body * @param writer * @param batchRequestBody */ export const serializeBatchRequestBody = (writer, batchRequestBody = {}) => { if (batchRequestBody) { writer.writeCollectionOfObjectValues("requests", batchRequestBody.requests, serializeBatchItem); } }; /** * Serializes the batch item * @param writer * @param batchRequestData */ export const serializeBatchItem = (writer, batchRequestData = {}) => { var _a; if (batchRequestData) { writer.writeStringValue("id", batchRequestData.id); writer.writeStringValue("method", batchRequestData.method); writer.writeStringValue("url", batchRequestData.url); writer.writeObjectValue("headers", batchRequestData.headers); // get contentType from headers // N:B watch out for text encoding as it might not be utf-8 const body = batchRequestData.body; if (body) { const contentType = (_a = batchRequestData.headers) === null || _a === void 0 ? void 0 : _a["Content-Type"]; if (contentType === "application/json") { // convert body to json writer.writeObjectValue("body", JSON.parse(new TextDecoder().decode(body))); } else { writer.writeByteArrayValue("body", body); } } writer.writeCollectionOfPrimitiveValues("dependsOn", batchRequestData.dependsOn); } }; /** * BatchResponseCollection ParsableFactory * @param _parseNode */ export const createBatchResponseContentFromDiscriminatorValue = (_parseNode) => { return deserializeIntoBatchResponseContent; }; /** * Deserializes the batch response body * @param batchResponseBody */ export const deserializeIntoBatchResponseContent = (batchResponseBody = {}) => { return { responses: n => { batchResponseBody.responses = n.getCollectionOfObjectValues(createBatchResponseFromDiscriminatorValue); }, }; }; /** * BatchRequestStep ParsableFactory * @param _parseNode */ export const createBatchResponseFromDiscriminatorValue = (_parseNode) => { return deserializeIntoBatchResponse; }; /** * Deserializes the batch item * @param batchResponse */ export const deserializeIntoBatchResponse = (batchResponse = {}) => { return { id: n => { batchResponse.id = n.getStringValue(); }, headers: n => { batchResponse.headers = n.getObjectValue(createUntypedNodeFromDiscriminatorValue); }, body: n => { batchResponse.body = n.getByteArrayValue(); }, status: n => { batchResponse.status = n.getNumberValue(); }, }; }; /** * Converts a `RequestInformation` object to a `BatchRequestStep`. * @param {RequestAdapter} requestAdapter - The request adapter containing the base URL. * @param {RequestInformation} requestInformation - The request information to convert. * @param {string} [batchId] - Optional batch ID to use for the `BatchRequestStep`. * @returns {BatchRequestStep} The converted `BatchRequestStep`. */ export const convertRequestInformationToBatchItem = (requestAdapter, requestInformation, batchId) => { var _a; if (requestInformation.pathParameters && requestInformation.pathParameters.baseurl === undefined) { requestInformation.pathParameters.baseurl = requestAdapter.baseUrl; } let uriString = requestInformation.URL; Object.keys(defaultUrlReplacementPairs).forEach(replacementKey => { uriString = uriString.replace(replacementKey, defaultUrlReplacementPairs[replacementKey]); }); const body = requestInformation.content; let headers; if (headers !== undefined) { headers = Object.fromEntries(requestInformation.headers.entries()); } const url = uriString.replace(requestAdapter.baseUrl, ""); const method = (_a = requestInformation.httpMethod) === null || _a === void 0 ? void 0 : _a.toString(); return { id: batchId !== null && batchId !== void 0 ? batchId : createGuid(), method: method, url, headers, body, }; }; //# sourceMappingURL=BatchRequestStep.js.map