Skip to content

Commit

Permalink
fix: support setting decodeDatesAsIso globally (#159)
Browse files Browse the repository at this point in the history
* Add session decodeDatesAsIso tests

---------

Co-authored-by: Lars Johansson <gismya@gmail.com>
Co-authored-by: Lars Johansson <lars.johansson@ftrack.com>
  • Loading branch information
3 people authored Oct 10, 2023
1 parent 0f58b6c commit daa9780
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
9 changes: 6 additions & 3 deletions source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class Session {
schemas?: Schema[];
serverInformation?: QueryServerInformationResponse;
serverVersion?: string;
private decodeDatesAsIso: boolean;
private schemasPromise?: Promise<Schema[]>;
private serverInformationPromise?: Promise<ServerInformation>;
private serverInformationValues?: string[];
Expand All @@ -80,7 +81,7 @@ export class Session {
* @param {string} [options.apiEndpoint=/api] - API endpoint.
* @param {object} [options.headers] - Additional headers to send with the request
* @param {object} [options.strictApi] - Turn on strict API mode
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
* @param {object} [options.decodeDatesAsIso] - Decode dates as ISO strings instead of moment objects
*
* @constructs Session
*/
Expand All @@ -96,6 +97,7 @@ export class Session {
apiEndpoint = "/api",
additionalHeaders = {},
strictApi = false,
decodeDatesAsIso = false,
}: SessionOptions = {},
) {
if (!serverUrl || !apiUser || !apiKey) {
Expand Down Expand Up @@ -190,6 +192,8 @@ export class Session {
{ action: "query_schemas" },
];

this.decodeDatesAsIso = decodeDatesAsIso;

/**
* true if session is initialized
* @memberof Session
Expand Down Expand Up @@ -578,14 +582,13 @@ export class Session {
pushToken,
signal,
additionalHeaders = {},
decodeDatesAsIso = false,
decodeDatesAsIso = this.decodeDatesAsIso,
}: CallOptions = {},
): Promise<IsTuple<T> extends true ? T : T[]> {
if (this.initializing) {
await this.initializing;
}
const url = `${this.serverUrl}${this.apiEndpoint}`;

try {
// Delay call until session is initialized if initialization is in
// progress.
Expand Down
1 change: 1 addition & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface SessionOptions {
apiEndpoint?: string;
additionalHeaders?: Data;
strictApi?: boolean;
decodeDatesAsIso?: boolean;
}

export interface CreateComponentOptions {
Expand Down
39 changes: 38 additions & 1 deletion test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ beforeAll(() => {
credentials.apiKey,
{
autoConnectEventHub: false,
decodeDatesAsIso: false,
},
);
});
Expand Down Expand Up @@ -134,6 +135,43 @@ describe("Session", () => {
);
expect(result.data[0].created_at).toEqual("2022-10-10T10:12:09.000Z");
});
it("Should allow querying with datetimes decoded as ISO objects, when set on session initialization", async () => {
const decodeDatesAsIsoSession = new Session(
credentials.serverUrl,
credentials.apiUser,
credentials.apiKey,
{
decodeDatesAsIso: true,
},
);
const result = await decodeDatesAsIsoSession.query(
"select name, created_at from Task limit 1",
);
expect(result.data[0].created_at).toEqual("2022-10-10T10:12:09.000Z");
});
it("Should allow overriding session decodeDatesAsIso when querying", async () => {
const decodeDatesAsIsoSession = new Session(
credentials.serverUrl,
credentials.apiUser,
credentials.apiKey,
{
decodeDatesAsIso: true,
},
);
const result = await decodeDatesAsIsoSession.query(
"select name, created_at from Task limit 1",
{ decodeDatesAsIso: false },
);
expect(result.data[0].created_at).to.be.instanceOf(moment);
expect(result.data[0].created_at.toISOString()).toEqual(
"2022-10-10T10:12:09.000Z",
);
const result2 = await session.query(
"select name, created_at from Task limit 1",
{ decodeDatesAsIso: true },
);
expect(result2.data[0].created_at).toEqual("2022-10-10T10:12:09.000Z");
});

it("Should allow querying with datetimes decoded as ISO objects with timezone support disabled", async () => {
server.use(
Expand Down Expand Up @@ -590,7 +628,6 @@ describe("Encoding entities", () => {
12321,
]);
});

it("Should support encoding moment dates to local timezone if timezone support is disabled", () => {
const now = moment();
server.use(
Expand Down

0 comments on commit daa9780

Please sign in to comment.