@7nohe/openapi-react-query-codegen 4.0.0-beta.2 → 4.0.0-beta.3

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/AGENTS.md CHANGED
@@ -21,7 +21,7 @@ npm install -D @7nohe/openapi-react-query-codegen
21
21
  npm install @tanstack/react-query
22
22
  ```
23
23
 
24
- `commander`, `ts-morph` and `typescript` are peer dependencies. If your package manager does not install peers automatically, add them as devDependencies. For `--client axios`, also add `axios`. Nothing else is needed at runtime; the client code is generated into `requests/`.
24
+ `commander` and `ts-morph` are peer dependencies. If your package manager does not install peers automatically, add them as devDependencies. The generated code compiles with TypeScript 5, 6 and 7; the generator itself does not need the `typescript` package. For `--client axios`, also add `axios`. Nothing else is needed at runtime; the client code is generated into `requests/`.
25
25
 
26
26
  Only the scoped `@7nohe/openapi-react-query-codegen` package belongs to this project. The unscoped `openapi-react-query-codegen` on npm is someone else's.
27
27
 
package/README.md CHANGED
@@ -61,7 +61,8 @@ The client generator is part of this package, so the generated `requests/` direc
61
61
 
62
62
  - Node.js 22.18+
63
63
  - `@tanstack/react-query` 5.x (peer dependency)
64
- - `typescript` 5.x or 6.x, `ts-morph` 28.x, `commander` 12–15 (peer dependencies)
64
+ - `ts-morph` 28.x, `commander` 12–15 (peer dependencies)
65
+ - TypeScript 5.x, 6.x or 7.x to compile the generated code. It is not a peer dependency: the generator does not use the `typescript` package.
65
66
 
66
67
  ## License
67
68
 
@@ -1,4 +1,3 @@
1
- import ts from "typescript";
2
1
  /**
3
2
  * Find the tsconfig that applies to the generated output by walking up from
4
3
  * `startDir`. Anchoring the search at the caller's directory finds the
@@ -10,4 +9,4 @@ export declare function findNearestTsConfigPath(startDir: string): string | unde
10
9
  * resolution and must not under bundler-style resolution. Read the caller's
11
10
  * tsconfig to pick the matching convention.
12
11
  */
13
- export declare function detectImportExtension(startDir: string, host?: ts.System | null): "" | ".js";
12
+ export declare function detectImportExtension(startDir: string): "" | ".js";
@@ -1,6 +1,6 @@
1
- import { readdirSync } from "node:fs";
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import { createRequire } from "node:module";
2
3
  import path from "node:path";
3
- import ts from "typescript";
4
4
  /**
5
5
  * Find the tsconfig that applies to the generated output by walking up from
6
6
  * `startDir`. Anchoring the search at the caller's directory finds the
@@ -20,37 +20,133 @@ export function findNearestTsConfigPath(startDir) {
20
20
  dir = parent;
21
21
  }
22
22
  }
23
+ /**
24
+ * Parse a tsconfig, which is JSON with comments and trailing commas. Done here
25
+ * and not with the `typescript` package, so that the generator works with
26
+ * whatever TypeScript the project has: version 7 has no API to read a config.
27
+ */
28
+ function parseJsonWithComments(text) {
29
+ let json = "";
30
+ let index = text.charCodeAt(0) === 0xfeff ? 1 : 0;
31
+ while (index < text.length) {
32
+ const char = text[index];
33
+ const next = text[index + 1];
34
+ if (char === '"') {
35
+ // A string, copied as it is so that nothing inside it is touched.
36
+ let end = index + 1;
37
+ while (end < text.length && text[end] !== '"') {
38
+ end += text[end] === "\\" ? 2 : 1;
39
+ }
40
+ json += text.slice(index, end + 1);
41
+ index = end + 1;
42
+ }
43
+ else if (char === "/" && next === "/") {
44
+ const end = text.indexOf("\n", index);
45
+ index = end === -1 ? text.length : end;
46
+ }
47
+ else if (char === "/" && next === "*") {
48
+ const end = text.indexOf("*/", index + 2);
49
+ index = end === -1 ? text.length : end + 2;
50
+ }
51
+ else {
52
+ json += char;
53
+ index += 1;
54
+ }
55
+ }
56
+ // The comments are gone, so a comma before a closing bracket is a trailing
57
+ // one. Strings are skipped as a whole by the first alternative.
58
+ return JSON.parse(json.replace(/("(?:[^"\\]|\\.)*")|,(\s*[}\]])/g, (_match, string, close) => string === undefined ? close : string));
59
+ }
60
+ /** Where an `extends` entry points to, the way TypeScript looks it up. */
61
+ function resolveExtends(target, from) {
62
+ if (target.startsWith(".") || path.isAbsolute(target)) {
63
+ const file = path.resolve(path.dirname(from), target);
64
+ return [file, `${file}.json`].find((candidate) => candidate.endsWith(".json") && existsSync(candidate));
65
+ }
66
+ // A package: its `exports`, or else the file itself or its tsconfig.json.
67
+ const require = createRequire(from);
68
+ for (const candidate of [
69
+ target,
70
+ `${target}.json`,
71
+ `${target}/tsconfig.json`,
72
+ ]) {
73
+ try {
74
+ const file = require.resolve(candidate);
75
+ if (file.endsWith(".json"))
76
+ return file;
77
+ }
78
+ catch {
79
+ // Not this spelling.
80
+ }
81
+ }
82
+ return undefined;
83
+ }
84
+ /** `module` and `moduleResolution` of a tsconfig, with what it extends. */
85
+ function readModuleOptions(configPath,
86
+ // The files on the way here, so that a circle of `extends` ends.
87
+ chain = new Set()) {
88
+ if (chain.has(configPath))
89
+ return {};
90
+ const config = parseJsonWithComments(readFileSync(configPath, "utf-8"));
91
+ if (!config || typeof config !== "object")
92
+ return {};
93
+ const options = {};
94
+ const apply = (source) => {
95
+ for (const key of ["module", "moduleResolution"]) {
96
+ if (source[key] !== undefined)
97
+ options[key] = source[key];
98
+ }
99
+ };
100
+ // Later entries win over earlier ones, and the file itself over all of them.
101
+ const parents = Array.isArray(config.extends)
102
+ ? config.extends
103
+ : [config.extends];
104
+ for (const parent of parents) {
105
+ if (typeof parent !== "string")
106
+ continue;
107
+ const file = resolveExtends(parent, configPath);
108
+ if (file === undefined)
109
+ continue;
110
+ try {
111
+ apply(readModuleOptions(file, new Set(chain).add(configPath)));
112
+ }
113
+ catch {
114
+ // A parent that cannot be read adds nothing.
115
+ }
116
+ }
117
+ const own = config.compilerOptions ?? {};
118
+ apply({
119
+ module: typeof own.module === "string" ? own.module.toLowerCase() : undefined,
120
+ moduleResolution: typeof own.moduleResolution === "string"
121
+ ? own.moduleResolution.toLowerCase()
122
+ : undefined,
123
+ });
124
+ return options;
125
+ }
23
126
  /**
24
127
  * Relative imports must spell out `.js` under Node16/NodeNext module
25
128
  * resolution and must not under bundler-style resolution. Read the caller's
26
129
  * tsconfig to pick the matching convention.
27
130
  */
28
- export function detectImportExtension(startDir, host = ts.sys) {
131
+ export function detectImportExtension(startDir) {
29
132
  const configPath = findNearestTsConfigPath(startDir);
30
133
  if (!configPath)
31
134
  return "";
135
+ let options;
32
136
  try {
33
- const read = ts.readConfigFile(configPath, host.readFile);
34
- if (read.error || !read.config)
35
- return "";
36
- const parsed = ts.parseJsonConfigFileContent(read.config, host, path.dirname(configPath));
37
- const { moduleResolution, module } = parsed.options;
38
- if (moduleResolution !== undefined) {
39
- return moduleResolution === ts.ModuleResolutionKind.Node16 ||
40
- moduleResolution === ts.ModuleResolutionKind.NodeNext
41
- ? ".js"
42
- : "";
43
- }
44
- // A range, so that `node18`, `node20` and later ones count as well.
45
- return module !== undefined &&
46
- module >= ts.ModuleKind.Node16 &&
47
- module <= ts.ModuleKind.NodeNext
48
- ? ".js"
49
- : "";
137
+ options = readModuleOptions(configPath);
50
138
  }
51
139
  catch {
52
- // An unusual TypeScript build without the compiler host API: fall back
53
- // to the bundler convention rather than failing the whole run.
140
+ // A tsconfig that cannot be read: fall back to the bundler convention
141
+ // rather than failing the whole run.
54
142
  return "";
55
143
  }
144
+ if (options.moduleResolution !== undefined) {
145
+ return options.moduleResolution === "node16" ||
146
+ options.moduleResolution === "nodenext"
147
+ ? ".js"
148
+ : "";
149
+ }
150
+ // `node16`, `node18`, `node20` and later ones resolve the Node way.
151
+ return /^node(1[6-9]|[2-9]\d|next)$/.test(options.module ?? "") ? ".js" : "";
56
152
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@7nohe/openapi-react-query-codegen",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.3",
4
4
  "description": "OpenAPI React Query Codegen",
5
5
  "bin": {
6
6
  "openapi-rq": "dist/cli.mjs"
@@ -59,8 +59,7 @@
59
59
  "peerDependencies": {
60
60
  "@tanstack/react-query": "^5.0.0",
61
61
  "commander": "12.x || 13.x || 14.x || 15.x",
62
- "ts-morph": "28.x",
63
- "typescript": "5.x || 6.x"
62
+ "ts-morph": "28.x"
64
63
  },
65
64
  "engines": {
66
65
  "node": ">=22.18.0",