Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support setting decodeDatesAsIso globally #159

Merged
merged 9 commits into from
Oct 10, 2023
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
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
Loading