Skip to content

Commit

Permalink
Patch TypeScript types (#17)
Browse files Browse the repository at this point in the history
* Fixes bug in TypeScript gen
  • Loading branch information
camargo authored May 16, 2023
1 parent 7ed88cc commit 16a743b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
54 changes: 30 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nasa-jpl/seq-json-schema",
"version": "1.0.18",
"version": "1.0.19",
"license": "MIT",
"type": "module",
"repository": {
Expand All @@ -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"
}
}
27 changes: 27 additions & 0 deletions scripts/generate-types.js
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 1 addition & 8 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type Request = {
steps: [Step, ...Step[]];
time?: Time;
type: 'request';
} & Request1;
};
/**
* Description. Can be attached to any sequence step.
*/
Expand All @@ -71,13 +71,6 @@ export type Args = (
| HexArgument
| RepeatArgument
)[];
export type Request1 =
| {
[k: string]: unknown;
}
| {
[k: string]: unknown;
};

export interface SeqJson {
/**
Expand Down

0 comments on commit 16a743b

Please sign in to comment.