@ndla/types-taxonomy 1.0.36 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -4,7 +4,8 @@
4
4
  "license": "GPL-3.0",
5
5
  "scripts": {
6
6
  "build": "tsc",
7
- "do-publish": "tsc"
7
+ "do-publish": "tsc",
8
+ "generate-typescript": "tsx scripts/generate-openapi.ts"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
@@ -15,7 +16,11 @@
15
16
  "types": "build/taxonomy-api.d.ts",
16
17
  "main": "taxonomy-api.js",
17
18
  "devDependencies": {
18
- "typescript": "^5.0.2"
19
+ "@types/node": "^22.13.16",
20
+ "openapi-typescript": "^7.6.1",
21
+ "tsx": "^4.19.3",
22
+ "typescript": "^5.3.3"
19
23
  },
20
- "version": "1.0.36"
24
+ "version": "1.0.37",
25
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
21
26
  }
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Part of NDLA taxonomy-api
3
+ * Copyright (C) 2025 NDLA
4
+ *
5
+ * See LICENSE
6
+ *
7
+ */
8
+
9
+ import fs from "node:fs";
10
+ import openapiTS, { astToString, TransformObject } from "openapi-typescript";
11
+ import ts, { TypeNode } from "typescript";
12
+
13
+ if (process.argv.length !== 2) {
14
+ throw new Error("Invalid use");
15
+ }
16
+
17
+ const BLOB = ts.factory.createTypeReferenceNode(
18
+ ts.factory.createIdentifier("Blob"),
19
+ );
20
+ const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull()); // `null`
21
+
22
+ async function generate_types(appName: string) {
23
+ const jsonFile = `./openapi.json`;
24
+ console.log(`Parsing ${jsonFile} to generate typescript files...`);
25
+ const schema = await fs.promises.readFile(jsonFile, "utf8");
26
+
27
+ const ast = await openapiTS(JSON.parse(schema), {
28
+ exportType: true,
29
+ // https://openapi-ts.dev/migration-guide#defaultnonnullable-true-by-default
30
+ defaultNonNullable: false,
31
+ transform(schemaObject, _options): TypeNode | undefined {
32
+ if (schemaObject.format === "binary") {
33
+ if (schemaObject.nullable) {
34
+ return ts.factory.createUnionTypeNode([BLOB, NULL]);
35
+ } else {
36
+ return BLOB;
37
+ }
38
+ }
39
+ },
40
+ });
41
+
42
+ const outputPath = `./taxonomy-api.ts`;
43
+ const output = astToString(ast);
44
+
45
+ console.log(`Outputting to ${outputPath}`);
46
+ fs.writeFileSync(outputPath, output);
47
+ }
48
+
49
+ generate_types(process.argv[2]);