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

Function schemaToResponse

packages/platform/src/HttpApiBuilder.ts:800–840  ·  view source on GitHub ↗
(
    data: any,
    _: AST.ParseOptions,
    ast: AST.Transformation
  )

Source from the content-addressed store, hash-verified

798const toResponseSchema = (getStatus: (ast: AST.AST) => number) => {
799 const cache = new WeakMap<AST.AST, Schema.Schema.All>()
800 const schemaToResponse = (
801 data: any,
802 _: AST.ParseOptions,
803 ast: AST.Transformation
804 ): Effect.Effect<HttpServerResponse.HttpServerResponse, ParseResult.ParseIssue> => {
805 const isEmpty = HttpApiSchema.isVoid(ast.to)
806 const status = getStatus(ast.to)
807 if (isEmpty) {
808 return HttpServerResponse.empty({ status })
809 }
810 const encoding = HttpApiSchema.getEncoding(ast.to)
811 switch (encoding.kind) {
812 case "Json": {
813 return Effect.mapError(
814 HttpServerResponse.json(data, {
815 status,
816 contentType: encoding.contentType
817 }),
818 (error) => new ParseResult.Type(ast, error, "Could not encode to JSON")
819 )
820 }
821 case "Text": {
822 return ParseResult.succeed(HttpServerResponse.text(data as any, {
823 status,
824 contentType: encoding.contentType
825 }))
826 }
827 case "Uint8Array": {
828 return ParseResult.succeed(HttpServerResponse.uint8Array(data as any, {
829 status,
830 contentType: encoding.contentType
831 }))
832 }
833 case "UrlParams": {
834 return ParseResult.succeed(HttpServerResponse.urlParams(data as any, {
835 status,
836 contentType: encoding.contentType
837 }))
838 }
839 }
840 }
841 return <A, I, R>(schema: Schema.Schema<A, I, R>): Schema.Schema<A, HttpServerResponse.HttpServerResponse, R> => {
842 if (cache.has(schema.ast)) {
843 return cache.get(schema.ast)! as any

Callers

nothing calls this directly

Calls 3

getStatusFunction · 0.85
jsonMethod · 0.45
textMethod · 0.45

Tested by

no test coverage detected