UNPKG

@microsoft/kiota-serialization-form

Version:

Implementation of Kiota Serialization interfaces for URI from encoded

164 lines 6.82 kB
/** * ------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. * See License in the project root for license information. * ------------------------------------------------------------------------------------------- */ import { createBackedModelProxyHandler, DateOnly, Duration, parseGuidString, TimeOnly, isBackingStoreEnabled, getEnumValueFromStringValue } from "@microsoft/kiota-abstractions"; export class FormParseNode { /** * Creates a new instance of FormParseNode * @param _rawString the raw string to parse * @param backingStoreFactory the factory to create backing stores */ constructor(_rawString, backingStoreFactory) { this._rawString = _rawString; this.backingStoreFactory = backingStoreFactory; this._fields = {}; this.getStringValue = () => this.getStringValueFromRaw(this._rawString); this.getChildNode = (identifier) => { if (this._fields[identifier]) { return new FormParseNode(this._fields[identifier], this.backingStoreFactory); } return undefined; }; this.getBooleanValue = () => this.getBooleanValueFromRaw(this._rawString); this.getNumberValue = () => this.getNumberValueFromRaw(this._rawString); this.getGuidValue = () => this.getGuidValueFromRaw(this._rawString); this.getDateValue = () => this.getDateValueFromRaw(this._rawString); this.getDateOnlyValue = () => this.getDateOnlyValueFromRaw(this._rawString); this.getTimeOnlyValue = () => this.getTimeOnlyValueFromRaw(this._rawString); this.getDurationValue = () => this.getDurationValueFromRaw(this._rawString); this.getCollectionOfPrimitiveValues = () => { const values = this._rawString.split(","); return values.map((x) => { const typeOfX = typeof x; if (typeOfX === "boolean") { return this.getBooleanValueFromRaw(x); } else if (typeOfX === "string") { return this.getStringValueFromRaw(x); } else if (typeOfX === "number") { return this.getNumberValueFromRaw(x); } else if (x instanceof Date) { return this.getDateValueFromRaw(x); } else if (x instanceof DateOnly) { return this.getDateOnlyValueFromRaw(x); } else if (x instanceof TimeOnly) { return this.getTimeOnlyValueFromRaw(x); } else if (x instanceof Duration) { return this.getDurationValueFromRaw(x); } else { throw new Error(`encountered an unknown type during deserialization ${typeof x}`); } }); }; this.getCollectionOfObjectValues = ( // eslint-disable-next-line @typescript-eslint/no-unused-vars parsableFactory) => { throw new Error(`serialization of collections is not supported with URI encoding`); }; this.getObjectValue = (parsableFactory) => { const temp = {}; const enableBackingStore = isBackingStoreEnabled(parsableFactory(this)(temp)); const value = enableBackingStore && this.backingStoreFactory ? new Proxy(temp, createBackedModelProxyHandler(this.backingStoreFactory)) : temp; if (this.onBeforeAssignFieldValues) { this.onBeforeAssignFieldValues(value); } this.assignFieldValues(value, parsableFactory); if (this.onAfterAssignFieldValues) { this.onAfterAssignFieldValues(value); } return value; }; this.getCollectionOfEnumValues = (type) => { const rawValues = this.getStringValue(); if (!rawValues) { return []; } // eslint-disable-next-line @typescript-eslint/no-unsafe-argument return rawValues.split(",").map((x) => getEnumValueFromStringValue(x, type)); }; this.getEnumValue = (type) => { const rawValue = this.getStringValue(); if (!rawValue) { return undefined; } return getEnumValueFromStringValue(rawValue, type); }; if (!_rawString) { throw new Error("rawString cannot be undefined"); } _rawString .split("&") .map((x) => x.split("=")) .filter((x) => x.length === 2) .forEach((x) => { const key = this.normalizeKey(x[0]); if (this._fields[key]) { this._fields[key] += "," + x[1]; } else { this._fields[key] = x[1]; } }); } normalizeKey(key) { return decodeURIComponent(key).trim(); } getStringValueFromRaw(value) { return decodeURIComponent(value); } getBooleanValueFromRaw(value) { const decoded = this.getStringValueFromRaw(value).toLowerCase(); if (decoded === "true" || decoded === "1") { return true; } else if (decoded === "false" || decoded === "0") { return false; } return undefined; } getNumberValueFromRaw(value) { return parseFloat(this.getStringValueFromRaw(value)); } getGuidValueFromRaw(value) { return parseGuidString(this.getStringValueFromRaw(value)); } getDateValueFromRaw(value) { return new Date(Date.parse(this.getStringValueFromRaw(value))); } getDateOnlyValueFromRaw(value) { return DateOnly.parse(this.getStringValueFromRaw(value)); } getTimeOnlyValueFromRaw(value) { return TimeOnly.parse(this.getStringValueFromRaw(value)); } getDurationValueFromRaw(value) { return Duration.parse(this.getStringValueFromRaw(value)); } getByteArrayValue() { throw new Error("serialization of byt arrays is not supported with URI encoding"); } assignFieldValues(model, parsableFactory) { const fields = parsableFactory(this)(model); Object.entries(this._fields) .filter((x) => !/^null$/i.test(x[1])) .forEach(([k, v]) => { const deserializer = fields[k]; if (deserializer) { deserializer(new FormParseNode(v, this.backingStoreFactory)); } else { model[k] = v; } }); } } //# sourceMappingURL=formParseNode.js.map