Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sbezludny/content-type-to-typescript
Converts Contentful Models to Typescript Definitions
https://github.com/sbezludny/content-type-to-typescript
cli contentful typescript
Last synced: 14 days ago
JSON representation
Converts Contentful Models to Typescript Definitions
- Host: GitHub
- URL: https://github.com/sbezludny/content-type-to-typescript
- Owner: sbezludny
- License: mit
- Archived: true
- Created: 2017-12-20T08:40:01.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-31T11:12:45.000Z (about 7 years ago)
- Last Synced: 2025-01-10T03:23:47.350Z (about 1 month ago)
- Topics: cli, contentful, typescript
- Language: TypeScript
- Homepage:
- Size: 87.9 KB
- Stars: 7
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Content-type-to-typescript
This is a tool to convert Contentful Models (Content Types) to the TS Definitions.
Provides a way to automate TS Definitions generation. Could be used as a library or cli tool.[Try-it-out](https://content-type-to-typescript.netlify.com/)
## Installation
```
$ npm install content-type-to-typescript --save
```## Usage
1. As CLI
```
$ ./node_modules/.bin/content-type-to-typescript --access-token --space --output
```This command will generate TS Definition file. Could also be used as a npm script.
package.json:
```json
"scripts": {
"sync-contentful-types": "content-type-to-typescript --access-token --space --output "
}
```
Usage:```
npm run sync-contentful-types
```2. As a library using JSON preview from Web App
Copy JSON Preview from [Contentful Web App](https://app.contentful.com/)
```js
import { compileFromContentTypes } from 'content-type-to-typescript';const category = {
name: 'Category',
description: null,
fields: [
{
id: 'title',
name: 'Title',
type: 'Text',
required: true,
omitted: false,
},
],
}const typings = await compileFromContentTypes([category]);
console.log(typings);
``````ts
compileFromContentTypes(
contentTypes: Array>,
options?: Partial
): Promise
```### ContentType
The structure of the Content Type is described here [Contentful data model](https://www.contentful.com/developers/docs/concepts/data-model/).
### Options
| Property | Type | Required? | Description |
| :------------ | :----- | :-------: | :----------------------------------- |
| bannerComment | String | | A comment at the top of the response |## Example
Input:
```json
{
"name": "Category",
"fields": [
{
"id": "title",
"name": "Title",
"type": "Text",
"required": true,
"omitted": false
},
{
"id": "icon",
"name": "Icon",
"type": "Link",
"required": false,
"omitted": false,
"linkType": "Asset"
},
{
"id": "categoryDescription",
"name": "Description",
"type": "Text",
"required": false,
"omitted": false
},
{
"id": "simpleTextField",
"name": "Simple text field",
"type": "Symbol",
"required": false,
"validations": [],
"disabled": false,
"omitted": false
}
]
}
```Output:
```ts
export interface Category {
title: string;
icon?: AssetLink;
categoryDescription?: string;
simpleTextField?: string;
}export interface AssetLink {
type: string;
linkType: string;
id: string;
}
```
## Built with* [json-schema-to-typescript](https://github.com/bcherny/json-schema-to-typescript/)
* [typescript-library-starter](https://github.com/alexjoverm/typescript-library-starter)## License
MIT License - fork, modify and use however you want.