From 1b7c7520b494fe2584bea45f769eace6115bbe96 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Thu, 3 Aug 2023 09:53:15 +0100 Subject: [PATCH] new Job -> client.defineJob --- docs/_snippets/delay-example.mdx | 2 +- docs/_snippets/how-to-pass-integrations.mdx | 2 +- docs/documentation/concepts/integrations.mdx | 28 +++------- docs/documentation/concepts/jobs.mdx | 8 +-- docs/documentation/concepts/runs.mdx | 8 +-- docs/documentation/concepts/tasks.mdx | 30 +++------- .../concepts/triggers/dynamic.mdx | 12 ++-- .../concepts/triggers/events.mdx | 9 ++- .../concepts/triggers/scheduled.mdx | 4 +- .../concepts/triggers/webhooks.mdx | 2 +- .../concepts/what-is-triggerdotdev.mdx | 2 +- .../guides/using-integrations-apikeys.mdx | 2 +- .../guides/using-integrations-oauth.mdx | 2 +- docs/documentation/guides/zod.mdx | 6 +- docs/documentation/quickstart.mdx | 2 +- docs/integrations/apis/github-tasks.mdx | 2 +- docs/integrations/apis/github-triggers.mdx | 2 +- docs/integrations/apis/github.mdx | 27 ++++----- docs/integrations/apis/plain.mdx | 55 +++++++++---------- docs/integrations/apis/resend.mdx | 2 +- docs/integrations/apis/slack.mdx | 2 +- docs/integrations/apis/typeform.mdx | 15 ++--- docs/integrations/create.mdx | 53 ++++++++---------- docs/sdk/crontrigger.mdx | 7 +-- docs/sdk/dynamicschedule/overview.mdx | 4 +- docs/sdk/dynamictrigger/overview.mdx | 8 +-- docs/sdk/eventtrigger.mdx | 2 +- docs/sdk/intervaltrigger.mdx | 2 +- docs/sdk/io/backgroundfetch.mdx | 16 ++---- docs/sdk/io/registercron.mdx | 10 ++-- docs/sdk/io/registerinterval.mdx | 5 +- docs/sdk/io/registertrigger.mdx | 20 +++---- docs/sdk/io/runtask.mdx | 4 +- docs/sdk/io/sendevent.mdx | 7 +-- docs/sdk/io/try.mdx | 2 +- docs/sdk/io/unregistercron.mdx | 9 ++- docs/sdk/io/unregisterinterval.mdx | 9 ++- docs/sdk/io/unregistertrigger.mdx | 5 +- docs/sdk/istriggererror.mdx | 2 +- docs/sdk/job.mdx | 30 +++++----- 40 files changed, 167 insertions(+), 252 deletions(-) diff --git a/docs/_snippets/delay-example.mdx b/docs/_snippets/delay-example.mdx index d9fdfcc2d..a63fc8269 100644 --- a/docs/_snippets/delay-example.mdx +++ b/docs/_snippets/delay-example.mdx @@ -1,5 +1,5 @@ ```typescript Wait example -new Job(client, { +client.defineJob({ id: "delay-job", name: "Delay Job", version: "0.0.1", diff --git a/docs/_snippets/how-to-pass-integrations.mdx b/docs/_snippets/how-to-pass-integrations.mdx index 255c16e0e..43740a5a6 100644 --- a/docs/_snippets/how-to-pass-integrations.mdx +++ b/docs/_snippets/how-to-pass-integrations.mdx @@ -1,5 +1,5 @@ ```typescript -new Job(client, { +client.defineJob({ //... other options integrations: { slack, diff --git a/docs/documentation/concepts/integrations.mdx b/docs/documentation/concepts/integrations.mdx index 13d1c7371..5b4d99256 100644 --- a/docs/documentation/concepts/integrations.mdx +++ b/docs/documentation/concepts/integrations.mdx @@ -4,9 +4,8 @@ description: "Integrations make it easy to use APIs in your Jobs" --- - You can use any API in your Jobs by using existing Node.js SDKs or HTTP - requests. Integrations just make it much easier especially when you want to - use OAuth. And you get great logging. + You can use any API in your Jobs by using existing Node.js SDKs or HTTP requests. Integrations + just make it much easier especially when you want to use OAuth. And you get great logging. An Integration is a package you install that makes it easy to work with a specific API. They: @@ -35,7 +34,7 @@ const slack = new Slack({ id: "slack", }); -new Job(client, { +client.defineJob({ id: "alert-on-new-github-issues", name: "Alert on new GitHub issues", version: "0.1.1", @@ -84,29 +83,16 @@ You can use OAuth to authenticate your internal team with an Integration or to a ## References - - The Integrations Dashboard allows you to manage your Integrations and setup - OAuth. + + The Integrations Dashboard allows you to manage your Integrations and setup OAuth. - + Authenticate your users with an Integration using Trigger.dev Connect. Trigger.dev integrates with a wide range of services. - + Create an Integration for your own use or as a public package. diff --git a/docs/documentation/concepts/jobs.mdx b/docs/documentation/concepts/jobs.mdx index c78072e1b..c21a3fbce 100644 --- a/docs/documentation/concepts/jobs.mdx +++ b/docs/documentation/concepts/jobs.mdx @@ -17,7 +17,7 @@ A Job is made up of a few things: ```ts //Job definition – uses the client -new Job(client, { +client.defineJob({ // 1. Metadata id: "event-1", name: "Run when the foo.bar event happens", @@ -51,11 +51,7 @@ Events [trigger](/documentation/concepts/triggers) Jobs. Jobs generate a [Run](/ Detailed SDK reference for Jobs. - + Viewing and managing your Jobs in the Dashboard. diff --git a/docs/documentation/concepts/runs.mdx b/docs/documentation/concepts/runs.mdx index 5e40df6dd..f23ed1d76 100644 --- a/docs/documentation/concepts/runs.mdx +++ b/docs/documentation/concepts/runs.mdx @@ -10,7 +10,7 @@ description: "When a [Job](/documentation/concepts/jobs) is [Triggered](/documen A Run is a record of the execution of a Job. It is created from `run()` function of a Job. ```ts -new Job(client, { +client.defineJob({ id: "event-1", name: "Run when the foo.bar event happens", version: "0.0.1", @@ -64,11 +64,7 @@ The `context` object gives you access to information about the current Run, Job, ## References - + View all Runs for a Job, all the way down to individual Tasks. diff --git a/docs/documentation/concepts/tasks.mdx b/docs/documentation/concepts/tasks.mdx index b69e94d13..fc117e406 100644 --- a/docs/documentation/concepts/tasks.mdx +++ b/docs/documentation/concepts/tasks.mdx @@ -10,7 +10,7 @@ description: "Tasks are individual building blocks of a Run." In the `run()` function you can use regular code and you can use Tasks. ```ts -new Job(client, { +client.defineJob({ id: "new-user", name: "Run when a new user signs up", version: "0.0.1", @@ -44,13 +44,9 @@ new Job(client, { await io.wait("wait", 60 * 60 * 3); // wait for 3 hours // You can wrap your own code in a Task, for retrying, resumability and logging - const response = await io.runTask( - "my-task", - { name: "My Task" }, - async () => { - return await longRunningCode(payload.userId); - } - ); + const response = await io.runTask("my-task", { name: "My Task" }, async () => { + return await longRunningCode(payload.userId); + }); return response; }, @@ -76,28 +72,16 @@ The first param of all Tasks is a `key`. This is a unique identifier for the Tas ## References - + Runs can be very long-running. Learn how we handle this. - + Integrations utilize Tasks. The `io` object allows you to easily run a Task yourself. - + View all Runs for a Job, all the way down to individual Tasks. diff --git a/docs/documentation/concepts/triggers/dynamic.mdx b/docs/documentation/concepts/triggers/dynamic.mdx index 79ca560af..dda10dfed 100644 --- a/docs/documentation/concepts/triggers/dynamic.mdx +++ b/docs/documentation/concepts/triggers/dynamic.mdx @@ -17,7 +17,7 @@ const dynamicSchedule = new DynamicSchedule(client, { }); //2. create a Job that is attached to the dynamic schedule -new Job(client, { +client.defineJob({ id: "user-dynamicinterval", name: "User Dynamic Interval", version: "0.1.1", @@ -41,7 +41,7 @@ async function registerUserCronJob(userId: string, userSchedule: string) { } //5. Register inside other Jobs -new Job(client, { +client.defineJob({ id: "register-dynamicinterval", name: "Register Dynamic Interval", version: "0.1.1", @@ -77,7 +77,7 @@ const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, { }); //2. create a Job that is attached to the dynamic trigger -new Job(client, { +client.defineJob({ id: "listen-for-dynamic-trigger", name: "Listen for dynamic trigger", version: "0.1.1", @@ -87,9 +87,7 @@ new Job(client, { }, run: async (payload, io, ctx) => { await io.slack.postMessage("Slack 📝", { - text: `New Issue opened on repo: ${ - payload.issue.html_url - }. \n\n${JSON.stringify(ctx)}`, + text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`, channel: "C04GWUTDC3W", }); }, @@ -105,7 +103,7 @@ async function registerRepo(owner: string, repo: string) { } //4. Register inside other Jobs -new Job(client, { +client.defineJob({ id: "new-repo", name: "New repo", version: "0.1.1", diff --git a/docs/documentation/concepts/triggers/events.mdx b/docs/documentation/concepts/triggers/events.mdx index e0b4ca7b4..765b873b2 100644 --- a/docs/documentation/concepts/triggers/events.mdx +++ b/docs/documentation/concepts/triggers/events.mdx @@ -23,7 +23,7 @@ You can always start out by using `z.any()` as your schema, and then later on yo ## Example ```ts -new Job(client, { +client.defineJob({ id: "new-user-slack", name: "New user slack message", version: "0.1.0", @@ -58,9 +58,8 @@ new Job(client, { ``` - You can subscribe to the same event from multiple different Jobs. This is - useful if you want to send an event to multiple different services or if you - want to keep each Job small and simple. + You can subscribe to the same event from multiple different Jobs. This is useful if you want to + send an event to multiple different services or if you want to keep each Job small and simple. ## Sending events @@ -84,7 +83,7 @@ await client.sendEvent({ You can use `io.sendEvent()` to send events from inside a Job run, to trigger another. [View the SDK reference](/sdk/io/sendevent). ```ts -new Job(client, { +client.defineJob({ id: "event-1", name: "Run when the foo.bar event happens", version: "0.0.1", diff --git a/docs/documentation/concepts/triggers/scheduled.mdx b/docs/documentation/concepts/triggers/scheduled.mdx index f92d60a2c..46809fc87 100644 --- a/docs/documentation/concepts/triggers/scheduled.mdx +++ b/docs/documentation/concepts/triggers/scheduled.mdx @@ -15,7 +15,7 @@ This job will run every 60 seconds, starting 60 seconds after this Job is first ```ts import { Job, intervalTrigger } from "@trigger.dev/sdk"; -new Job(client, { +client.defineJob({ id: "scheduled-job-1", name: "Scheduled Job 1", version: "0.1.1", @@ -43,7 +43,7 @@ This job will run at 2:30pm every Monday. You can get help with [CRON syntax](ht ```ts import { Job, cronTrigger } from "@trigger.dev/sdk"; -new Job(client, { +client.defineJob({ id: "scheduled-job-2", name: "Scheduled Job 2", version: "0.1.1", diff --git a/docs/documentation/concepts/triggers/webhooks.mdx b/docs/documentation/concepts/triggers/webhooks.mdx index f656e56be..76579a8a5 100644 --- a/docs/documentation/concepts/triggers/webhooks.mdx +++ b/docs/documentation/concepts/triggers/webhooks.mdx @@ -32,7 +32,7 @@ const github = new Github({ token: process.env.GITHUB_API_KEY!, }); -new Job(client, { +client.defineJob({ id: "critical-issue-alert", name: "Critical Issue Alert", version: "0.1.0", diff --git a/docs/documentation/concepts/what-is-triggerdotdev.mdx b/docs/documentation/concepts/what-is-triggerdotdev.mdx index ecf616c67..93fd1f44b 100644 --- a/docs/documentation/concepts/what-is-triggerdotdev.mdx +++ b/docs/documentation/concepts/what-is-triggerdotdev.mdx @@ -42,7 +42,7 @@ const slack = new Slack({ id: "slack", }); -new Job(client, { +client.defineJob({ id: "critical-issue-alert", name: "Critical Issue Alert", version: "0.1.0", diff --git a/docs/documentation/guides/using-integrations-apikeys.mdx b/docs/documentation/guides/using-integrations-apikeys.mdx index 53a130c85..330aee59b 100644 --- a/docs/documentation/guides/using-integrations-apikeys.mdx +++ b/docs/documentation/guides/using-integrations-apikeys.mdx @@ -46,7 +46,7 @@ There are two way to use Integrations in a Job: This example automatically assigns "matt-aitken" to any new issue in the `trigger.dev` repo (lucky him). ```ts -new Job(client, { +client.defineJob({ id: "assign-on-issue-opened", name: "Assign on Issue Opened", version: "0.1.0", diff --git a/docs/documentation/guides/using-integrations-oauth.mdx b/docs/documentation/guides/using-integrations-oauth.mdx index 497a283ff..29587c86b 100644 --- a/docs/documentation/guides/using-integrations-oauth.mdx +++ b/docs/documentation/guides/using-integrations-oauth.mdx @@ -35,7 +35,7 @@ There are two way to use Integrations in a Job: This example send a Slack message when someone stars the `trigger.dev` GitHub repo 🤩. ```ts -new Job(client, { +client.defineJob({ id: "star-slack-notification", name: "New Star Slack Notification", version: "0.1.0", diff --git a/docs/documentation/guides/zod.mdx b/docs/documentation/guides/zod.mdx index 0fb580879..38a92343c 100644 --- a/docs/documentation/guides/zod.mdx +++ b/docs/documentation/guides/zod.mdx @@ -13,7 +13,7 @@ We use it [extensively](https://github.com/search?q=repo%3Atriggerdotdev%2Ftrigg But there are a few places where we ask you to provide us with a Zod schema, for example when defining your own [events](/documentation/concepts/triggers/events): ```ts -new Job(client, { +client.defineJob({ id: "new-user", name: "New user", version: "0.1.0", @@ -36,8 +36,8 @@ new Job(client, { So it will help to know a little about Zod and how to use it. We definitely recommend the well written [Zod README](https://github.com/colinhacks/zod#readme) but we've included a short primer below. - Wherever we require you to pass in a Zod schema, you can always start with - `z.any()` which accepts `any` type and then add more strict validations later. + Wherever we require you to pass in a Zod schema, you can always start with `z.any()` which accepts + `any` type and then add more strict validations later. ## Basic Usage diff --git a/docs/documentation/quickstart.mdx b/docs/documentation/quickstart.mdx index 9341fb148..3ec512386 100644 --- a/docs/documentation/quickstart.mdx +++ b/docs/documentation/quickstart.mdx @@ -167,7 +167,7 @@ In there is this Job: ```typescript //Job definition – uses the client -new Job(client, { +client.defineJob({ // 1. Metadata id: "example-job", name: "Example Job", diff --git a/docs/integrations/apis/github-tasks.mdx b/docs/integrations/apis/github-tasks.mdx index b5850c591..410dc1170 100644 --- a/docs/integrations/apis/github-tasks.mdx +++ b/docs/integrations/apis/github-tasks.mdx @@ -23,7 +23,7 @@ title: Tasks ## Usage ```ts -new Job(client, { +client.defineJob({ id: "github-integration-on-issue-opened", name: "GitHub Integration - On Issue Opened", version: "0.1.0", diff --git a/docs/integrations/apis/github-triggers.mdx b/docs/integrations/apis/github-triggers.mdx index 84e78bab9..d66e62478 100644 --- a/docs/integrations/apis/github-triggers.mdx +++ b/docs/integrations/apis/github-triggers.mdx @@ -29,7 +29,7 @@ const github = new Github({ token: process.env.GITHUB_TOKEN!, }); -new Job(client, { +client.defineJob({ id: "github-integration-on-issue", name: "GitHub Integration - On Issue", version: "0.1.0", diff --git a/docs/integrations/apis/github.mdx b/docs/integrations/apis/github.mdx index a8288dff4..2fed5ba51 100644 --- a/docs/integrations/apis/github.mdx +++ b/docs/integrations/apis/github.mdx @@ -45,8 +45,7 @@ const github2 = new Github({ - Trigger Jobs when events happen in GitHub, such as a new commit or a new - issue. + Trigger Jobs when events happen in GitHub, such as a new commit or a new issue. Perform tasks such as creating a new issue or a new comment. @@ -58,8 +57,8 @@ const github2 = new Github({ You can use the underlying client to do anything Octokit supports. In this example we create a project card when a new issue is opened.. - View [the official GitHub docs](https://docs.github.com/en/rest) for - everything that is supported{" "} + View [the official GitHub docs](https://docs.github.com/en/rest) for everything that is + supported{" "} ```ts @@ -70,7 +69,7 @@ const github = new Github({ token: process.env.GITHUB_TOKEN!, }); -new Job(client, { +client.defineJob({ id: "alert-on-new-github-issues", name: "Alert on new GitHub issues", version: "0.1.1", @@ -84,17 +83,13 @@ new Job(client, { }, run: async (payload, io, ctx) => { //wrap the SDK call in runTask - const { data } = await io.runTask( - "create-card", - { name: "Create card" }, - async () => { - //create a project card using the underlying client - return io.github.client.rest.projects.createCard({ - column_id: 123, - note: "test", - }); - } - ); + const { data } = await io.runTask("create-card", { name: "Create card" }, async () => { + //create a project card using the underlying client + return io.github.client.rest.projects.createCard({ + column_id: 123, + note: "test", + }); + }); //log the url of the created card await io.logger.info(data.url); diff --git a/docs/integrations/apis/plain.mdx b/docs/integrations/apis/plain.mdx index 10a042bbe..3fce605fb 100644 --- a/docs/integrations/apis/plain.mdx +++ b/docs/integrations/apis/plain.mdx @@ -50,7 +50,7 @@ export const plain = new Plain({ apiKey: process.env.PLAIN_API_KEY!, }); -new Job(client, { +client.defineJob({ id: "plain-playground", name: "Plain Playground", version: "0.1.1", @@ -87,37 +87,34 @@ new Job(client, { customerId: customer.id, }); - const timelineEntry = await io.plain.upsertCustomTimelineEntry( - "upsert-timeline-entry", - { - customerId: customer.id, - title: "My timeline entry", - components: [ - { - componentText: { - text: `This is a nice title`, - }, + const timelineEntry = await io.plain.upsertCustomTimelineEntry("upsert-timeline-entry", { + customerId: customer.id, + title: "My timeline entry", + components: [ + { + componentText: { + text: `This is a nice title`, }, - { - componentDivider: { - dividerSpacingSize: ComponentDividerSpacingSize.M, - }, + }, + { + componentDivider: { + dividerSpacingSize: ComponentDividerSpacingSize.M, }, - { - componentText: { - textSize: ComponentTextSize.S, - textColor: ComponentTextColor.Muted, - text: "External id", - }, + }, + { + componentText: { + textSize: ComponentTextSize.S, + textColor: ComponentTextColor.Muted, + text: "External id", }, - { - componentText: { - text: foundCustomer?.externalId ?? "", - }, + }, + { + componentText: { + text: foundCustomer?.externalId ?? "", }, - ], - } - ); + }, + ], + }); }, }); ``` @@ -145,7 +142,7 @@ export const plain = new Plain({ apiKey: process.env.PLAIN_API_KEY!, }); -new Job(client, { +client.defineJob({ id: "plain-client", name: "Plain Client", version: "0.1.0", diff --git a/docs/integrations/apis/resend.mdx b/docs/integrations/apis/resend.mdx index 6925daa5e..70e6833f6 100644 --- a/docs/integrations/apis/resend.mdx +++ b/docs/integrations/apis/resend.mdx @@ -51,7 +51,7 @@ const resend = new Resend({ apiKey: process.env.RESEND_API_KEY!, }); -new Job(client, { +client.defineJob({ id: "send-resend-email", name: "Send Resend Email", version: "0.1.0", diff --git a/docs/integrations/apis/slack.mdx b/docs/integrations/apis/slack.mdx index d62b2ffc1..f1b4daeaf 100644 --- a/docs/integrations/apis/slack.mdx +++ b/docs/integrations/apis/slack.mdx @@ -37,7 +37,7 @@ const slack = new Slack({ ## Example ```ts -new Job(client, { +client.defineJob({ id: "slack-test", name: "Slack test", version: "0.0.1", diff --git a/docs/integrations/apis/typeform.mdx b/docs/integrations/apis/typeform.mdx index 7ee2e7d70..eb92da4be 100644 --- a/docs/integrations/apis/typeform.mdx +++ b/docs/integrations/apis/typeform.mdx @@ -50,7 +50,7 @@ export const typeform = new Typeform({ token: process.env.TYPEFORM_API_KEY!, }); -new Job(client, { +client.defineJob({ id: "do-something-on-new-responses", name: "Send a message to slack on new responses", version: "0.1.1", @@ -90,7 +90,7 @@ const typeform = new Typeform({ token: process.env.TYPEFORM_PAT!, }); -new Job(client, { +client.defineJob({ id: "typeform-tasks", name: "Typeform Tasks", version: "0.1.0", @@ -110,12 +110,9 @@ new Job(client, { pageSize: 50, }); - const allResponses = await io.typeform.getAllResponses( - "get-all-responses", - { - uid: payload.formId, - } - ); + const allResponses = await io.typeform.getAllResponses("get-all-responses", { + uid: payload.formId, + }); }, }); ``` @@ -133,7 +130,7 @@ const typeform = new Typeform({ token: process.env.TYPEFORM_PAT!, }); -new Job(client, { +client.defineJob({ id: "typeform-client", name: "Typeform Client", version: "0.1.0", diff --git a/docs/integrations/create.mdx b/docs/integrations/create.mdx index 8bf0ccd8f..af3d9c4cd 100644 --- a/docs/integrations/create.mdx +++ b/docs/integrations/create.mdx @@ -71,9 +71,9 @@ Once you've created your Integration package, you can start developing it. In th This is the entry point of the Integration package. It exports a main "integration" class that implements the `TriggerIntegration` interface. For example, the `@trigger.dev/github` Integration exports a `Github` class that implements. - We're adopting the naming convention of naming the class after the service, - without a suffix or prefix. We prefer the exported name be `Slack` instead of - something like `SlackIntegration` or `SlackConnector` + We're adopting the naming convention of naming the class after the service, without a suffix or + prefix. We prefer the exported name be `Slack` instead of something like `SlackIntegration` or + `SlackConnector` @@ -84,9 +84,7 @@ import { Configuration, OpenAIApi } from "openai"; import * as tasks from "./tasks"; import { OpenAIIntegrationOptions } from "./types"; -export class OpenAI - implements TriggerIntegration> -{ +export class OpenAI implements TriggerIntegration> { client: IntegrationClient; constructor(private options: OpenAIIntegrationOptions) { @@ -121,19 +119,18 @@ export class OpenAI The `TriggerIntegration` interface requires three properties to be implemented: - The `id` that uniquely identifies the Integration. This should always be - passed through the constructor options. + The `id` that uniquely identifies the Integration. This should always be passed through the + constructor options. - A unique identifier for the Integration. For example, the OpenAI - Integration has an id of `"openai"`. + A unique identifier for the Integration. For example, the OpenAI Integration has an id of + `"openai"`. - The name of the Integration. For example, the OpenAI Integration has a - name of `"OpenAI"`. + The name of the Integration. For example, the OpenAI Integration has a name of `"OpenAI"`. @@ -194,11 +191,7 @@ For example, here is the `getForm` authenticated task defined in the `@trigger.d import type { AuthenticatedTask } from "@trigger.dev/sdk"; import type { GetFormParams, GetFormResponse, TypeformSDK } from "./types"; -export const getForm: AuthenticatedTask< - TypeformSDK, - GetFormParams, - GetFormResponse -> = { +export const getForm: AuthenticatedTask = { init: (params) => { return { name: "Get Form", @@ -232,7 +225,7 @@ export type GetFormResponse = Prettify; ``` ```ts usage.ts -new Job(client, { +client.defineJob({ id: "typeform-playground", name: "Typeform Playground", version: "0.1.1", @@ -258,9 +251,9 @@ The first thing to notice is the explicit typing of the `getForm` export as an ` If you take a look at the `usage.ts` file above, you can see how this task is used in a job. The `io.typeform.getForm` function is typed as returning `Promise` and the `params` argument is typed as `GetFormParams`. - Notice how the params are the _second_ argument to `getForm`, that's because - the first argument is always the task key. See our [Keys and Resumability - docs](/documentation/concepts/resumability) for more on why this is important + Notice how the params are the _second_ argument to `getForm`, that's because the first argument is + always the task key. See our [Keys and Resumability docs](/documentation/concepts/resumability) + for more on why this is important #### `run` function @@ -268,8 +261,8 @@ If you take a look at the `usage.ts` file above, you can see how this task is us The `run` function is the main function that will be called when the task is run. It's an async function that takes up to 5 arguments: - The input params that were passed to the task. This is the second argument to - the `getForm` function in the example above. + The input params that were passed to the task. This is the second argument to the `getForm` + function in the example above. @@ -286,9 +279,9 @@ The `run` function is the main function that will be called when the task is run - If for some reason you need to access the auth object that was used to seed - the SDK client, you can access it here. The `AuthenticatedTask` generic type - takes an optional 4th type parameter that allows you to specify the auth type + If for some reason you need to access the auth object that was used to seed the SDK client, you + can access it here. The `AuthenticatedTask` generic type takes an optional 4th type parameter that + allows you to specify the auth type #### `init` function @@ -296,8 +289,8 @@ The `run` function is the main function that will be called when the task is run The `init` function is used to initialize the task. It's a synchronous function that takes a single argument: - The input params that were passed to the task. This is the second argument to - the `getForm` function in the example above. + The input params that were passed to the task. This is the second argument to the `getForm` + function in the example above. #### `onError` function @@ -466,9 +459,7 @@ export const backgroundCreateCompletion: AuthenticatedTask< headers: { "Content-Type": "application/json", Authorization: redactString`Bearer ${auth.apiKey}`, - ...(auth.organization - ? { "OpenAI-Organization": auth.organization } - : {}), + ...(auth.organization ? { "OpenAI-Organization": auth.organization } : {}), }, body: JSON.stringify(params), } diff --git a/docs/sdk/crontrigger.mdx b/docs/sdk/crontrigger.mdx index 92953ea9b..e377a9582 100644 --- a/docs/sdk/crontrigger.mdx +++ b/docs/sdk/crontrigger.mdx @@ -23,8 +23,7 @@ A useful tool when writing CRON expressions is [crontab guru](https://crontab.gu - A CRON expression that defines the schedule. Note that the timezone used - is always UTC. + A CRON expression that defines the schedule. Note that the timezone used is always UTC. @@ -32,7 +31,7 @@ A useful tool when writing CRON expressions is [crontab guru](https://crontab.gu ```typescript 9am UTC everyday -new Job(client, { +client.defineJob({ id: "scheduled-job-1", name: "Scheduled Job 1", version: "0.1.1", @@ -51,7 +50,7 @@ new Job(client, { ``` ```typescript First day of month -new Job(client, { +client.defineJob({ id: "scheduled-job-2", name: "Scheduled Job 2", version: "0.1.1", diff --git a/docs/sdk/dynamicschedule/overview.mdx b/docs/sdk/dynamicschedule/overview.mdx index b3fd0b8bf..4525aac74 100644 --- a/docs/sdk/dynamicschedule/overview.mdx +++ b/docs/sdk/dynamicschedule/overview.mdx @@ -39,7 +39,7 @@ const dynamicSchedule = new DynamicSchedule(client, { }); //2. create a Job that is attached to the dynamic schedule -new Job(client, { +client.defineJob({ id: "user-dynamicinterval", name: "User Dynamic Interval", version: "0.1.1", @@ -63,7 +63,7 @@ async function registerUserCronJob(userId: string, userSchedule: string) { } //5. Register inside other Jobs -new Job(client, { +client.defineJob({ id: "register-dynamicinterval", name: "Register Dynamic Interval", version: "0.1.1", diff --git a/docs/sdk/dynamictrigger/overview.mdx b/docs/sdk/dynamictrigger/overview.mdx index 159dd44fa..c6626d90a 100644 --- a/docs/sdk/dynamictrigger/overview.mdx +++ b/docs/sdk/dynamictrigger/overview.mdx @@ -40,7 +40,7 @@ const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, { }); //2. create a Job that is attached to the dynamic trigger -new Job(client, { +client.defineJob({ id: "listen-for-dynamic-trigger", name: "Listen for dynamic trigger", version: "0.1.1", @@ -50,9 +50,7 @@ new Job(client, { }, run: async (payload, io, ctx) => { await io.slack.postMessage("Slack 📝", { - text: `New Issue opened on repo: ${ - payload.issue.html_url - }. \n\n${JSON.stringify(ctx)}`, + text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`, channel: "C04GWUTDC3W", }); }, @@ -68,7 +66,7 @@ async function registerRepo(owner: string, repo: string) { } //4. Register inside other Jobs -new Job(client, { +client.defineJob({ id: "new-repo", name: "New repo", version: "0.1.1", diff --git a/docs/sdk/eventtrigger.mdx b/docs/sdk/eventtrigger.mdx index d04e1292a..fe94f8c96 100644 --- a/docs/sdk/eventtrigger.mdx +++ b/docs/sdk/eventtrigger.mdx @@ -53,7 +53,7 @@ You can have multiple Jobs that subscribe to the same event, they will all trigg ```typescript eventTrigger() //this Job subscribes to an event called new.user -new Job(client, { +client.defineJob({ id: "job-2", name: "Second job", version: "0.0.1", diff --git a/docs/sdk/intervaltrigger.mdx b/docs/sdk/intervaltrigger.mdx index b5874928b..727317cb6 100644 --- a/docs/sdk/intervaltrigger.mdx +++ b/docs/sdk/intervaltrigger.mdx @@ -30,7 +30,7 @@ If you wish to Run a Job at an exact time or less frequently than once pr day yo ```typescript Every 5 minutes -new Job(client, { +client.defineJob({ id: "scheduled-job-1", name: "Scheduled Job 1", version: "0.1.1", diff --git a/docs/sdk/io/backgroundfetch.mdx b/docs/sdk/io/backgroundfetch.mdx index 35a85c328..9dfb442bb 100644 --- a/docs/sdk/io/backgroundfetch.mdx +++ b/docs/sdk/io/backgroundfetch.mdx @@ -20,9 +20,8 @@ This is used inside the OpenAI Integration for Tasks like `backgroundCreateChatC The HTTP method to use for the request. - Any headers to send with the request. Note that you can use - [redactString](sdk/redactString) to prevent sensitive information from being - stored (e.g. in the logs), like API keys and tokens. + Any headers to send with the request. Note that you can use [redactString](sdk/redactString) to + prevent sensitive information from being stored (e.g. in the logs), like API keys and tokens. The body of the request. @@ -84,19 +83,16 @@ An individual retrying strategy can be one of two types: - The `headers` strategy retries the request using info from the response - headers. + The `headers` strategy retries the request using info from the response headers. - The header to use to determine the maximum number of times to retry the - request. + The header to use to determine the maximum number of times to retry the request. The header to use to determine the number of remaining retries. - The header to use to determine the time when the number of remaining retries - will be reset. + The header to use to determine the time when the number of remaining retries will be reset. @@ -111,7 +107,7 @@ A `Promise` that resolves after the specified amount of time. ```typescript backgroundFetch example -new Job(client, { +client.defineJob({ id: "background-fetch-job", name: "Background fetch Job", version: "0.0.1", diff --git a/docs/sdk/io/registercron.mdx b/docs/sdk/io/registercron.mdx index 52a7e9b1a..ab9bc9708 100644 --- a/docs/sdk/io/registercron.mdx +++ b/docs/sdk/io/registercron.mdx @@ -19,9 +19,8 @@ description: "`io.registerCron()` allows you to register a [DynamicSchedule](/sd - A CRON expression that defines the schedule. A useful tool when writing CRON - expressions is [crontab guru](https://crontab.guru). Note that the timezone - used is UTC. + A CRON expression that defines the schedule. A useful tool when writing CRON expressions is + [crontab guru](https://crontab.guru). Note that the timezone used is UTC. @@ -32,8 +31,7 @@ description: "`io.registerCron()` allows you to register a [DynamicSchedule](/sd A Promise that resolves to an object with the following fields: - A unique id for the interval. This is used to identify and unregister the - interval later. + A unique id for the interval. This is used to identify and unregister the interval later. Any additional metadata about the interval. @@ -63,7 +61,7 @@ A Promise that resolves to an object with the following fields: ```typescript -new Job(client, { +client.defineJob({ id: "my-job", name: "My job", version: "0.1.1", diff --git a/docs/sdk/io/registerinterval.mdx b/docs/sdk/io/registerinterval.mdx index 0ed211afc..48ef44bbf 100644 --- a/docs/sdk/io/registerinterval.mdx +++ b/docs/sdk/io/registerinterval.mdx @@ -30,8 +30,7 @@ description: "`io.registerInterval()` allows you to register a [DynamicSchedule] A Promise that resolves to an object with the following fields: - A unique id for the interval. This is used to identify and unregister the - interval later. + A unique id for the interval. This is used to identify and unregister the interval later. Any additional metadata about the interval. @@ -61,7 +60,7 @@ A Promise that resolves to an object with the following fields: ```typescript -new Job(client, { +client.defineJob({ id: "my-job", name: "My job", version: "0.1.1", diff --git a/docs/sdk/io/registertrigger.mdx b/docs/sdk/io/registertrigger.mdx index 6d71fa2c5..e3bf1fb02 100644 --- a/docs/sdk/io/registertrigger.mdx +++ b/docs/sdk/io/registertrigger.mdx @@ -8,16 +8,13 @@ description: "`io.registerTrigger()` allows you to register a [DynamicTrigger](/ - A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's - attached. + A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's attached. - A unique id for the registration. This is used to identify and unregister - later. + A unique id for the registration. This is used to identify and unregister later. - The params for the DynamicTrigger. These will vary depending on the type of - the DynamicTrigger. + The params for the DynamicTrigger. These will vary depending on the type of the DynamicTrigger. ## Returns @@ -25,8 +22,7 @@ description: "`io.registerTrigger()` allows you to register a [DynamicTrigger](/ A Promise that resolves to an object with the following fields: - A unique id for the registration. This is used to identify and unregister - later. + A unique id for the registration. This is used to identify and unregister later. The key of the registration. @@ -43,7 +39,7 @@ const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, { }); //2. create a Job that is attached to the dynamic trigger -new Job(client, { +client.defineJob({ id: "listen-for-dynamic-trigger", name: "Listen for dynamic trigger", version: "0.1.1", @@ -53,15 +49,13 @@ new Job(client, { }, run: async (payload, io, ctx) => { await io.slack.postMessage("Slack 📝", { - text: `New Issue opened on repo: ${ - payload.issue.html_url - }. \n\n${JSON.stringify(ctx)}`, + text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`, channel: "C04GWUTDC3W", }); }, }); -new Job(client, { +client.defineJob({ id: "new-repo", name: "New repo", version: "0.1.1", diff --git a/docs/sdk/io/runtask.mdx b/docs/sdk/io/runtask.mdx index c4df78a1a..6e6ca4ba3 100644 --- a/docs/sdk/io/runtask.mdx +++ b/docs/sdk/io/runtask.mdx @@ -122,7 +122,7 @@ A Promise that resolves with the returned value of the callback. ```typescript Run a task -new Job(client, { +client.defineJob({ id: "alert-on-new-github-issues", name: "Alert on new GitHub issues", version: "0.1.1", @@ -155,7 +155,7 @@ new Job(client, { ``` ```typescript onError callback -new Job(client, { +client.defineJob({ id: "custom-error-handling", name: "Custom Error handling", version: "0.1.1", diff --git a/docs/sdk/io/sendevent.mdx b/docs/sdk/io/sendevent.mdx index 89222eec3..b68c57585 100644 --- a/docs/sdk/io/sendevent.mdx +++ b/docs/sdk/io/sendevent.mdx @@ -13,8 +13,7 @@ Use [eventTrigger()](/sdk/eventtrigger) on a Job to listen for events. - The number of seconds to wait. This can be very long, serverless timeouts are - not an issue. + The number of seconds to wait. This can be very long, serverless timeouts are not an issue. @@ -27,7 +26,7 @@ Use [eventTrigger()](/sdk/eventtrigger) on a Job to listen for events. ```typescript Send an event //this Job sends an event that triggers the second job -new Job(client, { +client.defineJob({ id: "job-1", name: "First job", version: "0.0.1", @@ -45,7 +44,7 @@ new Job(client, { }, }); -new Job(client, { +client.defineJob({ id: "job-2", name: "Second job", version: "0.0.1", diff --git a/docs/sdk/io/try.mdx b/docs/sdk/io/try.mdx index dea7ca7a6..e44bf16c3 100644 --- a/docs/sdk/io/try.mdx +++ b/docs/sdk/io/try.mdx @@ -32,7 +32,7 @@ You have two options: ```typescript Using io.try() -new Job(client, { +client.defineJob({ id: "get-repo-info", name: "GitHub get repo info", version: "0.1.0", diff --git a/docs/sdk/io/unregistercron.mdx b/docs/sdk/io/unregistercron.mdx index 7b7a96a5f..7f750efef 100644 --- a/docs/sdk/io/unregistercron.mdx +++ b/docs/sdk/io/unregistercron.mdx @@ -8,12 +8,11 @@ description: "`io.unregisterCron()` allows you to unregister a [DynamicSchedule] - A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's - attached to on a regular interval. + A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's attached to on a regular + interval. - A unique id for the schedule. This is used to identify and unregister the - schedule later. + A unique id for the schedule. This is used to identify and unregister the schedule later. ## Returns @@ -27,7 +26,7 @@ A Promise with the following shape: ```typescript -new Job(client, { +client.defineJob({ id: "unregister-job", name: "Unregister dynamic schedule", version: "0.1.1", diff --git a/docs/sdk/io/unregisterinterval.mdx b/docs/sdk/io/unregisterinterval.mdx index 75f53c58a..86d0ff9bd 100644 --- a/docs/sdk/io/unregisterinterval.mdx +++ b/docs/sdk/io/unregisterinterval.mdx @@ -8,12 +8,11 @@ description: "`io.unregisterInterval()` allows you to unregister a [DynamicSched - A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's - attached to on a regular interval. + A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's attached to on a regular + interval. - A unique id for the interval. This is used to identify and unregister the - interval later. + A unique id for the interval. This is used to identify and unregister the interval later. ## Returns @@ -27,7 +26,7 @@ A Promise with the following shape: ```typescript -new Job(client, { +client.defineJob({ id: "unregister-job", name: "Unregister dynamic schedule", version: "0.1.1", diff --git a/docs/sdk/io/unregistertrigger.mdx b/docs/sdk/io/unregistertrigger.mdx index d85319fc3..ca47e9541 100644 --- a/docs/sdk/io/unregistertrigger.mdx +++ b/docs/sdk/io/unregistertrigger.mdx @@ -8,8 +8,7 @@ description: "`io.unregisterTrigger()` allows you to unregister a [DynamicTrigge - A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's - attached to. + A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's attached to. A unique id for the trigger. This is used to identify and unregister it later. @@ -26,7 +25,7 @@ A Promise with the following shape: ```typescript -new Job(client, { +client.defineJob({ id: "unregister-job", name: "Unregister dynamic trigger", version: "0.1.1", diff --git a/docs/sdk/istriggererror.mdx b/docs/sdk/istriggererror.mdx index 4ac3de10a..b1547c12a 100644 --- a/docs/sdk/istriggererror.mdx +++ b/docs/sdk/istriggererror.mdx @@ -32,7 +32,7 @@ You must rethrow the error if this function returns `true`. ```typescript Using io.try() -new Job(client, { +client.defineJob({ id: "get-repo-info", name: "GitHub get repo info", version: "0.1.0", diff --git a/docs/sdk/job.mdx b/docs/sdk/job.mdx index 9d1c629b2..a7031662e 100644 --- a/docs/sdk/job.mdx +++ b/docs/sdk/job.mdx @@ -12,7 +12,7 @@ By far the most important thing to understand is the constructor. ```ts cronTrigger -new Job(client, { +client.defineJob({ id: "slack-kpi-summary", name: "Slack kpi summary", version: "0.1.1", @@ -35,7 +35,7 @@ new Job(client, { ``` ```ts webhook -new Job(client, { +client.defineJob({ id: "github-integration-on-issue", name: "GitHub Integration - On Issue", version: "0.1.0", @@ -52,7 +52,7 @@ new Job(client, { ``` ```ts event -new Job(client, { +client.defineJob({ id: "openai-joke", name: "OpenAI Joke", version: "0.0.1", @@ -66,18 +66,15 @@ new Job(client, { openai, }, run: async (payload, io, ctx) => { - const joke = await io.openai.backgroundCreateChatCompletion( - "generate-jokes", - { - model: "gpt-3.5-turbo", - messages: [ - { - role: "user", - content: payload.jokePrompt, - }, - ], - } - ); + const joke = await io.openai.backgroundCreateChatCompletion("generate-jokes", { + model: "gpt-3.5-turbo", + messages: [ + { + role: "user", + content: payload.jokePrompt, + }, + ], + }); return joke.choices; }, @@ -89,8 +86,7 @@ new Job(client, { ## Parameters - An instance of [TriggerClient](/sdk/triggerclient) that is used to send events - to the Trigger API. + An instance of [TriggerClient](/sdk/triggerclient) that is used to send events to the Trigger API.