in src/parse.ts [271:331]
export async function loadSchema(filename_or_object: string | object) {
let spec: Model;
if (typeof filename_or_object === "string") {
spec = JSON.parse(
await readFile(filename_or_object, { encoding: "utf-8" }),
) as Model;
} else {
spec = filename_or_object as Model;
}
if (router.find("GET", "/") != undefined) {
// start from a clean router
router = Router.make<ESRoute>({
ignoreTrailingSlash: true,
maxParamLength: 1000,
});
}
for (const endpoint of spec.endpoints) {
for (const url of endpoint.urls) {
const { path, methods } = url;
let formattedPath = path
.split("/")
.map((p) => (p.startsWith("{") ? `:${p.slice(1, -1)}` : p))
.join("/");
/* istanbul ignore next */
if (!formattedPath.startsWith("/")) {
formattedPath = "/" + formattedPath;
}
// find the request in the spec
try {
let req: Request | undefined;
for (const type of spec.types) {
if (
type.name.namespace == endpoint.request?.namespace &&
type.name.name == endpoint.request?.name
) {
if (type.kind != "request") {
/* istanbul ignore next */
throw new Error(
`Unexpected request type ${type.kind} for URL ${url}`,
);
}
req = type as Request;
break;
}
}
const r = {
name: endpoint.name,
request: req as Request,
};
router.on(methods, formattedPath as Router.PathInput, r);
} catch (err) {
// in some cases there are routes that have the same url but different
// dynamic parameters, which causes find-my-way to fail
}
}
}
}