3.1.2 • Published 2 months ago

@talend/http v3.1.2

Weekly downloads
439
License
Apache-2.0
Repository
github
Last release
2 months ago

@talend/http

@talend/http is a fetch wrapper module to handle CSRF token.

Configuration

Global configuration

If you want to provide a global configuration for the calls, you can use this method :

import { setDefaultConfig } from '@talend/http/config';

setDefaultConfig({ credentials: 'same-origin' });

Default Language

If you want to provide the default language to be embedded in the header you can use this method :

import { setDefaultLanguage } from '@talend/http/config';

setDefaultLanguage('fr');

Usage

Parameters

  • url : the current endpoint targeted by the request
  • config : specific fetch configuration for this call https://github.github.io/fetch/
  • payload : payload embedded in the call

Methods available

  • get(url, config)
  • delete(url, config)
  • post(url, payload, config)
  • put(url, payload, config)
  • patch(url, payload, config)
import { http } from '@talend/http';

async function test() {
	const response = await http.get('/api/v1/my-resource');
}

Interceptors

You can add global response interceptors to catch or modify responses before resolve.

import { addHttpResponseInterceptor, http, HTTP_METHODS } from '@talend/http';
import type { TalendRequest } from '@talend/http';

addHttpResponseInterceptor('my-interceptor', async (response: Response, request: TalendRequest) => {
	if (request.method === HTTP_METHODS.GET) {
		// your custom logic here
	}

	return response;
});

You can add multiple interceptors. Each will be called in the order of registration and will receive the same request parameter, but response parameter will be the one returned by previous interceptor. If interceptor returns void, then it'll return received response.

Once your interceptor is not needed anymore, you can unregister it with removeHttpResponseInterceptor function of @talend/http package.

You can identify some requests in interceptor by using context property in fetch function config:

import { addHttpResponseInterceptor, http, HTTP_METHODS } from '@talend/http';

http.get('/api/v1/data', { context: { intercept: true } });

addHttpResponseInterceptor('my-interceptor', async (response: Response, request: TalendRequest) => {
	const { context } = request;
	if (request.method === HTTP_METHODS.GET && context.intercept) {
		// your custom logic here
	}
	return response;
});
3.1.2

2 months ago

3.1.1

5 months ago

3.1.0

6 months ago

3.0.0

7 months ago

2.3.0

10 months ago

2.4.0

7 months ago

2.2.0

1 year ago

2.1.0

2 years ago

1.2.0

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.6

2 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.0-alpha

4 years ago