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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core/types): export hooks function types #9129

Closed

Conversation

iamandrewluca
Copy link
Contributor

@iamandrewluca iamandrewluca commented May 2, 2024

It would be better to have the hooks function types exported from KS.

PR is in 2 commits. One formats the changed code, one does the actual changes.

Today, I had to write some code and reuse some KS types that are not exported directly but through the API. It was complicated, but I did it.

See the code 馃檲
import { type BaseListTypeInfo, type ListHooks } from '@keystone-6/core/types';

export type ExtractObject<T> = T extends Record<string, unknown> ? T : never;
export type ApplicableOperation = 'create' | 'update' | 'delete';

// 馃槺
type ValidateArgs<
	ListTypeInfo extends BaseListTypeInfo,
	Operation extends ApplicableOperation,
> = Omit<
	Parameters<
		NonNullable<ExtractObject<ListHooks<ListTypeInfo>['validate']>[Operation]>
	>[0],
	'addValidationError'
>;

export type ValidateOptions<ListTypeInfo extends BaseListTypeInfo> = (
	| ValidateArgs<ListTypeInfo, 'create'>
	| ValidateArgs<ListTypeInfo, 'update'>
	| ValidateArgs<ListTypeInfo, 'delete'>
) & {
	field: string;
};

export interface Validator<
	ListTypeInfo extends BaseListTypeInfo = BaseListTypeInfo,
> {
	validate: (
		options: ValidateOptions<ListTypeInfo>,
	) => Promise<string | undefined> | string | undefined;
}

Copy link

codesandbox-ci bot commented May 2, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 37f568f:

Sandbox Source
@keystone-6/sandbox Configuration

@iamandrewluca iamandrewluca force-pushed the feat/export-hooks-types branch 2 times, most recently from fab3cee to e90b963 Compare May 2, 2024 19:38
@@ -14,9 +9,9 @@ type CommonArgs<ListTypeInfo extends BaseListTypeInfo> = {
listKey: ListTypeInfo['key']
}

type ResolveInputListHook<
export type ResolveInputListHook<
Copy link
Member

Choose a reason for hiding this comment

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

Why did you need the Resolve* types?

) => MaybePromise<void>

type BeforeOperationListHook<
export type BeforeOperationListHook<
Copy link
Member

@dcousens dcousens May 5, 2024

Choose a reason for hiding this comment

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

Why not use Lists.Post['hooks']['beforeOperation'] as the type in your project? [assumes a Post list]
Where Lists is from import { type Lists } from '.keystone/types'

@dcousens
Copy link
Member

dcousens commented May 5, 2024

I am personally hesitant about this, as I think we should be promoting the usage of the refined, generated types, not these abstract types.

Maybe we can add additional exports to the refined types if something like Lists.Post['hooks']['beforeOperation'] is undesirable?

@iamandrewluca iamandrewluca marked this pull request as draft May 6, 2024 11:22
@iamandrewluca
Copy link
Contributor Author

Hey, @dcousens, I understand your point. In my case, I need only the args type of the validate function; all other exports I made are just for the sake of being exported. But I get your point.

Will close this PR for now (while rethinking how to use it the right way)

PS: Our project requirements started diverging from KS's base functionalities.
We might drop it in favour of NestJS. Thanks a lot for all the effort!

@iamandrewluca iamandrewluca deleted the feat/export-hooks-types branch May 14, 2024 16:00
@dcousens
Copy link
Member

@iamandrewluca no problem, and thanks for the contributions!

while rethinking how to use it the right way

Let me know if any examples could better help demonstrate how you could use the refined types in an abstract way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants