@7nohe/openapi-react-query-codegen 3.0.0-beta.5 → 3.0.1
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/dist/generate.d.mts
CHANGED
package/dist/generate.mjs
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readdirSync } from "node:fs";
|
|
2
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { createClient } from "@hey-api/openapi-ts";
|
|
4
5
|
import { buildQueriesOutputPath, buildRequestsOutputPath, formatOptions, } from "./common.mjs";
|
|
5
6
|
import { createSource } from "./createSource.mjs";
|
|
6
7
|
import { formatOutput, processOutput } from "./format.mjs";
|
|
7
8
|
import { print } from "./print.mjs";
|
|
9
|
+
// openapi-ts's own tsconfig auto-detection walks up from its *install*
|
|
10
|
+
// location, which in this monorepo reaches this repo's own root tsconfig
|
|
11
|
+
// (NodeNext) instead of the caller's project config. Anchoring the search
|
|
12
|
+
// at cwd instead finds the tsconfig that actually applies to the generated
|
|
13
|
+
// output, matching the resolution the caller's own `tsc` will use.
|
|
14
|
+
export function findNearestTsConfigPath(startDir) {
|
|
15
|
+
let dir = startDir;
|
|
16
|
+
while (true) {
|
|
17
|
+
const candidates = readdirSync(dir).filter((file) => file.startsWith("tsconfig") && file.endsWith(".json"));
|
|
18
|
+
if (candidates.length > 0) {
|
|
19
|
+
candidates.sort((a) => (a === "tsconfig.json" ? -1 : 1));
|
|
20
|
+
return path.join(dir, candidates[0]);
|
|
21
|
+
}
|
|
22
|
+
const parent = path.dirname(dir);
|
|
23
|
+
if (parent === dir)
|
|
24
|
+
return undefined;
|
|
25
|
+
dir = parent;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
8
28
|
export async function generate(options, version) {
|
|
9
29
|
const openApiOutputPath = buildRequestsOutputPath(options.output);
|
|
10
30
|
const formattedOptions = formatOptions(options);
|
|
@@ -55,13 +75,20 @@ export async function generate(options, version) {
|
|
|
55
75
|
const config = {
|
|
56
76
|
dryRun: false,
|
|
57
77
|
input: formattedOptions.input,
|
|
58
|
-
output:
|
|
78
|
+
output: {
|
|
79
|
+
path: openApiOutputPath,
|
|
80
|
+
tsConfigPath: findNearestTsConfigPath(process.cwd()) ?? null,
|
|
81
|
+
},
|
|
59
82
|
plugins,
|
|
60
83
|
};
|
|
61
84
|
await createClient(config);
|
|
62
85
|
// Generate backward-compatible services.gen.ts shim
|
|
63
|
-
// client.gen.ts has the `client` instance; sdk.gen.ts has SDK functions
|
|
64
|
-
|
|
86
|
+
// client.gen.ts has the `client` instance; sdk.gen.ts has SDK functions.
|
|
87
|
+
// Mirror whatever extension convention openapi-ts used for its own
|
|
88
|
+
// cross-file imports (e.g. `.js` for NodeNext, none for bundler resolution).
|
|
89
|
+
const sdkGenContent = await readFile(path.join(openApiOutputPath, "sdk.gen.ts"), "utf-8");
|
|
90
|
+
const importExtension = sdkGenContent.match(/from ['"]\.\/client\.gen(\.\S*)?['"]/)?.[1] ?? "";
|
|
91
|
+
const shimContent = `// This file is auto-generated for backward compatibility\nexport * from './client.gen${importExtension}';\nexport * from './sdk.gen${importExtension}';\n`;
|
|
65
92
|
await writeFile(path.join(openApiOutputPath, "services.gen.ts"), shimContent);
|
|
66
93
|
const source = await createSource({
|
|
67
94
|
outputPath: openApiOutputPath,
|
|
@@ -25,6 +25,8 @@ export declare function buildQueryOptionsImport(): ImportDeclarationStructure;
|
|
|
25
25
|
export declare function buildServiceImport(ctx: GenerationContext): ImportDeclarationStructure;
|
|
26
26
|
/**
|
|
27
27
|
* Build import structure for models.
|
|
28
|
+
* Emitted as a type-only import so generated code compiles under
|
|
29
|
+
* `verbatimModuleSyntax` (enabled by default in recent Vite templates).
|
|
28
30
|
*/
|
|
29
31
|
export declare function buildModelImport(ctx: GenerationContext): ImportDeclarationStructure | null;
|
|
30
32
|
/**
|
|
@@ -43,14 +43,14 @@ export function buildQueryImport() {
|
|
|
43
43
|
{ name: "useInfiniteQuery" },
|
|
44
44
|
{ name: "useSuspenseInfiniteQuery" },
|
|
45
45
|
{ name: "useMutation" },
|
|
46
|
-
{ name: "UseQueryResult" },
|
|
47
|
-
{ name: "UseQueryOptions" },
|
|
48
|
-
{ name: "UseInfiniteQueryOptions" },
|
|
49
|
-
{ name: "UseSuspenseInfiniteQueryOptions" },
|
|
50
|
-
{ name: "UseMutationOptions" },
|
|
51
|
-
{ name: "UseMutationResult" },
|
|
52
|
-
{ name: "UseSuspenseQueryOptions" },
|
|
53
|
-
{ name: "InfiniteData" },
|
|
46
|
+
{ name: "UseQueryResult", isTypeOnly: true },
|
|
47
|
+
{ name: "UseQueryOptions", isTypeOnly: true },
|
|
48
|
+
{ name: "UseInfiniteQueryOptions", isTypeOnly: true },
|
|
49
|
+
{ name: "UseSuspenseInfiniteQueryOptions", isTypeOnly: true },
|
|
50
|
+
{ name: "UseMutationOptions", isTypeOnly: true },
|
|
51
|
+
{ name: "UseMutationResult", isTypeOnly: true },
|
|
52
|
+
{ name: "UseSuspenseQueryOptions", isTypeOnly: true },
|
|
53
|
+
{ name: "InfiniteData", isTypeOnly: true },
|
|
54
54
|
{ name: "FetchQueryOptions", isTypeOnly: true },
|
|
55
55
|
{ name: "FetchInfiniteQueryOptions", isTypeOnly: true },
|
|
56
56
|
{ name: "EnsureQueryDataOptions", isTypeOnly: true },
|
|
@@ -79,6 +79,8 @@ export function buildServiceImport(ctx) {
|
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
81
|
* Build import structure for models.
|
|
82
|
+
* Emitted as a type-only import so generated code compiles under
|
|
83
|
+
* `verbatimModuleSyntax` (enabled by default in recent Vite templates).
|
|
82
84
|
*/
|
|
83
85
|
export function buildModelImport(ctx) {
|
|
84
86
|
if (ctx.modelNames.length === 0) {
|
|
@@ -87,6 +89,7 @@ export function buildModelImport(ctx) {
|
|
|
87
89
|
return {
|
|
88
90
|
kind: StructureKind.ImportDeclaration,
|
|
89
91
|
moduleSpecifier: "../requests/types.gen",
|
|
92
|
+
isTypeOnly: true,
|
|
90
93
|
namedImports: ctx.modelNames.map((name) => ({ name })),
|
|
91
94
|
};
|
|
92
95
|
}
|