Files
outline/app/models/decorators/Field.ts
T
Tom Moor 957a91129b fix: Key model fields by class, not minified class name (#14027)
The production bundle mangles class names, so unrelated models in separate
chunks (eg User/WebhookSubscription, Collection/Pin) shared one @Field list.
Adds a lint rule against reading constructor.name in client code.
2026-10-11 09:56:38 -04:00

41 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type Model from "../base/Model";
type ModelClass = Function;
// Keyed by the class itself – minified builds mangle class names and two
// classes in separate chunks can end up sharing one.
const fields = new WeakMap<ModelClass, (string | number | symbol)[]>();
/**
* Returns the keys recorded as serializable fields on a model.
*
* @param target the model to inspect.
* @returns the keys decorated with @Field.
*/
export const getFieldsForModel = <T extends Model>(target: T) =>
getFieldsForModelClass(target.constructor);
/**
* Returns the keys recorded as serializable fields on a model class.
*
* @param model the model class to inspect.
* @returns the keys decorated with @Field.
*/
export function getFieldsForModelClass(model: ModelClass) {
return fields.get(model) ?? [];
}
/**
* A decorator that records this key as a serializable field on the model.
* Properties decorated with @Field will be included in API requests by default.
*
* @param target the model the property is defined on.
* @param propertyKey the key to record as a field.
*/
const Field = (target: Model, propertyKey: string | symbol) => {
const modelClass = target.constructor;
fields.set(modelClass, [...(fields.get(modelClass) ?? []), propertyKey]);
};
export default Field;