Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aceandtate/contentful-ts-type-generator

Typescript types generator for contentful.js
https://github.com/aceandtate/contentful-ts-type-generator

Last synced: 3 months ago
JSON representation

Typescript types generator for contentful.js

Awesome Lists containing this project

README

        

## contentful-ts-type-generator

Note: This is a fork of github.com/arimkevi/contentful-ts-type-generator

## Usage

1. Get preview api token and spaceId from Contentful.

2. Install this repository into your node project

```
npm install @aceandtate/contentful-ts-type-generator
```

3. Run the script to get help options
```
npx generateContentfulTypes
```

4. Base usage

```
npx generateContentfulTypes
```

This will generate contentfulTypes.d.ts file that will contain all of the space model as interfaces and inheritance. Export contains also model sys.id.

If you use the `generateContentfulTypes` command in your package.json scripts, you can leave out the `npx` in front of it.

5. Options

```
-o, --output , Output file path. Default: './contentfulTypes.d.ts'
-e, --environment [value], Contentful environment id to use. Default: 'master'
-p, --prefix , Name prefix for generated interfaces. Default: ''
-h, --host [value], Default: 'api.contentful.com'
-i, --ignore [value], Ignored field(s): a single field id or comma separated list of field ids. Default: ''
```

6. Once the types are generated you can use contentful.js calling the following function:

```ts

const client = contentful.createClient({
host: 'contentfulHost',
accessToken: 'accessToken',
space: 'spaceId',
resolveLinks: true,
})

export function getContent(
contentfulLocale: string, contentType: string
): Promise> {
return client
.getEntries({ content_type: contentType, locale: contentfulLocale })
.then((response: contentful.EntryCollection) => response.items[0])
}

getContent(locale, YourContentfulType)

```