fix(factory): support `createFactory()` destruction (#2623)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(factory): support `createFactory()` destruction * denoify * fixed the arg * denoify
authored by Yusuke Wada · committed by GitHub · parent 0fafd7a · Browse files · 4 files·+83 added−70 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+83 added−70 removed
- tests:+9 added−0 removed
- other:+74 added−70 removed
Provenance
- Commit
4cd2dff- Tree
685e63f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
0fafd7a
4 files changed+83 added−70 removed
modified:deno_dist/helper/factory/index.ts+37 added-35 removed
2 unmodified lines3 import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../types.ts'4 5 type InitApp<E extends Env = Env> = (app: Hono<E>) => void6 7-export class Factory<E extends Env = any, P extends string = any> {8- private initApp?: InitApp<E>9-10- constructor(init?: { initApp?: InitApp<E> }) {11- this.initApp = init?.initApp12- }13-14- /**15- * @experimental16- * `createApp` is an experimental feature.17- */18- createApp = () => {19- const app = new Hono<E>()20- if (this.initApp) {21- this.initApp(app)22- }23- return app24- }25-26- createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware27-28- createHandlers<I extends Input = {}, R extends HandlerResponse<any> = any>(29- handler1: H<E, P, I, R>30- ): [H<E, P, I, R>]31-7+interface CreateHandlersInterface<E extends Env, P extends string> {8+ <I extends Input = {}, R extends HandlerResponse<any> = any>(handler1: H<E, P, I, R>): [9+ H<E, P, I, R>10+ ]11 // handler x233- createHandlers<I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(12+ <I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(13 handler1: H<E, P, I, R>,14 handler2: H<E, P, I2, R>15 ): [H<E, P, I, R>, H<E, P, I2, R>]16 17 // handler x339- createHandlers<18+ <19 I extends Input = {},20 I2 extends Input = I,21 I3 extends Input = I & I2,22 R extends HandlerResponse<any> = any3 unmodified lines26 handler3: H<E, P, I3, R>27 ): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>]28 29 // handler x451- createHandlers<30+ <31 I extends Input = {},32 I2 extends Input = I,33 I3 extends Input = I & I2,34 I4 extends Input = I & I2 & I3,5 unmodified lines40 handler4: H<E, P, I4, R>41 ): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>, H<E, P, I4, R>]42 43 // handler x565- createHandlers<44+ <45 I extends Input = {},46 I2 extends Input = I,47 I3 extends Input = I & I2,48 I4 extends Input = I & I2 & I3,7 unmodified lines56 handler5: H<E, P, I5, R>57 ): [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>]58 59 // handler x681- createHandlers<60+ <61 I extends Input = {},62 I2 extends Input = I,63 I3 extends Input = I & I2,64 I4 extends Input = I & I2 & I3,9 unmodified lines74 handler6: H<E, P, I6, R>75 ): [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>]76 77 // handler x799- createHandlers<78+ <79 I extends Input = {},80 I2 extends Input = I,81 I3 extends Input = I & I2,82 I4 extends Input = I & I2 & I3,19 unmodified lines102 H<E, P, I7, R>103 ]104 105 // handler x8127- createHandlers<106+ <107 I extends Input = {},108 I2 extends Input = I,109 I3 extends Input = I & I2,110 I4 extends Input = I & I2 & I3,22 unmodified lines133 H<E, P, I8, R>134 ]135 136 // handler x9158- createHandlers<137+ <138 I extends Input = {},139 I2 extends Input = I,140 I3 extends Input = I & I2,141 I4 extends Input = I & I2 & I3,25 unmodified lines167 H<E, P, I9, R>168 ]169 170 // handler x10192- createHandlers<171+ <172 I extends Input = {},173 I2 extends Input = I,174 I3 extends Input = I & I2,175 I4 extends Input = I & I2 & I3,26 unmodified lines202 H<E, P, I8, R>,203 H<E, P, I9, R>,204 H<E, P, I10, R>205 ]206+}207 228- createHandlers(...handlers: H[]) {208+export class Factory<E extends Env = any, P extends string = any> {209+ private initApp?: InitApp<E>210+211+ constructor(init?: { initApp?: InitApp<E> }) {212+ this.initApp = init?.initApp213+ }214+215+ /**216+ * @experimental217+ * `createApp` is an experimental feature.218+ */219+ createApp = () => {220+ const app = new Hono<E>()221+ if (this.initApp) {222+ this.initApp(app)223+ }224+ return app225+ }226+227+ createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware228+229+ createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {230+ // @ts-expect-error this should not be typed231 return handlers.filter((handler) => handler !== undefined)232 }233 }234 7 unmodified linesmodified:src/helper/factory/index.test.ts+9 added
221 unmodified lines222 expect(res.status).toBe(200)223 expect(await res.text()).toBe('bar')224 })225 })226+227+describe('Lint rules', () => {228+ it('Should not throw a eslint `unbound-method` error if destructed', () => {229+ const { createApp, createHandlers, createMiddleware } = createFactory()230+ expect(createApp).toBeDefined()231+ expect(createHandlers).toBeDefined()232+ expect(createMiddleware).toBeDefined()233+ })234+})modified:src/helper/factory/index.ts+37 added-35 removed
2 unmodified lines3 import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../types'4 5 type InitApp<E extends Env = Env> = (app: Hono<E>) => void6 7-export class Factory<E extends Env = any, P extends string = any> {8- private initApp?: InitApp<E>9-10- constructor(init?: { initApp?: InitApp<E> }) {11- this.initApp = init?.initApp12- }13-14- /**15- * @experimental16- * `createApp` is an experimental feature.17- */18- createApp = () => {19- const app = new Hono<E>()20- if (this.initApp) {21- this.initApp(app)22- }23- return app24- }25-26- createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware27-28- createHandlers<I extends Input = {}, R extends HandlerResponse<any> = any>(29- handler1: H<E, P, I, R>30- ): [H<E, P, I, R>]31-7+interface CreateHandlersInterface<E extends Env, P extends string> {8+ <I extends Input = {}, R extends HandlerResponse<any> = any>(handler1: H<E, P, I, R>): [9+ H<E, P, I, R>10+ ]11 // handler x233- createHandlers<I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(12+ <I extends Input = {}, I2 extends Input = I, R extends HandlerResponse<any> = any>(13 handler1: H<E, P, I, R>,14 handler2: H<E, P, I2, R>15 ): [H<E, P, I, R>, H<E, P, I2, R>]16 17 // handler x339- createHandlers<18+ <19 I extends Input = {},20 I2 extends Input = I,21 I3 extends Input = I & I2,22 R extends HandlerResponse<any> = any3 unmodified lines26 handler3: H<E, P, I3, R>27 ): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>]28 29 // handler x451- createHandlers<30+ <31 I extends Input = {},32 I2 extends Input = I,33 I3 extends Input = I & I2,34 I4 extends Input = I & I2 & I3,5 unmodified lines40 handler4: H<E, P, I4, R>41 ): [H<E, P, I, R>, H<E, P, I2, R>, H<E, P, I3, R>, H<E, P, I4, R>]42 43 // handler x565- createHandlers<44+ <45 I extends Input = {},46 I2 extends Input = I,47 I3 extends Input = I & I2,48 I4 extends Input = I & I2 & I3,7 unmodified lines56 handler5: H<E, P, I5, R>57 ): [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>]58 59 // handler x681- createHandlers<60+ <61 I extends Input = {},62 I2 extends Input = I,63 I3 extends Input = I & I2,64 I4 extends Input = I & I2 & I3,9 unmodified lines74 handler6: H<E, P, I6, R>75 ): [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>]76 77 // handler x799- createHandlers<78+ <79 I extends Input = {},80 I2 extends Input = I,81 I3 extends Input = I & I2,82 I4 extends Input = I & I2 & I3,19 unmodified lines102 H<E, P, I7, R>103 ]104 105 // handler x8127- createHandlers<106+ <107 I extends Input = {},108 I2 extends Input = I,109 I3 extends Input = I & I2,110 I4 extends Input = I & I2 & I3,22 unmodified lines133 H<E, P, I8, R>134 ]135 136 // handler x9158- createHandlers<137+ <138 I extends Input = {},139 I2 extends Input = I,140 I3 extends Input = I & I2,141 I4 extends Input = I & I2 & I3,25 unmodified lines167 H<E, P, I9, R>168 ]169 170 // handler x10192- createHandlers<171+ <172 I extends Input = {},173 I2 extends Input = I,174 I3 extends Input = I & I2,175 I4 extends Input = I & I2 & I3,26 unmodified lines202 H<E, P, I8, R>,203 H<E, P, I9, R>,204 H<E, P, I10, R>205 ]206+}207 228- createHandlers(...handlers: H[]) {208+export class Factory<E extends Env = any, P extends string = any> {209+ private initApp?: InitApp<E>210+211+ constructor(init?: { initApp?: InitApp<E> }) {212+ this.initApp = init?.initApp213+ }214+215+ /**216+ * @experimental217+ * `createApp` is an experimental feature.218+ */219+ createApp = () => {220+ const app = new Hono<E>()221+ if (this.initApp) {222+ this.initApp(app)223+ }224+ return app225+ }226+227+ createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware228+229+ createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {230+ // @ts-expect-error this should not be typed231 return handlers.filter((handler) => handler !== undefined)232 }233 }234 7 unmodified linesmodified:bun.lockbbinary
A binary file, modified.