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

Allow generating content item codenames #32

Open
ghost opened this issue Sep 20, 2022 · 3 comments
Open

Allow generating content item codenames #32

ghost opened this issue Sep 20, 2022 · 3 comments
Assignees

Comments

@ghost
Copy link

ghost commented Sep 20, 2022

Motivation

To increase type-safety and minimize errors when querying for content items by their codename.

Adding an option to output some, or all, content item codenames would allow us to further constrain the codenames of content items by letting us specify that content item codenames can't be just any string, but instead one of many very specific strings.

Proposed solution

One solution may be to add a property to the existing IExportProjectSettings interface so that users can choose to generate types for their content item codenames, like so:

const config: IGenerateModelsConfig = {
  sdkType: 'delivery',
  projectId: '<project id>',
  apiKey: '<api key>',

  exportProjectSettings: {
    /** set to true to generate a `contentItems.ts` file alongside the existing `contentTypes.ts` file. */
    exportContentItemCodenames: true, 

   /** or perhaps a function to allow the user to specify only certain content types / items ? */
   exportContentItemCodenames: ( content_type, content_item ) => true, 
  },

Then, given a project with a content type called Hyperlink, having 3 content items whose codenames are: contact_us, warranty_and_repair, and returns, the output file could look something like:

// contentTypes.ts

export type HyperlinkCodenames = 'contact_us' | 'warranty_and_repair' | 'returns';

We could then use these types to constrain a function like this:

async function getHyperlinks(codename: HyperlinkCodenames) {
  const hyperlinks = await KontentDeliveryClient.items<Hyperlink>()
    .type(contentTypes.hyperlink.codename)
    .equalsFilter('system.codename', codename)
    .toPromise()
    .then((response) => response.data.items);

  return hyperlinks;
}

Additional considerations

If a content type has more than a certain number of content items, the proposed solution may not be ideal; as it could lead to very large file(s) that are difficult to navigate and may slow the performance of the IDE.

It might be good to allow additional configuration in the IGenerateModelsConfig to adjust the limit on the number of generated codenames.

@Enngage Enngage self-assigned this Sep 22, 2022
@Enngage
Copy link
Contributor

Enngage commented Sep 22, 2022

Hey @bzs-ngraham,

I had similar thoughts in the past and while it would definitely be beneficial in some scenarios, it didn't make it into our backlog yet ;) It generally doesn't make sense to generate codenames for all content items so we would have to introduce new config for both type & number of content items.

I will likely implement this at some point, but I can't give any estimates at the moment.

@itworksafisher
Copy link

I think this needs to be closed. The generator currently exports a contentTypes const which supplies this functionality.

@Enngage
Copy link
Contributor

Enngage commented Jun 29, 2023

I think this needs to be closed. The generator currently exports a contentTypes const which supplies this functionality.

This was more about generating codenames of content items which is a valid point imho. My only concern is that it could be a very large file if you had say 100k content items in your project :)

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

No branches or pull requests

2 participants