• Stars
    star
    153
  • Rank 243,368 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert the strapi models to typescript.

Strapi-to-TypeScript

NPM version NPM download contributors

THIS PROJECT IS NOT MAINTAINED ANYMORE. Don't hesitate to fork it.

THIS PROJECT DOESN'T SUPPORT VERSION 4 OR LATER OF STRAPI. (see PR#49)

Convert the Strapi models to TypeScript interfaces by processing each of the ./api/**/models/*.settings.json recursively.

Install and Run

npm install -g strapi-to-typescript

sts path/to/strapi/api/ -o path/to/your/types/dir/

# see all doc
sts -h

# external conf. see: strapi-to-typescript/index.d.ts for format
sts -c .stsconfig.js

Command line option

sts input -g components -o output ...

required

  • input
    Strapi folder(s)/file(s) with models *.settings.json
    You may define multiple inputs. In case your API models have relations to other plugins like 'users-permissions'. sts path/to/strapi/api/ path/to/strapi/plugins/users-permissions/models -o path/to/your/types/dir/

    • Order matters, if you have two models with the same name, the last one is used.
    • Add '!' to exclude folder or subfolder, ex: !path/to/strapi/plugins_excluded.
  • -g components
    Strapi folder(s) with components models

optional

  • -o output
    Output folder
  • -n nested
    Put all interfaces in a nested tree instead of directly under the output folder
  • -u collectionCanBeUndefined
    By default, all collection can not be undefined. You can turn this off, so only unrequired collections may be undefined.
  • -e Enumeration
    You may generate enumeration or string literal Example:
// enumeration (with -e option) 
export interface IOrder {
    payment: IOrderPayment;
}
export enum IOrderPayment {
    card = "card",
    check = "check",
}
// OR string literal types (by default)
export interface IOrder {
    payment: "card" | "check";
}
  • -c Advanced configuration
    path to configuration file

Advanced configuration

.stsconfig

/**
 * @type {import('strapi-to-typescript')}
 */
const config = {

    //required 
    input: [
      'api',
      './node_modules/strapi-plugin-users-permissions/models/',
      './node_modules/strapi-plugin-upload/models/',
      './extensions/users-permissions/models/'
    ],
    components: './components/',
    output: './sts/',

    // optional
    enum: true,
    nested: false,
    excludeField: (interfaceName, fieldName) => fieldName === 'hide_field',
    addField: (interfaceName) => [{ name: "created_by", type: "string" }],

    // optional, builtin function used if undefined return
    fieldType: (fieldType, fieldName, interfaceName) => { if(fieldType == 'datetime') return 'string' },
    fieldName: (fieldName) => fieldName.replace('_', ''),
    interfaceName: (name) => `X${name}`,
    enumName: (name, interfaceName) => `Enum${interfaceName}${name}`,
    importAsType: (interfaceName) => interfaceName === 'MyInterfaceThatWantsToImportAsTypes' /* or just true */,
    outputFileName: (interfaceName, filename) => interfaceName;
}
module.exports = config;

package.json

{
  "//" : "...",

  "scripts": {
    "sts": "sts -c .stsconfig"
  },

  "///" : "..."
}

Issue

If you want to create an issue. First of all, be nice. Take the time to explain and format your post.

The better solution to explain your issue (and for me, to fix it) is to create a pull request with your data:

  1. fork this repo with the button "fork" on github website. wait a minute.
  2. git clone your master branch from the newly created repository.
  3. yarn install or npm install
  4. add your api in src/test/api src/test/components (if necessary)
  5. add your test:
  6. src/test/test<issue id>.config.js copy an other test and modify output conf
  7. src/test/test<issue id>.assert.ts copy another assert and modify the import accordingly to your conf output
  8. run your test with ./node_modules/.bin/ts-node src/test.ts test<issue id> or run all test yarn test
  9. create pull request on this repo

Explanation

The input folder is recursively processed and each model file is read. When done, the expected output folder is generated, and finally, the Strapi models are converted to TypeScript.

Build

npm install && npm run build
# output files generated in dist folder

More Repositories

1

mithril-materialized

A Materialize-css library for the Mithril framework.
TypeScript
25
star
2

mithril-tree-component

A tree component for mithril.
TypeScript
15
star
3

slimdown-js

A very basic regex-based Markdown parser.
TypeScript
15
star
4

deep-copy-ts

A deep-copy utility function for TypeScript
TypeScript
8
star
5

mithril-ui-form

Convert a (JSON) object to a dynamic (materialize-css) form.
TypeScript
6
star
6

mithril-markdown

A markdown editor component for the Mithril framework
TypeScript
5
star
7

kvk-query

CLI to query the Netherlands Chamber of Commerce (KvK - Kamer van Koophandel).
TypeScript
5
star
8

mithril-sparkline

Mithril component to create a sparkline.
TypeScript
4
star
9

mithril-leaflet

A map component, based on leaflet, for the Mithril framework.
TypeScript
4
star
10

mithril-contenteditable

Mithril component for a div with editable contents.
TypeScript
4
star
11

d3clock

An analogue clock with multiple faces, based on d3
TypeScript
3
star
12

rest-easy-loki

A simple REST interface for the in-memory database, lokijs
TypeScript
3
star
13

mithril-examples

Useful examples and flems from the Mithril's gitter.
2
star
14

mithril-scenario-timeline

A scenario timeline component for the Mithril framework.
TypeScript
2
star
15

docx-parser

Rust
2
star
16

meeting-o-meter

To measure the costs of a meeting (e.g. a general assembly)
C#
2
star
17

osrm-rest-client

REST client for OSRM, the Open Street Map routing service. Supports async, promises or regular callbacks.
TypeScript
2
star
18

memory-trainer

Train your memory, useful for learning words, chemistry definitions, etc.
TypeScript
2
star
19

angular-switch

A switch directive for Angular.js, based on an input checkbox and the css style generated by https://proto.io/freebies/onoff/
CSS
2
star
20

thw-pairs

TMT infrastructure example
Shell
1
star
21

xref2csv

Convert XREF bibliography file (in XML) to semicolon delimited CSV for ASreview.
TypeScript
1
star
22

agent-smith

An agent-based crowd simulator in 2D
TypeScript
1
star
23

taalgenie

Een Nederlandstalige website voor het leren van Nederlands
TypeScript
1
star
24

weaviate-class-updater

Update a database class in Weaviate
TypeScript
1
star
25

mithril-table

An editable table component for Mithril
TypeScript
1
star
26

ziekenhuizen

Welke ziekenhuizen dienen open te blijven voor goede geboortezorg
TypeScript
1
star
27

capability-assessment-tool

Select, prioritise and develop your organisation's capabilities.
TypeScript
1
star