UNPKG

@microsoft/kiota-serialization-text

Version:

Implementation of Kiota Serialization interfaces for text

125 lines 5.04 kB
/** * ------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. * See License in the project root for license information. * ------------------------------------------------------------------------------------------- */ /* eslint-disable @typescript-eslint/no-unused-vars */ import { inNodeEnv } from "@microsoft/kiota-abstractions"; export class TextSerializationWriter { constructor() { this.writer = []; this.writeStringValue = (key, value) => { if (key || key !== "") { throw new Error(TextSerializationWriter.noStructuredDataMessage); } if (value !== undefined) { if (this.writer.length > 0) { throw new Error("a value was already written for this serialization writer, text content only supports a single value"); } else { const isNullValue = value === null; this.writer.push(isNullValue ? "null" : value); } } }; this.writeBooleanValue = (key, value) => { if (value !== undefined) { this.writeStringValue(key, `${value}`); } }; this.writeNumberValue = (key, value) => { if (value === null) { return this.writeNullValue(key); } if (value) { this.writeStringValue(key, `${value}`); } }; this.writeGuidValue = (key, value) => { if (value === null) { return this.writeNullValue(key); } if (value) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions this.writeStringValue(key, `"${value}"`); } }; this.writeDateValue = (key, value) => { if (value === null) { return this.writeNullValue(key); } if (value) { this.writeStringValue(key, `"${value.toISOString()}"`); } }; this.writeDateOnlyValue = (key, value) => { if (value === null) { return this.writeNullValue(key); } if (value) { this.writeStringValue(key, `"${value.toString()}"`); } }; this.writeTimeOnlyValue = (key, value) => { if (value === null) { return this.writeNullValue(key); } if (value) { this.writeStringValue(key, `"${value.toString()}"`); } }; this.writeDurationValue = (key, value) => { if (value === null) { return this.writeNullValue(key); } if (value) { this.writeStringValue(key, `"${value.toString()}"`); } }; this.writeNullValue = (key) => { this.writeStringValue(key, `null`); }; this.writeCollectionOfPrimitiveValues = (key, values) => { throw new Error(TextSerializationWriter.noStructuredDataMessage); }; this.writeCollectionOfObjectValues = (key, values, serializerMethod) => { throw new Error(TextSerializationWriter.noStructuredDataMessage); }; this.writeObjectValue = (key, value, serializerMethod) => { throw new Error(TextSerializationWriter.noStructuredDataMessage); }; this.writeEnumValue = (key, ...values) => { if (values.length > 0) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions const rawValues = values.filter((x) => x !== undefined).map((x) => `${x}`); if (rawValues.length > 0) { this.writeStringValue(key, rawValues.reduce((x, y) => `${x},${y}`)); } } }; this.writeCollectionOfEnumValues = (key, values) => { this.writeEnumValue(key, values); }; this.getSerializedContent = () => { return this.convertStringToArrayBuffer(this.writer.join(``)); }; this.convertStringToArrayBuffer = (str) => { const encoder = new TextEncoder(); const encodedString = encoder.encode(str); return encodedString.buffer; }; this.writeAdditionalData = (value) => { throw new Error(TextSerializationWriter.noStructuredDataMessage); }; } writeByteArrayValue(key, value) { if (!value) { return; } const b64 = inNodeEnv() ? Buffer.from(value).toString("base64") : btoa(new TextDecoder().decode(value)); this.writeStringValue(key, b64); } } TextSerializationWriter.noStructuredDataMessage = "text does not support structured data"; //# sourceMappingURL=textSerializationWriter.js.map