Skip to content

Commit

Permalink
fix(CON-460): do not use Node.js built-in libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoerpenbeck committed Sep 4, 2024
1 parent 359b30b commit ee28685
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/common/httpClient/serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serializeWithDatesAsIsoString } from './serialize';
import { serializeWithDatesAsIsoString, isDate } from './serialize';

describe('serializeWithDatesAsIsoString', () => {
const now = new Date();
Expand All @@ -18,4 +18,13 @@ describe('serializeWithDatesAsIsoString', () => {
expect(parsed.inner.bar).toEqual(now.toISOString());
expect(parsed.inner.innerInner).toEqual(innerInner);
});
it('accepts only a Date', () => {
expect(isDate(new Date())).toBeTruthy();
expect(isDate(new String())).toBeFalsy();
expect(isDate(undefined)).toBeFalsy();
expect(isDate(null)).toBeFalsy();
expect(isDate(0)).toBeFalsy();
expect(isDate('')).toBeFalsy();
expect(isDate({})).toBeFalsy();
});
});
12 changes: 11 additions & 1 deletion src/common/httpClient/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { isDate } from 'util/types';
function objectToString(o: unknown): string {
return Object.prototype.toString.call(o);
}

function isObject(arg: unknown): arg is object {
return typeof arg === 'object' && arg !== null;
}

export function isDate(date: unknown): date is Date {
return isObject(date) && objectToString(date) === '[object Date]';
}

export function serializeWithDatesAsIsoString(key: string, value: unknown): unknown {
return isDate(value) ? value.toISOString() : value;
Expand Down
7 changes: 5 additions & 2 deletions src/fft-api/facility/fftFacilityService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { setTimeout } from 'timers/promises';
import {
CarrierStatus,
DefaultPickingTimesConfiguration,
Expand All @@ -20,6 +19,10 @@ import ActionEnum = ModifyShortpickAction.ActionEnum;
import { Logger } from 'tslog';
import { CustomLogger, QueryParams } from '../../common';

const sleep = async (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

export class FftFacilityService {
private static readonly facilityCache = new Map<string, string>();

Expand Down Expand Up @@ -224,7 +227,7 @@ export class FftFacilityService {
delay / 1000
} seconds`
);
await setTimeout(delay);
await sleep(delay);
return this.disableShortPick(facilityId, retries - 1, delay * 2);
}
this.logger.error(`Disabling ShortPick for Facility '${facilityId}' failed ${err},`);
Expand Down

0 comments on commit ee28685

Please sign in to comment.