npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-v4-to-typescript

v1.0.2

Published

Convert the strapi schemas to typescript interfaces.

Downloads

80

Readme

Strapi-v4-to-typescript

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

Install and Run

npm install -g strapi-v4-to-typescript

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

# see all doc
s4ts -h

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

Command line option

s4ts input -g components -o output ...

required

  • input
    Strapi folder(s)/file(s) with models */schema.json
    You may define multiple inputs. In case your API models have relations to other plugins like 'users-permissions'. s4ts 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 - deprecated in Strapi v4 - broke file structure 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-v4-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": "s4ts -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

Strapi v4 EDIT


Modified this repo to make interfaces from strapi v4 file structure, this breaks original functionality so please use this wisely and on Strapi v4 only.

WARNING

option -n was broken - thus removed

Usage

rm ./.s4ts -rf && s4ts ./src/api ./src/extensions -g ./src/components -o ./.s4ts 

KUDOS !!

"Big thanks to all authors and contributors on strapi-to-typescript, without which this fork could not exist!"