Functions can be registered in three ways:
- With typed request: Provides type inference and example JSON for the Hub.
- Without request: For tasks that don't require input data.
- With primitive request: For simple inputs like a single string or number.
Use .withRequest() to define the payload shape and .handle() to define the execution logic.
// Typed request
sdk.function('ProcessOrder', {
priority: TickerTaskPriority.High,
maxConcurrency: 3,
requestType: 'OrderRequest',
})
.withRequest({ orderId: 0, customerId: '', items: [''], total: 0 })
.handle(async (ctx, signal) => {
ctx.request.orderId; // number
ctx.request.customerId; // string
});
// Without request
sdk.function('DatabaseCleanup', {
cronExpression: '0 0 3 * * *',
priority: TickerTaskPriority.LongRunning,
})
.handle(async (ctx, signal) => {
console.log(`Running cleanup for ${ctx.functionName}`);
});
// Primitive request
sdk.function('ResizeImage')
.withRequest('default-url')
.handle(async (ctx, signal) => {
console.log(ctx.request); // string
});