Skip to content

Commit

Permalink
fix(code-gen): strip undefined query parameters in the fetch api clients
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed May 12, 2023
1 parent dc300a5 commit d3b51ec
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/code-gen/src/api-client/js-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fileContextSetIndent,
} from "../file/context.js";
import { fileWrite } from "../file/write.js";
import { referenceUtilsGetProperty } from "../processors/reference-utils.js";
import { structureResolveReference } from "../processors/structure.js";
import { JavascriptImportCollector } from "../target/javascript.js";
import { upperCaseFirst } from "../utils.js";
Expand Down Expand Up @@ -271,6 +272,33 @@ export function jsFetchGenerateFunction(
fileBlockEnd(file);
}

if (route.query) {
/** @type {import("../generated/common/types.d.ts").ExperimentalObjectDefinition} */
// @ts-expect-error
const type = structureResolveReference(
generateContext.structure,
route.query,
);

if (type.type === "object") {
for (const key of Object.keys(type.keys)) {
const isOptional = referenceUtilsGetProperty(
generateContext,
type.keys[key],
["isOptional"],
false,
);

if (isOptional) {
fileWrite(
file,
`if (query["${key}"] === null || query["${key}"] === undefined) { delete query["${key}"]; }`,
);
}
}
}
}

fileWrite(file, `const response = await fetchFn(`);
fileContextSetIndent(file, 1);

Expand Down
28 changes: 28 additions & 0 deletions packages/code-gen/src/api-client/ts-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fileContextSetIndent,
} from "../file/context.js";
import { fileWrite } from "../file/write.js";
import { referenceUtilsGetProperty } from "../processors/reference-utils.js";
import { structureResolveReference } from "../processors/structure.js";
import { JavascriptImportCollector } from "../target/javascript.js";
import { upperCaseFirst } from "../utils.js";
Expand Down Expand Up @@ -268,6 +269,33 @@ export function tsFetchGenerateFunction(
fileBlockEnd(file);
}

if (route.query) {
/** @type {import("../generated/common/types.d.ts").ExperimentalObjectDefinition} */
// @ts-expect-error
const type = structureResolveReference(
generateContext.structure,
route.query,
);

if (type.type === "object") {
for (const key of Object.keys(type.keys)) {
const isOptional = referenceUtilsGetProperty(
generateContext,
type.keys[key],
["isOptional"],
false,
);

if (isOptional) {
fileWrite(
file,
`if (query["${key}"] === null || query["${key}"] === undefined) { delete query["${key}"]; }`,
);
}
}
}
}

fileWrite(file, `const response = await fetchFn(`);
fileContextSetIndent(file, 1);

Expand Down

0 comments on commit d3b51ec

Please sign in to comment.