Skip to content

Commit daa9780

Browse files
ffMathygismya
andauthored
fix: support setting decodeDatesAsIso globally (#159)
* Add session decodeDatesAsIso tests --------- Co-authored-by: Lars Johansson <gismya@gmail.com> Co-authored-by: Lars Johansson <lars.johansson@ftrack.com>
1 parent 0f58b6c commit daa9780

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

source/session.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class Session {
6161
schemas?: Schema[];
6262
serverInformation?: QueryServerInformationResponse;
6363
serverVersion?: string;
64+
private decodeDatesAsIso: boolean;
6465
private schemasPromise?: Promise<Schema[]>;
6566
private serverInformationPromise?: Promise<ServerInformation>;
6667
private serverInformationValues?: string[];
@@ -80,7 +81,7 @@ export class Session {
8081
* @param {string} [options.apiEndpoint=/api] - API endpoint.
8182
* @param {object} [options.headers] - Additional headers to send with the request
8283
* @param {object} [options.strictApi] - Turn on strict API mode
83-
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
84+
* @param {object} [options.decodeDatesAsIso] - Decode dates as ISO strings instead of moment objects
8485
*
8586
* @constructs Session
8687
*/
@@ -96,6 +97,7 @@ export class Session {
9697
apiEndpoint = "/api",
9798
additionalHeaders = {},
9899
strictApi = false,
100+
decodeDatesAsIso = false,
99101
}: SessionOptions = {},
100102
) {
101103
if (!serverUrl || !apiUser || !apiKey) {
@@ -190,6 +192,8 @@ export class Session {
190192
{ action: "query_schemas" },
191193
];
192194

195+
this.decodeDatesAsIso = decodeDatesAsIso;
196+
193197
/**
194198
* true if session is initialized
195199
* @memberof Session
@@ -578,14 +582,13 @@ export class Session {
578582
pushToken,
579583
signal,
580584
additionalHeaders = {},
581-
decodeDatesAsIso = false,
585+
decodeDatesAsIso = this.decodeDatesAsIso,
582586
}: CallOptions = {},
583587
): Promise<IsTuple<T> extends true ? T : T[]> {
584588
if (this.initializing) {
585589
await this.initializing;
586590
}
587591
const url = `${this.serverUrl}${this.apiEndpoint}`;
588-
589592
try {
590593
// Delay call until session is initialized if initialization is in
591594
// progress.

source/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface SessionOptions {
1212
apiEndpoint?: string;
1313
additionalHeaders?: Data;
1414
strictApi?: boolean;
15+
decodeDatesAsIso?: boolean;
1516
}
1617

1718
export interface CreateComponentOptions {

test/session.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ beforeAll(() => {
3939
credentials.apiKey,
4040
{
4141
autoConnectEventHub: false,
42+
decodeDatesAsIso: false,
4243
},
4344
);
4445
});
@@ -134,6 +135,43 @@ describe("Session", () => {
134135
);
135136
expect(result.data[0].created_at).toEqual("2022-10-10T10:12:09.000Z");
136137
});
138+
it("Should allow querying with datetimes decoded as ISO objects, when set on session initialization", async () => {
139+
const decodeDatesAsIsoSession = new Session(
140+
credentials.serverUrl,
141+
credentials.apiUser,
142+
credentials.apiKey,
143+
{
144+
decodeDatesAsIso: true,
145+
},
146+
);
147+
const result = await decodeDatesAsIsoSession.query(
148+
"select name, created_at from Task limit 1",
149+
);
150+
expect(result.data[0].created_at).toEqual("2022-10-10T10:12:09.000Z");
151+
});
152+
it("Should allow overriding session decodeDatesAsIso when querying", async () => {
153+
const decodeDatesAsIsoSession = new Session(
154+
credentials.serverUrl,
155+
credentials.apiUser,
156+
credentials.apiKey,
157+
{
158+
decodeDatesAsIso: true,
159+
},
160+
);
161+
const result = await decodeDatesAsIsoSession.query(
162+
"select name, created_at from Task limit 1",
163+
{ decodeDatesAsIso: false },
164+
);
165+
expect(result.data[0].created_at).to.be.instanceOf(moment);
166+
expect(result.data[0].created_at.toISOString()).toEqual(
167+
"2022-10-10T10:12:09.000Z",
168+
);
169+
const result2 = await session.query(
170+
"select name, created_at from Task limit 1",
171+
{ decodeDatesAsIso: true },
172+
);
173+
expect(result2.data[0].created_at).toEqual("2022-10-10T10:12:09.000Z");
174+
});
137175

138176
it("Should allow querying with datetimes decoded as ISO objects with timezone support disabled", async () => {
139177
server.use(
@@ -590,7 +628,6 @@ describe("Encoding entities", () => {
590628
12321,
591629
]);
592630
});
593-
594631
it("Should support encoding moment dates to local timezone if timezone support is disabled", () => {
595632
const now = moment();
596633
server.use(

0 commit comments

Comments
 (0)