framework&ui: add serialize guard [skip-cache]

This commit is contained in:
undefined committed 2026-04-17 04:13:18 +08:00
1 parent 3d70cf3352
commit 0890c75c17
5 files changed
+24 -14

No files matched your search

+8 -5
View File
@@ -75,19 +75,22 @@ type Projection<T, S> = S extends ProjectionSchemaId
[K in keyof T & keyof S]: K extends keyof AsKeys<S> ? Projection<T[K], AsKeys<S>[K]> : never
};
export const projection = <T, S extends ProjectionSchema<T>>(input: T, schema: S): Projection<T, S> => {
export const projection = <T, S extends ProjectionSchema<T>>(input: T, schema: S, serializeCtx?: any): Projection<T, S> => {
if (typeof input !== 'object' || input === null) throw new Error('Input must be an object.');
type R = Projection<T, S>;
if ('serialize' in input && typeof (input as any).serialize === 'function') {
input = (input as any).serialize(serializeCtx);
}
if (Array.isArray(schema)) schema = Object.fromEntries(schema.map((s) => [s, 1])) as S;
if (Array.isArray(input)) {
return input.map((item) => projection(item, schema)) as R;
return input.map((item) => projection(item, schema, serializeCtx)) as R;
}
const result = {} as R;
for (const key of Reflect.ownKeys(input)) {
for (const key of Reflect.ownKeys(input as any)) {
const schemaIt = schema[key];
if (!schemaIt) continue;
if (schemaIt === 1 || !input[key]) result[key] = input[key];
else result[key] = projection(input[key], schemaIt);
else result[key] = projection(input[key], schemaIt, serializeCtx);
}
return result;
};
@@ -174,7 +177,7 @@ export class ApiService extends Service {
}
}
}
return (project && typeof result === 'object' && result !== null) ? projection(result, project) : result;
return (project && typeof result === 'object' && result !== null) ? projection(result, project, context) : result;
}
}