import type Jsonable from '../../Contracts/Jsonable'; import GuardsAttributes from './GuardsAttributes'; import type Model from '../Model'; import type ModelCollection from '../ModelCollection'; import type { KeysNotMatching, MaybeArray, UnionToIntersection } from '../../Support/type'; type InternalProperties = 'attributeCasing' | 'attributeCasts' | 'attributes' | 'casts' | 'endpoint' | 'exists' | 'fillable' | 'fillableAttributes' | 'guarded' | 'guardedAttributes' | 'hasOneOrManyParentKeyName' | 'loading' | 'mutatedEndpoint' | 'original' | 'primaryKey' | 'relationMethodPrefix' | 'relations' | 'requestCount' | 'serverAttributeCasing'; /** * All keys of the model except where the value is a method, * or it's a property defined internally. * * Results in a union of keys. */ export type AttributeKeys = Exclude, InternalProperties> | string; /** * All keys of the model except where the value is a method, * a relation, or it's a property defined internally. * * Results in a union of keys. */ export type SimpleAttributeKeys = AttributeKeys | undefined>; /** * The attributes the model is handling. */ export type Attributes = { [K in AttributeKeys]: T[K]; }; /** * The attributes the model is handling except relation values. */ export type SimpleAttributes = { [K in SimpleAttributeKeys]: T[K] extends Model | ModelCollection ? never : T[K]; }; /** * Unwrap the model attributes recursively into attributes or array of attributes. */ export type RawAttributes = { [K in AttributeKeys]: T[K] extends Model ? RawAttributes : T[K] extends ModelCollection ? RawAttributes[] : T[K]; }; type FilterGetters = { [K in keyof T as Extract]: T[K] extends (...args: any) => any ? T[K] : never; }; type TransformGetters any>> = { [K in keyof T]: Record : never, T[K] extends (...args: any) => infer R ? R : never>; }; /** * Intersect the type with the names of the accessors and their return types. * * This type is experimental and may change. * @experimental */ export type Getters = Omit>[keyof TransformGetters>]>, ''> & T; export default class HasAttributes extends GuardsAttributes implements Jsonable, Iterable { /** * The model attribute. */ [key: string]: unknown; /** * Property indicating how attributes and relation names should be cast by default. * * @type {'snake'|'camel'} * * @readonly */ protected get attributeCasing(): 'camel' | 'snake'; /** * Property indicating how attributes and relation names * should be cast by default when sent to the server. * * @type {'snake'|'camel'} * * @protected */ protected get serverAttributeCasing(): 'camel' | 'snake'; /** * The attributes. * * @protected * * @type {object} */ protected attributes: SimpleAttributes; /** * The attribute's original state. * * @protected * * @type {object} */ protected original: SimpleAttributes; /** * The iterator used for looping over the attributes and relations. * * @type {Symbol.iterator} */ [Symbol.iterator](): Iterator; /** * Utility to cast the given string to the attributeCasing's case. * * @param {string} key * * @see attributeCasing * * @protected * * @return {string} */ protected setStringCase(key: AttributeKeys | string): string; /** * Utility to cast the given string to the serverAttributeCasing's case. * * @param {string} key * * @see serverAttributeCasing * * @protected * * @return {string} */ protected setServerStringCase(key: AttributeKeys | string): string; /** * Get an attribute from the model. * * @param {string} key * @param {any=} defaultValue * * @return {any} */ getAttribute | string, T extends this[K]>(key: K, defaultValue: T): T; getAttribute | string, T extends this[K]>(key: K, defaultValue?: T): T | undefined; /** * Get all the attributes on the model. * * @return {object} */ getAttributes(): SimpleAttributes; /** * Get all the attributes on the model without casting or accessors. * * @return {object} */ getRawAttributes(): SimpleAttributes; /** * Get available attribute keys. * * @return {string[]} */ getAttributeKeys(): string[]; /** * Set a given attribute on the model. * * @param {string} key * @param {any} value * * @return {this} */ setAttribute, T extends this[K]>(key: K, value: T): this; setAttribute(key: K, value: T): this; setAttribute | string, T extends K extends AttributeKeys ? this[K] : unknown = K extends AttributeKeys ? this[K] : unknown>(key: K, value: T): this; /** * Create simple access for the getters and setters that have no underlying attribute set. * * Should only be called after the attributes have already been set. */ protected setupMagicAccess(this: this): void; /** * Create descriptors for the given key(s) therefore allowing magic access. * * @param {string|string[]} keys * * @return {this} */ protected createDescriptor(keys: MaybeArray | string>): this; /** * Remove the attribute and its magic access if set. * * @param {string} key * * @return {this} */ deleteAttribute(key: AttributeKeys | string): this; /** * Determine if a set mutator exists for an attribute. * * @param {string} key * * @return {boolean} */ protected hasSetMutator(key: AttributeKeys | string): boolean; /** * Determine if a get accessor exists for an attribute. * * @param {string} key * * @return {boolean} */ protected hasGetAccessor(key: AttributeKeys | string): boolean; /** * Fill the model with the given attributes while respecting the guarding settings. * * @param {object} attributes * * @return {this} */ fill(attributes: Attributes | Attributes): this; /** * Fill the model with the given attributes without respecting the guarding settings. * * @param {object} attributes * * @return {this} */ forceFill(attributes: Attributes | Attributes): this; /** * Sync the original attributes with the current. * * @param {string=} keys * * @return {this} */ syncOriginal(keys?: MaybeArray | string>): this; /** * Reset the attributes to the original values. * * @return {this} */ reset(): this; /** * Get the original attributes. * * @param {string} key * @param {any} defaultValue * * @return {any} */ getOriginal | string, T extends this[K]>(key: K, defaultValue?: T): T; getOriginal(): SimpleAttributes; /** * Get the original attributes without casting. * * @param {string=} key * @param {any=} defaultValue * * @return {any} */ getRawOriginal | string, T extends this[K]>(key: K, defaultValue?: T): T; getRawOriginal(): SimpleAttributes; /** * Get the attributes that were changed. * * @param {string=} key * * @return {object} */ getChanges>(key: K): Record; getChanges(key?: string): Partial>; /** * Get the deleted attributes if any. * * @param {string=} key * * @return {object} */ getDeletedAttributes>(key: K): Record; getDeletedAttributes(key?: string): Partial>; /** * Get the new attributes if any. * * @param {string=} key * * @return {object} */ getNewAttributes>(key: K): Record; getNewAttributes(key?: string): Partial>; /** * Determine whether the given attribute has changed * or attributes has been added or deleted. * * @param {key|undefined} key * * @return {boolean} */ hasChanges(key?: SimpleAttributeKeys | string): boolean; /** * Alias for the hasChanges method. * * @see hasChanges * * @param {string|undefined} key * * @return {boolean} */ isDirty(key?: SimpleAttributeKeys | string): boolean; /** * Determine whether given or any attributes was changed. * * @param {string|undefined} key */ isClean(key?: SimpleAttributeKeys | string): boolean; /** * Get a subset of the model's attributes. * * @param {string|string[]} attributes * * @return {object} */ only[], R = { [P in K[number]]?: SimpleAttributes[P]; }>(attributes: K): R; only, R = Record>(attributes: K): R; /** * Get all model attributes except the given keys. * * @param {string|string[]} attributes * * @return {object} */ except[], R = Omit, K[number]>>(attributes: K): R; except | string, R = Omit, K>>(attributes: K): R; /** * @inheritDoc */ toJSON = RawAttributes>(): T; /** * Show the model in a human-readable string representation. * * @return {string} */ toString(): string; } export {}; //# sourceMappingURL=HasAttributes.d.ts.map