| 307 | * @since 4.0.0 |
| 308 | */ |
| 309 | export const schemaJson = < |
| 310 | A, |
| 311 | I extends Partial<{ |
| 312 | readonly method: HttpMethod.HttpMethod |
| 313 | readonly url: string |
| 314 | readonly cookies: Readonly<Record<string, string | undefined>> |
| 315 | readonly headers: Readonly<Record<string, string | undefined>> |
| 316 | readonly pathParams: Readonly<Record<string, string | undefined>> |
| 317 | readonly searchParams: Readonly<Record<string, string | ReadonlyArray<string> | undefined>> |
| 318 | readonly body: any |
| 319 | }>, |
| 320 | RD |
| 321 | >( |
| 322 | schema: Schema.ConstraintCodec<A, I, RD, unknown>, |
| 323 | options?: ParseOptions | undefined |
| 324 | ): Effect.Effect< |
| 325 | A, |
| 326 | HttpServerError.HttpServerError | Schema.SchemaError, |
| 327 | HttpServerRequest.HttpServerRequest | HttpServerRequest.ParsedSearchParams | RouteContext | RD |
| 328 | > => { |
| 329 | const parse = Schema.decodeUnknownEffect(schema) |
| 330 | return Effect.contextWith( |
| 331 | ( |
| 332 | context: Context.Context< |
| 333 | HttpServerRequest.HttpServerRequest | HttpServerRequest.ParsedSearchParams | RouteContext |
| 334 | > |
| 335 | ) => { |
| 336 | const request = Context.get(context, HttpServerRequest.HttpServerRequest) |
| 337 | const searchParams = Context.get(context, HttpServerRequest.ParsedSearchParams) |
| 338 | const routeContext = Context.get(context, RouteContext) |
| 339 | return Effect.flatMap(request.json, (body) => |
| 340 | parse({ |
| 341 | method: request.method, |
| 342 | url: request.url, |
| 343 | headers: request.headers, |
| 344 | cookies: request.cookies, |
| 345 | pathParams: routeContext.params, |
| 346 | searchParams, |
| 347 | body |
| 348 | }, options)) |
| 349 | } |
| 350 | ) |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Decodes a schema from the current request without reading the request body. |