Middleware based
Configure APIs using middleware to connect to database, authenticate, validate, cors.
Middleware is the fundamental part of Triton.js, normally they are functions that are called before calling the main function that will deal with the business rule of the application. In Triton.js the middleware is represented by a class with two main lifecycles.
preThe method is called before executing the main function.postThe method is called before the request is resolved. Good for destroying instances or disconnecting from some service in serverless functions.
They are commonly used for validation of payload schema, cors, headers, authentication and for serverless functions as well as database connection.
import type {FleetRequest, FleetResponse} from '@fleetfn/types';
import {Middleware} from 'tritonjs/middleware';
class MyMiddleware extends Middleware {
name = 'MyMiddleware';
async pre(request: FleetRequest, response: FleetResponse) {
// some logic to check the request before executing the main function...
}
async post(request: FleetRequest, response: FleetResponse, payload: any) {
// some logic to clear state or cache before the request is resolved...
}
}
Defining middleware
Middlewares are defined in the configuration of each serverless function and are executed sequentially according to the order of declaration.
import MyMiddleware from '../path';
export default function handler() {
// ...
}
export const config = {
name: 'MyFunction',
middleware: [
new MyMiddleware(),
],
};
Following the example below, FirstMiddleware will be executed first and the next ones declared sequentially, if any middleware fails the execution is interrupted and Triton.js resolves the request with an error 500 if it is an unknown exception.
export const = {
middleware: [
new FirstMiddleware(),
new SecondMiddleware(),
new ThreeMiddleware(),
],
};
Exceptions
Middleware can interrupt sequential execution to prevent it from continuing to run the next middleware and executing the main function, throwing an exception during the execution of the pre method, when an unknown exception such as a TypeError error is thrown the execution is also interrupted .
There are two ways to throw an exception during the pre method call, asynchronous functions just need to throw a trown error during the method execution and synchronous functions need to call the next method which receives as a third parameter.
import type {FleetRequest, FleetResponse} from '@fleetfn/types';
import type {TritonNext} from 'tritonjs/middleware';
import {Middleware} from 'tritonjs/middleware';
// Synchronous function
class MySyncMiddleware extends Middleware {
name = 'MySyncMiddleware';
pre(request: FleetRequest, response: FleetResponse, next: TritonNext) {
next(new Error('Internal error'));
}
}
// Asynchronous function
class MyAsyncMiddleware extends Middleware {
name = 'MyAsyncMiddleware';
async pre(request: FleetRequest, response: FleetResponse) {
throw new Error('Internal error');
}
}
Unknown Exception
We recommended to read the Unknown Exception page.
Unknown exceptions, are know errors as a result of the payload schema validation or any other type of validation, when using the Triton.js exception class UnknownException the request response is a JSON structured allowing it to have a common pattern for type exceptions.
import type {FleetRequest, FleetResponse} from '@fleetfn/types';
import {Middleware} from 'tritonjs/middleware';
import {UnknownException} from 'tritonjs/exception';
class MyMiddleware extends Middleware {
name = 'MyMiddleware';
async pre(request: FleetRequest, response: FleetResponse) {
throw new UnknownException(
'parameter_unknown',
'Received unknown parameter state',
400
);
}
}