Generate typescript type definitions and enumerations from business definition json records.
Generate typescript type definitions and enumerations from business definition json records.
``shell`
npm i definition
definition.config.mjs
`javascript`
/**
* @return {import('definition').ConfigOptions}
*/
export default () => {
return {
dir: "./jsons",
output: "./definitions",
};
};
See ConfigOptions Options for details.
./jsons/hello.json
`json`
[
{ "key": "hello-1", "value": "hello", "label": "cba" },
{ "key": "hello-2", "value": "hello2", "label": "cba2" }
]
Records is extendable, for example { "key": "hello-1", "value": "hello", "label": "cba", "createBy": "god" }, is okay.
value should be string or number.
key and value are required, and key can be customized via the configuration file.
`shell`
npx def generate # or npx def g
or add command to scripts with npm run
`json`
{
"scripts": {
"definition": "def generate" // or def g
}
}
``
definitions
├── hello.ts
└── utils
├── index.ts
├── kr-map.ts
├── kv-map.ts
├── types.ts
└── vr-map.ts
./definitions/hello.ts
`typescript`
import { krMap, vrMap, kvMap } from "../utils";
export const ROUTES_records_readonly = [{ "key": "hello-1", "value": "hello", "label": "cba" }, { "key": "hello-2", "value": "hello2", "label": "cba2" }] as const;
export const ROUTES_records = ROUTES_records_readonly.concat(); // mutable records
export type ROUTES_Records = typeof ROUTES_records_readonly; // record type
export type ROUTES_Values = ROUTES_Records[number]["value"]; // values's type
export const ROUTES_Keys = {
Hello1: "Hello1",
Hello2: "Hello2"
} as const;
export const ROUTES_key_list = [ROUTES_Keys.Hello1, ROUTES_Keys.Hello2] as const; // key list
export const ROUTES_kr = krMap(ROUTES_records_readonly, ROUTES_key_list); // key -> record map
export const ROUTES_vr = vrMap(ROUTES_records_readonly, ROUTES_key_list, "value"); // value -> record map
export const ROUTES_kv = kvMap(ROUTES_records_readonly, ROUTES_key_list, "value"); // key -> value map
`
Usage: cli [options] [command]
Options:
-h, --help display help for command
Commands:
generate|g [options]
help [command] display help for command
`
command generate
`
sage: cli generate|g [options]
Options:
-c, --config [path] Config file path
-h, --help display help for command
`
`typescript`
interface ConfigOptions {
dir: string; // Json records directory
output: string; // Definitions output directory
valueField?: string; // The value field key
keyField?: string; // The key field key
keyFormat?: "camelback" | "camelcase" | "underscore"; // The format of the generated key
keyGen?: (record: Record
}
Note: when keyGen is configured, keyFormat` will be ignored.
MIT