Skip to content

Commit 4dd4242

Browse files
committed
Expanding things.
Signed-off-by: Alexander Trauzzi <[email protected]>
1 parent ff5c841 commit 4dd4242

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

src/implementation/Client/HTTPClient/jobs.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import IClientJobs from "../../../interfaces/Client/IClientJobs";
1515
import HTTPClient from "./HTTPClient";
1616
import { THTTPExecuteParams } from "../../../types/http/THTTPExecuteParams.type";
1717
import { Job } from "../../../types/jobs/Job.type";
18-
import { JobSchedule } from "../../../types/jobs/JobSchedule.type";
18+
import { Schedule, ScheduleUnboxer } from "../../../types/jobs/JobSchedule.type";
1919

2020
export default class HTTPClientJobs implements IClientJobs {
2121

@@ -27,19 +27,22 @@ export default class HTTPClientJobs implements IClientJobs {
2727
async schedule(
2828
jobName: string,
2929
data: object | string,
30-
schedule: JobSchedule | null = null,
30+
schedule: Schedule | null = null,
3131
dueTime: string | Date | null = null,
3232
repeats: number | null = null,
3333
ttl: string | null = null,
3434
): Promise<void> {
35+
36+
const boxedSchedule = new ScheduleUnboxer(schedule);
37+
3538
await this.httpClient.executeWithApiVersion(HTTPClientJobs.ApiVersion, `/${HTTPClientJobs.Path}/${jobName}`, {
3639
method: "POST",
3740
body: {
3841
data,
39-
schedule,
4042
dueTime,
4143
repeats,
4244
ttl,
45+
schedule: boxedSchedule.unbox(),
4346
},
4447
headers: {
4548
"content-type": "application/json",

src/types/jobs/CronExpression.type.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14+
// note: This can get crazy, more than TS can really handle.
15+
export type CronExpressionString = `${string} ${string} ${string} ${string} ${string} ${string}`;
16+
1417
export enum CronPeriod {
1518
Second,
1619
Minute,
@@ -191,7 +194,7 @@ export class CronExpression {
191194
return this;
192195
}
193196

194-
public toString() {
197+
public toString(): CronExpressionString {
195198
return `${this.seconds} ${this.minutes} ${this.hours} ${this.dayOfMonth} ${this.month} ${this.dayOfWeek}`;
196199
}
197200

src/types/jobs/JobSchedule.type.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,57 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
export enum PrefixedPeriodExpression {
14+
import { CronExpression, CronExpressionString } from "./CronExpression.type";
15+
16+
export enum PeriodConstant {
1517
Yearly = "@yearly",
1618
Monthly = "@monthly",
1719
Weekly = "@weekly",
1820
Daily = "@daily",
1921
Hourly = "@hourly",
2022
}
2123

22-
type EveryPeriod = `@every ${string}`;
24+
type EveryExpressionString = `@every ${string}`;
25+
26+
class EveryExpression {
2327

24-
// note: This can get crazy, more than TS can really handle.
25-
type SystemDCronExpression = `${string} ${string} ${string} ${string} ${string} ${string}`;
28+
private static readonly EveryExpressionValue = /^(d+(m?s|m|h))+$/;
2629

27-
type ScheduleString = PrefixedPeriodExpression | EveryPeriod | SystemDCronExpression;
30+
private readonly value: string;
2831

29-
class Schedule {
32+
public static isEveryExpression(value: string) {
33+
return EveryExpression.EveryExpressionValue.test(`@every ${value}`);
34+
}
3035

31-
private static readonly IsEveryExpression = /^@every (d+(m?s|m|h))+$/;
36+
constructor(value: string) {
3237

33-
private readonly value: ScheduleString;
38+
if (! EveryExpression.isEveryExpression(value))
39+
throw new Error(`${value} is not a valid every expression value.`);
3440

35-
constructor(scheduleString: ScheduleString) {
36-
this.value = scheduleString;
41+
this.value = value;
3742
}
3843

39-
public get isPrefixedPeriodExpression() {
40-
return (
41-
Object.values(PrefixedPeriodExpression).includes(this.value as PrefixedPeriodExpression)
42-
|| this.value.match(Schedule.IsEveryExpression)
43-
);
44+
public toString(): EveryExpressionString {
45+
return `@every ${this.value}`;
4446
}
4547
}
4648

47-
export type JobSchedule = ScheduleString | Schedule;
49+
export type Schedule = PeriodConstant | EveryExpression | EveryExpressionString | CronExpression | CronExpressionString;
50+
51+
export class ScheduleUnboxer {
52+
53+
private readonly value: Schedule | null;
54+
55+
constructor(value: Schedule | null) {
56+
this.value = value;
57+
}
58+
59+
public unbox(): string | null {
60+
if(this.value instanceof EveryExpression)
61+
return this.value.toString();
62+
if(this.value instanceof CronExpression)
63+
return this.value.toString();
64+
65+
return this.value;
66+
}
67+
}

test/unit/jobs/jobs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ limitations under the License.
1313

1414
import HTTPClient from "../../../src/implementation/Client/HTTPClient/HTTPClient";
1515
import HTTPClientJobs from "../../../src/implementation/Client/HTTPClient/jobs";
16-
import { PrefixedPeriodExpression } from "../../../src/types/jobs/JobSchedule.type";
16+
import { PeriodConstant } from "../../../src/types/jobs/JobSchedule.type";
1717

1818
jest.mock("../../../src/implementation/Client/HTTPClient/HTTPClient");
1919

@@ -35,7 +35,7 @@ describe("Jobs Client", () => {
3535
{
3636
some: "data",
3737
},
38-
PrefixedPeriodExpression.Daily
38+
PeriodConstant.Daily
3939
);
4040

4141
expect(httpClient.executeWithApiVersion).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)