Integration
The Integration
class allows you to create a new integration. Each Integration powers its own web server, on which
multiple handlers can be attached.
Methods
start()
Starts the Integration server, which will expose it as a web server on port 9200. Must be done as the last step, once everything has been configured.
integration.start();
addHandler()
Exposes a handler on a path. The web server will automatically route incoming requests to the appropriate handler action.
integration.addHandler('/', { ... });
Example
A typical integration will look like this;
import { Integration } from '@unito/integration-sdk';
const integration = new Integration();
integration.addHandler('/', {});
integration.addHandler('/me', {});
integration.start();
The above would expose a web server on port 9200, with handlers defined for /
and /me
. As for the second
parameter, it is of type HandlersInput
— you'll learn more about it in the
Handlers section.
$ npm run dev
> [email protected] dev
> tsx watch --no-warnings src/index.ts
Mounting handler at path /
Enabling GET /
Mounting handler at path /me
Enabling GET /me
Server started on port 9200.
A path can also contain identifiers, which will be used to map to the proper handler.
integration.addHandler('/containers/:containerId/items/:itemId', { getItem, getCollection });
Given the above example, a call to /containers/1/items/
will automatically map to collection handlers, such as
getCollection, while /containers/1/items/2
will map to individual item handlers, such as getItem. In both cases,
you'll be able to extract these identifiers through the params property of each context.