Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions genTypeFromJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { compile } = require('json-schema-to-typescript');
const fs = require('fs');
const ocpp1_6_json = require('./lib/schemas/ocpp1_6.json');
const ocpp2_0_1json = require('./lib/schemas/ocpp2_0_1.json');
const jsonArray = [
{
version: 'v16',
json: ocpp1_6_json
},
{
version: 'v201',
json: ocpp2_0_1json
}
];
/**
*
* @param {string} id $if of schema
* @returns {string} The name used for namespace and filename
*/
const compileSchemaId = (id) => {
const [method, type] = id.substring(4).split('.');
switch (type) {
case 'req':
return method + 'Request';
case 'conf':
return method + 'Response';
default:
throw Error('Invalid Schema $id:' + id)
}
}
(async function loop() {
for (const data of jsonArray) {
console.log(data.version);
for (const schema of data.json) {
const name = compileSchemaId(schema.$id);
const ts = await compile(schema, name, {
// The array type will be too large if the maxItem property is too large.
// Ref: https://github.com/bcherny/json-schema-to-typescript/issues/372
ignoreMinAndMaxItems: true
})
await fs.writeFileSync(`./lib/types/${data.version}/${name}.d.ts`, ts);
console.log(name);
}
}
})()
Loading