API / geotoolkit / http / AbstractHttpService / AbstractHttpService
http.AbstractHttpService.AbstractHttpService
Provide an abstract service for HTTP requests
Example
// Cancellation of request from external token
import {Cancel} from '@int/geotoolkit/util/Cancel';
...
const token = new Cancel();
http.get('/cancel', {
'cancel': token
}).then( (response) => { ... }, (error) => {
// TODO: Cancel
}).catch((error) => { ... });
setTimeout(() => {
token.cancel();
});Constructors
Methods
Methods
▸ addRequestInterceptor(onFulfilled, onRejected?): string
Add request interceptor
Example
// Add request interceptor
import {deepMergeObject} from '@int/geotoolkit/base';
http.addRequestInterceptor((options) => {
return new Promise((resolve, reject) => {
const newOptions = deepMergeObject({'headers': {
'Authorization': 'Bearer ' + token
}}, options, true);
resolve(newOptions);
});
}, (error) => Promise.reject(error)
);| Name | Type | Description |
|---|---|---|
onFulfilled | (options: any) => Promise<any> | on fulfilled function to be called in promise |
Optional onRejected | (reason: Error) => Promise<any> | on function to be called in promise if it is rejected |
string
id of the interceptor, which can be used to delete
▸ addResponseInterceptor(onFulfilled, onRejected?): string
Add response interceptor
Example
// Add response interceptor
http.addResponseInterceptor(function (response) {
return new Promise((resolve, reject) => {
// TODO with response
resolve(response);
});
}, (error) => Promise.reject(error) );| Name | Type | Description |
|---|---|---|
onFulfilled | (options: any) => Promise<any> | on fulfilled function to be called in promise |
Optional onRejected | (reason: Error) => Promise<any> | on function to be called in promise if it is rejected |
string
id of the interceptor, which can be used to delete
▸ Abstract clone(options?): AbstractHttpService
Create an instance of a new service with new default settings
| Name | Type | Description |
|---|---|---|
Optional options | Options | options |
▸ containsRequestInterceptor(id): boolean
Verify if request interceptor with specified id exists
| Name | Type | Description |
|---|---|---|
id | string | id of interceptor to verify |
boolean
▸ containsResponseInterceptor(id): boolean
Verify if response interceptor with specified id exists
| Name | Type | Description |
|---|---|---|
id | string | id of interceptor to verify |
boolean
▸ delete(url, options?): Promise<any>
Send delete request
| Name | Type | Description |
|---|---|---|
url | string | url |
Optional options | RequestOptions | options |
Promise<any>
▸ Protected Abstract dispatch(options?): Promise<any>
Dispatch request
See
- https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
- https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials|withCredentials
| Name | Type | Description |
|---|---|---|
Optional options | RequestOptions | options |
Promise<any>
▸ get(url, options?): Promise<any>
Send get request
Example
// make a simple GET request
http.get(url).then((response) => {
// Process data
}, (error) => {
// Error
});Example
// make GET request and transform output
http.get(url, {
'responsetype': 'json',
'transformresponse': (response) => {
if (response['data']['version'] != null) {
return Promise.resolve(response['data']);
}
return Promise.reject('Server error');
}
}).then((data) => {
// Process data
}, (error) => {
// Error
});| Name | Type | Description |
|---|---|---|
url | string | url |
Optional options | RequestOptions | options |
Promise<any>
▸ getClassName(): string
string
▸ getOptions(): Options
Gets a default options
options all options keys are case insensitive. It will be transformed to lowercase
▸ Protected getRequestInterceptors(): any[]
any[]
▸ Protected getResponseInterceptors(): any[]
any[]
▸ head(url, options?): Promise<any>
Send head request
| Name | Type | Description |
|---|---|---|
url | string | url |
Optional options | RequestOptions | options |
Promise<any>
▸ Protected mergeOptions<T>(options): T
| Name | Type |
|---|---|
T | extends Record<string, any> = Record<string, any> |
| Name | Type | Description |
|---|---|---|
options | T | options to merge |
T
merged options
▸ patch(url, data, options?): Promise<any>
Send patch request
| Name | Type | Description |
|---|---|---|
url | string | url |
data | any | data to be sent as the request message data. |
Optional options | RequestOptions | options |
Promise<any>
▸ post(url, data, options?): Promise<any>
Send post request
Example
// make a simple POST request
http.post(url, data).then((response) => {
// Process data
}, (error) => {
// Error
});| Name | Type | Description |
|---|---|---|
url | string | url |
data | any | data to be sent as the request message data. |
Optional options | RequestOptions | options |
Promise<any>
▸ put(url, data, options?): Promise<any>
Send put request
| Name | Type | Description |
|---|---|---|
url | string | url |
data | any | data to be sent as the request message data. |
Optional options | RequestOptions | options |
Promise<any>
▸ removeRequestInterceptor(id): boolean
Remove interceptor
| Name | Type | Description |
|---|---|---|
id | string | id of interceptor to remove |
boolean
true if interceptor is removed
▸ removeResponseInterceptor(id): boolean
Remove response interceptor
| Name | Type | Description |
|---|---|---|
id | string | id of interceptor to remove |
boolean
true if interceptor is removed
▸ request(options?): Promise<any>
Send request It is necessary to specify baseUrl, url or both to make request. This method return promise to execute query.
Response object contains:
- data: response data
- options: provided options
- status: as a rule response status of response
- statusText: status text
- request: an instance of request
See
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials|withCredentials
Example
// make a simple GET request
http.request({ method: 'get', 'url': url ).then((response) => {
// Process data
}, (error) => {
// Error
});| Name | Type | Description |
|---|---|---|
Optional options | RequestOptions | options all options keys are case insensitive |
Promise<any>
▸ setOptions(options?): void
Sets default options for the current instance of the service
| Name | Type | Description |
|---|---|---|
Optional options | Options | options all options keys are case insensitive |
void
▸ Static getClassName(): string
string