Skip to content

Taxum / @taxum/core / extract / json

Function: json()

json<T>(schema): Extractor<InferOutput<T>>

Defined in: extract/json.ts:69

Extractor that will get JSON from the body and parse it.

The schema can be anything implementing the Standard Schema.

If the request is lacking a JSON 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 JSON 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

ts
import { json } 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([json(bodySchema)], (body) => {
    const foo = body.foo;

    // ...
});

const router = new Router()
    .route("/users", m.post(handler));

Throws

MissingJsonContentTypeError if the request is lacking a JSON content type.

Throws

MalformedJsonError if the body is malformed.

Throws

InvalidJsonError if the JSON cannot be parsed.