UNPKG

@unito/integration-sdk

Version:

Integration SDK

63 lines (62 loc) 3.21 kB
import { HandlersInput } from './handler.js'; type Options = { port?: number; }; /** * Main class for the Integration SDK providing an abstraction layer between the Integration's Graph definition * and the underlying HTTP server. * * An `Integration` instance can have multiple handlers configured to handle different routes. Upon receiving a request, * the Integration will parse the request to extract meaninful information, match the request to the appropriate handler * method and forward that information in the form a {@link Context} object. * The Integration also offer standardized error handling and logging to help you build a robust * and reliable Integration. * * See our {@link https://dev.unito.io/docs/ | documentation} for more examples on how to build an integration. */ export default class Integration { private handlers; private instance; private port; /** * Creates a new Integration instance with default port set to 9200. * * @param options The {@link Options} to configure the Integration instance. Can be used to override the default port. */ constructor(options?: Options); /** * Adds a group of common handlers to the integration. * * Handlers added to the integration can be one of the following: * - `ItemHandlers`: A group of handlers defining the implementation of the Operations available for a given item. * - `CredentialAccountHandlers`: A handler returning the CredentialAccount linked to the caller's credentials. * - `ParseWebhookHandlers`: A handler parsing the content of an incoming webhook. * - `WebhookSubscriptionHandlers`: A handler subscribing or unsubscribing to a particular webhook. * - `AcknowledgeWebhookHandlers`: A handler acknowledging the reception of a webhook. * * To accomodate the fact that ItemHandlers may specify multiple operations, some at the collection level, some at the * item level, we need a way to define the route for each of these operations. * To achieve this, we assume that if the last part of the path is a variable, then it is the item identifier. * * @example The following path: `/trainer/:trainerId/pokemons/:pokemonId` will lead to the following * routes: * - getCollection will be called for `GET /trainer/:trainerId/pokemons/` requests * - getItem will be called for `GET /trainer/:trainerId/pokemons/:pokemonId` requests * - createItem will be called for `POST /trainer/:trainerId/pokemons/` requests * - updateItem will be called for `PATCH /trainer/:trainerId/pokemons/:pokemonId` requests * - deleteItem will be called for `DELETE /trainer/:trainerId/pokemons/:pokemonId` requests * * @param path The path to be used as Route for the handlers. * @param handlers The Handlers definition. */ addHandler(path: string, handlers: HandlersInput): void; /** * Starts the server and listens on the specified port (default to 9200). * * @remarks * This function should be called after all the handlers have been added to the integration * and any other configuration is completed. */ start(): void; } export {};