Skip to content

How to attach middleware to route tree? #802

Answered by zdllucky
zdllucky asked this question in Q&A
Discussion options

You must be logged in to vote

The answer is: you can configure and reuse same factory several times

/// file1.ts
import contextProvider from "../middlewares/zod/contextProvider";
import { defaultResultHandler, EndpointsFactory } from "express-zod-api";
import { config } from "../index";

export const kCtxFactory = new EndpointsFactory({
  resultHandler: defaultResultHandler,
  config,
}).addMiddleware(contextProvider);

// or use defaultEndpointsFactory^ no difference

/// file2.ts
import { kCtxFactory } from "./file1";


export const get = kCtxFactory.build({/* ... */})
export const post = kCtxFactory.build({/* ... */})
export const patch = kCtxFactory.build({/* ... */})

Felt myself kinda dumb... The answer is very e…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zdllucky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant