feat: make jobs client type safe

This commit is contained in:
Mythie
2024-05-22 21:57:05 +10:00
parent 002dc0fdae
commit f28334bff7
11 changed files with 252 additions and 172 deletions

View File

@ -3,12 +3,12 @@ import type { BaseJobProvider as JobClientProvider } from './base';
import { LocalJobProvider } from './local';
import { TriggerJobProvider } from './trigger';
export class JobClient {
export class JobClient<T extends Array<JobDefinition> = []> {
private static _instance: JobClient;
private _provider: JobClientProvider;
private constructor() {
public constructor(definitions: T) {
if (process.env.NEXT_PRIVATE_JOBS_PROVIDER === 'trigger') {
this._provider = TriggerJobProvider.getInstance();
@ -16,23 +16,27 @@ export class JobClient {
}
this._provider = LocalJobProvider.getInstance();
definitions.forEach((definition) => {
this._provider.defineJob(definition);
});
}
public static getInstance() {
if (!this._instance) {
this._instance = new JobClient();
}
// public static getInstance() {
// if (!this._instance) {
// this._instance = new JobClient();
// }
return this._instance;
}
// return this._instance;
// }
public async triggerJob(options: TriggerJobOptions) {
public async triggerJob(options: TriggerJobOptions<T>) {
return this._provider.triggerJob(options);
}
public defineJob<T>(job: JobDefinition<T>) {
return this._provider.defineJob(job);
}
// public defineJob<N extends string, T>(job: JobDefinition<N, T>) {
// return this._provider.defineJob(job);
// }
public getApiHandler() {
return this._provider.getApiHandler();