Middleware
The Apophis Middleware system is a highly generic and extensible framework to allow third parties to hook into all sorts of procedures.
Registering Middleware
As an Apophis SDK consumer, you will only ever need to add middlewares like so:
import { Apophis, DefaultCosmosMiddlewares } from '@apophis-sdk/cosmos';
Apophis.use(DefaultCosmosMiddlewares);This will register your desired middlewares. You will rarely ever need to interact with the middleware system directly.
De-Duping
When registering the same middleware twice, either directly or inadvertently through bundled middleware systems defined by another library, only the first instance of this middleware will be registered. The order in which middlewares are registered matters. While it generally shouldn't cause any issues, this de-duplication could potentially be a source of confusion. Thus, all bundled middleware should also be exported in isolation for troubleshooting purposes.
Implementing Middlewares
Middlewares are arbitrary multi-level objects that are registered in a global array and executed by some strategy defined at the callsite. If a middleware does not implement a method, it is automatically skipped. The following examples are valid middlewares:
import { type MiddlewareImpl } from '@apophis-sdk/core/middleware.js';
const MyMiddleware1 = {
addresses: {
alias(address: string): string {
return address;
},
},
} satisfies MiddlewareImpl;
const MyMiddleware2: MiddlewareImpl = {
addresses: {
resolve(address: string): string {
return address;
},
},
encoding: {
encode(network: NetworkConfig, encoding: string, value: unknown) {
if (network.ecosystem !== 'cosmos') return;
if (encoding !== 'json') return;
return JSON.stringify(value);
},
},
};
const MyMiddleware3 = {} satisfies MiddlewareImpl;Pipelines
Callsites of middlewares must specify the strategy by which to execute the middleware stack. There are various strategies implemented by the internal MiddlewarePipeline class, which adheres to the following interface:
KP is the key path to the middleware and serves to extract the correct function signature. Following is an example of how to use the mw function which produces such a MiddlewarePipeline:
Middleware Stack
The mw method exposes some additional properties. One of these properties is mw.stack, which is an array of all registered middlewares. If necessary, you can inspect or even alter the stack:
Last updated