intercom / contentful-typescript-codegen

Generate TypeScript interfaces from a Contentful environment
MIT License
281 stars 52 forks source link

Bug: field-types are not correct when calling getEntries with locale = '*' #51

Open Jgfrausing opened 3 years ago

Jgfrausing commented 3 years ago

A call to: getEntries({content_type: 'example', locale: 'en'}) matches the following generated types

export interface IExampleFields {
    /** Title */
    title: string;

    /** Slug */
    slug: string;
}

However, if requesting all languages with getEntries({content_type: 'example', locale: '*'}), the return type is instead (assuming title is not localised and slug is):

export interface IExampleFields {
    /** Title */
    title: string;

    /** Slug */
    slug: {string: string}[];
}

where the types in slug corresponds to {locale: slug}

I suggest extending the codegen to create IExampleFieldsWithLocalisation. This way it is possible to extend getEntries to return different types based on the locale.

I suspect contentful has designed it like this to avoid returning the sys object multiple times.

cjimmy commented 3 years ago

I second this issue, and will add that fields that are not localized still get an additional nesting to explicitly say which locale it is.

While a getEntries() query with default locale used locale: 'en' might result in

{
  title: 'Foo',
  slug: 'Bar'
}

A query with locale: '*' might look like this (assuming slug is localized and title is not)

{
  title: {
    en: 'Foo'
  },
  slug: {
    en: 'Bar',
    es: 'Bár'
  }
}

This may be specific to my particular Contentful setup, but it would be nice if this package supported this! (I'm very appreciative this package exists in the first place, to be clear.)