-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
DRAFT: "AutoRouter" to reduce router initialization boilerplate #195
Conversation
…equest used, but Args changed
* Add the required `extends` calls to fix the ts-expect-error * Get rid of the other typescript ignores * Replace one last expect-error with an `as` In this case the 'as' is asserting "I know better than the compiler" and it is both true and more accurate than saying "I expect typescript to error out here." As an added benefit, `as` does more type checking than 'expect error', so you get just a small bit more type safety.
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.
@kwhitley this is mostly an improvement, but you're missing more generics. That's what causes some 95% of your ts-ignore-error and any
usages.
Pretty much every any
you have can be replaced 1:1 with a generic type parameter for significantly better type signatures.
src/Router.ts
Outdated
export type RouteHandler<I = IRequest, A extends any[] = any[]> = { | ||
(request: I, ...args: A): any | ||
export type RouteHandler<R = IRequest, Args = any[]> = { | ||
// @ts-expect-error - TS never likes this syntax |
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.
Doesn’t like the syntax because you need [] array type for spread args.
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.
You’d need an extends Array in your type handler here, I think
export interface CorsFns { | ||
corsify(req: Request): Response; | ||
preflight(r: IRequest): Response; | ||
export interface CorsHandlers { |
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.
MUCH better name. Thanks for the fix.
preflight(r: IRequest): Response; | ||
export interface CorsHandlers { | ||
corsify(response: Response): any | ||
preflight(Request: IRequest): any |
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.
Never use a direct IRequest
anywhere but as a default for a generic type. It'd be best if you setup a linter for this, tbh. It's the source of 99% of your type bugs.
@@ -38,20 +38,20 @@ export const createCors = (options: CorsOptions = {}): CorsFns => { | |||
if (maxAge) rHeaders['Access-Control-Max-Age'] = maxAge | |||
|
|||
// Pre-flight function. | |||
const preflight = (r: IRequest) => { | |||
const preflight = (request: IRequest): any => { |
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.
createCors
needs to be generic to get the types to behave.
@@ -20,7 +20,7 @@ export type FlowOptions = { | |||
after?: afterFunction | |||
} | |||
|
|||
export type Flowed = (request: RequestLike, ...extra: any[]) => Promise<any> | |||
export type Flowed = (request: IRequest, ...extra: any[]) => Promise<any> |
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.
This has to be generic to get the handler types working nicely.
@@ -20,7 +20,7 @@ export type FlowOptions = { | |||
after?: afterFunction | |||
} | |||
|
|||
export type Flowed = (request: RequestLike, ...extra: any[]) => Promise<any> | |||
export type Flowed = (request: IRequest, ...extra: any[]) => Promise<any> | |||
export type FlowedAndFetch = Flowed & { fetch: Flowed } |
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.
This is a real weird type. It's a fetch function that owns a ... fetch function?
Adding to the discussion here... |
Killed in favor of #222 |
Description
Adds
flow(router: Router, options?: FlowOptions): Response
to reduce boilerplate. This function makes the following assumptions (with override controls):json
API (options)
false
false
(off/ignore),true
(defaultcreateCors()
),CorsOptions
json
false
(off/ignore),Function<Response>
error
false
(off/ignore),Function<Response>
() => error(404)
false
(off/ignore),Function<Response>
Type of Change (select one and follow subtasks)