UNPKG

@oada/well-known-json

Version:

Middleware for JSON Well-Known resources

54 lines 2.13 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = void 0; const tslib_1 = require("tslib"); const fastify_plugin_1 = tslib_1.__importDefault(require("fastify-plugin")); const WellKnownJSON_js_1 = require("./WellKnownJSON.js"); exports.plugin = (0, fastify_plugin_1.default)(async (fastify, { resources = {}, ...options }) => { const wkj = new WellKnownJSON_js_1.WellKnownJSON(resources, options); const method = Array.from(options.methods ?? WellKnownJSON_js_1.WellKnownJSON.METHODS); fastify.route({ url: '/.well-known/:name', method, handler(request, reply) { const accepts = request.accepts(); if (!accepts.type(['json'])) { return reply.status(406).send('Not Acceptable'); } if (options.headers) { void reply.headers(options.headers); } const { name } = request.params; const base = `${request.protocol}://${request.hostname}`; const resource = wkj.getResource(name, base, request, reply); if (resource === undefined) { return reply.code(404).send(); } return reply.type('application/json').code(200).send(resource); }, }); }, { fastify: '>=3', name: 'well-known-json', dependencies: ['@fastify/cors', '@fastify/accepts'], decorators: { request: ['accepts'], }, }); //# sourceMappingURL=plugin.js.map