-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation & response serialization improvements (#152)
* Fix request body & path param parsing This fixes a bug where the Zod-parsed request body accessed via `req.json()` had potentially incorrect types due to JSON serialization. Now the `json()` method is overridden so that the parsed data is returned as is without the JSON serialization. Additionally, the request path parameters are now also validated when using app router when previously they were used only for providing types for the used path parameters. * Add 10s timeout for form parsing * Improve RPC route response serialization This change serializes Blob data for API routes and creates a JSON object from form responses, although these are rare cases.
- Loading branch information
Showing
15 changed files
with
266 additions
and
52 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
apps/example/src/app/api/v2/route-with-path-params/[slug]/route.ts
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,30 @@ | ||
import { TypedNextResponse, route, routeOperation } from 'next-rest-framework'; | ||
import { z } from 'zod'; | ||
|
||
const paramsSchema = z.object({ | ||
slug: z.enum(['foo', 'bar', 'baz']) | ||
}); | ||
|
||
export const runtime = 'edge'; | ||
|
||
export const { GET } = route({ | ||
getPathParams: routeOperation({ | ||
method: 'GET' | ||
}) | ||
.input({ | ||
contentType: 'application/json', | ||
params: paramsSchema | ||
}) | ||
.outputs([ | ||
{ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: paramsSchema | ||
} | ||
]) | ||
.handler((_req, { params: { slug } }) => { | ||
return TypedNextResponse.json({ | ||
slug | ||
}); | ||
}) | ||
}); |
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
26 changes: 26 additions & 0 deletions
26
apps/example/src/pages/api/v1/route-with-path-params/[slug]/index.ts
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,26 @@ | ||
import { apiRoute, apiRouteOperation } from 'next-rest-framework'; | ||
import { z } from 'zod'; | ||
|
||
const paramsSchema = z.object({ | ||
slug: z.enum(['foo', 'bar', 'baz']) | ||
}); | ||
|
||
export default apiRoute({ | ||
getQueryParams: apiRouteOperation({ | ||
method: 'GET' | ||
}) | ||
.input({ | ||
contentType: 'application/json', | ||
query: paramsSchema | ||
}) | ||
.outputs([ | ||
{ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: paramsSchema | ||
} | ||
]) | ||
.handler((req, res) => { | ||
res.json(req.query); | ||
}) | ||
}); |
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
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
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
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
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
Oops, something went wrong.