UNPKG

counterfact

Version:

a library for building a fake REST API for testing

20 lines (19 loc) 665 B
import yaml from "js-yaml"; import { readFile } from "../util/read-file.js"; export function openapiMiddleware(openApiPath, url) { return async (ctx, next) => { if (ctx.URL.pathname === "/counterfact/openapi") { const openApiDocument = (await yaml.load(await readFile(openApiPath))); openApiDocument.servers ??= []; openApiDocument.servers.unshift({ description: "Counterfact", url, }); // OpenApi 2 support: openApiDocument.host = url; ctx.body = yaml.dump(openApiDocument); return; } await next(); }; }