prismicio / prismic-ts-codegen

A Prismic model-to-TypeScript-type generator.
Apache License 2.0
18 stars 6 forks source link

feat: support custom Integration Fields data with catalog-specific types #2

Closed angeloashmore closed 2 years ago

angeloashmore commented 2 years ago

Types of changes

Description

This PR adds the ability to type Integration Fields fields with custom catalog-specific types.

For example, if you know the object type returned by a Shopify catalog, you can configure the output to include your custom type.

Custom types can be provided to generateTypes()'s fieldConfigs.integrationFields.catalogTypes option:

const model = prismicM.model.customType({
    seed: t.title,
    id: "foo",
    fields: {
        bar: prismicM.model.integrationFields({
            seed: t.title,
            catalog: "baz"
        }),
    },
});

const types = generateTypes({
    customTypeModels: [model],
    fieldConfigs: {
        integrationFields: {
            catalogTypes: {
                baz: "BazType",
            },
        },
    },
});

This will provide the following output (this is not the full output, only the relevant part):

interface FooDocumentData {
    bar: prismicT.IntegrationFields<BazType>;
}

If no provider-specific types are given, the field will be typed as prismicT.IntegrationFields.

Checklist:

🐤

github-actions[bot] commented 2 years ago

size-limit report 📦

Path Size
dist/index.js 1.04 MB (+0.01% 🔺)
dist/index.cjs 1.04 MB (+0.01% 🔺)