Skip to content

Commit d43725b

Browse files
mertkirbugaMert Kirbuga
and
Mert Kirbuga
authored
feat: Remove moment js (#302)
* Replace moment with dayjs * Set decodeDatesAsIso as true by default * Remove decodeDatesAsIso param entirely * Return iso date string instead dayjs objects * Remove corresponding test * Remove is_timezone_support_enabled flag * Remove unused dayjs instances --------- Co-authored-by: Mert Kirbuga <[email protected]>
1 parent 64291ec commit d43725b

File tree

7 files changed

+18
-247
lines changed

7 files changed

+18
-247
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"homepage": "http://ftrack.com",
6363
"dependencies": {
6464
"loglevel": "^1.9.2",
65-
"moment": "^2.30.1",
6665
"uuid": "^11.1.0"
6766
},
6867
"peerDependencies": {

source/session.ts

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// :copyright: Copyright (c) 2016 ftrack
2-
import moment from "moment";
32
import loglevel from "loglevel";
43
import { v4 as uuidV4 } from "uuid";
54

@@ -41,8 +40,6 @@ import getSchemaMappingFromSchemas from "./util/get_schema_mapping.js";
4140

4241
const logger = loglevel.getLogger("ftrack_api");
4342

44-
const ENCODE_DATETIME_FORMAT = "YYYY-MM-DDTHH:mm:ss";
45-
4643
/**
4744
* ftrack API session
4845
* @class Session
@@ -64,7 +61,6 @@ export class Session<
6461
serverInformation?: QueryServerInformationResponse;
6562
serverVersion?: string;
6663
private ensureSerializableResponse: boolean;
67-
private decodeDatesAsIso: boolean;
6864
private schemasPromise?: Promise<Schema<TEntityTypeMap>[]>;
6965
private serverInformationPromise?: Promise<ServerInformation>;
7066
private serverInformationValues?: string[];
@@ -86,7 +82,6 @@ export class Session<
8682
* @param {string} [options.apiEndpoint=/api] - API endpoint.
8783
* @param {object} [options.headers] - Additional headers to send with the request
8884
* @param {object} [options.strictApi] - Turn on strict API mode
89-
* @param {object} [options.decodeDatesAsIso] - Decode dates as ISO strings instead of moment objects
9085
* @param {object} [options.ensureSerializableResponse] - Disable normalization of response data
9186
*
9287
* @constructs Session
@@ -103,7 +98,6 @@ export class Session<
10398
apiEndpoint = "/api",
10499
additionalHeaders = {},
105100
strictApi = false,
106-
decodeDatesAsIso = false,
107101
ensureSerializableResponse = false,
108102
}: SessionOptions = {},
109103
) {
@@ -181,15 +175,6 @@ export class Session<
181175
this.clientToken = `ftrack-javascript-api--${uuidV4()}`;
182176
}
183177

184-
// Always include is_timezone_support_enabled as required by API.
185-
// TODO: Remove this in next major.
186-
if (
187-
serverInformationValues &&
188-
!serverInformationValues.includes("is_timezone_support_enabled")
189-
) {
190-
serverInformationValues.push("is_timezone_support_enabled");
191-
}
192-
193178
// TODO: remove these operations from session initialization in next major
194179
const operations: [
195180
operation.QueryServerInformationOperation,
@@ -202,8 +187,6 @@ export class Session<
202187
{ action: "query_schemas" },
203188
];
204189

205-
this.decodeDatesAsIso = decodeDatesAsIso;
206-
207190
/**
208191
* By default the API server will return normalized responses, and we denormalize them in the client.
209192
* This might cause cyclical references in the response data, making it non-JSON serializable.
@@ -292,7 +275,7 @@ export class Session<
292275
/**
293276
* Return encoded *data* as JSON string.
294277
*
295-
* This will translate date, moment, and dayjs objects into ISO8601 string representation in UTC.
278+
* This will translate date and dayjs objects into ISO8601 string representation in UTC.
296279
*
297280
* @private
298281
* @param {*} data The data to encode.
@@ -316,23 +299,9 @@ export class Session<
316299

317300
const date = convertToIsoString(data);
318301
if (date) {
319-
if (
320-
this.serverInformation &&
321-
this.serverInformation.is_timezone_support_enabled
322-
) {
323-
// Ensure that the moment object is in UTC and format
324-
// to timezone naive string.
325-
return {
326-
__type__: "datetime",
327-
value: date,
328-
};
329-
}
330-
331-
// Ensure that the moment object is in local time zone and format
332-
// to timezone naive string.
333302
return {
334303
__type__: "datetime",
335-
value: moment(date).local().format(ENCODE_DATETIME_FORMAT),
304+
value: date,
336305
};
337306
}
338307

@@ -375,7 +344,7 @@ export class Session<
375344
* de-duplicated in the back end and point them to a single object in
376345
* *identityMap*.
377346
*
378-
* datetime objects will be converted to timezone-aware moment objects.
347+
* datetime objects will be converted to timezone-aware dayjs objects.
379348
*
380349
* @private
381350
* @param {*} data The data to decode.
@@ -386,33 +355,26 @@ export class Session<
386355
data: any,
387356
identityMap: Data = {},
388357
{
389-
decodeDatesAsIso = false,
390358
ensureSerializableResponse = false,
391359
}: {
392-
decodeDatesAsIso?: boolean;
393360
ensureSerializableResponse?: boolean;
394361
} = {},
395362
): any {
396363
if (Array.isArray(data)) {
397364
return this._decodeArray(data, identityMap, {
398-
decodeDatesAsIso,
399365
ensureSerializableResponse,
400366
});
401367
}
402368
if (!!data && typeof data === "object") {
403369
if (data.__entity_type__ && !ensureSerializableResponse) {
404370
return this._mergeEntity(data, identityMap, {
405-
decodeDatesAsIso,
406371
ensureSerializableResponse,
407372
});
408373
}
409-
if (data.__type__ === "datetime" && decodeDatesAsIso) {
374+
if (data.__type__ === "datetime") {
410375
return this._decodeDateTimeAsIso(data);
411-
} else if (data.__type__ === "datetime") {
412-
return this._decodeDateTimeAsMoment(data);
413376
}
414377
return this._decodePlainObject(data, identityMap, {
415-
decodeDatesAsIso,
416378
ensureSerializableResponse,
417379
});
418380
}
@@ -422,19 +384,14 @@ export class Session<
422384
/**
423385
* Decode datetime *data* into ISO 8601 strings.
424386
*
425-
* Translate objects with __type__ equal to 'datetime' into moment
387+
* Translate objects with __type__ equal to 'datetime' into dayjs
426388
* datetime objects. If time zone support is enabled on the server the date
427-
* will be assumed to be UTC and the moment will be in utc.
389+
* will be assumed to be UTC and the dayjs will be in utc.
428390
* @private
429391
*/
430392
private _decodeDateTimeAsIso(data: any) {
431393
let dateValue = data.value;
432-
if (
433-
this.serverInformation &&
434-
this.serverInformation.is_timezone_support_enabled &&
435-
!dateValue.endsWith("Z") &&
436-
!dateValue.includes("+")
437-
) {
394+
if (!dateValue.endsWith("Z") && !dateValue.includes("+")) {
438395
// Server responds with timezone naive strings, add Z to indicate UTC.
439396
// If the string somehow already contains a timezone offset, do not add Z.
440397
dateValue += "Z";
@@ -443,27 +400,6 @@ export class Session<
443400
return new Date(dateValue).toISOString();
444401
}
445402

446-
/**
447-
* Decode datetime *data* into moment objects.
448-
*
449-
* Translate objects with __type__ equal to 'datetime' into moment
450-
* datetime objects. If time zone support is enabled on the server the date
451-
* will be assumed to be UTC and the moment will be in utc.
452-
* @private
453-
*/
454-
private _decodeDateTimeAsMoment(data: any) {
455-
if (
456-
this.serverInformation &&
457-
this.serverInformation.is_timezone_support_enabled
458-
) {
459-
// Return date as moment object with UTC set to true.
460-
return moment.utc(data.value);
461-
}
462-
463-
// Return date as local moment object.
464-
return moment(data.value);
465-
}
466-
467403
/**
468404
* Return new object where all values have been decoded.
469405
* @private
@@ -472,16 +408,13 @@ export class Session<
472408
object: Data,
473409
identityMap: Data,
474410
{
475-
decodeDatesAsIso,
476411
ensureSerializableResponse,
477412
}: {
478-
decodeDatesAsIso?: boolean;
479413
ensureSerializableResponse?: boolean;
480414
} = {},
481415
) {
482416
return Object.keys(object).reduce<Data>((previous, key) => {
483417
previous[key] = this.decode(object[key], identityMap, {
484-
decodeDatesAsIso,
485418
ensureSerializableResponse,
486419
});
487420
return previous;
@@ -496,16 +429,13 @@ export class Session<
496429
collection: any[],
497430
identityMap: Data,
498431
{
499-
decodeDatesAsIso = false,
500432
ensureSerializableResponse = false,
501433
}: {
502-
decodeDatesAsIso?: boolean;
503434
ensureSerializableResponse?: boolean;
504435
} = {},
505436
): any[] {
506437
return collection.map((item) =>
507438
this.decode(item, identityMap, {
508-
decodeDatesAsIso,
509439
ensureSerializableResponse,
510440
}),
511441
);
@@ -519,10 +449,8 @@ export class Session<
519449
entity: Data,
520450
identityMap: Data,
521451
{
522-
decodeDatesAsIso,
523452
ensureSerializableResponse,
524453
}: {
525-
decodeDatesAsIso?: boolean;
526454
ensureSerializableResponse?: boolean;
527455
} = {},
528456
) {
@@ -547,7 +475,6 @@ export class Session<
547475
for (const key in entity) {
548476
if (Object.hasOwn(entity, key)) {
549477
mergedEntity[key] = this.decode(entity[key], identityMap, {
550-
decodeDatesAsIso,
551478
ensureSerializableResponse,
552479
});
553480
}
@@ -630,7 +557,6 @@ export class Session<
630557
* @param {AbortSignal} options.signal - Abort signal
631558
* @param {string} options.pushToken - push token to associate with the request
632559
* @param {object} options.headers - Additional headers to send with the request
633-
* @param {string} options.decodeDatesAsIso - Return dates as ISO strings instead of moment objects
634560
*
635561
*/
636562
async call<T = ActionResponse<keyof TEntityTypeMap>>(
@@ -640,7 +566,6 @@ export class Session<
640566
pushToken,
641567
signal,
642568
additionalHeaders = {},
643-
decodeDatesAsIso = this.decodeDatesAsIso,
644569
ensureSerializableResponse = this.ensureSerializableResponse,
645570
}: CallOptions = {},
646571
): Promise<IsTuple<T> extends true ? T : T[]> {
@@ -690,11 +615,7 @@ export class Session<
690615
throw this.getErrorFromResponse(response);
691616
}
692617
try {
693-
return this.decode(
694-
response,
695-
{},
696-
{ decodeDatesAsIso, ensureSerializableResponse },
697-
);
618+
return this.decode(response, {}, { ensureSerializableResponse });
698619
} catch (reason) {
699620
logger.warn("Server reported error in unexpected format. ", reason);
700621
throw this.getErrorFromResponse({
@@ -856,7 +777,6 @@ export class Session<
856777
* @param {object} options.abortController - Deprecated in favour of options.signal
857778
* @param {object} options.signal - Abort signal user for aborting requests prematurely
858779
* @param {object} options.headers - Additional headers to send with the request
859-
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
860780
* @param {object} options.ensureSerializableResponse - Disable normalization of response data
861781
* @return {Promise} Promise which will be resolved with an object
862782
* containing action, data and metadata
@@ -885,7 +805,6 @@ export class Session<
885805
* @param {object} options.abortController - Deprecated in favour of options.signal
886806
* @param {object} options.signal - Abort signal user for aborting requests prematurely
887807
* @param {object} options.headers - Additional headers to send with the request
888-
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
889808
* @param {object} options.ensureSerializableResponse - Disable normalization of response data
890809
* @return {Promise} Promise which will be resolved with an object
891810
* containing data and metadata
@@ -931,7 +850,6 @@ export class Session<
931850
* @param {Object} options
932851
* @param {string} options.pushToken - push token to associate with the request
933852
* @param {object} options.headers - Additional headers to send with the request
934-
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
935853
* @param {object} options.ensureSerializableResponse - Disable normalization of response data
936854
* @return {Promise} Promise which will be resolved with the response.
937855
*/
@@ -957,7 +875,6 @@ export class Session<
957875
* @param {Object} options
958876
* @param {string} options.pushToken - push token to associate with the request
959877
* @param {object} options.headers - Additional headers to send with the request
960-
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
961878
* @param {object} options.ensureSerializableResponse - Disable normalization of response data
962879
* @return {Promise} Promise resolved with the response.
963880
*/
@@ -983,7 +900,6 @@ export class Session<
983900
* @param {Object} options
984901
* @param {string} options.pushToken - push token to associate with the request
985902
* @param {object} options.headers - Additional headers to send with the request
986-
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
987903
* @return {Promise} Promise resolved with the response.
988904
*/
989905
async delete<TEntityType extends keyof TEntityTypeMap>(

source/util/convert_to_iso_string.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Moment } from "moment";
21
import type dayjs from "dayjs";
32

43
/**
@@ -18,17 +17,17 @@ function isIsoDate(str: string) {
1817

1918
/**
2019
* Converts a string or date object to ISO 6801 compatible string.
21-
* Supports converting regular date objects, or any object that has toISOString() method such as moment or dayjs.
20+
* Supports converting regular date objects, or any object that has toISOString() method such as dayjs.
2221
*
2322
* @param data - string or date object
2423
* @returns ISO 6801 compatible string, or null if invalid date
2524
*/
2625
export function convertToIsoString(
27-
data: string | Date | Moment | ReturnType<typeof dayjs>,
26+
data: string | Date | ReturnType<typeof dayjs>,
2827
) {
2928
if (
3029
data &&
31-
// if this is a date object of type moment or dayjs, or regular date object (all of them has toISOString)
30+
// if this is a date object of type dayjs, or regular date object (all of them has toISOString)
3231
((typeof data !== "string" && typeof data.toISOString === "function") ||
3332
// if it's a ISO string already
3433
(typeof data == "string" && isIsoDate(data)))

test/convert_to_iso_string.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// :copyright: Copyright (c) 2022 ftrack
22

33
import { convertToIsoString } from "../source/util/convert_to_iso_string.js";
4-
import moment from "moment";
54
import dayjs from "dayjs";
65
import { describe, it, expect } from "vitest";
76

@@ -27,13 +26,6 @@ describe("convertToIsoString", () => {
2726
expect(converted).toEqual(isoDate);
2827
});
2928

30-
it("should convert moment objects to ISO strings in UTC", () => {
31-
const tzDate = "2023-01-01T01:00:00+01:00";
32-
const isoDate = "2023-01-01T00:00:00.000Z";
33-
const converted = convertToIsoString(moment(tzDate));
34-
expect(converted).toEqual(isoDate);
35-
});
36-
3729
it("should convert dayjs objects to ISO strings", () => {
3830
const tzDate = "2023-01-01T01:00:00+01:00";
3931
const isoDate = "2023-01-01T00:00:00.000Z";

0 commit comments

Comments
 (0)