Skip to content

Commit ae10749

Browse files
feat: add schedule helper (#226)
* feat: add schedule helper * feat: dont alter handler behaviour Co-authored-by: Netlify Team Account 1 <[email protected]>
1 parent e916b78 commit ae10749

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ To use On-demand Builders, wrap your function handler with the `builder` functio
4949
export { handler }
5050
```
5151

52+
### Scheduled Functions (currently in beta)
53+
54+
To use Scheduled Functions, wrap your function handler with the `schedule` function.
55+
56+
- With JavaScript:
57+
58+
```js
59+
const { schedule } = require('@netlify/functions')
60+
61+
exports.handler = schedule('5 4 * * *', async () => {
62+
console.log("It's 04:05 AM!")
63+
})
64+
```
65+
66+
- With TypeScript:
67+
68+
```ts
69+
import { schedule } from '@netlify/functions'
70+
71+
export const handler = schedule("5 4 * * *", async () => {
72+
console.log("It's 04:05 AM!")
73+
})
74+
```
75+
5276
### TypeScript typings
5377

5478
This module exports typings for authoring Netlify Functions in TypeScript.

src/lib/schedule.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Handler } from '../function'
2+
3+
/**
4+
* Declares a function to run on a cron schedule.
5+
* Not reachable via HTTP.
6+
*
7+
* @example
8+
* ```
9+
* export const handler = cron("5 4 * * *", async () => {
10+
* // ...
11+
* })
12+
* ```
13+
*
14+
* @param schedule expressed as cron string. see https://crontab.guru for help.
15+
* @param handler
16+
* @see https://docs.netlify.com/functions/<schedule>
17+
* @tutorial https://announcement-blogpost
18+
*/
19+
const schedule = (cron: string, handler: Handler): Handler => handler
20+
21+
export { schedule }

src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { builder } from './lib/builder'
2+
export { schedule } from './lib/schedule'
23
export * from './function'

0 commit comments

Comments
 (0)