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

Function requestPayload

packages/platform/src/HttpApiBuilder.ts:544–585  ·  view source on GitHub ↗
(
  request: HttpServerRequest.HttpServerRequest,
  urlParams: ReadonlyRecord<string, string | Array<string>>,
  multipartLimits: Option.Option<Multipart.withLimits.Options>
)

Source from the content-addressed store, hash-verified

542// internal
543
544const requestPayload = (
545 request: HttpServerRequest.HttpServerRequest,
546 urlParams: ReadonlyRecord<string, string | Array<string>>,
547 multipartLimits: Option.Option<Multipart.withLimits.Options>
548): Effect.Effect<
549 unknown,
550 ParseResult.ParseError,
551 | FileSystem
552 | Path
553 | Scope
554> => {
555 if (!HttpMethod.hasBody(request.method)) {
556 return Effect.succeed(urlParams)
557 }
558 const contentType = request.headers["content-type"]
559 ? request.headers["content-type"].toLowerCase().trim()
560 : "application/json"
561 if (contentType.includes("application/json") || contentType.split(";", 1)[0].trimEnd().endsWith("+json")) {
562 return Effect.flatMap(Effect.orDie(request.text), (text) => {
563 if (text === "") return Effect.succeed(null)
564 try {
565 return Effect.succeed(JSON.parse(text) as unknown)
566 } catch {
567 return Effect.fail(
568 new ParseResult.ParseError({
569 issue: new ParseResult.Type(Schema.Unknown.ast, text, "Expected a valid JSON body")
570 })
571 )
572 }
573 })
574 } else if (contentType.includes("multipart/form-data")) {
575 return Effect.orDie(Option.match(multipartLimits, {
576 onNone: () => request.multipart,
577 onSome: (limits) => Multipart.withLimits(request.multipart, limits)
578 }))
579 } else if (contentType.includes("x-www-form-urlencoded")) {
580 return Effect.map(Effect.orDie(request.urlParamsBody), UrlParams.toRecord)
581 } else if (contentType.startsWith("text/")) {
582 return Effect.orDie(request.text)
583 }
584 return Effect.map(Effect.orDie(request.arrayBuffer), (buffer) => new Uint8Array(buffer))
585}
586
587type MiddlewareMap = Map<string, {
588 readonly tag: HttpApiMiddleware.TagClassAny

Callers 1

handlerToRouteFunction · 0.85

Calls 4

splitMethod · 0.80
parseMethod · 0.80
failMethod · 0.65
mapMethod · 0.65

Tested by

no test coverage detected