definition
v2.0.11
Published
Generate typescript type definitions and enumerations from business definition json records.
Downloads
253
Maintainers
Readme
Definition 
Generate typescript type definitions and enumerations from business definition json records.
Install
npm i definitionUsage
Create config file.
definition.config.mjs
/**
* @return {import('definition').ConfigOptions}
*/
export default () => {
return {
dir: "./jsons",
output: "./definitions",
};
};See ConfigOptions Options for details.
Create json records in target directory.
./jsons/hello.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.
Run command
npx def generate # or npx def gor add command to scripts with npm run
{
"scripts": {
"definition": "def generate" // or def g
}
}Result
definitions
├── hello.ts
└── utils
├── index.ts
├── kr-map.ts
├── kv-map.ts
├── types.ts
└── vr-map.ts./definitions/hello.ts
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 mapCLI
Usage: cli [options] [command]
Options:
-h, --help display help for command
Commands:
generate|g [options]
help [command] display help for commandcommand generate
sage: cli generate|g [options]
Options:
-c, --config [path] Config file path
-h, --help display help for commandConfigOptions
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<string, any>) => string; // Custom key generate function
}Note: when keyGen is configured, keyFormat will be ignored.
License
MIT
