keystonejs / keystone

The most powerful headless CMS for Node.js β€” built with GraphQL and React
https://keystonejs.com
MIT License
8.98k stars 1.13k forks source link

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

Closed iamandrewluca closed 1 month ago

iamandrewluca commented 2 months ago

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 πŸ™ˆ ```ts import { type BaseListTypeInfo, type ListHooks } from '@keystone-6/core/types'; export type ExtractObject = T extends Record ? T : never; export type ApplicableOperation = 'create' | 'update' | 'delete'; // 😱 type ValidateArgs< ListTypeInfo extends BaseListTypeInfo, Operation extends ApplicableOperation, > = Omit< Parameters< NonNullable['validate']>[Operation]> >[0], 'addValidationError' >; export type ValidateOptions = ( | ValidateArgs | ValidateArgs | ValidateArgs ) & { field: string; }; export interface Validator< ListTypeInfo extends BaseListTypeInfo = BaseListTypeInfo, > { validate: ( options: ValidateOptions, ) => Promise | string | undefined; } ```
codesandbox-ci[bot] commented 2 months ago

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 37f568fb33317d7c7c62bbf080b061db2feb903a:

Sandbox Source
@keystone-6/sandbox Configuration
dcousens commented 2 months ago

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 commented 1 month ago

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!

dcousens commented 1 month ago

@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