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

Function decodePayload

packages/effect/src/http-api/HttpApiBuilder.ts:804–873  ·  view source on GitHub ↗
(
  payloadBy: Map<string, PayloadDecoder>,
  httpRequest: HttpServerRequest,
  query: Record<string, string | Array<string>>
)

Source from the content-addressed store, hash-verified

802}
803
804function decodePayload(
805 payloadBy: Map<string, PayloadDecoder>,
806 httpRequest: HttpServerRequest,
807 query: Record<string, string | Array<string>>
808): Effect.Effect<unknown, Schema.SchemaError, unknown> | HttpServerResponse | undefined {
809 const hasBody = HttpMethod.hasBody(httpRequest.method)
810 const contentType = hasBody
811 ? MediaType.normalize(httpRequest.headers["content-type"] ?? "application/json")
812 : "application/x-www-form-urlencoded"
813 const existing = payloadBy.get(contentType)
814 if (!existing) {
815 return Response.text(`Unsupported content-type: ${contentType}`, { status: 415 })
816 }
817 if (existing._tag === "Stream") {
818 if (existing.maxBytes !== undefined && Number(httpRequest.headers["content-length"]) > existing.maxBytes) {
819 return payloadTooLargeResponse
820 }
821 // The stream can be pulled after the handler returns, inside a streaming
822 // response, so it carries the element schema's services with it.
823 return Effect.map(
824 Effect.context<unknown>(),
825 (context) => Stream.provideContext(existing.decode(httpRequest), context)
826 )
827 }
828 const { _tag, decode } = existing
829 switch (_tag) {
830 case "Multipart": {
831 if (existing.mode === "buffered") {
832 let eff = Effect.orDie(httpRequest.multipart)
833 if (existing.limits) {
834 eff = Effect.provideContext(eff, Multipart.limitsServices(existing.limits))
835 }
836 return Effect.flatMap(eff, decode)
837 }
838 return Effect.succeed(
839 existing.limits
840 ? Stream.provideContext(httpRequest.multipartStream, Multipart.limitsServices(existing.limits))
841 : httpRequest.multipartStream
842 )
843 }
844 case "Json":
845 return Effect.flatMap(Effect.orDie(httpRequest.text), (text) => {
846 if (text === "") {
847 return decode(existing.nullOnEmpty ? null : undefined)
848 }
849 try {
850 return decode(JSON.parse(text))
851 } catch {
852 return Effect.fail(
853 new Schema.SchemaError(
854 new SchemaIssue.InvalidValue({ message: "Expected a valid JSON body" })
855 )
856 )
857 }
858 })
859 case "Text":
860 return Effect.flatMap(Effect.orDie(httpRequest.text), decode)
861 case "FormUrlEncoded": {

Callers 1

handlerToHttpEffectFunction · 0.70

Calls 10

contextMethod · 0.80
decodeFunction · 0.70
getMethod · 0.65
textMethod · 0.65
failMethod · 0.65
NumberFunction · 0.50
mapMethod · 0.45
decodeMethod · 0.45
succeedMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected