Skip to content

Commit

Permalink
Refactor to separate itty-router from openapi logic
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed May 27, 2024
1 parent 5cd88e4 commit 6ea2011
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 387 deletions.
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"eslint-config-prettier": "^8.8.0",
"husky": "^7.0.2",
"isomorphic-fetch": "^3.0.0",
"itty-router": "^5.0.17",
"jest": "^29.0.0",
"jest-openapi": "^0.14.2",
"pinst": "^2.1.6",
Expand All @@ -93,7 +94,6 @@
},
"dependencies": {
"@asteasolutions/zod-to-openapi": "^6.4.0",
"itty-router": "4.0.26",
"js-yaml": "^4.1.0",
"openapi3-ts": "^4.1.2",
"zod": "^3.21.4"
Expand Down
44 changes: 44 additions & 0 deletions src/adapters/hono.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { RouterOptions } from '../types'
import { OpenAPIHandler, OpenAPIRouterType } from '../openapi'

export class HonoOpenAPIHandler extends OpenAPIHandler {
getRequest(args: any[]) {
return args[0].req.raw
}
}

export function fromHono<M>(
router: M,
options?: RouterOptions,
): M & OpenAPIRouterType<M> & any {
const openapiRouter = new HonoOpenAPIHandler(router, options)

return new Proxy(router, {
get: (target: any, prop: string, ...args: any[]) => {
const _result = openapiRouter.handleCommonProxy(target, prop, ...args)
if (_result !== undefined) {
return _result
}

return (route: string, ...handlers: any[]) => {
if (prop !== 'fetch') {
if (handlers.length === 1 && handlers[0].registry) {
handlers = openapiRouter.registerNestedRouter({
method: prop,
path: route,
nestedRouter: handlers[0],
})
} else if (prop !== 'all') {
handlers = openapiRouter.registerRoute({
method: prop,
path: route,
handlers: handlers,
})
}
}

return Reflect.get(target, prop, ...args)(route, ...handlers)
}
},
})
}
41 changes: 41 additions & 0 deletions src/adapters/ittyRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { RouterOptions } from '../types'
import { OpenAPIHandler, OpenAPIRouterType } from '../openapi'

export class IttyRouterOpenAPIHandler extends OpenAPIHandler {
}

export function fromIttyRouter<M>(
router: M,
options?: RouterOptions,
): M & OpenAPIRouterType<M> & any {
const openapiRouter = new IttyRouterOpenAPIHandler(router, options)

return new Proxy(router, {
get: (target: any, prop: string, ...args: any[]) => {
const _result = openapiRouter.handleCommonProxy(target, prop, ...args)
if (_result !== undefined) {
return _result
}

return (route: string, ...handlers: any[]) => {
if (prop !== 'fetch') {
if (handlers.length === 1 && handlers[0].registry) {
handlers = openapiRouter.registerNestedRouter({
method: prop,
path: route,
nestedRouter: handlers[0],
})
} else if (prop !== 'all') {
handlers = openapiRouter.registerRoute({
method: prop,
path: route,
handlers: handlers,
})
}
}

return Reflect.get(target, prop, ...args)(route, ...handlers)
}
},
})
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './types'
export * from './route'
export * from './ui'
export * from './utils'
export * from './adapters/ittyRouter'
export * from './adapters/hono'
Loading

0 comments on commit 6ea2011

Please sign in to comment.