UNPKG

2.54 kBTypeScriptView Raw
1/// <reference types="node" />
2import { SpawnOptions } from 'child_process';
3import { DateTime } from 'luxon';
4import { CONSTRAINTS, TIME_UNITS_MAP } from '../constants';
5import { CronJob } from '../job';
6import { IntRange } from './utils';
7interface BaseCronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> {
8 cronTime: string | Date | DateTime;
9 onTick: CronCommand<C, WithOnComplete<OC>>;
10 onComplete?: OC;
11 start?: boolean | null;
12 context?: C;
13 runOnInit?: boolean | null;
14 unrefTimeout?: 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 {};