UNPKG

@oada/well-known-json

Version:

Middleware for JSON Well-Known resources

102 lines 3.88 kB
"use strict"; /** * @license * Copyright 2014-2022 Open Ag Data Alliance * * 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. */ var _WellKnownJSON_base, _WellKnownJSON_json; Object.defineProperty(exports, "__esModule", { value: true }); exports.WellKnownJSON = void 0; const tslib_1 = require("tslib"); const RELATIVE = /^\.\.?\//; function isIterable(value) { return typeof value === 'object' && Symbol.iterator in (value ?? {}); } function hasEntries(value) { return (typeof value === 'object' && // @ts-expect-error stuff typeof value?.entries === 'function'); } function normalize(collection) { const entries = isIterable(collection) ? collection : Object.entries(collection); return new Map(Array.from(entries, ([k, v]) => [ k, new Map(isIterable(v) ? v : Object.entries(v)), ])); } class WellKnownJSON { constructor(resources, options = {}) { _WellKnownJSON_base.set(this, void 0); _WellKnownJSON_json.set(this, void 0); tslib_1.__classPrivateFieldSet(this, _WellKnownJSON_base, options.baseUri, "f"); tslib_1.__classPrivateFieldSet(this, _WellKnownJSON_json, normalize(resources), "f"); } /** * Get a particular well-known resource * * @param name * @param base * @returns */ getResource(name, baseIn, ...rest) { const base = tslib_1.__classPrivateFieldGet(this, _WellKnownJSON_base, "f") ?? baseIn; const match = tslib_1.__classPrivateFieldGet(this, _WellKnownJSON_json, "f").get(name); return resourceify(match); function resourceify(value) { if (!value) { return value; } switch (typeof value) { case 'string': { return RELATIVE.test(value) ? new URL(value, base).toString() : value; } case 'object': { if (!value) { return value; } if (Array.isArray(value)) { return value.map((element) => resourceify(element)); } const entries = hasEntries(value) ? value.entries() : Object.entries(value); return Object.fromEntries(Array.from(entries, ([k, v]) => [k, resourceify(v)])); } case 'function': { /* Call functions */ return resourceify(value(...rest)); } default: { return value; } } } } /** * Add Well-Known resource, merging with any preexisting ones */ addResource(uri, collection) { const entries = isIterable(collection) ? collection : Object.entries(collection); tslib_1.__classPrivateFieldGet(this, _WellKnownJSON_json, "f").set(uri, new Map([...(tslib_1.__classPrivateFieldGet(this, _WellKnownJSON_json, "f").get(uri) ?? []), ...entries])); } } _WellKnownJSON_base = new WeakMap(), _WellKnownJSON_json = new WeakMap(); // eslint-disable-next-line @typescript-eslint/naming-convention WellKnownJSON.METHODS = ['OPTIONS', 'GET', 'HEAD']; exports.WellKnownJSON = WellKnownJSON; //# sourceMappingURL=WellKnownJSON.js.map