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