-
Notifications
You must be signed in to change notification settings - Fork 41
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
Refactor to separate itty-router from openapi logic #151
Conversation
🧪 A prerelease is available for testing 🧪 You can install this latest build in your project with: npm install --save https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/9555081565/npm-package-itty-router-openapi-151 Or you can immediately run this with npx https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/9555081565/npm-package-itty-router-openapi-151 |
6eee299
to
6ea2011
Compare
That's great! |
dcafc96
to
bf31198
Compare
a8cc538
to
16ae377
Compare
16ae377
to
4711c09
Compare
7151dc9
to
e93663d
Compare
4cedb31
to
f3fd898
Compare
7ce627b
to
9c23342
Compare
9c23342
to
dea759a
Compare
Awesome! I'll check the new stuff |
This refactors the OpenAPI logic and adds support for latest versions of itty-router and possibly other router (like hono)
The new version will require users to instantiate their own
itty-router
router like:Breaking changes:
OpenAPIRouter()
methodaiPlugin
optionskipValidation
option (see new validation method bellow)itty-router
is no longer a dependency and users must manually add it to their projectschema
is no longer a static variableschema.parameters
was renamed toschema.request
to follow zod-to-openapi structurePath
parameters are now calledparams
to match the validated Data object nameschema.requestBody
was renamed toschema.request.body
to follow zod-to-openapi structurehandle
function is called, developers must manually call thegetValidatedData
to execute the data validation steptry {} catch (e) {}
to handle input errors, otherwise this library will return the errors in a Json response with http 400Additions:
await this.getValidatedData<typeof this.schema>()
method to retrieve all validated data, when sending<typeof this.schema>
in the generic the returned value will have full typescript inference based on the schemafromIttyRouter()
instantiate methodfromHono()
instantiate methodMigration guide:
npm install itty-router@latest --save
)const router = OpenAPIRouter({...})
const router = fromIttyRouter(Router(), {...})
new
from all "legacy type" call fromnew Str(...)
intoStr(...)
async handle(request: Request, env: Env, ctx: Context, data: any) {...}
async handle(request: Request, env: Env, ctx: Context) {...}
const data = await this.getValidatedData<typeof this.schema>()
static schema
to:schema
schema.parameters.query
intoschema.request.query
schema.parameters.path
intoschema.request.params
schema.parameters.headers
intoschema.request.headers
z.object({...})
schema.requestBody
intoschema.request.body: contentJson(z.object({...params.newModel}))
Minimal Hono example