introduce durationToMilliseconds

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-06-16 11:46:13 +02:00
parent cf1376ff88
commit a5c5491ff5
7 changed files with 58 additions and 47 deletions
@@ -15,7 +15,11 @@
*/
import { ResponseError } from '@backstage/errors';
import { HumanDuration, JsonObject } from '@backstage/types';
import {
HumanDuration,
JsonObject,
durationToMilliseconds,
} from '@backstage/types';
import isEqual from 'lodash/isEqual';
import fetch from 'node-fetch';
import yaml from 'yaml';
@@ -29,27 +33,6 @@ import {
const DEFAULT_RELOAD_INTERVAL = { seconds: 60 };
function durationToMs(duration: HumanDuration): number {
const {
years = 0,
months = 0,
weeks = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0,
} = duration;
const totalDays = years * 365 + months * 30 + weeks * 7 + days;
const totalHours = totalDays * 24 + hours;
const totalMinutes = totalHours * 60 + minutes;
const totalSeconds = totalMinutes * 60 + seconds;
const totalMilliseconds = totalSeconds * 1000 + milliseconds;
return totalMilliseconds;
}
/**
* Options for {@link RemoteConfigSource.create}.
*
@@ -104,7 +87,7 @@ export class RemoteConfigSource implements ConfigSource {
private constructor(options: RemoteConfigSourceOptions) {
this.#url = options.url;
this.#reloadIntervalMs = durationToMs(
this.#reloadIntervalMs = durationToMilliseconds(
options.reloadInterval ?? DEFAULT_RELOAD_INTERVAL,
);
this.#transformer = createConfigTransformer({