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

feat: adds support for client generics, improving type safety in comb… #401

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

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 IvanKiral and a team 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?

@@ -18,15 +19,15 @@ interface IRichTextImageUrlRecord {
newUrl: string;
}

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'>>

}

export class ItemMapper {
private readonly elementMapper: ElementMapper;
export class ItemMapper<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.

the same as the comment for element mapper

lib/mappers/item.mapper.ts Outdated Show resolved Hide resolved
lib/models/item-models.ts Outdated Show resolved Hide resolved
lib/query/item/multiple-items-query.class.ts Outdated Show resolved Hide resolved
lib/query/taxonomy/taxonomies-query.class.ts Outdated Show resolved Hide resolved
lib/query/taxonomy/taxonomy-query.class.ts Outdated Show resolved Hide resolved
@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 ?

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.

2 participants