Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support method chaining #78

Merged
merged 3 commits into from
Mar 22, 2024
Merged

Support method chaining #78

merged 3 commits into from
Mar 22, 2024

Conversation

mizdra
Copy link
Owner

@mizdra mizdra commented Mar 20, 2024

This is miscellaneous improvements for #75.

@mizdra mizdra added the Type: Add Add new features. label Mar 20, 2024
@mizdra mizdra marked this pull request as draft March 20, 2024 16:01
@mizdra mizdra force-pushed the support-method-chaining branch from a14b6c0 to 293debe Compare March 21, 2024 11:35
@mizdra mizdra marked this pull request as ready for review March 21, 2024 11:40
@mizdra mizdra force-pushed the support-method-chaining branch from b131793 to 43bb989 Compare March 22, 2024 11:07
Comment on lines +145 to +184
class DefineTypeFactory<
Type extends Record<string, unknown>,
TransientFields extends Record<string, unknown>,
> extends Function {
_defaultTransientFields: TransientFields;
constructor(defaultTransientFields: TransientFields) {
super();
this._defaultTransientFields = defaultTransientFields;

// ref: https://gist.github.com/arccoza/50fe61c8430fc97a463bf6b8960776ce
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
apply: (target, _thisArg, [options]: [any]) => target._apply(options),
});
}

_apply<
_DefaultFieldsResolver extends FieldsResolver<Type & TransientFields>,
_Traits extends Traits<Type, TransientFields> = Traits<Type, TransientFields>,
>(
options: TypeFactoryDefineOptions<Type, TransientFields, _DefaultFieldsResolver, _Traits>,
): TypeFactoryInterface<Type, TransientFields, _DefaultFieldsResolver, _Traits> {
return defineTypeFactoryInternal<Type, TransientFields, _DefaultFieldsResolver, _Traits>(
Object.keys(this._defaultTransientFields),
{
...options,
defaultFields: { ...this._defaultTransientFields, ...options.defaultFields },
},
);
}

withTransientFields<NewTransientFields extends Record<string, unknown>>(
defaultTransientFields: NewTransientFields,
): DefineTypeFactoryInterface<Type, Merge<TransientFields, NewTransientFields>> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new DefineTypeFactory({ ...this._defaultTransientFields, ...defaultTransientFields }) as any;
}
}
export const defineTypeFactory = new DefineTypeFactory({}) as unknown as DefineTypeFactoryInterfaceRequired<{}, {}>;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meta programming :)

@mizdra mizdra merged commit efcceff into main Mar 22, 2024
6 checks passed
@mizdra mizdra deleted the support-method-chaining branch March 22, 2024 11:12
@mizdra mizdra added Type: Change Change existing functionality. and removed Type: Change Change existing functionality. labels Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Add Add new features.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant