UNPKG

2.6 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 errorHandler?: ((error: unknown) => void) | null;
16}
17export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> & ({
18 timeZone?: string | null;
19 utcOffset?: never;
20} | {
21 timeZone?: never;
22 utcOffset?: number | null;
23});
24export type CronContext<C> = C extends null ? CronJob : NonNullable<C>;
25export type CronCallback<C, WithOnCompleteBool extends boolean = false> = (this: CronContext<C>, onComplete: WithOnCompleteBool extends true ? CronOnCompleteCallback : never) => void | Promise<void>;
26export type CronOnCompleteCallback = () => void | Promise<void>;
27export type CronSystemCommand = string | {
28 command: string;
29 args?: readonly string[] | null;
30 options?: SpawnOptions | null;
31};
32export type CronCommand<C, WithOnCompleteBool extends boolean = false> = CronCallback<C, WithOnCompleteBool> | CronSystemCommand;
33export type CronOnCompleteCommand = CronOnCompleteCallback | CronSystemCommand;
34export type WithOnComplete<OC> = OC extends null ? false : true;
35export type TimeUnit = (typeof TIME_UNITS_MAP)[keyof typeof TIME_UNITS_MAP];
36export type TimeUnitField<T extends TimeUnit> = Partial<Record<Ranges[T], boolean>>;
37export interface Ranges {
38 second: SecondRange;
39 minute: MinuteRange;
40 hour: HourRange;
41 dayOfMonth: DayOfMonthRange;
42 month: MonthRange;
43 dayOfWeek: DayOfWeekRange;
44}
45export type SecondRange = IntRange<(typeof CONSTRAINTS)['second'][0], (typeof CONSTRAINTS)['second'][1]>;
46export type MinuteRange = IntRange<(typeof CONSTRAINTS)['minute'][0], (typeof CONSTRAINTS)['minute'][1]>;
47export type HourRange = IntRange<(typeof CONSTRAINTS)['hour'][0], (typeof CONSTRAINTS)['hour'][1]>;
48export type DayOfMonthRange = IntRange<(typeof CONSTRAINTS)['dayOfMonth'][0], (typeof CONSTRAINTS)['dayOfMonth'][1]>;
49export type MonthRange = IntRange<(typeof CONSTRAINTS)['month'][0], (typeof CONSTRAINTS)['month'][1]>;
50export type DayOfWeekRange = IntRange<(typeof CONSTRAINTS)['dayOfWeek'][0], (typeof CONSTRAINTS)['dayOfWeek'][1]>;
51export {};