@7nohe/openapi-react-query-codegen
Advanced tools
+1
-0
@@ -35,2 +35,3 @@ #!/usr/bin/env node | ||
| .option("--initialPageParam <value>", "Initial page value to query", "1") | ||
| .option("--omitInitialPageParam", "Send no initial page parameter at all (overrides --initialPageParam)") | ||
| .parse(); | ||
@@ -37,0 +38,0 @@ const options = program.opts(); |
+6
-0
@@ -55,2 +55,8 @@ import { stat } from "node:fs/promises"; | ||
| export function safeParseNumber(value) { | ||
| // `Number("")` is 0, which would silently turn a blank option such as | ||
| // `--initialPageParam ""` into a numeric 0. Treat blank strings as NaN so | ||
| // callers keep the original value. | ||
| if (typeof value === "string" && value.trim() === "") { | ||
| return Number.NaN; | ||
| } | ||
| const parsed = Number(value); | ||
@@ -57,0 +63,0 @@ if (!Number.isNaN(parsed) && Number.isFinite(parsed)) { |
@@ -8,3 +8,3 @@ import { join } from "node:path"; | ||
| */ | ||
| export const createSource = async ({ outputPath, client, version, pageParam, nextPageParam, initialPageParam, }) => { | ||
| export const createSource = async ({ outputPath, client, version, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, }) => { | ||
| // Initialize ts-morph project to read the generated OpenAPI client | ||
@@ -19,5 +19,5 @@ const project = new Project({ | ||
| // Build generation context | ||
| const ctx = buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, version); | ||
| const ctx = buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, version); | ||
| // Generate all files using ts-morph | ||
| return generateAllFiles(operations, ctx); | ||
| }; |
@@ -62,2 +62,3 @@ import { writeFile } from "node:fs/promises"; | ||
| initialPageParam: formattedOptions.initialPageParam.toString(), | ||
| omitInitialPageParam: formattedOptions.omitInitialPageParam ?? false, | ||
| }); | ||
@@ -64,0 +65,0 @@ await print(source, formattedOptions); |
@@ -99,3 +99,3 @@ import ts from "typescript"; | ||
| */ | ||
| export function buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, version) { | ||
| export function buildGenerationContext(project, client, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, version) { | ||
| const modelsFile = project | ||
@@ -121,4 +121,5 @@ .getSourceFiles() | ||
| initialPageParam, | ||
| omitInitialPageParam, | ||
| version, | ||
| }; | ||
| } |
@@ -61,9 +61,18 @@ import { StructureKind, VariableDeclarationKind, } from "ts-morph"; | ||
| : ".then(response => response.data)"; | ||
| return `({ pageParam }) => ${op.methodName}({ ...clientOptions, query: { ...clientOptions.query, ${ctx.pageParam}: pageParam as number }, throwOnError: true } as Options<${dataTypeName}, true>)${thenClause}`; | ||
| // When the initial page param is omitted, the first request must send no | ||
| // page param at all, so spread it in only once TanStack Query provides one. | ||
| const pageQuery = ctx.omitInitialPageParam | ||
| ? `...(pageParam === undefined ? {} : { ${ctx.pageParam}: pageParam as number })` | ||
| : `${ctx.pageParam}: pageParam as number`; | ||
| return `({ pageParam }) => ${op.methodName}({ ...clientOptions, query: { ...clientOptions.query, ${pageQuery} }, throwOnError: true } as Options<${dataTypeName}, true>)${thenClause}`; | ||
| } | ||
| /** | ||
| * Format the initialPageParam literal. Emits a numeric literal when possible | ||
| * so the inferred pageParam type matches what getNextPageParam returns. | ||
| * Format the initialPageParam literal. Emits `undefined` when the caller opted | ||
| * to omit it (#177); otherwise a numeric literal when possible so the inferred | ||
| * pageParam type matches what getNextPageParam returns. | ||
| */ | ||
| export function formatInitialPageParam(ctx) { | ||
| if (ctx.omitInitialPageParam) { | ||
| return "undefined"; | ||
| } | ||
| return /^-?\d+$/.test(ctx.initialPageParam) | ||
@@ -70,0 +79,0 @@ ? ctx.initialPageParam |
+1
-1
| { | ||
| "name": "@7nohe/openapi-react-query-codegen", | ||
| "version": "3.0.0-beta.3", | ||
| "version": "3.0.0-beta.4", | ||
| "description": "OpenAPI React Query Codegen", | ||
@@ -5,0 +5,0 @@ "bin": { |
78223
1.34%1741
1.04%