-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support variadic middleware for routes #85
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,7 @@ export const implementSchema = < | |
data, | ||
}); | ||
}, | ||
[], | ||
routeHandler, | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ import { fromZodError } from 'zod-validation-error'; | |
import zodToJsonSchema from 'zod-to-json-schema'; | ||
import compose = require('koa-compose'); | ||
import { | ||
ContextOfEndpoint, | ||
EndpointImplementation, | ||
implementRoute, | ||
OneSchemaRouterMiddleware, | ||
PathParamsOf, | ||
} from './koa-utils'; | ||
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
|
@@ -97,15 +99,24 @@ export class OneSchemaRouter< | |
|
||
implement<Route extends keyof Schema & string>( | ||
route: Route, | ||
implementation: EndpointImplementation< | ||
Route, | ||
z.output<Schema[Route]['request']>, | ||
z.infer<Schema[Route]['response']>, | ||
R | ||
>, | ||
) { | ||
...middlewares: [ | ||
...OneSchemaRouterMiddleware< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, TIL you can spread a type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this is a rest, not a spread. |
||
ContextOfEndpoint<Route, z.output<Schema[Route]['request']>, R> | ||
>[], | ||
EndpointImplementation< | ||
Route, | ||
z.output<Schema[Route]['request']>, | ||
z.infer<Schema[Route]['response']>, | ||
R | ||
>, | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this above type for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @epeters3 Exactly! Previously, I thought this was impossible to model using TS. I was wrong :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Way cool. I think the fact that Typescript is fundamentally a correctness checker and DX tool and not just a necessary part of code compilation like with Java or C, has enabled the Typescript maintainers and community to do some pretty incredible things. |
||
): this { | ||
const endpoint = this.schema[route]; | ||
|
||
const mws = middlewares.slice(0, -1) as any[]; | ||
|
||
const implementation = middlewares.at(-1) as any; | ||
Comment on lines
+116
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the reason for having to cast as any here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
implementRoute( | ||
route, | ||
this.router, | ||
|
@@ -119,6 +130,8 @@ export class OneSchemaRouter< | |
} | ||
return res.data; | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
mws, | ||
async (ctx) => { | ||
const result = await implementation(ctx); | ||
const res = endpoint.response.safeParse(result); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is actually making the jest assertions here? Or are we just relying on the
@ts-expect-error
directives to act like a unit test?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, just the ts-expect-error directives. This pattern is used in many tests in this project.