Skip to content

Commit

Permalink
feat(context): introduce c.var (#1406)
Browse files Browse the repository at this point in the history
* feat(context): introduce `c.var`

* denoify

* make it a property

* denoify

* make it a read-only

* fix the complex pattern
  • Loading branch information
yusukebe authored Sep 9, 2023
1 parent 0f6f403 commit 665f456
Show file tree
Hide file tree
Showing 5 changed files with 579 additions and 112 deletions.
13 changes: 9 additions & 4 deletions deno_dist/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export class Context<
> {
req: HonoRequest<P, I['out']>
env: E['Bindings'] = {}
private _var: E['Variables'] = {}
finalized: boolean = false
error: Error | undefined = undefined

private _status: StatusCode = 200
private _exCtx: FetchEventLike | ExecutionContext | undefined // _executionCtx
private _map: Record<string, unknown> | undefined
private _h: Headers | undefined = undefined // _headers
private _pH: Record<string, string> | undefined = undefined // _preparedHeaders
private _res: Response | undefined
Expand Down Expand Up @@ -214,12 +214,17 @@ export class Context<
}

set: Set<E> = (key: string, value: unknown) => {
this._map ||= {}
this._map[key as string] = value
this._var ??= {}
this._var[key as string] = value
}

get: Get<E> = (key: string) => {
return this._map ? this._map[key] : undefined
return this._var ? this._var[key] : undefined
}

// c.var.propName is a read-only
get var(): Readonly<E['Variables']> {
return { ...this._var }
}

newResponse: NewResponse = (
Expand Down
202 changes: 151 additions & 51 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ export type H<
E extends Env = any,
P extends string = any,
I extends Input = {},
E2 extends Env = E,
R extends HandlerResponse<any> = any
> = Handler<E, P, I, R> | MiddlewareHandler<E, P, I>
> = Handler<E2, P, I, R> | MiddlewareHandler<E2, P, I>

export type NotFoundHandler<E extends Env = any> = (c: Context<E>) => Response | Promise<Response>
export type ErrorHandler<E extends Env = any> = (
Expand All @@ -74,13 +75,26 @@ export interface HandlerInterface<
> {
//// app.get(...handlers[])

// app.get(handler)
<
P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
I extends Input = {},
R extends HandlerResponse<any> = any,
Temp extends Env = E
>(
handler: H<E, P, I, Temp, R>
): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(handler, handler)
<
P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
I extends Input = {},
R extends HandlerResponse<any> = any
R extends HandlerResponse<any> = any,
E2 extends Env = E,
E3 extends Env = E,
Temp extends Env = E & E2
>(
...handlers: [H<E, P, I, R>, H<E, P, I, R>]
...handlers: [H<E2, P, I, E2, R>, H<E3, P, I, Temp, R>]
): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(handler x 3)
Expand All @@ -89,9 +103,13 @@ export interface HandlerInterface<
R extends HandlerResponse<any> = any,
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2
I3 extends Input = I & I2,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
Temp extends Env = E & E2 & E3
>(
...handlers: [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>]
...handlers: [H<E2, P, I, E2, R>, H<E3, P, I2, E3, R>, H<E4, P, I3, Temp, R>]
): Hono<E, S & ToSchema<M, P, I3['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(handler x 4)
Expand All @@ -101,9 +119,19 @@ export interface HandlerInterface<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I & I2 & I3
I4 extends Input = I & I2 & I3,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = E,
Temp extends Env = E & E2 & E3 & E4
>(
...handlers: [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>, H<E, P, I4, R>]
...handlers: [
H<E2, P, I, E2, R>,
H<E3, P, I2, E3, R>,
H<E4, P, I3, E4, R>,
H<E5, P, I3, Temp, R>
]
): Hono<E, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(handler x 5)
Expand All @@ -114,9 +142,21 @@ export interface HandlerInterface<
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I2 & I3,
I5 extends Input = I & I2 & I3 & I4
I5 extends Input = I & I2 & I3 & I4,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = E,
E6 extends Env = E,
Temp extends Env = E & E2 & E3 & E4 & E5
>(
...handlers: [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>, H<E, P, I4, R>, H<E, P, I5, R>]
...handlers: [
H<E2, P, I, E2, R>,
H<E3, P, I2, E3, R>,
H<E4, P, I3, E4, R>,
H<E5, P, I3, E5, R>,
H<E6, P, I3, Temp, R>
]
): Hono<E, S & ToSchema<M, P, I5['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(...handlers[])
Expand All @@ -125,21 +165,45 @@ export interface HandlerInterface<
I extends Input = {},
R extends HandlerResponse<any> = any
>(
...handlers: Handler<E, P, I, R>[]
...handlers: H<E, P, I, E, R>[]
): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>

//// app.get(path)

// app.get(path)
<P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(path: P): Hono<
E,
S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,
BasePath
>

//// app.get(path, ...handlers[])

// app.get(path, handler)
<P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(
<
P extends string,
R extends HandlerResponse<any> = any,
I extends Input = {},
Temp extends Env = E
>(
path: P,
handler: H<E, MergePath<BasePath, P>, I, R>
handler: H<E, MergePath<BasePath, P>, I, Temp, R>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler, handler)
<P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(
<
P extends string,
R extends HandlerResponse<any> = any,
I extends Input = {},
E2 extends Env = E,
E3 extends Env = E,
Temp extends Env = E & E2
>(
path: P,
...handlers: [H<E, MergePath<BasePath, P>, I, R>, H<E, MergePath<BasePath, P>, I, R>]
...handlers: [
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I, Temp, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x3)
Expand All @@ -148,13 +212,17 @@ export interface HandlerInterface<
R extends HandlerResponse<any> = any,
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2
I3 extends Input = I & I2,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
Temp extends Env = E & E2 & E3
>(
path: P,
...handlers: [
H<E, MergePath<BasePath, P>, I, R>,
H<E, MergePath<BasePath, P>, I2, R>,
H<E, MergePath<BasePath, P>, I3, R>
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I2, E3, R>,
H<E4, MergePath<BasePath, P>, I3, Temp, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>, BasePath>

Expand All @@ -165,14 +233,19 @@ export interface HandlerInterface<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I & I2 & I3
I4 extends Input = I & I2 & I3,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = E,
Temp extends Env = E & E2 & E3 & E4
>(
path: P,
...handlers: [
H<E, MergePath<BasePath, P>, I, R>,
H<E, MergePath<BasePath, P>, I2, R>,
H<E, MergePath<BasePath, P>, I3, R>,
H<E, MergePath<BasePath, P>, I4, R>
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I2, E3, R>,
H<E4, MergePath<BasePath, P>, I3, E4, R>,
H<E5, MergePath<BasePath, P>, I4, Temp, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>, BasePath>

Expand All @@ -184,22 +257,28 @@ export interface HandlerInterface<
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I2 & I3,
I5 extends Input = I & I2 & I3 & I4
I5 extends Input = I & I2 & I3 & I4,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = E,
E6 extends Env = E,
Temp extends Env = E & E2 & E3 & E4 & E5
>(
path: P,
...handlers: [
H<E, MergePath<BasePath, P>, I, R>,
H<E, MergePath<BasePath, P>, I2, R>,
H<E, MergePath<BasePath, P>, I3, R>,
H<E, MergePath<BasePath, P>, I4, R>,
H<E, MergePath<BasePath, P>, I5, R>
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I2, E3, R>,
H<E4, MergePath<BasePath, P>, I3, E4, R>,
H<E5, MergePath<BasePath, P>, I4, E5, R>,
H<E6, MergePath<BasePath, P>, I5, Temp, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, ...handlers[])
<P extends string, I extends Input = {}, R extends HandlerResponse<any> = any>(
path: P,
...handlers: H<E, MergePath<BasePath, P>, I, R>[]
...handlers: H<E, MergePath<BasePath, P>, I, E, R>[]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>
}

Expand Down Expand Up @@ -236,10 +315,20 @@ export interface OnHandlerInterface<
BasePath extends string = '/'
> {
// app.on(method, path, handler, handler)
<M extends string, P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(
<
M extends string,
P extends string,
R extends HandlerResponse<any> = any,
I extends Input = {},
E2 extends Env = E,
E3 extends Env = E
>(
method: M,
path: P,
...handlers: [H<E, MergePath<BasePath, P>, I, R>, H<E, MergePath<BasePath, P>, I, R>]
...handlers: [
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I, E & E2, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(method, path, handler x3)
Expand All @@ -249,17 +338,19 @@ export interface OnHandlerInterface<
R extends HandlerResponse<any> = any,
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2
I3 extends Input = I & I2,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E
>(
method: M,
path: P,
...handlers: [
H<E, MergePath<BasePath, P>, I, R>,
H<E, MergePath<BasePath, P>, I2, R>,
H<E, MergePath<BasePath, P>, I3, R>
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I2, E3, R>,
H<E4, MergePath<BasePath, P>, I3, E & E2 & E3, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(method, path, handler x4)
<
M extends string,
Expand All @@ -268,15 +359,19 @@ export interface OnHandlerInterface<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I2 & I3
I4 extends Input = I & I2 & I3,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = E
>(
method: M,
path: P,
...handlers: [
H<E, MergePath<BasePath, P>, I, R>,
H<E, MergePath<BasePath, P>, I2, R>,
H<E, MergePath<BasePath, P>, I3, R>,
H<E, MergePath<BasePath, P>, I4, R>
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I2, E3, R>,
H<E4, MergePath<BasePath, P>, I3, E4, R>,
H<E5, MergePath<BasePath, P>, I4, E & E2 & E3 & E4, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>, BasePath>

Expand All @@ -288,31 +383,36 @@ export interface OnHandlerInterface<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I2 & I3,
I5 extends Input = I3 & I4
I4 extends Input = I & I2 & I3,
I5 extends Input = I & I2 & I3 & I4,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = E,
E6 extends Env = E
>(
method: M,
path: P,
...handlers: [
H<E, MergePath<BasePath, P>, I, R>,
H<E, MergePath<BasePath, P>, I2, R>,
H<E, MergePath<BasePath, P>, I3, R>,
H<E, MergePath<BasePath, P>, I4, R>,
H<E, MergePath<BasePath, P>, I5, R>
H<E2, MergePath<BasePath, P>, I, E2, R>,
H<E3, MergePath<BasePath, P>, I2, E3, R>,
H<E4, MergePath<BasePath, P>, I3, E4, R>,
H<E5, MergePath<BasePath, P>, I4, E5, R>,
H<E6, MergePath<BasePath, P>, I5, E & E2 & E3 & E4 & E5, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>, BasePath>

<M extends string, P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(
method: M,
path: P,
...handlers: H<E, MergePath<BasePath, P>, I, R>[]
...handlers: H<E, MergePath<BasePath, P>, I, E, R>[]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.on(method[], path, ...handler)
<P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(
methods: string[],
path: P,
...handlers: H<E, MergePath<BasePath, P>, I, R>[]
...handlers: H<E, MergePath<BasePath, P>, I, E, R>[]
): Hono<
E,
S & ToSchema<string, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,
Expand Down
Loading

0 comments on commit 665f456

Please sign in to comment.