| 402 | * @since 4.0.0 |
| 403 | */ |
| 404 | export const schemaJson = < |
| 405 | A, |
| 406 | I extends Partial<{ |
| 407 | readonly method: HttpMethod.HttpMethod |
| 408 | readonly url: string |
| 409 | readonly cookies: Readonly<Record<string, string | undefined>> |
| 410 | readonly headers: Readonly<Record<string, string | undefined>> |
| 411 | readonly pathParams: Readonly<Record<string, string | undefined>> |
| 412 | readonly searchParams: Readonly<Record<string, string | ReadonlyArray<string> | undefined>> |
| 413 | readonly body: any |
| 414 | }>, |
| 415 | RD |
| 416 | >( |
| 417 | schema: Schema.ConstraintCodec<A, I, RD, unknown>, |
| 418 | options?: (ParseOptions & HttpIncomingMessage.JsonOptions) | undefined |
| 419 | ): Effect.Effect< |
| 420 | A, |
| 421 | HttpServerError.HttpServerError | Schema.SchemaError, |
| 422 | HttpServerRequest.HttpServerRequest | HttpServerRequest.ParsedSearchParams | RouteContext | RD |
| 423 | > => { |
| 424 | const parse = Schema.decodeUnknownEffect(schema) |
| 425 | const parseBody = HttpIncomingMessage.schemaBodyJson(Schema.Unknown, options) |
| 426 | return Effect.contextWith( |
| 427 | ( |
| 428 | context: Context.Context< |
| 429 | HttpServerRequest.HttpServerRequest | HttpServerRequest.ParsedSearchParams | RouteContext |
| 430 | > |
| 431 | ) => { |
| 432 | const request = Context.get(context, HttpServerRequest.HttpServerRequest) |
| 433 | const searchParams = Context.get(context, HttpServerRequest.ParsedSearchParams) |
| 434 | const routeContext = Context.get(context, RouteContext) |
| 435 | return Effect.flatMap(parseBody(request), (body) => |
| 436 | parse({ |
| 437 | method: request.method, |
| 438 | url: request.url, |
| 439 | headers: request.headers, |
| 440 | cookies: request.cookies, |
| 441 | pathParams: routeContext.params, |
| 442 | searchParams, |
| 443 | body |
| 444 | }, options)) |
| 445 | } |
| 446 | ) |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Decodes a schema from the current request without reading the request body. |