-
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.
- Loading branch information
Showing
23 changed files
with
599 additions
and
228 deletions.
There are no files selected for viewing
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,48 @@ | ||
import { TypedNextResponse, route, routeOperation } from 'next-rest-framework'; | ||
import { zfd } from 'zod-form-data'; | ||
import { z } from 'zod'; | ||
import { jsonFileSchema } from '@/utils'; | ||
|
||
/* | ||
* Example app router route handler with multipart form-data. | ||
* Edge runtime is not supported for multipart/form-data. | ||
* A zod-form-data schema is required for the input instead of a regular Zod schema. | ||
*/ | ||
export const { POST } = route({ | ||
multipartFormData: routeOperation({ | ||
method: 'POST', | ||
openApiOperation: { | ||
tags: ['example-api', 'app-router', 'form-data', 'multipart'] | ||
} | ||
}) | ||
.input({ | ||
contentType: 'multipart/form-data', | ||
body: zfd.formData( | ||
z.object({ | ||
text: z.string(), | ||
file: jsonFileSchema | ||
}) | ||
) | ||
}) | ||
.outputs([ | ||
{ | ||
status: 200, | ||
contentType: 'application/octet-stream', | ||
schema: jsonFileSchema | ||
} | ||
]) | ||
.handler(async (req) => { | ||
// const json = await req.json(); // Strongly typed parsed form data as JSON - multipart content like files are empty objects in the JSON but can be accessed from the form data below. | ||
const formData = await req.formData(); // Strongly typed form data. | ||
const file = formData.get('file'); | ||
|
||
// Type-checked response. | ||
return new TypedNextResponse(file, { | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
'Content-Disposition': `attachment; filename="${file.name}"` | ||
} | ||
}); | ||
}) | ||
}); |
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
apps/example/src/app/api/v2/form-data/url-encoded/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,42 @@ | ||
import { TypedNextResponse, route, routeOperation } from 'next-rest-framework'; | ||
import { zfd } from 'zod-form-data'; | ||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
text: z.string() | ||
}); | ||
|
||
export const runtime = 'edge'; // Edge runtime is supported for application/x-www-form-urlencoded. | ||
|
||
/* | ||
* Example app router route handler with application/x-www-form-urlencoded form data. | ||
* A zod-form-data schema is required for the input instead of a regular Zod schema. | ||
*/ | ||
export const { POST } = route({ | ||
urlEncodedFormData: routeOperation({ | ||
method: 'POST', | ||
openApiOperation: { | ||
tags: ['example-api', 'app-router', 'form-data', 'url-encoded'] | ||
} | ||
}) | ||
.input({ | ||
contentType: 'application/x-www-form-urlencoded', | ||
body: zfd.formData(schema) | ||
}) | ||
.outputs([ | ||
{ | ||
status: 200, | ||
contentType: 'application/octet-stream', | ||
schema | ||
} | ||
]) | ||
.handler(async (req) => { | ||
const { text } = await req.json(); // Strongly typed parsed form data as JSON. | ||
// const formData = await req.formData(); // Strongly typed form data. | ||
|
||
// Type-checked response. | ||
return TypedNextResponse.json({ | ||
text | ||
}); | ||
}) | ||
}); |
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
apps/example/src/pages/api/v1/form-data/multipart/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,58 @@ | ||
import { jsonFileSchema } from '@/utils'; | ||
import { apiRoute, apiRouteOperation } from 'next-rest-framework'; | ||
import { z } from 'zod'; | ||
import { zfd } from 'zod-form-data'; | ||
|
||
const schema = z.object({ | ||
text: z.string(), | ||
file: jsonFileSchema | ||
}); | ||
|
||
// Body parser must be disabled when parsing multipart/form-data requests with pages router. | ||
export const config = { | ||
api: { | ||
bodyParser: false | ||
} | ||
}; | ||
|
||
/* | ||
* Example pages router route handler with multipart form data. | ||
* A zod-form-data schema is required for the input instead of a regular Zod schema. | ||
*/ | ||
export default apiRoute({ | ||
multipartFormData: apiRouteOperation({ | ||
method: 'POST', | ||
openApiOperation: { | ||
tags: ['example-api', 'pages-router', 'form-data', 'multipart'] | ||
} | ||
}) | ||
.input({ | ||
contentType: 'multipart/form-data', | ||
body: zfd.formData(schema) | ||
}) | ||
.handler(async (req, res) => { | ||
const formData = req.body; // Strongly typed form data. | ||
const file = formData.get('file'); | ||
const reader = file.stream().getReader(); | ||
res.setHeader('Content-Type', 'application/octet-stream'); | ||
|
||
res.setHeader( | ||
'Content-Disposition', | ||
`attachment; filename="${file.name}"` | ||
); | ||
|
||
const pump = async () => { | ||
await reader.read().then(async ({ done, value }) => { | ||
if (done) { | ||
res.end(); | ||
return; | ||
} | ||
|
||
res.write(value); | ||
await pump(); | ||
}); | ||
}; | ||
|
||
await pump(); | ||
}) | ||
}); |
39 changes: 39 additions & 0 deletions
39
apps/example/src/pages/api/v1/form-data/url-encoded/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,39 @@ | ||
import { apiRoute, apiRouteOperation } from 'next-rest-framework'; | ||
import { z } from 'zod'; | ||
import { zfd } from 'zod-form-data'; | ||
|
||
const schema = z.object({ | ||
text: z.string() | ||
}); | ||
|
||
/* | ||
* Example pages router route handler with application/x-www-form-urlencoded form data form data. | ||
* A zod-form-data schema is required for the input instead of a regular Zod schema. | ||
*/ | ||
export default apiRoute({ | ||
urlEncodedFormData: apiRouteOperation({ | ||
method: 'POST', | ||
openApiOperation: { | ||
tags: ['example-api', 'pages-router', 'form-data', 'url-encoded'] | ||
} | ||
}) | ||
.input({ | ||
contentType: 'application/x-www-form-urlencoded', | ||
body: zfd.formData(schema) | ||
}) | ||
.outputs([ | ||
{ | ||
status: 200, | ||
contentType: 'application/json', | ||
schema | ||
} | ||
]) | ||
.handler((req, res) => { | ||
const formData = req.body; // Strongly typed form data. | ||
|
||
// Type-checked response. | ||
res.json({ | ||
text: formData.get('text') | ||
}); | ||
}) | ||
}); |
Oops, something went wrong.