(input: T, schema: S, serializeCtx?: any)
| 76 | }; |
| 77 | |
| 78 | export const projection = <T, S extends ProjectionSchema<T>>(input: T, schema: S, serializeCtx?: any): Projection<T, S> => { |
| 79 | if (typeof input !== 'object' || input === null) throw new Error('Input must be an object.'); |
| 80 | type R = Projection<T, S>; |
| 81 | if ('serialize' in input && typeof (input as any).serialize === 'function') { |
| 82 | input = (input as any).serialize(serializeCtx); |
| 83 | } |
| 84 | if (Array.isArray(schema)) schema = Object.fromEntries(schema.map((s) => [s, 1])) as S; |
| 85 | if (Array.isArray(input)) { |
| 86 | return input.map((item) => projection(item, schema, serializeCtx)) as R; |
| 87 | } |
| 88 | const result = {} as R; |
| 89 | for (const key of Reflect.ownKeys(input as any)) { |
| 90 | const schemaIt = schema[key]; |
| 91 | if (!schemaIt) continue; |
| 92 | if (schemaIt === 1 || !input[key]) result[key] = input[key]; |
| 93 | else result[key] = projection(input[key], schemaIt, serializeCtx); |
| 94 | } |
| 95 | return result; |
| 96 | }; |
| 97 | |
| 98 | export interface ApiExecutionContext { |
| 99 | } |
no test coverage detected