mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
framework&ui: add serialize guard [skip-cache]
This commit is contained in:
5 files changed
+24
-14
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user