diff --git a/src/job.ts b/src/job.ts index 4e301194..a94d94f2 100644 --- a/src/job.ts +++ b/src/job.ts @@ -43,40 +43,10 @@ export class CronJob { onTick: CronJobParams['onTick'], onComplete?: CronJobParams['onComplete'], start?: CronJobParams['start'], - timeZone?: CronJobParams['timeZone'], + timeZone?: string | null, context?: CronJobParams['context'], runOnInit?: CronJobParams['runOnInit'], - utcOffset?: null, - unrefTimeout?: CronJobParams['unrefTimeout'], - waitForCompletion?: CronJobParams['waitForCompletion'], - errorHandler?: CronJobParams['errorHandler'], - name?: CronJobParams['name'], - threshold?: CronJobParams['threshold'] - ); - constructor( - cronTime: CronJobParams['cronTime'], - onTick: CronJobParams['onTick'], - onComplete?: CronJobParams['onComplete'], - start?: CronJobParams['start'], - timeZone?: null, - context?: CronJobParams['context'], - runOnInit?: CronJobParams['runOnInit'], - utcOffset?: CronJobParams['utcOffset'], - unrefTimeout?: CronJobParams['unrefTimeout'], - waitForCompletion?: CronJobParams['waitForCompletion'], - errorHandler?: CronJobParams['errorHandler'], - name?: CronJobParams['name'], - threshold?: CronJobParams['threshold'] - ); - constructor( - cronTime: CronJobParams['cronTime'], - onTick: CronJobParams['onTick'], - onComplete?: CronJobParams['onComplete'], - start?: CronJobParams['start'], - timeZone?: CronJobParams['timeZone'], - context?: CronJobParams['context'], - runOnInit?: CronJobParams['runOnInit'], - utcOffset?: CronJobParams['utcOffset'], + utcOffset?: number | null, unrefTimeout?: CronJobParams['unrefTimeout'], waitForCompletion?: CronJobParams['waitForCompletion'], errorHandler?: CronJobParams['errorHandler'], @@ -93,13 +63,7 @@ export class CronJob { throw new ExclusiveParametersError('timeZone', 'utcOffset'); } - if (timeZone != null) { - this.cronTime = new CronTime(cronTime, timeZone, null); - } else if (utcOffset != null) { - this.cronTime = new CronTime(cronTime, null, utcOffset); - } else { - this.cronTime = new CronTime(cronTime, timeZone, utcOffset); - } + this.cronTime = new CronTime(cronTime, timeZone ?? null, utcOffset ?? null); if (unrefTimeout != null) { this.unrefTimeout = unrefTimeout; @@ -137,61 +101,23 @@ export class CronJob { static from( params: CronJobParams ) { - // runtime check for JS users - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - if (params.timeZone != null && params.utcOffset != null) { - throw new ExclusiveParametersError('timeZone', 'utcOffset'); - } - - if (params.timeZone != null) { - return new CronJob( - params.cronTime, - params.onTick, - params.onComplete, - params.start, - params.timeZone, - params.context, - params.runOnInit, - params.utcOffset, - params.unrefTimeout, - params.waitForCompletion, - params.errorHandler, - params.name, - params.threshold - ); - } else if (params.utcOffset != null) { - return new CronJob( - params.cronTime, - params.onTick, - params.onComplete, - params.start, - null, - params.context, - params.runOnInit, - params.utcOffset, - params.unrefTimeout, - params.waitForCompletion, - params.errorHandler, - params.name, - params.threshold - ); - } else { - return new CronJob( - params.cronTime, - params.onTick, - params.onComplete, - params.start, - params.timeZone, - params.context, - params.runOnInit, - params.utcOffset, - params.unrefTimeout, - params.waitForCompletion, - params.errorHandler, - params.name, - params.threshold - ); - } + // The constructor's runtime check handles the exclusivity validation for JS users. + // The CronJobParams type enforces it at compile time for TS users. + return new CronJob( + params.cronTime, + params.onTick, + params.onComplete, + params.start, + ('timeZone' in params ? params.timeZone : null) as string | null, + params.context, + params.runOnInit, + ('utcOffset' in params ? params.utcOffset : null) as number | null, + params.unrefTimeout, + params.waitForCompletion, + params.errorHandler, + params.name, + params.threshold + ); } private _fnWrap(cmd: CronCommand): CronCallback { diff --git a/src/time.ts b/src/time.ts index 137ea42e..6de2aba0 100644 --- a/src/time.ts +++ b/src/time.ts @@ -43,18 +43,8 @@ export class CronTime { constructor( source: CronJobParams['cronTime'], - timeZone?: CronJobParams['timeZone'], - utcOffset?: null - ); - constructor( - source: CronJobParams['cronTime'], - timeZone?: null, - utcOffset?: CronJobParams['utcOffset'] - ); - constructor( - source: CronJobParams['cronTime'], - timeZone?: CronJobParams['timeZone'], - utcOffset?: CronJobParams['utcOffset'] + timeZone?: string | null, + utcOffset?: number | null ) { // runtime check for JS users if (timeZone != null && utcOffset != null) {