REST API
Build REST APIs with ease.
The REST API Module is a module that allows you to build REST APIs with ease.
modules::rest_api::RestApiModuleSample Configuration
- class: modules::api::RestApiModule
config:
port: 3111
host: 0.0.0.0
cors:
allowed_origins:
- http://localhost:3000
- http://localhost:5173
allowed_methods:
- GET
- POST
- PUT
- DELETE
- OPTIONSConfiguration
Trigger Type
This Module add a new Trigger Type: api.
Sample code
bridge.registerTrigger({
trigger_type: 'api',
functionPath,
config: {
api_path: '/api/v1/users',
http_method: 'GET',
},
})Request & Response Objects
ApiRequest
When an API trigger fires, the function receives an ApiRequest object:
ApiResponse
Functions must return an ApiResponse object:
Request Lifecycle
Example Handler
import { ApiRequest, ApiResponse } from '@iii-dev/sdk'
async function getUser(req: ApiRequest) {
const userId = req.path_params.id
const user = await database.findUser(userId)
return {
status: 200,
body: { user },
headers: {
'Content-Type': 'application/json',
},
}
}
bridge.registerFunction({
function_path: 'api.getUser',
handler: getUser,
})
bridge.registerTrigger({
trigger_type: 'api',
function_path: 'api.getUser',
config: {
api_path: '/users/:id',
http_method: 'GET',
},
})