Skip to content

Commit cdff7ff

Browse files
committed
refactor(types): reduce internal any usage and tighten dispatcher typing
1 parent da7b175 commit cdff7ff

File tree

11 files changed

+65
-40
lines changed

11 files changed

+65
-40
lines changed

src/drivers/fake_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface FakeJobRecord {
3737

3838
export type FakeJobMatcher = string | JobClass | ((job: JobData) => boolean)
3939
export type FakePayloadMatcher =
40-
| ((payload: any) => boolean)
40+
| ((payload: unknown) => boolean)
4141
| object
4242
| string
4343
| number

src/drivers/knex_adapter.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ export interface KnexAdapterOptions {
2222
}
2323

2424
type KnexConfig = Knex | Knex.Config
25+
type DbRow = Record<string, unknown>
26+
27+
interface ScheduleRow extends DbRow {
28+
id: string
29+
name: string
30+
payload: unknown
31+
cron_expression: string | null
32+
every_ms: number | string | null
33+
timezone: string | null
34+
from_date: Date | string | number | null
35+
to_date: Date | string | number | null
36+
run_limit: number | string | null
37+
run_count: number | string | null
38+
next_run_at: Date | string | number | null
39+
last_run_at: Date | string | number | null
40+
status: string
41+
created_at: Date | string | number | null
42+
}
2543

2644
/**
2745
* Create a new Knex adapter factory.
@@ -516,9 +534,9 @@ export class KnexAdapter implements Adapter {
516534
}
517535

518536
async getSchedule(id: string): Promise<ScheduleData | null> {
519-
const row = await this.#connection(this.#schedulesTable)
537+
const row = (await this.#connection(this.#schedulesTable)
520538
.where('id', id)
521-
.first()
539+
.first()) as ScheduleRow | undefined
522540
if (!row) return null
523541

524542
return this.#rowToScheduleData(row)
@@ -531,15 +549,15 @@ export class KnexAdapter implements Adapter {
531549
query = query.where('status', options.status)
532550
}
533551

534-
const rows = await query
535-
return rows.map((row: any) => this.#rowToScheduleData(row))
552+
const rows = (await query) as ScheduleRow[]
553+
return rows.map((row) => this.#rowToScheduleData(row))
536554
}
537555

538556
async updateSchedule(
539557
id: string,
540558
updates: Partial<Pick<ScheduleData, 'status' | 'nextRunAt' | 'lastRunAt' | 'runCount'>>
541559
): Promise<void> {
542-
const data: Record<string, any> = {}
560+
const data: Record<string, unknown> = {}
543561

544562
if (updates.status !== undefined) data.status = updates.status
545563
if (updates.nextRunAt !== undefined) data.next_run_at = updates.nextRunAt
@@ -581,12 +599,12 @@ export class KnexAdapter implements Adapter {
581599
query = query.forUpdate().skipLocked()
582600
}
583601

584-
const row = await query.first()
602+
const row = (await query.first()) as ScheduleRow | undefined
585603
if (!row) return null
586604

587605
// Calculate next run time
588606
let nextRunAt: Date | null = null
589-
const newRunCount = (row.run_count ?? 0) + 1
607+
const newRunCount = Number(row.run_count ?? 0) + 1
590608

591609
if (row.every_ms) {
592610
nextRunAt = new Date(now.getTime() + Number(row.every_ms))
@@ -601,7 +619,7 @@ export class KnexAdapter implements Adapter {
601619
}
602620

603621
// Check if limit will be reached
604-
if (row.run_limit !== null && newRunCount >= row.run_limit) {
622+
if (row.run_limit !== null && newRunCount >= Number(row.run_limit)) {
605623
nextRunAt = null
606624
}
607625

@@ -624,7 +642,7 @@ export class KnexAdapter implements Adapter {
624642
})
625643
}
626644

627-
#rowToScheduleData(row: any): ScheduleData {
645+
#rowToScheduleData(row: ScheduleRow): ScheduleData {
628646
return {
629647
id: row.id,
630648
name: row.name,
@@ -638,7 +656,7 @@ export class KnexAdapter implements Adapter {
638656
runCount: Number(row.run_count ?? 0),
639657
nextRunAt: row.next_run_at ? new Date(row.next_run_at) : null,
640658
lastRunAt: row.last_run_at ? new Date(row.last_run_at) : null,
641-
status: row.status === 'cancelled' ? 'paused' : row.status,
659+
status: row.status === 'paused' || row.status === 'cancelled' ? 'paused' : 'active',
642660
createdAt: row.created_at ? new Date(row.created_at) : new Date(),
643661
}
644662
}

src/drivers/sync_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class SyncAdapter implements Adapter {
142142
return Promise.resolve(null)
143143
}
144144

145-
async #execute(jobName: string, payload: any, queue: string = 'default'): Promise<any> {
145+
async #execute(jobName: string, payload: unknown, queue: string = 'default'): Promise<void> {
146146
const JobClass = Locator.get(jobName)
147147

148148
if (!JobClass) {

src/job.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ export abstract class Job<Payload = any> {
171171
* ```
172172
*/
173173
static dispatch<T extends Job>(
174-
this: new (...args: any[]) => T,
174+
this: new (...args: unknown[]) => T,
175175
payload: T extends Job<infer P> ? P : never
176176
): JobDispatcher<T extends Job<infer P> ? P : never> {
177-
const options = (this as any).options || {}
177+
const jobClass = this as unknown as { options?: JobOptions; name: string }
178+
const options = jobClass.options || {}
178179
const jobName = options.name || this.name
179180

180181
const dispatcher = new JobDispatcher<T extends Job<infer P> ? P : never>(jobName, payload)
@@ -222,10 +223,11 @@ export abstract class Job<Payload = any> {
222223
* ```
223224
*/
224225
static dispatchMany<T extends Job>(
225-
this: new (...args: any[]) => T,
226+
this: new (...args: unknown[]) => T,
226227
payloads: (T extends Job<infer P> ? P : never)[]
227228
): JobBatchDispatcher<T extends Job<infer P> ? P : never> {
228-
const options = (this as any).options || {}
229+
const jobClass = this as unknown as { options?: JobOptions; name: string }
230+
const options = jobClass.options || {}
229231
const jobName = options.name || this.name
230232

231233
const dispatcher = new JobBatchDispatcher<T extends Job<infer P> ? P : never>(jobName, payloads)
@@ -271,13 +273,14 @@ export abstract class Job<Payload = any> {
271273
* ```
272274
*/
273275
static schedule<T extends Job>(
274-
this: new (...args: any[]) => T,
276+
this: new (...args: unknown[]) => T,
275277
payload: T extends Job<infer P> ? P : never
276-
): ScheduleBuilder {
277-
const options = (this as any).options || {}
278+
): ScheduleBuilder<T extends Job<infer P> ? P : never> {
279+
const jobClass = this as unknown as { options?: JobOptions; name: string }
280+
const options = jobClass.options || {}
278281
const jobName = options.name || this.name
279282

280-
return new ScheduleBuilder(jobName, payload)
283+
return new ScheduleBuilder<T extends Job<infer P> ? P : never>(jobName, payload)
281284
}
282285

283286
/**

src/job_batch_dispatcher.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ export class JobBatchDispatcher<T> {
172172
* @param onRejected - Error callback
173173
* @returns Promise resolving to the DispatchManyResult
174174
*/
175-
then(
176-
onFulfilled?: (value: DispatchManyResult) => any,
177-
onRejected?: (reason: any) => any
178-
): Promise<any> {
175+
then<TResult1 = DispatchManyResult, TResult2 = never>(
176+
onFulfilled?: ((value: DispatchManyResult) => TResult1 | PromiseLike<TResult1>) | null,
177+
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
178+
): Promise<TResult1 | TResult2> {
179179
return this.run().then(onFulfilled, onRejected)
180180
}
181181

src/job_dispatcher.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ export class JobDispatcher<T> {
216216
* @param onRejected - Error callback
217217
* @returns Promise resolving to the DispatchResult
218218
*/
219-
then(
220-
onFulfilled?: (value: DispatchResult) => any,
221-
onRejected?: (reason: any) => any
222-
): Promise<any> {
219+
then<TResult1 = DispatchResult, TResult2 = never>(
220+
onFulfilled?: ((value: DispatchResult) => TResult1 | PromiseLike<TResult1>) | null,
221+
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
222+
): Promise<TResult1 | TResult2> {
223223
return this.run().then(onFulfilled, onRejected)
224224
}
225225

src/schedule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Schedule {
3636
return this.#data.name
3737
}
3838

39-
get payload(): any {
39+
get payload(): unknown {
4040
return this.#data.payload
4141
}
4242

src/schedule_builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import * as errors from './exceptions.js'
2222
* .run()
2323
* ```
2424
*/
25-
export class ScheduleBuilder implements PromiseLike<ScheduleResult> {
25+
export class ScheduleBuilder<TPayload = unknown> implements PromiseLike<ScheduleResult> {
2626
#name: string
27-
#payload: any
27+
#payload: TPayload
2828
#id?: string
2929
#cronExpression?: string
3030
#everyMs?: number
@@ -33,7 +33,7 @@ export class ScheduleBuilder implements PromiseLike<ScheduleResult> {
3333
#to?: Date
3434
#limit?: number
3535

36-
constructor(name: string, payload: any) {
36+
constructor(name: string, payload: TPayload) {
3737
this.#name = name
3838
this.#payload = payload
3939
}
@@ -202,7 +202,7 @@ export class ScheduleBuilder implements PromiseLike<ScheduleResult> {
202202
*/
203203
then<TResult1 = ScheduleResult, TResult2 = never>(
204204
onfulfilled?: ((value: ScheduleResult) => TResult1 | PromiseLike<TResult1>) | null,
205-
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
205+
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
206206
): Promise<TResult1 | TResult2> {
207207
return this.run().then(onfulfilled, onrejected)
208208
}

src/types/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export interface JobContext {
257257
* The constructor accepts any arguments for dependency injection.
258258
* Payload and context are provided separately via `$hydrate()`.
259259
*/
260-
export type JobClass<T extends Job = Job> = (new (...args: any[]) => T) & {
260+
export type JobClass<T extends Job = Job> = (new (...args: unknown[]) => T) & {
261261
options?: JobOptions
262262
}
263263

@@ -303,7 +303,7 @@ export interface BackoffConfig {
303303

304304
export interface QueueConfig {
305305
adapter?: string
306-
retry?: any
306+
retry?: RetryConfig
307307
defaultJobOptions?: JobOptions
308308
}
309309

@@ -364,8 +364,8 @@ export interface WorkerConfig {
364364
}
365365

366366
export type WorkerCycle =
367-
| { type: 'started'; queue: string; job: any }
368-
| { type: 'completed'; queue: string; job: any }
367+
| { type: 'started'; queue: string; job: JobData }
368+
| { type: 'completed'; queue: string; job: JobData }
369369
| { type: 'idle'; suggestedDelay: Duration }
370370
| { type: 'error'; error: Error; suggestedDelay: Duration }
371371

src/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class Worker {
394394
options: JobOptions
395395
timeout: number | undefined
396396
context: JobContext
397-
payload: any
397+
payload: unknown
398398
}> {
399399
try {
400400
const JobClass = Locator.getOrThrow(job.name)
@@ -437,7 +437,7 @@ export class Worker {
437437

438438
async #executeWithTimeout(
439439
instance: Job,
440-
payload: any,
440+
payload: unknown,
441441
context: JobContext,
442442
timeout?: number
443443
): Promise<void> {

0 commit comments

Comments
 (0)