@salesforce/b2c-tooling-sdk / cli / HttpMiddlewareHook
Type Alias: HttpMiddlewareHook
HttpMiddlewareHook =
Hook<"b2c:http-middleware">
Defined in: packages/b2c-tooling-sdk/src/cli/hooks.ts:222
Hook type for b2c:http-middleware.
Implement this hook in your oclif plugin to provide custom HTTP middleware that will be applied to all API clients (OCAPI, SLAS, WebDAV, etc.).
The hook is called during command initialization, after flags are parsed but before any API clients are created.
Plugin Registration
Register the hook in your plugin's package.json:
json
{
"oclif": {
"hooks": {
"b2c:http-middleware": "./dist/hooks/http-middleware.js"
}
}
}Hook Context
Inside the hook function, you have access to:
this.config- oclif Config objectthis.debug(),this.log(),this.warn(),this.error()- logging methods
Example
typescript
import type { HttpMiddlewareHook } from '@salesforce/b2c-tooling-sdk/cli';
import type { HttpMiddlewareProvider } from '@salesforce/b2c-tooling-sdk/clients';
const hook: HttpMiddlewareHook = async function(options) {
this.debug('Registering custom middleware');
const metricsProvider: HttpMiddlewareProvider = {
name: 'metrics-collector',
getMiddleware(clientType) {
return {
onRequest({ request }) {
(request as any)._startTime = Date.now();
return request;
},
onResponse({ request, response }) {
const duration = Date.now() - (request as any)._startTime;
console.log(`[${clientType}] ${request.method} ${request.url} ${response.status} ${duration}ms`);
return response;
},
};
},
};
return { providers: [metricsProvider] };
};
export default hook;