Taxum / @taxum/core / extract / form
Function: form()
form<
T
>(schema
):Extractor
<InferOutput
<T
>>
Defined in: extract/form.ts:71
Extractor that will get form data from the request and parse it.
The schema can be anything implementing the Standard Schema.
The source of the form data depends on the request method:
- If the request has a method of
GET
orHEAD
, the form data will be read from the query string. - If the request has a different method, the form will be read from the body of the request. It must have a
content-type
ofapplication/x-www-form-urlencoded
for this to work.
If the request is not GET
or HEAD
and is lacking a form data content type, it will reject the request with a 415 Unsupported Media Type
response.
If the body is malformed, it will reject the request with a 400 Bad Request
response.
If the form data cannot be parsed, it will reject the request with a 422 Unprocessable Content
response.
Type Parameters
T
T
extends StandardSchemaV1
<unknown
, unknown
>
Parameters
schema
T
Returns
Extractor
<InferOutput
<T
>>
Example
import { form } from "@taxum/core/extract";
import { m, Router } from "@taxum/core/routing";
import { z } from "zod";
const bodySchema = z.object({
foo: z.string(),
});
const handler = handler([form(bodySchema)], (body) => {
const foo = body.foo;
// ...
});
const router = new Router()
.route("/users", m.post(handler));
Throws
MissingFormDataContentTypeError if the request is not GET
or HEAD
and is lacking a form data content type.
Throws
InvalidFormDataError if the form data cannot be parsed.