UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

91 lines 2.92 kB
"use strict"; /* * Copyright © 2020 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractPreferenceStore = void 0; const preferenceStore_1 = require("../../api/context/preferenceStore"); /** * Abstract PreferenceStore implementation to handle ttl and key scoping */ class AbstractPreferenceStore { constructor(ctx) { this.ctx = ctx; } async get(key, options = {}) { const pref = await this.doGet(key, this.scope(options.scope)); const defaultValue = !!options ? options.defaultValue : undefined; if (!pref) { return defaultValue; } if (!!pref.ttl && pref.ttl < Date.now()) { return defaultValue; } else { return JSON.parse(pref.value); } } async put(key, value, options = {}) { const pref = { name: key, namespace: this.scope(options.scope), value: JSON.stringify(value), ttl: options.ttl, }; await this.doPut(pref); return value; } async list(scope) { const prefs = await this.doList(this.scope(scope)); if (!prefs) { return []; } else { const values = prefs.map(pref => { if (!!pref.ttl && pref.ttl < Date.now()) { return undefined; } else { return { key: pref.name, value: JSON.parse(pref.value) }; } }); return values.filter(v => !!v); } } async delete(key, options = {}) { return this.doDelete(key, this.scope(options.scope)); } scopeKey(key, scope) { if (!!scope && scope.length > 0) { return `${scope}_$_${key}`; } return key; } scope(scope) { if (!!scope) { switch (scope) { case preferenceStore_1.PreferenceScope.Sdm: return this.ctx.configuration.name; case preferenceStore_1.PreferenceScope.Workspace: return ""; default: return scope; } } return ""; } } exports.AbstractPreferenceStore = AbstractPreferenceStore; //# sourceMappingURL=AbstractPreferenceStore.js.map