MCPcopy Create free account
hub / github.com/Effect-TS/effect / schemaJson

Function schemaJson

packages/effect/src/http/HttpRouter.ts:404–447  ·  view source on GitHub ↗
(
  schema: Schema.ConstraintCodec<A, I, RD, unknown>,
  options?: (ParseOptions & HttpIncomingMessage.JsonOptions) | undefined
)

Source from the content-addressed store, hash-verified

402 * @since 4.0.0
403 */
404export 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.

Callers

nothing calls this directly

Calls 2

getMethod · 0.65
parseFunction · 0.50

Tested by

no test coverage detected