diff --git a/.npmignore b/.npmignore index 58fc57d..0561220 100644 --- a/.npmignore +++ b/.npmignore @@ -2,9 +2,8 @@ src/**/*.* dist/**/*.test.js dist/**/*.test.d.ts schemas/**/*.* -json2esm.js -json2types.js -json2types-blocks.js tsconfig.json node_modules -BUILD.md \ No newline at end of file +BUILD.md +dist/specgen/**/*.* +.github \ No newline at end of file diff --git a/package.json b/package.json index 0cad2ea..de8c26b 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,24 @@ { "name": "typed-ocpp", - "version": "0.3.2", + "version": "0.3.3", "description": "A library for type-aware parsing, serialization and validation of OCPP messages", "main": "dist/index.js", "type": "module", "devDependencies": { - "@types/node": "^20.8.2", - "ajv": "^8.12.0", - "ajv-formats": "^2.1.1", - "json-schema-to-typescript": "^14.0.4", - "typescript": "^5.2.2" + "@types/node": "^20.14.2", + "ajv": "^8.16.0", + "ajv-formats": "^3.0.1", + "json-schema-to-typescript": "^14.0.5", + "typescript": "^5.4.5" }, "scripts": { "test": "node --test --test-reporter=spec ./dist", - "specgen:ocpp16:types": "node json2types schemas/ocpp16 src/ocpp16/types.ts OCPP16", - "specgen:ocpp16:schemas": "node json2esm schemas/ocpp16 src/ocpp16/schemas.ts", + "specgen:ocpp16:types": "node specgen/json2types.js schemas/ocpp16 src/ocpp16/types.ts OCPP16", + "specgen:ocpp16:schemas": "node specgen/json2esm.js schemas/ocpp16 src/ocpp16/schemas.ts", "specgen:ocpp16:clean": "rm -f ./src/ocpp16/types.ts && rm -rf ./src/ocpp16/schemas.ts", "specgen:ocpp16": "npm run specgen:ocpp16:clean && npm run specgen:ocpp16:types && npm run specgen:ocpp16:schemas", - "specgen:ocpp20:types": "node json2types schemas/ocpp20 src/ocpp20/types.ts OCPP20", - "specgen:ocpp20:schemas": "node json2esm schemas/ocpp20 src/ocpp20/schemas.ts", + "specgen:ocpp20:types": "node specgen/json2types.js schemas/ocpp20 src/ocpp20/types.ts OCPP20", + "specgen:ocpp20:schemas": "node specgen/json2esm.js schemas/ocpp20 src/ocpp20/schemas.ts", "specgen:ocpp20:clean": "rm -f ./src/ocpp20/types.ts && rm -f ./src/ocpp20/schemas.ts", "specgen:ocpp20": "npm run specgen:ocpp20:clean && npm run specgen:ocpp20:types && npm run specgen:ocpp20:schemas", "specgen": "npm run specgen:ocpp16 && npm run specgen:ocpp20", diff --git a/json2esm.js b/specgen/json2esm.js similarity index 100% rename from json2esm.js rename to specgen/json2esm.js diff --git a/json2types-blocks.js b/specgen/json2types-blocks.js similarity index 98% rename from json2types-blocks.js rename to specgen/json2types-blocks.js index 308661a..fb63f82 100644 --- a/json2types-blocks.js +++ b/specgen/json2types-blocks.js @@ -1,7 +1,10 @@ /** * Code blocks to be deduplicated by ./json2types.js . */ -export const deduplicate_blocks = { OCPP16: [], OCPP20: [] }; +export const deduplicate_blocks = { + OCPP16: [], + OCPP20: [], + }; deduplicate_blocks.OCPP16.push(`export interface CustomDataType { vendorId: string; diff --git a/json2types.js b/specgen/json2types.js similarity index 97% rename from json2types.js rename to specgen/json2types.js index 2cd9ec9..b3d1ead 100644 --- a/json2types.js +++ b/specgen/json2types.js @@ -54,7 +54,8 @@ const output_file_header = `/* const input_file_data = await readFile(input_file_abspath, 'utf8'); const input_schema = JSON.parse(input_file_data); - if (input_schema.$id?.indexOf(':') > -1) { + + if (mode === 'OCPP20' && input_schema.$id.indexOf(':') > -1) { // The schema files within the OCPP 2.0.1 spec have namespaced $id // attributes such as `urn:OCPP:Cp:2:2020:3:CancelReservationRequest`. // Here we get rid of the "urn" part of the $id so that json2ts will diff --git a/src/common/ajv.ts b/src/common/ajv.ts index 6099dc0..7f5f59c 100644 --- a/src/common/ajv.ts +++ b/src/common/ajv.ts @@ -1,16 +1,29 @@ let __ajv: any = null; +const isAjv = (ajv: any): boolean => { + return !!ajv + && typeof ajv === 'object' + && typeof ajv.validate === 'function' + ; +}; + export const setAjv = (ajv: any) => { + if (!isAjv(ajv)) { + throw new Error(` + The argument provided to \`setAjv()\` does not look like an instance of + Ajv. See https://npm.im/typed-ocpp. + `); + } __ajv = ajv; }; export const getAjv = (): any => { - if (!__ajv || typeof __ajv !== 'object' || typeof __ajv.validate !== 'function') { + if (!isAjv(__ajv)) { throw new Error(` The typed-ocpp library requires an instance of Ajv (https://npm.im/ajv) extended with the ajv-formats plugin (https://npm.im/ajv-formats) to be - provided using \`OCPP.setAjv()\`. See https://npm.im/typed-ocpp. + provided using \`setAjv()\`. See https://npm.im/typed-ocpp. `); } return __ajv; diff --git a/src/index.test.js b/src/index.test.ts similarity index 93% rename from src/index.test.js rename to src/index.test.ts index a448643..4ea4dcc 100644 --- a/src/index.test.js +++ b/src/index.test.ts @@ -5,7 +5,7 @@ import { OCPP20 } from './index.js'; import assert from 'node:assert'; import { describe, it } from 'node:test'; -import './ajv.test.js'; +import './common/ajv.test.js'; describe('schemas', () => { diff --git a/src/ocpp16/schemas.ts b/src/ocpp16/schemas.ts index ab9e726..2c84268 100644 --- a/src/ocpp16/schemas.ts +++ b/src/ocpp16/schemas.ts @@ -3,7 +3,7 @@ * THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NEVER BE EDITED DIRECTLY. * SEE ../../BUILD.md * - * GENERATED ON: 2024-05-27T08:46:12.016Z + * GENERATED ON: 2024-06-10T08:20:22.280Z * */ diff --git a/src/ocpp16/types.ts b/src/ocpp16/types.ts index d081500..f77935a 100644 --- a/src/ocpp16/types.ts +++ b/src/ocpp16/types.ts @@ -3,7 +3,7 @@ * THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NEVER BE EDITED DIRECTLY. * SEE ../../BUILD.md * - * GENERATED ON: 2024-05-27T08:46:11.601Z + * GENERATED ON: 2024-06-10T08:20:21.870Z * */ diff --git a/src/ocpp20/schemas.ts b/src/ocpp20/schemas.ts index 4595369..a50469b 100644 --- a/src/ocpp20/schemas.ts +++ b/src/ocpp20/schemas.ts @@ -3,7 +3,7 @@ * THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NEVER BE EDITED DIRECTLY. * SEE ../../BUILD.md * - * GENERATED ON: 2024-05-27T08:46:13.263Z + * GENERATED ON: 2024-06-10T08:20:23.517Z * */ diff --git a/src/ocpp20/types.ts b/src/ocpp20/types.ts index 587ce49..0267974 100644 --- a/src/ocpp20/types.ts +++ b/src/ocpp20/types.ts @@ -3,7 +3,7 @@ * THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NEVER BE EDITED DIRECTLY. * SEE ../../BUILD.md * - * GENERATED ON: 2024-05-27T08:46:12.595Z + * GENERATED ON: 2024-06-10T08:20:22.859Z * */