Skip to content

Commit

Permalink
fix(factory): support createFactory() destruction (#2623)
Browse files Browse the repository at this point in the history
* fix(factory): support `createFactory()` destruction

* denoify

* fixed the arg

* denoify
  • Loading branch information
yusukebe authored May 6, 2024
1 parent 0fafd7a commit 4cd2dff
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 70 deletions.
Binary file modified bun.lockb
Binary file not shown.
72 changes: 37 additions & 35 deletions deno_dist/helper/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,18 @@ import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../ty

type InitApp<E extends Env = Env> = (app: Hono<E>) => void

export class Factory<E extends Env = any, P extends string = any> {
private initApp?: InitApp<E>

constructor(init?: { initApp?: InitApp<E> }) {
this.initApp = init?.initApp
}

/**
* @experimental
* `createApp` is an experimental feature.
*/
createApp = () => {
const app = new Hono<E>()
if (this.initApp) {
this.initApp(app)
}
return app
}

createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware

createHandlers<I extends Input = {}, R extends HandlerResponse<any> = any>(
handler1: H<E, P, I, R>
): [H<E, P, I, R>]

interface CreateHandlersInterface<E extends Env, P extends string> {
<I extends Input = {}, R extends HandlerResponse<any> = any>(handler1: H<E, P, I, R>): [
H<E, P, I, R>
]
// handler x2
createHandlers<I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(
<I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(
handler1: H<E, P, I, R>,
handler2: H<E, P, I2, R>
): [H<E, P, I, R>, H<E, P, I2, R>]

// handler x3
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -48,7 +27,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>]

// handler x4
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -62,7 +41,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>, H<E, P, I4, R>]

// handler x5
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -78,7 +57,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [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>]

// handler x6
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -96,7 +75,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [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>, H<E, P, I6, R>]

// handler x7
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -124,7 +103,7 @@ export class Factory<E extends Env = any, P extends string = any> {
]

// handler x8
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -155,7 +134,7 @@ export class Factory<E extends Env = any, P extends string = any> {
]

// handler x9
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -189,7 +168,7 @@ export class Factory<E extends Env = any, P extends string = any> {
]

// handler x10
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -224,8 +203,31 @@ export class Factory<E extends Env = any, P extends string = any> {
H<E, P, I9, R>,
H<E, P, I10, R>
]
}

export class Factory<E extends Env = any, P extends string = any> {
private initApp?: InitApp<E>

constructor(init?: { initApp?: InitApp<E> }) {
this.initApp = init?.initApp
}

/**
* @experimental
* `createApp` is an experimental feature.
*/
createApp = () => {
const app = new Hono<E>()
if (this.initApp) {
this.initApp(app)
}
return app
}

createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware

createHandlers(...handlers: H[]) {
createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {
// @ts-expect-error this should not be typed
return handlers.filter((handler) => handler !== undefined)
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/helper/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,12 @@ describe('createApp', () => {
expect(await res.text()).toBe('bar')
})
})

describe('Lint rules', () => {
it('Should not throw a eslint `unbound-method` error if destructed', () => {
const { createApp, createHandlers, createMiddleware } = createFactory()
expect(createApp).toBeDefined()
expect(createHandlers).toBeDefined()
expect(createMiddleware).toBeDefined()
})
})
72 changes: 37 additions & 35 deletions src/helper/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,18 @@ import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../ty

type InitApp<E extends Env = Env> = (app: Hono<E>) => void

export class Factory<E extends Env = any, P extends string = any> {
private initApp?: InitApp<E>

constructor(init?: { initApp?: InitApp<E> }) {
this.initApp = init?.initApp
}

/**
* @experimental
* `createApp` is an experimental feature.
*/
createApp = () => {
const app = new Hono<E>()
if (this.initApp) {
this.initApp(app)
}
return app
}

createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware

createHandlers<I extends Input = {}, R extends HandlerResponse<any> = any>(
handler1: H<E, P, I, R>
): [H<E, P, I, R>]

interface CreateHandlersInterface<E extends Env, P extends string> {
<I extends Input = {}, R extends HandlerResponse<any> = any>(handler1: H<E, P, I, R>): [
H<E, P, I, R>
]
// handler x2
createHandlers<I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(
<I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(
handler1: H<E, P, I, R>,
handler2: H<E, P, I2, R>
): [H<E, P, I, R>, H<E, P, I2, R>]

// handler x3
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -48,7 +27,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>]

// handler x4
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -62,7 +41,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>, H<E, P, I4, R>]

// handler x5
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -78,7 +57,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [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>]

// handler x6
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand All @@ -96,7 +75,7 @@ export class Factory<E extends Env = any, P extends string = any> {
): [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>, H<E, P, I6, R>]

// handler x7
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -124,7 +103,7 @@ export class Factory<E extends Env = any, P extends string = any> {
]

// handler x8
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -155,7 +134,7 @@ export class Factory<E extends Env = any, P extends string = any> {
]

// handler x9
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -189,7 +168,7 @@ export class Factory<E extends Env = any, P extends string = any> {
]

// handler x10
createHandlers<
<
I extends Input = {},
I2 extends Input = I,
I3 extends Input = I & I2,
Expand Down Expand Up @@ -224,8 +203,31 @@ export class Factory<E extends Env = any, P extends string = any> {
H<E, P, I9, R>,
H<E, P, I10, R>
]
}

export class Factory<E extends Env = any, P extends string = any> {
private initApp?: InitApp<E>

constructor(init?: { initApp?: InitApp<E> }) {
this.initApp = init?.initApp
}

/**
* @experimental
* `createApp` is an experimental feature.
*/
createApp = () => {
const app = new Hono<E>()
if (this.initApp) {
this.initApp(app)
}
return app
}

createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware

createHandlers(...handlers: H[]) {
createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {
// @ts-expect-error this should not be typed
return handlers.filter((handler) => handler !== undefined)
}
}
Expand Down

0 comments on commit 4cd2dff

Please sign in to comment.