(getStatus: (schema: Schema.Constraint) => number)
| 1130 | } |
| 1131 | |
| 1132 | function toResponseSchema(getStatus: (schema: Schema.Constraint) => number) { |
| 1133 | // WithHeaders wrappers share a single declaration AST, so they are cached by instance |
| 1134 | const cache = new WeakMap<object, Schema.Top>() |
| 1135 | |
| 1136 | return (schema: Schema.Constraint): Schema.ConstraintEncoder<HttpServerResponse, unknown> => { |
| 1137 | const key = HttpApiSchema.isWithHeaders(schema) ? schema : schema.ast |
| 1138 | const cached = cache.get(key) |
| 1139 | if (cached !== undefined) { |
| 1140 | return cached as any |
| 1141 | } |
| 1142 | const bodySchema = HttpApiSchema.isWithHeaders(schema) ? schema.schema : schema |
| 1143 | const responseSchema = $HttpServerResponse.pipe( |
| 1144 | Schema.decodeTo(bodySchema, getResponseTransformation(getStatus, schema)) |
| 1145 | ) |
| 1146 | cache.set(key, responseSchema) |
| 1147 | return responseSchema |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | function getResponseTransformation( |
| 1152 | getStatus: (schema: Schema.Constraint) => number, |
no test coverage detected