-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to separate itty-router from openapi logic
- Loading branch information
Showing
14 changed files
with
398 additions
and
387 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.