const client = new HttpClient();
const data = await client.get('https://my-api.com/data').json();
const client = new HttpClient();
const request = {
id: 1,
name: 'test'
};
await client.post('https://my-api.com/data', { json: request }).execute();
@injectable()
class MyClass {
constructor(
@inject(IHttpClient$) private readonly _client: IHttpClient
) {}
}
// myApiInterceptor.ts
@extension({ name: 'MyApiInterceptor' })
export class MyApiInterceptor extends HttpInterceptor<ApiClientOptions> {
override async enhanceOptions(
context: HttpInterceptorOptionsContext<ApiClientOptions>
): Promise<void> {
const { options } = context;
options.baseURL = 'https://my-api.com';
}
}
// registrator.ts
extensionContainer.registerExtension({
extension: MyApiInterceptor,
when: whenClientTypeIs(IApiClient$)
})
const data = await client.get('https://my-api.com/data', {
hooks: [{
beforeRequest: async (ctx) => {
ctx.request.headers.set('my-header', 'my-header-value');
}
}]
}).typedJson();
const context = new HttpHooksContext([{
beforeRequest: async (ctx) => {
ctx.request.headers.set('my-header', 'my-header-value');
}
}]);
await HttpHooksContext
.create(context)
.run(async () => await someLogicWithHttpRequestInside());
container
.bind(IHttpInterceptorHooksProvider$)
.toFunction(() => [
{
beforeRequest: async ctx => {
ctx.request.headers.set('my-header', 'my-header-value');
}
}
])
.whenAnyAncestorIs(IOperationService$);
const context = new CancellationContext();
CancellationContext
.create(context)
.run(() => await someLogicWithHttpRequestInside());
context.cancel();
Generated using TypeDoc