Skip to content

Commit

Permalink
upd types
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Svitek committed Jul 29, 2022
1 parent 4e38e6d commit 42e9191
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/agenda/every.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createDebugger from "debug";
import { Agenda } from ".";
import { Job } from "../job";
import { Job, JobAttributesData } from "../job";
import { JobOptions } from "../job/repeat-every";

const debug = createDebugger("agenda:every");
Expand All @@ -15,11 +15,11 @@ const debug = createDebugger("agenda:every");
* @param options - options to run job for
* @returns Job/s created. Resolves when schedule fails or passes
*/
export const every = async function (
export const every = async function<T extends JobAttributesData> (
this: Agenda,
interval: string,
names: string | string[],
data?: unknown,
data?: T,
options?: JobOptions
): Promise<any> {
/**
Expand All @@ -30,13 +30,13 @@ export const every = async function (
* @param [options] options to run job for
* @returns instance of job
*/
const createJob = async (
const createJob = async<T extends JobAttributesData> (
interval: string,
name: string,
data?: unknown,
data?: T,
options?: JobOptions
): Promise<Job> => {
const job = this.create(name, data);
const job = this.create(name, data || {});

job.attrs.type = "single";
job.repeatEvery(interval, options);
Expand All @@ -54,7 +54,7 @@ export const every = async function (
const createJobs = async (
interval: string,
names: string[],
data?: unknown,
data?: T,
options?: JobOptions
): Promise<Job[] | undefined> => {
try {
Expand Down
6 changes: 4 additions & 2 deletions lib/job/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { Agenda } from "../agenda";
import { JobPriority } from "../agenda/define";
import * as mongodb from "mongodb";

type Modify<T, R> = Omit<T, keyof R> & R

export interface JobAttributesData {
[key: string]: any;
}
Expand All @@ -28,7 +30,7 @@ export interface JobAttributes<
/**
* The record identity.
*/
_id?: mongodb.ObjectId;
_id: mongodb.ObjectId;

agenda: Agenda;

Expand Down Expand Up @@ -163,7 +165,7 @@ class Job<T extends JobAttributesData = JobAttributesData> {
touch!: typeof touch;
setShouldSaveResult!: typeof setShouldSaveResult;

constructor(options: JobAttributes<T>) {
constructor(options: Modify<JobAttributes<T>, { _id?: mongodb.ObjectId; }>) {
const { agenda, type, nextRunAt, ...args } = options ?? {};

// Save Agenda instance
Expand Down

0 comments on commit 42e9191

Please sign in to comment.