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: add support for contentful.js v10 #247

Merged
merged 5 commits into from
Apr 25, 2023
Merged

feat: add support for contentful.js v10 #247

merged 5 commits into from
Apr 25, 2023

Conversation

veu
Copy link
Collaborator

@veu veu commented Apr 21, 2023

Add --v10 option to generate types compatible with contentful.js v10.

Details

  • Add new renderer for v10 types
  • Add new renderer for v10 type guards
  • Update JsDoc renderer to support new skeleton type
  • Add --v10 option to switch to v10 renderer
  • Support combining other options with --v10 (except for --localized which is redundant and throws error)

Example output

import type { ChainModifiers, Entry, EntryFieldTypes, EntrySkeletonType, LocaleCode } from "contentful";

export interface TypeAllFieldsFields {
    richText?: EntryFieldTypes.RichText;
    symbol?: EntryFieldTypes.Symbol;
    symbolWithValues?: EntryFieldTypes.Symbol<"option a" | "option b">;
    symbolList?: EntryFieldTypes.Array<EntryFieldTypes.Symbol>;
    symbolListWithValues?: EntryFieldTypes.Array<EntryFieldTypes.Symbol<"option a" | "option b">>;
    text?: EntryFieldTypes.Text;
    textWithValues?: EntryFieldTypes.Text<"option a" | "option b">;
    integer?: EntryFieldTypes.Integer;
    integerWithValues?: EntryFieldTypes.Integer<1 | 2 | 3>;
    decimal?: EntryFieldTypes.Number;
    decimalWithValues?: EntryFieldTypes.Number<1 | 2 | 3>;
    datetime?: EntryFieldTypes.Date;
    location?: EntryFieldTypes.Location;
    media?: EntryFieldTypes.AssetLink;
    mediaList?: EntryFieldTypes.Array<EntryFieldTypes.AssetLink>;
    boolean?: EntryFieldTypes.Boolean;
    json?: EntryFieldTypes.Object;
    reference?: EntryFieldTypes.EntryLink<EntrySkeletonType>;
    referenceWith1Ct?: EntryFieldTypes.EntryLink<TypeLinkedContentType1Skeleton>;
    referenceWith2CTs?: EntryFieldTypes.EntryLink<TypeLinkedContentType1Skeleton | TypeLinkedContentType2Skeleton>;
    referenceList?: EntryFieldTypes.Array<EntryFieldTypes.EntryLink<EntrySkeletonType>>;
    referenceListWith1Ct?: EntryFieldTypes.Array<EntryFieldTypes.EntryLink<TypeLinkedContentType1Skeleton>>;
    referenceListWith2CTs?: EntryFieldTypes.Array<EntryFieldTypes.EntryLink<TypeLinkedContentType1Skeleton | TypeLinkedContentType2Skeleton>>;
    resourceLink?: EntryFieldTypes.EntryResourceLink<EntrySkeletonType>;
}

export type TypeAllFieldsSkeleton = EntrySkeletonType<TypeAllFieldsFields, "allFields">;
export type TypeAllFields<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry<TypeAllFieldsSkeleton, Modifiers, Locales>;

export function isTypeAllFields<Modifiers extends ChainModifiers, Locales extends LocaleCode>(entry: Entry<EntrySkeletonType, Modifiers, Locales>): entry is TypeAllFields<Modifiers, Locales> {
    return entry.sys.contentType.sys.id === 'allFields'
}

export interface TypeLinkedContentType1Fields {
    title?: EntryFieldTypes.Symbol;
}

export type TypeLinkedContentType1Skeleton = EntrySkeletonType<TypeLinkedContentType1Fields, "linkedContentType1">;
export type TypeLinkedContentType1<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry<TypeLinkedContentType1Skeleton, Modifiers, Locales>;

export function isTypeLinkedContentType1<Modifiers extends ChainModifiers, Locales extends LocaleCode>(entry: Entry<EntrySkeletonType, Modifiers, Locales>): entry is TypeLinkedContentType1<Modifiers, Locales> {
    return entry.sys.contentType.sys.id === 'linkedContentType1'
}

export interface TypeLinkedContentType2Fields {
    title?: EntryFieldTypes.Symbol;
}

export type TypeLinkedContentType2Skeleton = EntrySkeletonType<TypeLinkedContentType2Fields, "linkedContentType2">;
export type TypeLinkedContentType2<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry<TypeLinkedContentType2Skeleton, Modifiers, Locales>;

export function isTypeLinkedContentType2<Modifiers extends ChainModifiers, Locales extends LocaleCode>(entry: Entry<EntrySkeletonType, Modifiers, Locales>): entry is TypeLinkedContentType2<Modifiers, Locales> {
    return entry.sys.contentType.sys.id === 'linkedContentType2'
}

@veu veu marked this pull request as ready for review April 21, 2023 08:40
Copy link
Collaborator

@marcolink marcolink left a comment

Choose a reason for hiding this comment

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

I think we should give it a try :)

@marcolink
Copy link
Collaborator

Solves #203

@veu veu merged commit 7e360bf into contentful-userland:master Apr 25, 2023
@veu veu deleted the contentful-v10 branch April 25, 2023 07:17
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.

None yet

2 participants