Gathering detailed insights and metrics for cf-content-types-generator
Gathering detailed insights and metrics for cf-content-types-generator
Gathering detailed insights and metrics for cf-content-types-generator
Gathering detailed insights and metrics for cf-content-types-generator
npm install cf-content-types-generator
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
117 Stars
214 Commits
26 Forks
19 Watching
7 Branches
17 Contributors
Updated on 28 Nov 2024
TypeScript (99.6%)
JavaScript (0.36%)
Batchfile (0.05%)
Cumulative downloads
Total Downloads
Last day
-13%
7,541
Compared to previous day
Last week
-5.2%
42,749
Compared to previous week
Last month
17.6%
188,620
Compared to previous month
Last year
180.6%
1,854,568
Compared to previous year
8
23
A CLI to generate Typescript definitions based on JSON export generated with contentful CLI.
1npm install cf-content-types-generator
1Contentful Content Types (TS Definitions) Generator 2 3USAGE 4 $ cf-content-types-generator [FILE] 5 6ARGUMENTS 7 FILE local export (.json) 8 9OPTIONS 10 -e, --environment=environment environment 11 -h, --help show CLI help 12 -o, --out=out output directory 13 -p, --preserve preserve output folder 14 -X --v10 create contentful.js v10 types 15 -r, --response add response types (only for v10 types) 16 -l, --localized add localized types 17 -d, --jsdoc add JSDoc comments 18 -g, --typeguard add type guards 19 -s, --spaceId=spaceId space id 20 -t, --token=token management token 21 -v, --version show CLI version 22 -a, --host The Management API host 23
Use a local JSON
file to load contentTypes
. Flags for spaceId
, token
and environement
will be ignored.
Will print result to console
1cf-content-types-generator path/to/exported/file.json
in a real world scenario, you would pipe the result to a file.
Will store resulting files in target directory
1cf-content-types-generator path/to/exported/file.json -o path/to/target/out/directory
existing directory content will be removed.
If no file
arg provided, remote mode is enabled.
spaceId
and token
flags need to be set.
1cf-content-types-generator -s 2l3j7k267xxx -t CFPAT-64FtZEIOruksuaE_Td0qBvHdELNWBCC0fZUWq1NFxxx
As input a json file with a contentTypes
field is expected:
1{ 2 "contentTypes": [ 3 { 4 "sys": { 5 "id": "artist", 6 "type": "ContentType" 7 }, 8 "displayField": "name", 9 "name": "Artist", 10 "fields": [ 11 { 12 "id": "name", 13 "name": "Name", 14 "type": "Symbol", 15 "required": true, 16 "validations": [ 17 { 18 "unique": true 19 } 20 ] 21 }, 22 { 23 "id": "profilePicture", 24 "name": "Profile Picture", 25 "type": "Link", 26 "required": false, 27 "validations": [ 28 { 29 "linkMimetypeGroup": ["image"] 30 } 31 ], 32 "linkType": "Asset" 33 }, 34 { 35 "id": "bio", 36 "name": "Bio", 37 "type": "RichText", 38 "required": false, 39 "validations": [ 40 { 41 "nodes": {} 42 }, 43 { 44 "enabledMarks": [], 45 "message": "Marks are not allowed" 46 }, 47 { 48 "enabledNodeTypes": [], 49 "message": "Nodes are not allowed" 50 } 51 ] 52 } 53 ] 54 }, 55 { 56 "sys": { 57 "id": "artwork", 58 "type": "ContentType" 59 }, 60 "displayField": "name", 61 "name": "Artwork", 62 "fields": [ 63 { 64 "id": "name", 65 "name": "Name", 66 "type": "Symbol", 67 "required": true, 68 "validations": [] 69 }, 70 { 71 "id": "type", 72 "name": "Type", 73 "type": "Symbol", 74 "required": false, 75 "validations": [ 76 { 77 "in": ["print", "drawing", "painting"], 78 "message": "Hello - this is a custom error message." 79 } 80 ] 81 }, 82 { 83 "id": "preview", 84 "name": "Preview", 85 "type": "Array", 86 "required": false, 87 "validations": [], 88 "items": { 89 "type": "Link", 90 "validations": [ 91 { 92 "linkMimetypeGroup": ["image", "audio", "video"] 93 } 94 ], 95 "linkType": "Asset" 96 } 97 }, 98 { 99 "id": "artist", 100 "name": "Artist", 101 "type": "Link", 102 "required": true, 103 "validations": [ 104 { 105 "linkContentType": ["artist"] 106 } 107 ], 108 "linkType": "Entry" 109 } 110 ] 111 } 112 ] 113}
This example shows a subset of the actual payload provided by contentful's cli export command.
1import * as CFRichTextTypes from '@contentful/rich-text-types'; 2import { Entry, EntryFields } from 'contentful'; 3 4export interface TypeArtistFields { 5 name: Contentful.EntryFields.Symbol; 6 profilePicture?: Contentful.Asset; 7 bio?: EntryFields.RichText; 8} 9 10export type TypeArtist = Entry<TypeArtistFields>; 11 12export interface TypeArtworkFields { 13 name: EntryFields.Symbol; 14 type?: 'print' | 'drawing' | 'painting'; 15 preview?: Asset[]; 16 artist: Entry<TypeArtistFields>; 17} 18 19export type TypeArtwork = Entry<TypeArtworkFields>;
This all only works if you add the contentful
package to your target project to get all relevant type definitions.
Extend the default BaseContentTypeRenderer
class, or implement the ContentTypeRenderer
interface for custom rendering.
Relevant methods to override:
Methods | Description | Override |
---|---|---|
render | Enriches a SourceFile with all relevant nodes | To control content type rendering (you should know what you're doing) |
getContext | Returns new render context object | To define custom type renderer and custom module name function |
addDefaultImports | Define set of default imports added to every file | To control default imported modules |
renderField | Returns a PropertySignatureStructure representing a field property | To control Field property rendering |
renderFieldType | Returns a string representing a field type | To control field type rendering (recommended) |
renderEntry | Returns a TypeAliasDeclarationStructure representing an entry type alias | To control entry type alias rendering |
renderEntryType | Returns a string representing an entry type | To control entry type rendering (recommended) |
Table represents order of execution
Set content type renderers:
1import { 2 CFDefinitionsBuilder, 3 DefaultContentTypeRenderer, 4 LocalizedContentTypeRenderer, 5} from 'cf-content-types-generator'; 6 7const builder = new CFDefinitionsBuilder([ 8 new DefaultContentTypeRenderer(), 9 new LocalizedContentTypeRenderer(), 10]);
A renderer to render type fields and entry definitions. For most scenarios, this renderer is sufficient.
If no custom renderers given, CFDefinitionsBuilder
creates a DefaultContentTypeRenderer
by default.
1import { CFDefinitionsBuilder, DefaultContentTypeRenderer } from 'cf-content-types-generator'; 2 3const builder = new CFDefinitionsBuilder([new DefaultContentTypeRenderer()]);
A renderer to render type fields and entry definitions compatible with contentful.js v10.
1import { CFDefinitionsBuilder, V10ContentTypeRenderer } from 'cf-content-types-generator'; 2 3const builder = new CFDefinitionsBuilder([new V10ContentTypeRenderer()]);
Add additional types for localized fields. It adds utility types to transform fields into localized fields for given locales
More details on the utility types can be found here: Issue 121
Note that these types are not needed when using V10ContentTypeRenderer
as the v10 entry type already supports localization.
1import { 2 CFDefinitionsBuilder, 3 DefaultContentTypeRenderer, 4 LocalizedContentTypeRenderer, 5} from 'cf-content-types-generator'; 6 7const builder = new CFDefinitionsBuilder([ 8 new DefaultContentTypeRenderer(), 9 new LocalizedContentTypeRenderer(), 10]);
1export interface TypeCategoryFields { 2 title: Contentful.EntryFields.Text; 3 icon?: Contentful.Asset; 4 categoryDescription?: Contentful.EntryFields.Text; 5} 6 7export type TypeCategory = Contentful.Entry<TypeCategoryFields>; 8 9export type LocalizedTypeCategoryFields<Locales extends keyof any> = LocalizedFields< 10 TypeCategoryFields, 11 Locales 12>; 13 14export type LocalizedTypeCategory<Locales extends keyof any> = LocalizedEntry< 15 TypeCategory, 16 Locales 17>;
1const localizedCategory: LocalizedTypeCategory<'DE-de' | 'En-en'> = { 2 fields: { 3 categoryDescription: { 4 'DE-de': 'german description', 5 'En-en': 'english description', 6 }, 7 }, 8};
Adds JSDoc Comments to every Entry type and Field type (created by the default renderer, or a renderer that creates the same entry and field type names). This renderer can be customized through renderer options.
JSDocContentTypeRenderer can only render comments for already rendered types. It's essential to add it after the default renderer, or any renderer that creates entry and field types based on the context moduleName resolution.
1import { CFDefinitionsBuilder, JsDocRenderer } from 'cf-content-types-generator'; 2 3const builder = new CFDefinitionsBuilder([new DefaultContentTypeRenderer(), new JsDocRenderer()]);
1import * as Contentful from 'contentful'; 2/** 3 * Fields type definition for content type 'TypeAnimal' 4 * @name TypeAnimalFields 5 * @type {TypeAnimalFields} 6 * @memberof TypeAnimal 7 */ 8export interface TypeAnimalFields { 9 /** 10 * Field type definition for field 'bread' (Bread) 11 * @name Bread 12 * @localized false 13 */ 14 bread: Contentful.EntryFields.Symbol; 15} 16 17/** 18 * Entry type definition for content type 'animal' (Animal) 19 * @name TypeAnimal 20 * @type {TypeAnimal} 21 */ 22export type TypeAnimal = Contentful.Entry<TypeAnimalFields>;
Adds type guard functions for every content type
1import { 2 CFDefinitionsBuilder, 3 DefaultContentTypeRenderer, 4 TypeGuardRenderer, 5} from 'cf-content-types-generator'; 6 7const builder = new CFDefinitionsBuilder([ 8 new DefaultContentTypeRenderer(), 9 new TypeGuardRenderer(), 10]);
1import { Entry, EntryFields } from 'contentful'; 2import type { WithContentTypeLink } from 'TypeGuardTypes'; 3 4export interface TypeAnimalFields { 5 bread: EntryFields.Symbol; 6} 7 8export type TypeAnimal = Entry<TypeAnimalFields>; 9 10export function isTypeAnimal(entry: WithContentTypeLink): entry is TypeAnimal { 11 return entry.sys.contentType.sys.id === 'animal'; 12}
Adds type guard functions for every content type which are compatible with contentful.js v10.
1import { 2 CFDefinitionsBuilder, 3 V10ContentTypeRenderer, 4 V10TypeGuardRenderer, 5} from 'cf-content-types-generator'; 6 7const builder = new CFDefinitionsBuilder([ 8 new V10ContentTypeRenderer(), 9 new V10TypeGuardRenderer(), 10]);
1import type { 2 ChainModifiers, 3 Entry, 4 EntryFieldTypes, 5 EntrySkeletonType, 6 LocaleCode, 7} from 'contentful'; 8 9export interface TypeAnimalFields { 10 bread?: EntryFieldTypes.Symbol; 11} 12 13export type TypeAnimalSkeleton = EntrySkeletonType<TypeAnimalFields, 'animal'>; 14export type TypeAnimal<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry< 15 TypeAnimalSkeleton, 16 Modifiers, 17 Locales 18>; 19 20export function isTypeAnimal<Modifiers extends ChainModifiers, Locales extends LocaleCode>( 21 entry: Entry<EntrySkeletonType, Modifiers, Locales>, 22): entry is TypeAnimal<Modifiers, Locales> { 23 return entry.sys.contentType.sys.id === 'animal'; 24}
Adds response types for every content type which are compatible with contentful.js v10.
1import { 2 CFDefinitionsBuilder, 3 V10ContentTypeRenderer, 4 ResponseTypeRenderer, 5} from 'cf-content-types-generator'; 6 7const builder = new CFDefinitionsBuilder([ 8 new V10ContentTypeRenderer(), 9 new ResponseTypeRenderer(), 10]);
1import type { 2 ChainModifiers, 3 Entry, 4 EntryFieldTypes, 5 EntrySkeletonType, 6 LocaleCode, 7} from 'contentful'; 8 9export interface TypeAnimalFields { 10 bread?: EntryFieldTypes.Symbol; 11} 12 13export type TypeAnimalSkeleton = EntrySkeletonType<TypeAnimalFields, 'animal'>; 14export type TypeAnimal<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry< 15 TypeAnimalSkeleton, 16 Modifiers, 17 Locales 18>; 19 20export type TypeAnimalWithoutLinkResolutionResponse = TypeAnimal<'WITHOUT_LINK_RESOLUTION'>; 21export type TypeAnimalWithoutUnresolvableLinksResponse = TypeAnimal<'WITHOUT_UNRESOLVABLE_LINKS'>; 22export type TypeAnimalWithAllLocalesResponse<Locales extends LocaleCode = LocaleCode> = 23 TypeAnimal<'WITH_ALL_LOCALES'>; 24export type TypeAnimalWithAllLocalesAndWithoutLinkResolutionResponse< 25 Locales extends LocaleCode = LocaleCode, 26> = TypeAnimal<'WITH_ALL_LOCALES' | 'WITHOUT_LINK_RESOLUTION', Locales>; 27export type TypeAnimalWithAllLocalesAndWithoutUnresolvableLinksResponse< 28 Locales extends LocaleCode = LocaleCode, 29> = TypeAnimal<'WITH_ALL_LOCALES' | 'WITHOUT_UNRESOLVABLE_LINKS', Locales>;
If you're not a CLI person, or you want to integrate it with your tooling workflow, you can also directly use the CFDefinitionsBuilder
from cf-definitions-builder.ts
1import CFDefinitionsBuilder from 'cf-content-types-generator'; 2 3const stringContent = new CFDefinitionsBuilder() 4 .appendType({ 5 name: 'My Entry', 6 sys: { 7 id: 'myEntry', 8 type: 'ContentType', 9 }, 10 fields: [ 11 { 12 id: 'myField', 13 name: 'My Field', 14 type: 'Symbol', 15 required: true, 16 validations: [], 17 disabled: false, 18 localized: false, 19 omitted: false, 20 }, 21 ], 22 }) 23 .toString(); 24 25console.log(stringContent); 26 27// import { Entry, EntryFields } from "contentful"; 28 29// 30// export interface TypeMyEntryFields { 31// myField: EntryFields.Symbol; 32// } 33// 34// export type TypeMyEntry = Entry<TypeMyEntryFields>;
You can use CFDefinitionsBuilder
also in a browser environment.
Example: TS Content Types Generator App
No vulnerabilities found.
No security vulnerabilities found.