@7nohe/openapi-react-query-codegen 3.0.0-beta.3 → 3.0.0-beta.4
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/cli.mjs +1 -0
- package/dist/common.mjs +6 -0
- package/dist/createSource.mjs +2 -2
- package/dist/generate.mjs +1 -0
- package/dist/parseOperations.mjs +2 -1
- package/dist/tsmorph/buildQueryHooks.mjs +12 -3
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -32,6 +32,7 @@ async function setupProgram() {
|
|
|
32
32
|
.option("--pageParam <value>", "Name of the query parameter used for pagination", "page")
|
|
33
33
|
.option("--nextPageParam <value>", "Name of the response parameter used for next page", "nextPage")
|
|
34
34
|
.option("--initialPageParam <value>", "Initial page value to query", "1")
|
|
35
|
+
.option("--omitInitialPageParam", "Send no initial page parameter at all (overrides --initialPageParam)")
|
|
35
36
|
.parse();
|
|
36
37
|
const options = program.opts();
|
|
37
38
|
await generate(options, version);
|
package/dist/common.mjs
CHANGED
|
@@ -52,6 +52,12 @@ export function BuildCommonTypeName(name) {
|
|
|
52
52
|
* @returns The parsed number or NaN if the value is not a valid number.
|
|
53
53
|
*/
|
|
54
54
|
export function safeParseNumber(value) {
|
|
55
|
+
// `Number("")` is 0, which would silently turn a blank option such as
|
|
56
|
+
// `--initialPageParam ""` into a numeric 0. Treat blank strings as NaN so
|
|
57
|
+
// callers keep the original value.
|
|
58
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
59
|
+
return Number.NaN;
|
|
60
|
+
}
|
|
55
61
|
const parsed = Number(value);
|
|
56
62
|
if (!Number.isNaN(parsed) && Number.isFinite(parsed)) {
|
|
57
63
|
return parsed;
|
package/dist/createSource.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { generateAllFiles } from "./tsmorph/index.mjs";
|
|
|
5
5
|
/**
|
|
6
6
|
* Create source files using ts-morph based generation.
|
|
7
7
|
*/
|
|
8
|
-
export const createSource = async ({ outputPath, client, version, pageParam, nextPageParam, initialPageParam, }) => {
|
|
8
|
+
export const createSource = async ({ outputPath, client, version, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, }) => {
|
|
9
9
|
// Initialize ts-morph project to read the generated OpenAPI client
|
|
10
10
|
const project = new Project({
|
|
11
11
|
skipAddingFilesFromTsConfig: true,
|
|
@@ -15,7 +15,7 @@ export const createSource = async ({ outputPath, client, version, pageParam, nex
|
|
|
15
15
|
// Parse operations from the service file
|
|
16
16
|
const operations = await parseOperations(project, pageParam);
|
|
17
17
|
// Build generation context
|
|
18
|
-
const ctx = buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, version);
|
|
18
|
+
const ctx = buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, version);
|
|
19
19
|
// Generate all files using ts-morph
|
|
20
20
|
return generateAllFiles(operations, ctx);
|
|
21
21
|
};
|
package/dist/generate.mjs
CHANGED
|
@@ -59,6 +59,7 @@ export async function generate(options, version) {
|
|
|
59
59
|
pageParam: formattedOptions.pageParam,
|
|
60
60
|
nextPageParam: formattedOptions.nextPageParam,
|
|
61
61
|
initialPageParam: formattedOptions.initialPageParam.toString(),
|
|
62
|
+
omitInitialPageParam: formattedOptions.omitInitialPageParam ?? false,
|
|
62
63
|
});
|
|
63
64
|
await print(source, formattedOptions);
|
|
64
65
|
const queriesOutputPath = buildQueriesOutputPath(options.output);
|
package/dist/parseOperations.mjs
CHANGED
|
@@ -96,7 +96,7 @@ export async function parseOperations(project, pageParam) {
|
|
|
96
96
|
/**
|
|
97
97
|
* Build generation context from project configuration.
|
|
98
98
|
*/
|
|
99
|
-
export function buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, version) {
|
|
99
|
+
export function buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, version) {
|
|
100
100
|
const modelsFile = project
|
|
101
101
|
.getSourceFiles()
|
|
102
102
|
.find((sf) => sf.getFilePath().includes(modelsFileName));
|
|
@@ -117,6 +117,7 @@ export function buildGenerationContext(project, client, pageParam, nextPageParam
|
|
|
117
117
|
pageParam,
|
|
118
118
|
nextPageParam,
|
|
119
119
|
initialPageParam,
|
|
120
|
+
omitInitialPageParam,
|
|
120
121
|
version,
|
|
121
122
|
};
|
|
122
123
|
}
|
|
@@ -58,13 +58,22 @@ export function buildPagedQueryFn(op, ctx, castTData) {
|
|
|
58
58
|
const thenClause = castTData
|
|
59
59
|
? ".then(response => response.data as TData) as TData"
|
|
60
60
|
: ".then(response => response.data)";
|
|
61
|
-
|
|
61
|
+
// When the initial page param is omitted, the first request must send no
|
|
62
|
+
// page param at all, so spread it in only once TanStack Query provides one.
|
|
63
|
+
const pageQuery = ctx.omitInitialPageParam
|
|
64
|
+
? `...(pageParam === undefined ? {} : { ${ctx.pageParam}: pageParam as number })`
|
|
65
|
+
: `${ctx.pageParam}: pageParam as number`;
|
|
66
|
+
return `({ pageParam }) => ${op.methodName}({ ...clientOptions, query: { ...clientOptions.query, ${pageQuery} }, throwOnError: true } as Options<${dataTypeName}, true>)${thenClause}`;
|
|
62
67
|
}
|
|
63
68
|
/**
|
|
64
|
-
* Format the initialPageParam literal. Emits
|
|
65
|
-
*
|
|
69
|
+
* Format the initialPageParam literal. Emits `undefined` when the caller opted
|
|
70
|
+
* to omit it (#177); otherwise a numeric literal when possible so the inferred
|
|
71
|
+
* pageParam type matches what getNextPageParam returns.
|
|
66
72
|
*/
|
|
67
73
|
export function formatInitialPageParam(ctx) {
|
|
74
|
+
if (ctx.omitInitialPageParam) {
|
|
75
|
+
return "undefined";
|
|
76
|
+
}
|
|
68
77
|
return /^-?\d+$/.test(ctx.initialPageParam)
|
|
69
78
|
? ctx.initialPageParam
|
|
70
79
|
: JSON.stringify(ctx.initialPageParam);
|