UNPKG

@whook/whook

Version:

Build strong and efficient REST web services.

103 lines 3.98 kB
import { autoService, location } from 'knifecycle'; import { ensureResolvedObject, pathItemToOperationMap, isValidOpenAPIPath, isValidOpenAPIMethod, } from 'ya-open-api-types'; async function removeMutedParameters(API, parameters, mutedParameters) { const filteredParameters = []; for (const parameter of parameters) { const dereferencedParameter = await ensureResolvedObject(API, parameter); if (mutedParameters.includes(dereferencedParameter.name)) { continue; } filteredParameters.push(parameter); } return filteredParameters; } /* Architecture Note #3: the routes Routes are services that provide a definition and implements API routes. */ export const definition = { path: '/openAPI', method: 'get', operation: { operationId: 'getOpenAPI', summary: 'Get the API documentation.', tags: ['system'], // Here we do not declare the OpenAPI JSON Schema to KISS responses: { '200': { description: 'Provides the private Open API documentation', content: { 'application/json': { schema: { type: 'object', }, }, }, }, }, }, }; async function initGetOpenAPI({ API }) { const getOpenAPI = async ({ query: { mutedMethods = ['options'], mutedParameters = [], mutedTags = [] }, options: { authenticated = false } = { authenticated: false }, }) => { const tagIsPresent = {}; const newPaths = {}; for (const [path, pathItem] of Object.entries(API.paths || {})) { if (!isValidOpenAPIPath(path)) { continue; } for (const [method, operation] of Object.entries(pathItemToOperationMap(pathItem || {}))) { if (!isValidOpenAPIMethod(method)) { continue; } if (mutedMethods.includes(method)) { continue; } if (operation.tags?.every((tag) => mutedTags.includes(tag))) { continue; } if (typeof operation['x-whook'] === 'object' && operation['x-whook'] && 'private' in operation['x-whook'] && operation['x-whook'].private && !authenticated) { continue; } for (const tag of operation?.tags || []) { tagIsPresent[tag] = true; } newPaths[path] = { ...newPaths[path], ...(API?.paths?.[path]?.parameters ? { parameters: await removeMutedParameters(API, API.paths[path].parameters, mutedParameters), } : {}), [method]: { ...(API?.paths?.[path]?.[method] || {}), ...(operation.parameters && operation.parameters.length && { parameters: await removeMutedParameters(API, operation.parameters, mutedParameters), }), ...(authenticated ? {} : { 'x-whook': undefined, }), }, }; } } const CLEANED_API = { ...API, paths: newPaths, tags: API.tags ? API.tags.filter((tag) => tagIsPresent[tag.name]) : [], }; return { status: 200, body: CLEANED_API, }; }; return getOpenAPI; } export default location(autoService(initGetOpenAPI), import.meta.url); //# sourceMappingURL=getOpenAPI.js.map