prismicio / prismic-ts-codegen

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

feat: set the types provider using the `typesProvider` option #42

Closed angeloashmore closed 1 year ago

angeloashmore commented 1 year ago

Types of changes

Description

This PR allows prismic-ts-codegen to use different type providers. The following providers are supported:

@prismicio/types is used by default for backwards compatibility, but @prismicio/client is preferred since @prismicio/types is now deprecated. @prismicio/types was merged into @prismicio/client v7.

This PR also adds a detectTypesProvider() helper. See below for more details.

prismic-ts-codegen CLI

The prismic-ts-codegen CLI can be configured using a prismicCodegen.config.ts file.

A new typesProvider option allows for configuring the package used in the generated types file. If no value is provided, the correct provider is automatically detected. If one cannot be detected, @prismicio/types is used.

// prismicCodegen.config.ts

import type { Config } from "prismic-ts-codegen";

const config: Config = {
  typesProvider: "@prismicio/client", // "@prismicio/types" can also be provided
};

export default config;

The generated file's import statement will reflect the typesProvider value.

// Generated file

import * as prismic from "@prismicio/client";

generateTypes()

The generateTypes() function accepts a new typesProvider option that specifies the package used in the generated types file. If no value is provided, @prismicio/types is used.

generateTypes({
  typesProvider: "@prismicio/client", // "@prismicio/types" can also be provided
});

The output's import statement will reflect the typesProvider value.

// output

import * as prismic from "@prismicio/client";

detectTypesProvider()

The detectTypesProvider() helper attemps to detect a Node.js project's type provider. It loads @prismicio/client and @prismicio/types to determine which package is available, if any.

@prismicio/client v7 and above is supported. If <6 is found, it is ignored.

If no provider is detected, the helper returns undefined.

await detectTypesProvider();
// => "@prismicio/client" | "@prismicio/types" | undefined

This function can be used in combination with generateTypes() if automatic types provider detection is desired when generating types.

generateTypes({
  typesProvider: await detectTypesProvider(),
});

Checklist:

🐃

codecov-commenter commented 1 year ago

Codecov Report

Merging #42 (f95689a) into main (00814a9) will decrease coverage by 0.88%. The diff coverage is 84.31%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             main      #42      +/-   ##
==========================================
- Coverage   98.75%   97.87%   -0.88%     
==========================================
  Files          22       23       +1     
  Lines        1601     1644      +43     
  Branches      135      136       +1     
==========================================
+ Hits         1581     1609      +28     
- Misses         20       35      +15     
Impacted Files Coverage Δ
src/detectTypesProvider.ts 53.12% <53.12%> (ø)
src/lib/addInterfacePropertiesForFields.ts 98.65% <96.55%> (ø)
src/constants.ts 100.00% <100.00%> (ø)
src/generateTypes.ts 100.00% <100.00%> (ø)
src/index.ts 100.00% <100.00%> (ø)
src/lib/addTypeAliasForCustomType.ts 100.00% <100.00%> (ø)
src/lib/addTypeAliasForSharedSlice.ts 100.00% <100.00%> (ø)
src/lib/buildFieldDocs.ts 100.00% <100.00%> (ø)
src/lib/getAPIIDPath.ts 100.00% <100.00%> (ø)
src/lib/getFieldDocumentationURL.ts 100.00% <100.00%> (ø)
... and 7 more