How to walk routing #2257
-
Hello, I've tried to walk the routing object, as you do to generate the documentation, but My 2 usages are:
For usage 2, do you see another way to do this ? I already tried with static type analysis, using NestedKeyOf but my ts server was getting awfully slow for autocomplete. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Here's what I came up with to count routes: import { DependsOnMethod, Routing } from "express-zod-api";
const countNbEndpoints = (obj: Routing) => {
let nbEndpoints = 0;
const keys = Object.keys(obj);
// ENDPOINT LEAF
if (keys.length === 0) {
return 1;
}
keys.forEach(key => {
const value = obj[key];
// DEPENDS ON METHOD LEAF
if (value instanceof DependsOnMethod) {
nbEndpoints += value.entries.length;
return;
}
// NODE
if (typeof value === "object" && value !== null) {
nbEndpoints += countNbEndpoints(value as Routing);
}
});
return nbEndpoints;
}; I'm not using |
Beta Was this translation helpful? Give feedback.
-
How about this, @JonathanCabezas ? const documentation = new Documentation({
routing,
config,
version: "1.2.3",
title: "Example API",
serverUrl: "https://example.com",
});
console.log(
Object.keys(documentation.rootDoc.paths!)
); |
Beta Was this translation helpful? Give feedback.
How about this, @JonathanCabezas ?