MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / projection

Function projection

framework/framework/api.ts:78–96  ·  view source on GitHub ↗
(input: T, schema: S, serializeCtx?: any)

Source from the content-addressed store, hash-verified

76 };
77
78export 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
98export interface ApiExecutionContext {
99}

Callers 2

projection.spec.tsFile · 0.90
executeMethod · 0.85

Calls 1

serializeMethod · 0.45

Tested by

no test coverage detected