UNPKG

@azure/app-configuration

Version:
57 lines 2.48 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { logger } from "./logger.js"; /** * content-type for the snapshot reference. */ export const snapshotReferenceContentType = 'application/json; profile="https://azconfig.io/mime-profiles/snapshot-ref"; charset=utf-8'; /** * @internal */ export const SnapshotReferenceHelper = { /** * Takes the SnapshotReference (JSON) and returns a ConfigurationSetting (with the props encoded in the value). */ toConfigurationSettingParam: (snapshotReference) => { logger.info("Encoding SnapshotReference value in a ConfigurationSetting:", snapshotReference); if (!snapshotReference.value) { logger.error(`SnapshotReference has an unexpected value`, snapshotReference); throw new TypeError(`SnapshotReference has an unexpected value - ${snapshotReference.value}`); } const jsonSnapshotReferenceValue = { snapshot_name: snapshotReference.value.snapshotName, }; const configSetting = { ...snapshotReference, value: JSON.stringify(jsonSnapshotReferenceValue), }; return configSetting; }, }; /** * Takes the ConfigurationSetting as input and returns the ConfigurationSetting<SnapshotReferenceValue> by parsing the value string. */ export function parseSnapshotReference(setting) { logger.info("[parseSnapshotReference] Parsing the value to return the SnapshotReferenceValue", setting); if (!isSnapshotReference(setting)) { logger.error("Invalid SnapshotReference input", setting); throw TypeError(`Setting with key ${setting.key} is not a valid SnapshotReference, make sure to have the correct content-type and a valid non-null value.`); } const jsonSnapshotReferenceValue = JSON.parse(setting.value); const snapshotReference = { ...setting, value: { snapshotName: jsonSnapshotReferenceValue.snapshot_name }, }; return snapshotReference; } /** * Lets you know if the ConfigurationSetting is a snapshot reference. * * [Checks if the content type is snapshotReferenceContentType `"application/json; profile=\"https://azconfig.io/mime-profiles/snapshot-ref\"; charset=utf-8"`] */ export function isSnapshotReference(setting) { return (setting && setting.contentType === snapshotReferenceContentType && typeof setting.value === "string"); } //# sourceMappingURL=snapshotReference.js.map