Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/aceandtate/contentful-ts-type-generator
- Owner: aceandtate
- License: mit
- Fork: true (arimkevi/contentful-ts-type-generator)
- Created: 2021-01-04T12:34:59.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-09-27T14:52:04.000Z (about 3 years ago)
- Last Synced: 2024-09-25T01:28:33.013Z (3 months ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)
```