mancku / strapi-plugin-schemas-to-ts

Strapi Plugin Schemas to TS is a plugin for Strapi v4 that automatically converts your Strapi schemas into Typescript interfaces.
MIT License
62 stars 16 forks source link
plugin strapi typescript

Strapi Plugin Schemas to TS

NPM Version NPM Downloads

Strapi-Plugin-Schemas-to-TS is a plugin for Strapi v4 that automatically converts your Strapi schemas into Typescript interfaces.

Features

How it works

In every execution of the Strapi server, it reads all files containing schema definitions, both content types and components. Then generates Typescript interfaces based on those definitions.

The interfaces will only be generated if they don't exist or if there have been changes. Otherwise they will be skipped, preventing the Strapi server to restart (in development) when modifying files.

At the end of the process will delete all interfaces generated in previous executions that are no longer valid, due for instace to a class or component being removed or renamed.

How to set it up

Here are the instructions to install and configure the package:

Installation

To install the plugin execute either one of the following commands:

# Using Yarn
yarn add strapi-plugin-schemas-to-ts

# Using NPM
npm install strapi-plugin-schemas-to-ts

Configuration

The plugin needs to be configured in the ./config/plugins.ts file of Strapi. The file might need to be created if it does not exists. In that file, the plugin must be enabled in order for it to work:

export default {
  // ...
  'schemas-to-ts': {
    enabled: true,
  },
  // ...
}

While the previous example is enough to get it working, there are other properties that can be configured. Their default values are the ones in this example:

export default {
  // ...
  'schemas-to-ts': {
    enabled: true,
    config: {
      acceptedNodeEnvs: ["development"],
      commonInterfacesFolderName: 'schemas-to-ts',
      alwaysAddEnumSuffix: false,
      alwaysAddComponentSuffix: false,
      usePrettierIfAvailable: true,
      logLevel: 2,
      destinationFolder: undefined,
    }
  },
  // ...
}

Interfaces sources

There are 3 different interface sources: API, Component & Common.

Interfaces types

For every schema, different types of interfaces will be generated. That is because Strapi v4 does not always represent the data using the same structure.

Enums

Strapi enumeration attributes will be generated as typescript enums. However there are some considerations regarding enum names:

Here's an example of the last two points:

export enum Year {
  Starting2012 = 'Starting-2012',
  _2013 = '2013',
  Ending2014 = 'Ending-2014'
}

Interfaces paths

CLI

The Cli allows to execute some functions without the need to run Strapi. As the Cli has been added to the scripts and the bin sections of the package.json, it can be executed with schemas-to-ts.

As it provides help, this command will print it out:

schemas-to-ts --help

All command parameters are case sensitive and can be written both in camel case and in kebab case. You can see more about command parameters using the help:

 schemas-to-ts {CommandName} --help

Delete All Generated Files. Command name: deleteAllGeneratedFiles

This command deletes all files that have a first line with the text '// Interface automatically generated by schemas-to-ts'. It allows this parameters:

Examples:

schemas-to-ts deleteAllGeneratedFiles --strapi-root-path /path/to/strapi
schemas-to-ts deleteAllGeneratedFiles --strapi-root-path /path/to/strapi --logLevel Information

Generate Interfaces. Command name: generateInterfaces

This command generates TypeScript interfaces for your Strapi project. It allows this parameters:

Examples:

schemas-to-ts generateInterfaces  --strapi-root-path /path/to/strapi
schemas-to-ts generateInterfaces  --strapi-root-path /path/to/strapi --acceptedNodeEnvs staging development,test --commonInterfacesFolderName interfaces --alwaysAddEnumSuffix true --alwaysAddComponentSuffix false --usePrettierIfAvailable false --logLevel Information
schemas-to-ts generateInterfaces  --strapi-root-path /path/to/strapi --acceptedNodeEnvs staging development,test --destinationFolder src/schemas-to-ts --alwaysAddEnumSuffix true --alwaysAddComponentSuffix false --usePrettierIfAvailable false --logLevel Information

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

Please, review the changelog to know about the differences between published versions of this project.

Acknowledgements

This project began as a fork of the Types-4-Strapi created by Francesco Lorenzetti, but at the end it was so different on it's purpose (being a plugin Vs being executed on demand) and there was so much new code that I turned it into a new whole project. However the algorithm to convert the schema into an interface is heavily inspired in Francesco's work.