Skip to content

Conversation

@Enngage
Copy link
Member

@Enngage Enngage commented Feb 6, 2025

This PR adds ability to provide generics to client such as:

type CoreContentType = IContentItem<IContentItemElements, 'a', 'b', 'c', 'd', 'e', 'd'>;

type ClientTypes = {
    readonly contentItemType: CoreContentType;
    readonly contentTypeCodenames: 'a' | 'b';
    readonly workflowCodenames: 'a' | 'b';
    readonly worfklowStepCodenames: 'a' | 'b';
    readonly collectionCodenames: 'a' | 'b';
    readonly taxonomyCodenames: 'a' | 'b';
    readonly languageCodenames: 'a' | 'b';
    readonly elementCodenames: 'a' | 'b';
};

const client = new DeliveryClient<ClientTypes>

The idea is that the ClientTypes would be generated by https://github.com/kontent-ai/model-generator-js

@Enngage Enngage requested review from a team and IvanKiral as code owners February 6, 2025 13:21
Copy link
Contributor

@IvanKiral IvanKiral left a comment

Choose a reason for hiding this comment

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

I don't see any updates on readme :/ How I as the end user would know how to utilize model generator with this?

}

export class ElementMapper {
export class ElementMapper<TClientTypes extends ClientTypes> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this class doesn't need to send the whole type of object TClientTypes it just needs need type TClientTypes['contentItemType'].

It would be easier to test this class if you need to pass only the necessary stuff

Check also the other classes for this

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't, the only reason I passed all types is for consistency and ease of use. Do you think it's worth splitting?

Copy link
Contributor

Choose a reason for hiding this comment

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

As I understand the point of ease of use, I am not sure about consistency. These generic types are new after all, so they are only consistent with the new code you are writing :).
For me there are two options:

  1. Pass only the required type, which is more correct of system modelling. This improves the type dependency coupling.
    • If I wanted to unit test only ElementMapper I would need to define the whole type to the generic. That would be bothering as the class really only needs type of contentItemType. So for me as a tester of ElementMapper would be nice to pass just ElementMapper<TContentItemType>
  2. If you just want to pass the object everywhere, I guess there is not as elegant workaround to satisfy both ends (ease of use and creating only part of the object)
    ElementMapper<TClientTypes extends Pick<ClientTypes, 'contentItemType'>>

Copy link
Member Author

Choose a reason for hiding this comment

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

To me it seemed more straightforward to just pass the TClientTypes as a whole, but I changed it so the mappers now need only required generic arguments :)

@Enngage
Copy link
Member Author

Enngage commented Feb 11, 2025

That's a fair point, I will have to update the readme, but currently, the model generator does not yet support this. I wanted to finalize this first.

One idea I was toying with is to enforce the generic argument when creating a delivery client. This way developers would have to look at this & either use the default types or generate the models. It would also help with the discoverability of the model generator and therefore improve the developer experience across all projects using it. What do you think @IvanKiral ?

@Enngage Enngage mentioned this pull request Feb 13, 2025
@Enngage
Copy link
Member Author

Enngage commented Feb 13, 2025

Preview released as @kontent-ai/[email protected]

@Enngage Enngage merged commit 89cbc39 into master Mar 10, 2025
1 check passed
@Enngage Enngage deleted the adding-client-generics branch April 8, 2025 11:25
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.

3 participants