Skip to content

Commit f896d99

Browse files
committed
cleanup
1 parent e92c8cc commit f896d99

5 files changed

Lines changed: 26 additions & 14 deletions

File tree

oxide-openapi-gen-ts/src/__snapshots__/http-client.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ function encodeQueryParam(key: string, value: unknown) {
6666
)}`;
6767
}
6868

69-
export const handleResponseWithMapper =
70-
(valueMapper: ValueMapper) =>
71-
async <Data>(response: Response): Promise<ApiResult<Data>> => {
69+
export const handleResponseWithMapper = (valueMapper: ValueMapper) => {
70+
const processResponseBody = mapObj(snakeToCamel, valueMapper);
71+
return async <Data>(response: Response): Promise<ApiResult<Data>> => {
7272
const respText = await response.text();
7373

7474
// catch JSON parse or processing errors
@@ -77,9 +77,7 @@ export const handleResponseWithMapper =
7777
// don't bother trying to parse empty responses like 204s
7878
// TODO: is empty object what we want here?
7979
respJson =
80-
respText.length > 0
81-
? mapObj(snakeToCamel, valueMapper)(JSON.parse(respText))
82-
: {};
80+
respText.length > 0 ? processResponseBody(JSON.parse(respText)) : {};
8381
} catch (e) {
8482
return {
8583
type: "client_error",
@@ -104,6 +102,7 @@ export const handleResponseWithMapper =
104102
data: respJson as Data,
105103
};
106104
};
105+
};
107106

108107
// has to be any. the particular query params types don't like unknown
109108
// eslint-disable-next-line @typescript-eslint/no-explicit-any

oxide-openapi-gen-ts/src/__snapshots__/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export const makeParseIfDate =
6464
(dateProps: Set<string>, dateArrayProps: Set<string>): ValueMapper =>
6565
(k, v) => {
6666
if (k === undefined) return v;
67+
if (Array.isArray(v)) return dateArrayProps.has(k) ? v.map(parseDate) : v;
6768
if (dateProps.has(k)) return parseDate(v);
68-
if (dateArrayProps.has(k) && Array.isArray(v)) return v.map(parseDate);
6969
return v;
7070
};
7171

oxide-openapi-gen-ts/src/client/static/http-client.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ function encodeQueryParam(key: string, value: unknown) {
6666
)}`;
6767
}
6868

69-
export const handleResponseWithMapper =
70-
(valueMapper: ValueMapper) =>
71-
async <Data>(response: Response): Promise<ApiResult<Data>> => {
69+
export const handleResponseWithMapper = (valueMapper: ValueMapper) => {
70+
const processResponseBody = mapObj(snakeToCamel, valueMapper);
71+
return async <Data>(response: Response): Promise<ApiResult<Data>> => {
7272
const respText = await response.text();
7373

7474
// catch JSON parse or processing errors
@@ -77,9 +77,7 @@ export const handleResponseWithMapper =
7777
// don't bother trying to parse empty responses like 204s
7878
// TODO: is empty object what we want here?
7979
respJson =
80-
respText.length > 0
81-
? mapObj(snakeToCamel, valueMapper)(JSON.parse(respText))
82-
: {};
80+
respText.length > 0 ? processResponseBody(JSON.parse(respText)) : {};
8381
} catch (e) {
8482
return {
8583
type: "client_error",
@@ -104,6 +102,7 @@ export const handleResponseWithMapper =
104102
data: respJson as Data,
105103
};
106104
};
105+
};
107106

108107
// has to be any. the particular query params types don't like unknown
109108
// eslint-disable-next-line @typescript-eslint/no-explicit-any

oxide-openapi-gen-ts/src/client/static/util.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ describe("makeParseIfDate", () => {
131131
expect((value[1] as Date).getTime()).toEqual(timestamp);
132132
});
133133

134+
it("handles a name present in both sets for both strings and arrays of strings", () => {
135+
const parse = makeParseIfDate(new Set(["at"]), new Set(["at"]));
136+
137+
const single = parse("at", dateStr);
138+
expect(single).toBeInstanceOf(Date);
139+
expect((single as Date).getTime()).toEqual(timestamp);
140+
141+
const array = parse("at", [dateStr, dateStr]) as unknown[];
142+
expect(array.map((d) => (d as Date).getTime())).toEqual([
143+
timestamp,
144+
timestamp,
145+
]);
146+
});
147+
134148
it.each([
135149
"2023-01-01T12:00:00Z",
136150
"2023-01-01T12:00:00.1Z",

oxide-openapi-gen-ts/src/client/static/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export const makeParseIfDate =
6464
(dateProps: Set<string>, dateArrayProps: Set<string>): ValueMapper =>
6565
(k, v) => {
6666
if (k === undefined) return v;
67+
if (Array.isArray(v)) return dateArrayProps.has(k) ? v.map(parseDate) : v;
6768
if (dateProps.has(k)) return parseDate(v);
68-
if (dateArrayProps.has(k) && Array.isArray(v)) return v.map(parseDate);
6969
return v;
7070
};
7171

0 commit comments

Comments
 (0)