From 16a743b036e6d81ea037c3cbb6c63c0c003dfdfb Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 16 May 2023 13:05:33 -0700 Subject: [PATCH] Patch TypeScript types (#17) * Fixes bug in TypeScript gen --- package-lock.json | 54 ++++++++++++++++++++++----------------- package.json | 8 +++--- scripts/generate-types.js | 27 ++++++++++++++++++++ types.ts | 9 +------ 4 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 scripts/generate-types.js diff --git a/package-lock.json b/package-lock.json index f2dcfdd..ba2fab5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,29 +1,35 @@ { "name": "@nasa-jpl/seq-json-schema", - "version": "1.0.18", + "version": "1.0.19", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@nasa-jpl/seq-json-schema", - "version": "1.0.18", + "version": "1.0.19", "license": "MIT", "devDependencies": { "ajv": "^8.12.0", - "json-schema-to-typescript": "^11.0.3", - "prettier": "^2.8.3" + "json-schema-to-typescript": "^13.0.1", + "prettier": "^2.8.8" } }, "node_modules/@bcherny/json-schema-ref-parser": { - "version": "9.0.9", - "resolved": "https://registry.npmjs.org/@bcherny/json-schema-ref-parser/-/json-schema-ref-parser-9.0.9.tgz", - "integrity": "sha512-vmEmnJCfpkLdas++9OYg6riIezTYqTHpqUTODJzHLzs5UnXujbOJW9VwcVCnyo1mVRt32FRr23iXBx/sX8YbeQ==", + "version": "10.0.5-fork", + "resolved": "https://registry.npmjs.org/@bcherny/json-schema-ref-parser/-/json-schema-ref-parser-10.0.5-fork.tgz", + "integrity": "sha512-E/jKbPoca1tfUPj3iSbitDZTGnq6FUFjkH6L8U2oDwSuwK1WhnnVtCG7oFOTg/DDnyoXbQYUiUiGOibHqaGVnw==", "dev": true, "dependencies": { "@jsdevtools/ono": "^7.1.3", "@types/json-schema": "^7.0.6", "call-me-maybe": "^1.0.1", "js-yaml": "^4.1.0" + }, + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://github.com/sponsors/philsturgeon" } }, "node_modules/@jsdevtools/ono": { @@ -346,12 +352,12 @@ } }, "node_modules/json-schema-to-typescript": { - "version": "11.0.3", - "resolved": "https://registry.npmjs.org/json-schema-to-typescript/-/json-schema-to-typescript-11.0.3.tgz", - "integrity": "sha512-EaEE9Y4VZ8b9jW5zce5a9L3+p4C9AqgIRHbNVDJahfMnoKzcd4sDb98BLxLdQhJEuRAXyKLg4H66NKm80W8ilg==", + "version": "13.0.1", + "resolved": "https://registry.npmjs.org/json-schema-to-typescript/-/json-schema-to-typescript-13.0.1.tgz", + "integrity": "sha512-VU+Spn84eLJyt8R0Bmg2soTwHQFgvrGrU7V8mXbhqWFdEYCFekSYk4JcpCzx0i/WzjEVJKAU7r0y0PVYT14E6Q==", "dev": true, "dependencies": { - "@bcherny/json-schema-ref-parser": "9.0.9", + "@bcherny/json-schema-ref-parser": "10.0.5-fork", "@types/json-schema": "^7.0.11", "@types/lodash": "^4.14.182", "@types/prettier": "^2.6.1", @@ -488,9 +494,9 @@ } }, "node_modules/prettier": { - "version": "2.8.3", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.3.tgz", - "integrity": "sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==", + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", "dev": true, "bin": { "prettier": "bin-prettier.js" @@ -575,9 +581,9 @@ }, "dependencies": { "@bcherny/json-schema-ref-parser": { - "version": "9.0.9", - "resolved": "https://registry.npmjs.org/@bcherny/json-schema-ref-parser/-/json-schema-ref-parser-9.0.9.tgz", - "integrity": "sha512-vmEmnJCfpkLdas++9OYg6riIezTYqTHpqUTODJzHLzs5UnXujbOJW9VwcVCnyo1mVRt32FRr23iXBx/sX8YbeQ==", + "version": "10.0.5-fork", + "resolved": "https://registry.npmjs.org/@bcherny/json-schema-ref-parser/-/json-schema-ref-parser-10.0.5-fork.tgz", + "integrity": "sha512-E/jKbPoca1tfUPj3iSbitDZTGnq6FUFjkH6L8U2oDwSuwK1WhnnVtCG7oFOTg/DDnyoXbQYUiUiGOibHqaGVnw==", "dev": true, "requires": { "@jsdevtools/ono": "^7.1.3", @@ -866,12 +872,12 @@ } }, "json-schema-to-typescript": { - "version": "11.0.3", - "resolved": "https://registry.npmjs.org/json-schema-to-typescript/-/json-schema-to-typescript-11.0.3.tgz", - "integrity": "sha512-EaEE9Y4VZ8b9jW5zce5a9L3+p4C9AqgIRHbNVDJahfMnoKzcd4sDb98BLxLdQhJEuRAXyKLg4H66NKm80W8ilg==", + "version": "13.0.1", + "resolved": "https://registry.npmjs.org/json-schema-to-typescript/-/json-schema-to-typescript-13.0.1.tgz", + "integrity": "sha512-VU+Spn84eLJyt8R0Bmg2soTwHQFgvrGrU7V8mXbhqWFdEYCFekSYk4JcpCzx0i/WzjEVJKAU7r0y0PVYT14E6Q==", "dev": true, "requires": { - "@bcherny/json-schema-ref-parser": "9.0.9", + "@bcherny/json-schema-ref-parser": "10.0.5-fork", "@types/json-schema": "^7.0.11", "@types/lodash": "^4.14.182", "@types/prettier": "^2.6.1", @@ -984,9 +990,9 @@ "dev": true }, "prettier": { - "version": "2.8.3", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.3.tgz", - "integrity": "sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==", + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", "dev": true }, "punycode": { diff --git a/package.json b/package.json index fa3eff3..f2e1119 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nasa-jpl/seq-json-schema", - "version": "1.0.18", + "version": "1.0.19", "license": "MIT", "type": "module", "repository": { @@ -15,11 +15,11 @@ "format": "prettier --write .", "prepublishOnly": "npm run types", "test": "node --no-warnings test/test.js", - "types": "json2ts --input schema.json --output types.ts && npm run format" + "types": "node scripts/generate-types.js && npm run format" }, "devDependencies": { "ajv": "^8.12.0", - "json-schema-to-typescript": "^11.0.3", - "prettier": "^2.8.3" + "json-schema-to-typescript": "^13.0.1", + "prettier": "^2.8.8" } } diff --git a/scripts/generate-types.js b/scripts/generate-types.js new file mode 100644 index 0000000..2c845d1 --- /dev/null +++ b/scripts/generate-types.js @@ -0,0 +1,27 @@ +import { compileFromFile } from 'json-schema-to-typescript'; +import { writeFileSync } from 'fs'; + +/** + * Patches the types generated from 'json-schema-to-typescript' since the library has bugs. + * @param {string} types + * @return {string} + */ +function patchTypes(types) { + // Remove the 'Request1' object since the library does not generate the + // correct type for the request 'oneOf' in the schema. + // See: https://github.com/bcherny/json-schema-to-typescript/issues/381 + types = types.replace(/ \& Request1/, ''); + types = types.replace( + `export type Request1 =\n | {\n [k: string]: unknown;\n }\n | {\n [k: string]: unknown;\n };\n`, + '', + ); + return types; +} + +async function main() { + let types = await compileFromFile('schema.json'); + types = patchTypes(types); + writeFileSync('types.ts', types); +} + +main(); diff --git a/types.ts b/types.ts index 5ca6cd5..d2d8447 100644 --- a/types.ts +++ b/types.ts @@ -54,7 +54,7 @@ export type Request = { steps: [Step, ...Step[]]; time?: Time; type: 'request'; -} & Request1; +}; /** * Description. Can be attached to any sequence step. */ @@ -71,13 +71,6 @@ export type Args = ( | HexArgument | RepeatArgument )[]; -export type Request1 = - | { - [k: string]: unknown; - } - | { - [k: string]: unknown; - }; export interface SeqJson { /**