UNPKG

2.55 kBTypeScriptView Raw
1import { SpawnOptions } from 'child_process';
2import { DateTime } from 'luxon';
3import { CONSTRAINTS, TIME_UNITS_MAP } from '../constants';
4import { CronJob } from '../job';
5import { IntRange } from './utils';
6interface BaseCronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> {
7 cronTime: string | Date | DateTime;
8 onTick: CronCommand<C, WithOnComplete<OC>>;
9 onComplete?: OC;
10 start?: boolean | null;
11 context?: C;
12 runOnInit?: boolean | null;
13 unrefTimeout?: boolean | null;
14 waitForCompletion?: boolean | null;
15}
16export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> & ({
17 timeZone?: string | null;
18 utcOffset?: never;
19} | {
20 timeZone?: never;
21 utcOffset?: number | null;
22});
23export type CronContext<C> = C extends null ? CronJob : NonNullable<C>;
24export type CronCallback<C, WithOnCompleteBool extends boolean = false> = (this: CronContext<C>, onComplete: WithOnCompleteBool extends true ? CronOnCompleteCallback : never) => void | Promise<void>;
25export type CronOnCompleteCallback = () => void | Promise<void>;
26export type CronSystemCommand = string | {
27 command: string;
28 args?: readonly string[] | null;
29 options?: SpawnOptions | null;
30};
31export type CronCommand<C, WithOnCompleteBool extends boolean = false> = CronCallback<C, WithOnCompleteBool> | CronSystemCommand;
32export type CronOnCompleteCommand = CronOnCompleteCallback | CronSystemCommand;
33export type WithOnComplete<OC> = OC extends null ? false : true;
34export type TimeUnit = (typeof TIME_UNITS_MAP)[keyof typeof TIME_UNITS_MAP];
35export type TimeUnitField<T extends TimeUnit> = Partial<Record<Ranges[T], boolean>>;
36export interface Ranges {
37 second: SecondRange;
38 minute: MinuteRange;
39 hour: HourRange;
40 dayOfMonth: DayOfMonthRange;
41 month: MonthRange;
42 dayOfWeek: DayOfWeekRange;
43}
44export type SecondRange = IntRange<(typeof CONSTRAINTS)['second'][0], (typeof CONSTRAINTS)['second'][1]>;
45export type MinuteRange = IntRange<(typeof CONSTRAINTS)['minute'][0], (typeof CONSTRAINTS)['minute'][1]>;
46export type HourRange = IntRange<(typeof CONSTRAINTS)['hour'][0], (typeof CONSTRAINTS)['hour'][1]>;
47export type DayOfMonthRange = IntRange<(typeof CONSTRAINTS)['dayOfMonth'][0], (typeof CONSTRAINTS)['dayOfMonth'][1]>;
48export type MonthRange = IntRange<(typeof CONSTRAINTS)['month'][0], (typeof CONSTRAINTS)['month'][1]>;
49export type DayOfWeekRange = IntRange<(typeof CONSTRAINTS)['dayOfWeek'][0], (typeof CONSTRAINTS)['dayOfWeek'][1]>;
50export {};