Functions
A Function in an Andera Worker is a unit of business logic that can be called via the /task
endpoint. Functions are defined in the src/functions/
directory and are auto-discovered at startup.
Structure of a Function
A function is defined using defineFunction
from @andera-top/worker-core
. Each function specifies:
params
: The input parameters and their typesconfig
: Optional configuration (timeout, retries, streaming, etc.)handler
: The async function that implements the logic
Parameter Schema Options
Each parameter in params
can have the following options:
Option | Type | Description | Default |
---|---|---|---|
type | string | Parameter type: string , number , boolean , object , array | 'string' |
required | boolean | Whether the parameter is required | true |
default | any | Default value if not provided | 'World' |
enum | any[] | Allowed values (for enums) | ["foo", "bar"] |
pattern | string | Regex pattern (for strings) | '^\w+$' |
min | number | Minimum value/length (number, string, array) | 1 |
max | number | Maximum value/length (number, string, array) | 10 |
tip
Use enum
, pattern
, min
, and max
to validate and restrict input values.
Function Config Options
The config
object can have the following options:
Option | Type | Description | Default |
---|---|---|---|
timeout | number | Timeout in ms for the function | 5000 |
maxRetries | number | Number of times to retry on failure | 1 |
supportsStreaming | boolean | Whether the function supports streaming mode | false |
logResult | boolean | Whether to log the function result (default: true). This avoids polluting logs with heavy data or secrets. | false |
Example
import { defineFunction } from '@andera-top/worker-core'
import { log } from '@andera-top/worker-core/dist/utils/logger'
export const hello = defineFunction({
params: {
name: { type: 'string', required: false, default: 'World' }
},
config: {
timeout: 5000,
maxRetries: 0,
supportsStreaming: true
},
handler: async (params, context) => {
log('[HELLO]', `Hello function called for ${params.name}`)
if (context?.stream) {
context.stream(`Hello ${params.name}!`)
return { streaming: true }
}
return { message: `Hello ${params.name}!` }
}
})
Logging in Functions
You can use the logging utilities to trace function calls, warnings, or errors (see Logging):
import { log, warn, error } from '@andera-top/worker-core/dist/utils/logger'
log('[MYFUNC]', 'Function called')
warn('[MYFUNC]', 'Something might be wrong')
error('[MYFUNC]', 'Something went wrong')
Creating a New Function
- Create a new file in
src/functions/
, e.g.myFunction.ts
. - Export your function using
defineFunction
as shown above. - The function will be auto-registered at startup.
Testing a Function
You can test your function by sending a POST request to the /task
endpoint of your Worker:
curl -X POST http://localhost:3000/task \
-H 'Authorization: <AUTH_KEY>' \
-H 'Content-Type: application/json' \
-d '{
"function": "myFunction",
"input": { "name": "Function" },
"contract": 1,
"mode": "sync"
}'
The response will contain the result of your function.