Skip to content

Commit

Permalink
allow singular rate frequencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Jun 8, 2023
1 parent 28d29b0 commit cc01646
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/resources/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
// limitations under the License.
import { EventMiddleware, Faas, ScheduleMiddleware } from '../faas';

type Frequency = 'days' | 'hours' | 'minutes';
const Frequencies = [
'day',
'days',
'hour',
'hours',
'minute',
'minutes',
] as const;

const FREQUENCIES: Frequency[] = ['days', 'hours', 'minutes'];
type Frequency = (typeof Frequencies)[number];

export class RateWorkerOptions {
public readonly description: string;
Expand Down Expand Up @@ -65,9 +72,11 @@ class Rate {
);
}

if (!FREQUENCIES.includes(normalizedFrequency)) {
if (!Frequencies.includes(normalizedFrequency)) {
throw new Error(
`invalid rate expression, frequency must be one of ${FREQUENCIES}, received ${frequency}`
`invalid rate expression, frequency must one of [${Frequencies.join(
', '
)}] received ${frequency}`
);
}

Expand Down Expand Up @@ -131,7 +140,7 @@ class Schedule {
...middleware: ScheduleMiddleware[]
): Promise<void> => {
// handle singular frequencies. e.g. schedule('something').every('day')
if (FREQUENCIES.indexOf(`${rate}s` as Frequency) !== -1) {
if (Frequencies.indexOf(`${rate}s` as Frequency) !== -1) {
rate = `1 ${rate}s`; // 'day' becomes '1 days'
}

Expand Down

0 comments on commit cc01646

Please sign in to comment.