Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Version 25

### v25.3.1

- Small optimization for running diagnostics (non-production mode).

### v25.3.0

- Changed bundler from `tsup` to `tsdown`.
Expand Down
33 changes: 19 additions & 14 deletions express-zod-api/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export interface Routing {

export type Parsers = Partial<Record<ContentType, RequestHandler[]>>;

interface InitProps {
app: IRouter;
getLogger: GetLogger;
config: CommonConfig;
routing: Routing;
parsers?: Parsers;
}

const lineUp = (methods: CORSMethod[]) =>
methods // auxiliary methods go last
.sort((a, b) => +isMethod(b) - +isMethod(a) || a.localeCompare(b))
Expand All @@ -50,34 +58,31 @@ const makeCorsHeaders = (accessMethods: CORSMethod[]) => ({

type Siblings = Map<CORSMethod, [RequestHandler[], AbstractEndpoint]>;

export const initRouting = ({
/** This fn exists to reduce the complexity of initRouting and to ensure the disposal of Diagnostics ASAP */
const collectSiblings = ({
app,
getLogger,
config,
routing,
parsers,
}: {
app: IRouter;
getLogger: GetLogger;
config: CommonConfig;
routing: Routing;
parsers?: Parsers;
}) => {
let doc = isProduction() ? undefined : new Diagnostics(getLogger()); // disposable
}: InitProps) => {
const doc = isProduction() ? undefined : new Diagnostics(getLogger());
Comment thread
RobinTail marked this conversation as resolved.
const familiar = new Map<string, Siblings>();
const onEndpoint: OnEndpoint = (method, path, endpoint) => {
if (!isProduction()) {
doc?.checkSchema(endpoint, { path, method });
doc?.checkPathParams(path, endpoint, { method });
}
doc?.checkSchema(endpoint, { path, method });
doc?.checkPathParams(path, endpoint, { method });
const matchingParsers = parsers?.[endpoint.requestType] || [];
const value = R.pair(matchingParsers, endpoint);
if (!familiar.has(path))
familiar.set(path, new Map(config.cors ? [["options", value]] : []));
familiar.get(path)?.set(method, value);
};
Comment thread
RobinTail marked this conversation as resolved.
walkRouting({ routing, onEndpoint, onStatic: app.use.bind(app) });
doc = undefined; // hint for garbage collector
return familiar;
};

export const initRouting = ({ app, config, getLogger, ...rest }: InitProps) => {
const familiar = collectSiblings({ app, getLogger, config, ...rest });
const deprioritized = new Map<string, RequestHandler>();
for (const [path, methods] of familiar) {
const accessMethods = Array.from(methods.keys());
Expand Down
Loading