Skip to content

Commit

Permalink
fix: strip out nulls in url encoded curl (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Nov 6, 2024
1 parent b2adc6e commit 00a82a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 16 additions & 1 deletion packages/fdr-sdk/src/api-definition/snippets/curl.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertToCurl } from "./curl";
import { convertToCurl, getUrlQueriesGetString } from "./curl";

describe("curl", () => {
it("generates basic GET request", () => {
Expand Down Expand Up @@ -264,4 +264,19 @@ describe("curl", () => {
`,
);
});

it("does not include nulls in urlencoded parameters", () => {
expect(getUrlQueriesGetString({ a: null, b: "b" })).toMatchInlineSnapshot(`
[
"-d b=b",
]
`);

expect(getUrlQueriesGetString({ a: ["b1", null, "b2"] })).toMatchInlineSnapshot(`
[
"-d "a[]"=b1",
"-d "a[]"=b2",
]
`);
});
});
11 changes: 8 additions & 3 deletions packages/fdr-sdk/src/api-definition/snippets/curl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlainObject, unknownToString } from "@fern-api/ui-core-utils";
import { isNonNullish, isPlainObject, unknownToString } from "@fern-api/ui-core-utils";
import { compact } from "es-toolkit/array";
import { UnreachableCaseError } from "ts-essentials";
import {
Expand Down Expand Up @@ -46,7 +46,7 @@ function getBasicAuthString(basicAuth: { username: string; password: string }):
return [`-u "${basicAuth.username}:${basicAuth.password}"`];
}

function getUrlQueriesGetString(searchParams: Record<string, unknown>): string[] {
export function getUrlQueriesGetString(searchParams: Record<string, unknown>): string[] {
return toUrlEncoded(searchParams).map(
([key, value]) =>
`${requiresUrlEncode(value) ? "--data-urlencode" : "-d"} ${key.includes("[") ? `"${key}"` : key}=${value.includes(" ") ? `"${value}"` : value}`,
Expand Down Expand Up @@ -196,8 +196,13 @@ function unsafeStringifyHttpRequestExampleToCurl(
function toUrlEncoded(urlQueries: Record<string, unknown>): Array<[string, string]> {
return Object.entries(urlQueries).flatMap(([key, value]): [string, string][] => {
if (Array.isArray(value)) {
return value.map((v) => [`${key}[]`, unknownToString(v)]);
return value.filter(isNonNullish).map((v) => [`${key}[]`, unknownToString(v)]);
}

if (value == null) {
return [];
}

return [[key, unknownToString(value)]];
});
}

0 comments on commit 00a82a0

Please sign in to comment.