Skip to content
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

fix(types): allow blank env #2834

Merged
merged 9 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
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>()
fzn0x marked this conversation as resolved.
Show resolved Hide resolved

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>()
fzn0x marked this conversation as resolved.
Show resolved Hide resolved

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,10 +3,10 @@ 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'

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
Loading