-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e305c8e
commit 53fe187
Showing
1 changed file
with
10 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,18 @@ | ||
# `@defer/client` | ||
|
||
`defer` is your favorite background tasks handler to offload your JavaScript API. | ||
Zero infrastructure Node.js background jobs. | ||
|
||
---- | ||
|
||
## Install | ||
## Get started | ||
|
||
- [Next.js Quickstart](https://docs.defer.run/quickstart/next/) | ||
- [Express/Koa/hapi Quickstart](https://docs.defer.run/quickstart/express-koa-hapi/) | ||
|
||
```sh | ||
yarn add @defer/client | ||
|
||
# or | ||
## API documentation | ||
|
||
npm i @defer/client | ||
``` | ||
|
||
## Configuration | ||
|
||
Make sure to define the `DEFER_TOKEN` environment variable in production/staging environments. | ||
|
||
## Usage | ||
|
||
|
||
### 1. Define your background function | ||
|
||
A background function should be a unique default export of a file placed in `background-functions/` folder. | ||
|
||
```ts | ||
import { defer } from "@defer/client"; | ||
|
||
function importContacts(intercomId: string) { | ||
// import contacts from Hubspot and insert them in the database | ||
} | ||
|
||
export default defer(importContacts) | ||
``` | ||
|
||
|
||
### 2. Call your background function | ||
|
||
For a seamless integration with your project, we recommend to install our `@defer.run/babel` babel plugin. | ||
Our babel plugin will replace all usage of defer functions to client pushes: | ||
|
||
```ts | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import sentToIntercom from "../../background-functions/importContacts"; | ||
|
||
export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
// will be executed in the background | ||
await importContacts(currentUser.intercomId); | ||
|
||
res.status(200).json({ name: "John Doe" }); | ||
} | ||
``` | ||
- [Configuration options: retries, concurrency](https://docs.defer.run/features/retries-concurrency/) | ||
- [Delayed Function](https://docs.defer.run/features/delay/) | ||
- [CRON](https://docs.defer.run/features/cron/) | ||
- [Workflows](https://docs.defer.run/features/workflows/) |