MCPcopy
hub / github.com/Effect-TS/effect / toResponseSchema

Function toResponseSchema

packages/platform/src/HttpApiBuilder.ts:787–841  ·  view source on GitHub ↗
(getStatus: (ast: AST.AST) => number)

Source from the content-addressed store, hash-verified

785 ParseResult.fail(new ParseResult.Forbidden(ast, _, "Encode only schema"))
786
787const 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
833 }
834 const transform = Schema.transformOrFail(responseSchema, schema, {
835 decode: decodeForbidden,
836 encode: schemaToResponse
837 })
838 cache.set(transform.ast, transform)
839 return transform
840 }
841}
842
843const toResponseSuccess = toResponseSchema(HttpApiSchema.getStatusSuccessAST)
844const toResponseError = toResponseSchema(HttpApiSchema.getStatusErrorAST)

Callers 1

HttpApiBuilder.tsFile · 0.85

Calls 2

getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected