UNPKG

@azure/app-configuration

Version:
62 lines 2.79 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.SnapshotReferenceHelper = exports.snapshotReferenceContentType = void 0; exports.parseSnapshotReference = parseSnapshotReference; exports.isSnapshotReference = isSnapshotReference; const logger_js_1 = require("./logger.js"); /** * content-type for the snapshot reference. */ exports.snapshotReferenceContentType = 'application/json; profile="https://azconfig.io/mime-profiles/snapshot-ref"; charset=utf-8'; /** * @internal */ exports.SnapshotReferenceHelper = { /** * Takes the SnapshotReference (JSON) and returns a ConfigurationSetting (with the props encoded in the value). */ toConfigurationSettingParam: (snapshotReference) => { logger_js_1.logger.info("Encoding SnapshotReference value in a ConfigurationSetting:", snapshotReference); if (!snapshotReference.value) { logger_js_1.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. */ function parseSnapshotReference(setting) { logger_js_1.logger.info("[parseSnapshotReference] Parsing the value to return the SnapshotReferenceValue", setting); if (!isSnapshotReference(setting)) { logger_js_1.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"`] */ function isSnapshotReference(setting) { return (setting && setting.contentType === exports.snapshotReferenceContentType && typeof setting.value === "string"); } //# sourceMappingURL=snapshotReference.js.map