Skip to content

Commit

Permalink
fix(types): allow blank env (#2834)
Browse files Browse the repository at this point in the history
* fix(types): allow blank env

* types: blank env

* fix(types): add mock binding

* cleanup

* Update yarn.lock

* fix: bun lock conflict

* revert

* fix(types): update quick and tiny preset
  • Loading branch information
fzn0x authored May 29, 2024
1 parent 18324b8 commit 16f7393
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
10 changes: 8 additions & 2 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ function throwExpression(errorMessage: string): never {
throw new Error(errorMessage)
}

type Env = {
Bindings: {
_: string
}
}

describe('GET Request', () => {
describe('without middleware', () => {
// In other words, this is a test for cases that do not use `compose()`

const app = new Hono()
const app = new Hono<Env>()

app.get('/hello', async () => {
return new Response('hello', {
Expand Down Expand Up @@ -130,7 +136,7 @@ describe('GET Request', () => {
describe('with middleware', () => {
// when using `compose()`

const app = new Hono()
const app = new Hono<Env>()

app.use('*', async (ctx, next) => {
await next()
Expand Down
4 changes: 2 additions & 2 deletions src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { HonoOptions } from './hono-base'
import { RegExpRouter } from './router/reg-exp-router'
import { SmartRouter } from './router/smart-router'
import { TrieRouter } from './router/trie-router'
import type { BlankSchema, Env, Schema } from './types'
import type { BlankEnv, BlankSchema, Env, Schema } from './types'

/**
* The Hono class extends the functionality of the HonoBase class.
Expand All @@ -14,7 +14,7 @@ import type { BlankSchema, Env, Schema } from './types'
* @template BasePath - The base path type.
*/
export class Hono<
E extends Env = Env,
E extends Env = BlankEnv,
S extends Schema = BlankSchema,
BasePath extends string = '/'
> extends HonoBase<E, S, BasePath> {
Expand Down
4 changes: 2 additions & 2 deletions src/preset/quick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type { HonoOptions } from '../hono-base'
import { LinearRouter } from '../router/linear-router'
import { SmartRouter } from '../router/smart-router'
import { TrieRouter } from '../router/trie-router'
import type { BlankSchema, Env, Schema } from '../types'
import type { BlankEnv, BlankSchema, Env, Schema } from '../types'

export class Hono<
E extends Env = Env,
E extends Env = BlankEnv,
S extends Schema = BlankSchema,
BasePath extends string = '/'
> extends HonoBase<E, S, BasePath> {
Expand Down
4 changes: 2 additions & 2 deletions src/preset/tiny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import { HonoBase } from '../hono-base'
import type { HonoOptions } from '../hono-base'
import { PatternRouter } from '../router/pattern-router'
import type { BlankSchema, Env, Schema } from '../types'
import type { BlankEnv, BlankSchema, Env, Schema } from '../types'

export class Hono<
E extends Env = Env,
E extends Env = BlankEnv,
S extends Schema = BlankSchema,
BasePath extends string = '/'
> extends HonoBase<E, S, BasePath> {
Expand Down
24 changes: 14 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
export type Bindings = Record<string, unknown>
export type Variables = Record<string, unknown>

export type BlankEnv = {}
export type Env = {
Bindings?: Bindings
Variables?: Variables
Expand Down Expand Up @@ -104,7 +105,7 @@ export type ErrorHandler<E extends Env = any> = (
export interface HandlerInterface<
E extends Env = Env,
M extends string = string,
S extends Schema = {},
S extends Schema = BlankSchema,
BasePath extends string = '/'
> {
// app.get(handler)
Expand Down Expand Up @@ -700,11 +701,9 @@ export interface HandlerInterface<
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>, BasePath>

// 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, MergeTypedResponse<R>>,
BasePath
>
<P extends string, R extends HandlerResponse<any> = any, I extends Input = BlankInput>(
path: P
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>, BasePath>
}

////////////////////////////////////////
Expand All @@ -715,7 +714,7 @@ export interface HandlerInterface<

export interface MiddlewareHandlerInterface<
E extends Env = Env,
S extends Schema = {},
S extends Schema = BlankSchema,
BasePath extends string = '/'
> {
//// app.use(...handlers[])
Expand Down Expand Up @@ -918,7 +917,7 @@ export interface MiddlewareHandlerInterface<

export interface OnHandlerInterface<
E extends Env = Env,
S extends Schema = {},
S extends Schema = BlankSchema,
BasePath extends string = '/'
> {
// app.on(method, path, handler)
Expand Down Expand Up @@ -1247,7 +1246,12 @@ export interface OnHandlerInterface<
>

// app.get(method, path, ...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 = BlankInput
>(
method: M,
path: P,
...handlers: H<E, MergePath<BasePath, P>, I, R>[]
Expand Down Expand Up @@ -1578,7 +1582,7 @@ export interface OnHandlerInterface<
>

// app.on(method[], path, ...handler)
<P extends string, R extends HandlerResponse<any> = any, I extends Input = {}>(
<P extends string, R extends HandlerResponse<any> = any, I extends Input = BlankInput>(
methods: string[],
path: P,
...handlers: H<E, MergePath<BasePath, P>, I, R>[]
Expand Down

0 comments on commit 16f7393

Please sign in to comment.