Convert the Strapi models to TypeScript interfaces by processing each of the ./api/**/models/*.settings.json
recursively.
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
sts input -g inputGroup -o output ...
-
input
Strapi folder(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/
-
-g inputGroup
Strapi folder(s) with groups models
- -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
.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/'
],
inputGroup: './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}`,
}
module.exports = config;
package.json
{
"//" : "...",
"scripts": {
"sts": "sts -c .stsconfig"
},
"///" : "..."
}
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.
npm install && npm run build
# output files generated in dist folder