Skip to content

Commit 0505365

Browse files
committed
Stricter string to Date conversion
1 parent da88c3e commit 0505365

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

packages/las-sdk-core/src/client.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,21 @@ import type {
158158
import { toGetAgentStatisticsQueryParams, toListAgentRunsQueryParams } from './types';
159159
import { buildURL } from './utils';
160160

161-
const maybeParseDate = (val: JSONValue) => {
162-
if (typeof val === 'string') {
163-
const re = /2\d{3}-[0-1]\d-[0-3]\dT[0-2]\d:[0-6]\d:[0-6]\d\.\d+(\+0000|Z)/;
161+
const maybeParseDate = (key: string, val: JSONValue) => {
162+
const dateKeys = [
163+
'after',
164+
'before',
165+
'createdTime',
166+
'createdTimeAfter',
167+
'createdTimeBefore',
168+
'timestamp',
169+
'updatedTime',
170+
'updatedTimeAfter',
171+
'updatedTimeBefore',
172+
];
173+
174+
if (dateKeys.includes(key) && typeof val === 'string') {
175+
const re = /^2\d{3}-[0-1]\d-[0-3]\dT[0-2]\d:[0-6]\d:[0-6]\d\.\d+(\+0000|Z)$/;
164176
if (val.match(re)) {
165177
return new Date(val);
166178
}
@@ -1586,7 +1598,7 @@ export class Client {
15861598
}
15871599

15881600
const result = await axiosFn<T>(endpoint, config);
1589-
return JSON.parse(JSON.stringify(result.data), (key, val) => maybeParseDate(val));
1601+
return JSON.parse(JSON.stringify(result.data), (key, val) => maybeParseDate(key, val));
15901602
}
15911603

15921604
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
@@ -1600,7 +1612,7 @@ export class Client {
16001612
}
16011613

16021614
const result = await axiosFn<T>(endpoint, body, config);
1603-
return JSON.parse(JSON.stringify(result.data), (key, val) => maybeParseDate(val));
1615+
return JSON.parse(JSON.stringify(result.data), (key, val) => maybeParseDate(key, val));
16041616
}
16051617

16061618
private async getAuthorizationHeaders(): Promise<AuthorizationHeaders> {

0 commit comments

Comments
 (0)