Skip to content

Commit

Permalink
fix: auto typing generation
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Oct 9, 2024
1 parent a2c3747 commit e19a15c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/universal-middleware/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const typesByServer: Record<
handler?: string;
selfImports?: string[];
outContext?: (type: string) => string;
generics?: (type: string) => string;
typeHandler?: string;
typeMiddleware?: string;
target?: string;
Expand All @@ -190,6 +191,7 @@ const typesByServer: Record<
fastify: {
middleware: "FastifyMiddleware",
handler: "FastifyHandler",
generics: (type) => (type === "handler" ? "Args, InContext" : "Args, InContext, OutContext"),
},
h3: {
middleware: "H3Middleware",
Expand All @@ -200,6 +202,7 @@ const typesByServer: Record<
handler: "WebrouteHandler",
selfImports: ["type MiddlewareFactoryDataResult"],
outContext: (type) => `MiddlewareFactoryDataResult<typeof ${type}>`,
generics: (type) => (type === "handler" ? "Args, InContext" : "Args, InContext, OutContext"),
},
"cloudflare-worker": {
handler: "CloudflareHandler",
Expand Down Expand Up @@ -237,6 +240,7 @@ function loadDts(id: string, resolve?: (handler: string, type: string) => string
const info = typesByServer[target as (typeof defaultWrappers)[number]];
const fn = type === "handler" ? (info.typeHandler ?? "createHandler") : (info.typeMiddleware ?? "createMiddleware");
const t = info[type as "middleware" | "handler"];
const generics = info.generics ? info.generics(type) : type === "handler" ? "Args" : "Args, InContext, OutContext";
if (t === undefined) return;

const selfImports = [fn, `type ${t}`, ...(info.selfImports ?? [])];
Expand All @@ -248,7 +252,7 @@ type ExtractInContext<T> = T extends (...args: any[]) => UniversalMiddleware<inf
export type InContext = ExtractInContext<typeof ${type}>;
export type OutContext = ${info.outContext?.(type) ?? "unknown"};
export type Args = ExtractT<typeof ${type}>;
export type Middleware = ReturnType<ReturnType<typeof ${fn}<Args, InContext, OutContext>>>;
export type Middleware = ReturnType<ReturnType<typeof ${fn}<${generics}>>>;
export default ${fn}(${type}) as (...args: Args) => Middleware;
`;

Expand Down
2 changes: 1 addition & 1 deletion tests-examples/tests-tool/test-type.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test("webroute", () => {
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
WebrouteMiddleware<Universal.Context, void | Universal.Context>
>();
expectTypeOf(webrouteHandler).returns.toEqualTypeOf<WebrouteHandler<Universal.Context, Universal.Context>>();
expectTypeOf(webrouteHandler).returns.toEqualTypeOf<WebrouteHandler<Universal.Context, void>>();
});

test("fastify", () => {
Expand Down

0 comments on commit e19a15c

Please sign in to comment.