Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion oxide-api/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ export const mapObj =
return newObj;
};

const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,6})?Z$/;

export const parseIfDate = (k: string | undefined, v: unknown) => {
if (typeof v === "string" && (k?.startsWith("time_") || k === "timestamp")) {
if (
typeof v === "string" &&
isoDateRegex.test(v) &&
(k?.startsWith("time_") ||
k?.endsWith("_time") ||
k?.endsWith("_expiration") ||
k === "timestamp")
) {
const d = new Date(v);
if (isNaN(d.getTime())) return v;
return d;
Expand Down
4 changes: 2 additions & 2 deletions oxide-openapi-gen-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion oxide-openapi-gen-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oxide/openapi-gen-ts",
"version": "0.5.0",
"version": "0.6.0",
"description": "OpenAPI client generator used to generate Oxide TypeScript SDK",
"keywords": [
"oxide",
Expand Down
4 changes: 2 additions & 2 deletions oxide-openapi-gen-ts/src/client/static/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ describe("handleResponse", () => {
});

it("parses dates and converts to camel case", async () => {
const resp = json({ time_created: "2022-05-01" });
const resp = json({ time_created: "2022-05-01T02:03:04Z" });
const { response, ...rest } = await handleResponse(resp);
expect(rest).toMatchObject({
type: "success",
data: {
timeCreated: new Date(Date.UTC(2022, 4, 1)),
timeCreated: new Date(Date.UTC(2022, 4, 1, 2, 3, 4)),
},
});
expect(response.headers.get("Content-Type")).toBe("application/json");
Expand Down
19 changes: 8 additions & 11 deletions oxide-openapi-gen-ts/src/client/static/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,14 @@ describe("parseIfDate", () => {
expect(parseIfDate("abc", dateStr)).toEqual(dateStr);
});

it("parses dates if key starts with time_", () => {
const value = parseIfDate("time_whatever", dateStr);
expect(value).toBeInstanceOf(Date);
expect((value as Date).getTime()).toEqual(timestamp);
});

it("parses dates if key = 'timestamp'", () => {
const value = parseIfDate("timestamp", dateStr);
expect(value).toBeInstanceOf(Date);
expect((value as Date).getTime()).toEqual(timestamp);
});
it.each(["time_whatever", "auto_thing_expiration", "timestamp"])(
"parses dates if key is '%s'",
(key) => {
const value = parseIfDate(key, dateStr);
expect(value).toBeInstanceOf(Date);
expect((value as Date).getTime()).toEqual(timestamp);
}
);

it("passes through values that fail to parse as dates", () => {
const value = parseIfDate("time_whatever", "blah");
Expand Down
11 changes: 10 additions & 1 deletion oxide-openapi-gen-ts/src/client/static/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ export const mapObj =
return newObj;
};

const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,6})?Z$/;

export const parseIfDate = (k: string | undefined, v: unknown) => {
if (typeof v === "string" && (k?.startsWith("time_") || k === "timestamp")) {
if (
typeof v === "string" &&
isoDateRegex.test(v) &&
(k?.startsWith("time_") ||
k?.endsWith("_time") ||
k?.endsWith("_expiration") ||
k === "timestamp")
) {
const d = new Date(v);
if (isNaN(d.getTime())) return v;
return d;
Expand Down
Loading