From f46e36ac9bc1c5acd00ddf4ba593b1f32eb290c0 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Thu, 6 Feb 2025 22:47:14 -0600 Subject: [PATCH 1/2] add endsWith expiration to date conditions --- oxide-api/src/util.ts | 11 ++++++++++- .../src/client/static/http-client.test.ts | 4 ++-- .../src/client/static/util.test.ts | 19 ++++++++----------- .../src/client/static/util.ts | 11 ++++++++++- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/oxide-api/src/util.ts b/oxide-api/src/util.ts index a4438bd..477d3c0 100644 --- a/oxide-api/src/util.ts +++ b/oxide-api/src/util.ts @@ -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; diff --git a/oxide-openapi-gen-ts/src/client/static/http-client.test.ts b/oxide-openapi-gen-ts/src/client/static/http-client.test.ts index ae75d50..ac69052 100644 --- a/oxide-openapi-gen-ts/src/client/static/http-client.test.ts +++ b/oxide-openapi-gen-ts/src/client/static/http-client.test.ts @@ -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"); diff --git a/oxide-openapi-gen-ts/src/client/static/util.test.ts b/oxide-openapi-gen-ts/src/client/static/util.test.ts index 9a1f1ff..dc9e044 100644 --- a/oxide-openapi-gen-ts/src/client/static/util.test.ts +++ b/oxide-openapi-gen-ts/src/client/static/util.test.ts @@ -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"); diff --git a/oxide-openapi-gen-ts/src/client/static/util.ts b/oxide-openapi-gen-ts/src/client/static/util.ts index 21cf097..9fe7ce0 100644 --- a/oxide-openapi-gen-ts/src/client/static/util.ts +++ b/oxide-openapi-gen-ts/src/client/static/util.ts @@ -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; From 8e401f57fe6854fa39b08f115847d793e7018910 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Thu, 6 Feb 2025 22:47:14 -0600 Subject: [PATCH 2/2] npm version minor --- oxide-openapi-gen-ts/package-lock.json | 4 ++-- oxide-openapi-gen-ts/package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/oxide-openapi-gen-ts/package-lock.json b/oxide-openapi-gen-ts/package-lock.json index 5d07766..5cc98d5 100644 --- a/oxide-openapi-gen-ts/package-lock.json +++ b/oxide-openapi-gen-ts/package-lock.json @@ -1,12 +1,12 @@ { "name": "@oxide/openapi-gen-ts", - "version": "0.5.0", + "version": "0.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@oxide/openapi-gen-ts", - "version": "0.5.0", + "version": "0.6.0", "license": "MPL-2.0", "dependencies": { "minimist": "^1.2.8", diff --git a/oxide-openapi-gen-ts/package.json b/oxide-openapi-gen-ts/package.json index 6304939..37838e9 100644 --- a/oxide-openapi-gen-ts/package.json +++ b/oxide-openapi-gen-ts/package.json @@ -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",