Skip to content

Commit

Permalink
v0.3.3, moves specgen scripts to dedicated folder, improves error mes…
Browse files Browse the repository at this point in the history
…sages when Ajv is missing
  • Loading branch information
jacoscaz committed Jun 10, 2024
1 parent 6b58a2e commit 5a909b0
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 23 deletions.
7 changes: 3 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
BUILD.md
dist/specgen/**/*.*
.github
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion json2types-blocks.js → specgen/json2types-blocks.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion json2types.js → specgen/json2types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/common/ajv.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.js → src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {

Expand Down
2 changes: 1 addition & 1 deletion src/ocpp16/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/

Expand Down
2 changes: 1 addition & 1 deletion src/ocpp16/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/

Expand Down
2 changes: 1 addition & 1 deletion src/ocpp20/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/

Expand Down
2 changes: 1 addition & 1 deletion src/ocpp20/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/

Expand Down

0 comments on commit 5a909b0

Please sign in to comment.