@aircall/http
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -6,2 +6,14 @@ # Change Log | ||
# [0.3.0](http://bitbucket.org/aircall/front-end-modules/compare/@aircall/http@0.2.2...@aircall/http@0.3.0) (2021-03-19) | ||
### Features | ||
* **http:** add header prop to service options ([a4edae3](http://bitbucket.org/aircall/front-end-modules/commits/a4edae3708591389bf67bbd081ac9a5d057c34ff)) | ||
* **http:** replace searchParams with full axios config ([82158dc](http://bitbucket.org/aircall/front-end-modules/commits/82158dc1fc602bfc61e8c2513f20292157ae9ee1)) | ||
## [0.2.2](http://bitbucket.org/aircall/front-end-modules/compare/@aircall/http@0.2.1...@aircall/http@0.2.2) (2020-11-20) | ||
@@ -8,0 +20,0 @@ |
import { AxiosError, AxiosPromise, AxiosRequestConfig } from 'axios'; | ||
import { HttpServiceOptions, RequestLogPayload, SearchParams, HTTP_LOG_TYPE } from './typing/HttpService'; | ||
import { HttpServiceOptions, RequestLogPayload, HTTP_LOG_TYPE } from './typing/HttpService'; | ||
declare class HttpService { | ||
@@ -34,15 +34,8 @@ private options; | ||
handleError: (error: AxiosError<any>) => Error; | ||
get(path: string, config?: AxiosRequestConfig): AxiosPromise<object>; | ||
post(path: string, payload?: {}, config?: AxiosRequestConfig): AxiosPromise<object>; | ||
patch(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<object>; | ||
put(path: string, payload?: {}, config?: AxiosRequestConfig): Promise<object>; | ||
delete(path: string, config?: AxiosRequestConfig): Promise<object>; | ||
/** | ||
* Concatenate the path with the given search params | ||
* @param path | ||
* @param searchParams | ||
*/ | ||
getPathWithParams(path: string, searchParams?: SearchParams): string; | ||
get(path: string, searchParams?: SearchParams): AxiosPromise<object>; | ||
post(path: string, payload?: {}, searchParams?: SearchParams): AxiosPromise<object>; | ||
postToExternal(path: string, payload?: {}, config?: AxiosRequestConfig): AxiosPromise<object>; | ||
patch(path: string, payload?: {}, searchParams?: SearchParams): Promise<object>; | ||
put(path: string, payload?: {}, searchParams?: SearchParams): Promise<object>; | ||
delete(path: string, payload?: {}, searchParams?: SearchParams): Promise<object>; | ||
/** | ||
* Return the first error message on the first field from the Api response | ||
@@ -49,0 +42,0 @@ * {foo:bar} will return null |
@@ -88,3 +88,3 @@ "use strict"; | ||
}; | ||
const { apiBaseUrl, logger } = this.options; | ||
const { apiBaseUrl, logger, headers } = this.options; | ||
if (!apiBaseUrl) { | ||
@@ -102,6 +102,3 @@ throw new Error('Missing API URL!'); | ||
baseURL: apiBaseUrl, | ||
headers: { | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
Accept: 'application/json, text/plain, */*' | ||
} | ||
headers: Object.assign(Object.assign({}, headers), { 'Content-Type': 'application/json; charset=UTF-8', Accept: 'application/json, text/plain, */*' }) | ||
})); | ||
@@ -128,55 +125,29 @@ this.axiosInstance.interceptors.request.use(this.authInterceptor); | ||
} | ||
/** | ||
* Concatenate the path with the given search params | ||
* @param path | ||
* @param searchParams | ||
*/ | ||
getPathWithParams(path, searchParams) { | ||
const queryString = searchParams | ||
? Object.keys(searchParams) | ||
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(searchParams[key])) | ||
.join('&') | ||
: undefined; | ||
return queryString ? `${path}?${queryString}` : path; | ||
get(path, config) { | ||
return this.axiosInstance.get(path, config).then(this.handleResponse).catch(this.handleError); | ||
} | ||
get(path, searchParams) { | ||
post(path, payload = {}, config) { | ||
return this.axiosInstance | ||
.get(path, { | ||
params: Object.assign({}, searchParams) | ||
}) | ||
.post(path, payload, config) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
post(path, payload = {}, searchParams) { | ||
patch(path, payload = {}, config) { | ||
return this.axiosInstance | ||
.post(this.getPathWithParams(path, searchParams), Object.assign({}, payload)) | ||
.patch(path, payload, config) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
postToExternal(path, payload = {}, config) { | ||
return this.axiosExternalInstance | ||
.post(path, Object.assign({}, payload), config) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
patch(path, payload = {}, searchParams) { | ||
put(path, payload = {}, config) { | ||
return this.axiosInstance | ||
.patch(this.getPathWithParams(path, searchParams), Object.assign({}, payload)) | ||
.put(path, payload, config) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
put(path, payload = {}, searchParams) { | ||
delete(path, config) { | ||
return this.axiosInstance | ||
.put(this.getPathWithParams(path, searchParams), Object.assign({}, payload)) | ||
.delete(path, config) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
delete(path, payload = {}, searchParams) { | ||
return this.axiosInstance | ||
.delete(this.getPathWithParams(path, searchParams), { | ||
data: Object.assign({}, payload) | ||
}) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
/** | ||
@@ -183,0 +154,0 @@ * Return the first error message on the first field from the Api response |
@@ -23,2 +23,3 @@ import { AxiosRequestConfig, AxiosError } from 'axios'; | ||
apiBaseUrl?: string; | ||
headers?: AxiosRequestConfig['headers']; | ||
logger?: Logger; | ||
@@ -25,0 +26,0 @@ logErrorInterceptor?: (error: AxiosError) => Promise<AxiosError> | void; |
{ | ||
"name": "@aircall/http", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"main": "dist/index.js", | ||
@@ -14,3 +14,3 @@ "types": "dist/index.d.ts", | ||
}, | ||
"gitHead": "08adb2f18414cafa5f4c207e77a625d237775095", | ||
"gitHead": "8ab9258a5f144ed095bc32435f51aa619f47c4c9", | ||
"dependencies": { | ||
@@ -17,0 +17,0 @@ "@aircall/logger": "^2.5.2", |
@@ -66,3 +66,3 @@ import axios, { AxiosError } from 'axios'; | ||
const response = await httpService.post(EXAMPLE_PATH, undefined, searchParams); | ||
const response = await httpService.post(pathWithParams, undefined, { params: searchParams }); | ||
@@ -87,3 +87,3 @@ expect(response).toEqual({ | ||
const response = await httpService.post(EXAMPLE_PATH, payload, searchParams); | ||
const response = await httpService.post(pathWithParams, payload, { params: searchParams }); | ||
@@ -96,24 +96,2 @@ expect(response).toEqual({ | ||
describe('postToExternal', () => { | ||
it('should call axios with POST verb', async () => { | ||
const payload = { | ||
attr: true | ||
}; | ||
mock.onPost(EXAMPLE_PATH).reply(200, 'ok'); | ||
const response = await httpService.postToExternal(EXAMPLE_PATH, payload); | ||
expect(response).toEqual('ok'); | ||
}); | ||
it('should POST empty payload if none provided', async () => { | ||
mock.onPost(EXAMPLE_PATH, {}).reply(200, 'ok'); | ||
const response = await httpService.postToExternal(EXAMPLE_PATH); | ||
expect(response).toEqual('ok'); | ||
}); | ||
}); | ||
describe('patch', () => { | ||
@@ -174,8 +152,7 @@ it('should call axios with PATCH verb', async () => { | ||
}; | ||
mock.onDelete(EXAMPLE_PATH).reply(202, { | ||
mock.onDelete(EXAMPLE_PATH, payload).reply(202, { | ||
data: { message: 'using delete' } | ||
}); | ||
const response = await httpService.delete(EXAMPLE_PATH, { data: payload }); | ||
const response = await httpService.delete(EXAMPLE_PATH, payload); | ||
expect(response).toEqual({ | ||
@@ -232,28 +209,2 @@ data: { message: 'using delete' } | ||
describe('getPathWithParams', () => { | ||
const basePath = 'v3/contacts'; | ||
it('should return path if there is no search params', () => { | ||
const path = httpService.getPathWithParams(basePath); | ||
expect(path).toEqual(basePath); | ||
}); | ||
it('should serialize an array as a parameter', () => { | ||
const path = httpService.getPathWithParams(basePath, { | ||
numbers: ['331234567890', '330987654321'], | ||
foo: null | ||
}); | ||
expect(path).toEqual(`${basePath}?numbers=331234567890%2C330987654321&foo=null`); | ||
}); | ||
it('should append a param', () => { | ||
const path = httpService.getPathWithParams(basePath, { | ||
action: 'bulk_fetch' | ||
}); | ||
expect(path).toEqual(`${basePath}?action=bulk_fetch`); | ||
}); | ||
}); | ||
describe('handleError', () => { | ||
@@ -260,0 +211,0 @@ it('should throw the default error if an unknown one is given', () => { |
@@ -33,3 +33,4 @@ import axios, { | ||
public constructor(private options: HttpServiceOptions) { | ||
const { apiBaseUrl, logger } = this.options; | ||
const { apiBaseUrl, logger, headers } = this.options; | ||
if (!apiBaseUrl) { | ||
@@ -52,2 +53,3 @@ throw new Error('Missing API URL!'); | ||
headers: { | ||
...headers, | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
@@ -201,25 +203,9 @@ Accept: 'application/json, text/plain, */*' | ||
/** | ||
* Concatenate the path with the given search params | ||
* @param path | ||
* @param searchParams | ||
*/ | ||
public getPathWithParams(path: string, searchParams?: SearchParams): string { | ||
const queryString = searchParams | ||
? Object.keys(searchParams) | ||
.map( | ||
key => encodeURIComponent(key) + '=' + encodeURIComponent(searchParams[key] as string) | ||
) | ||
.join('&') | ||
: undefined; | ||
return queryString ? `${path}?${queryString}` : path; | ||
public get(path: string, config?: AxiosRequestConfig): AxiosPromise<object> { | ||
return this.axiosInstance.get(path, config).then(this.handleResponse).catch(this.handleError); | ||
} | ||
public get(path: string, searchParams?: SearchParams): AxiosPromise<object> { | ||
public post(path: string, payload: {} = {}, config?: AxiosRequestConfig): AxiosPromise<object> { | ||
return this.axiosInstance | ||
.get(path, { | ||
params: { | ||
...searchParams | ||
} | ||
}) | ||
.post(path, payload, config) | ||
.then(this.handleResponse) | ||
@@ -229,5 +215,5 @@ .catch(this.handleError); | ||
public post(path: string, payload: {} = {}, searchParams?: SearchParams): AxiosPromise<object> { | ||
public patch(path: string, payload: {} = {}, config?: AxiosRequestConfig): Promise<object> { | ||
return this.axiosInstance | ||
.post(this.getPathWithParams(path, searchParams), { ...payload }) | ||
.patch(path, payload, config) | ||
.then(this.handleResponse) | ||
@@ -237,18 +223,5 @@ .catch(this.handleError); | ||
public postToExternal( | ||
path: string, | ||
payload: {} = {}, | ||
config?: AxiosRequestConfig | ||
): AxiosPromise<object> { | ||
return this.axiosExternalInstance | ||
.post(path, { ...payload }, config) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
public patch(path: string, payload: {} = {}, searchParams?: SearchParams): Promise<object> { | ||
public put(path: string, payload: {} = {}, config?: AxiosRequestConfig): Promise<object> { | ||
return this.axiosInstance | ||
.patch(this.getPathWithParams(path, searchParams), { | ||
...payload | ||
}) | ||
.put(path, payload, config) | ||
.then(this.handleResponse) | ||
@@ -258,7 +231,5 @@ .catch(this.handleError); | ||
public put(path: string, payload: {} = {}, searchParams?: SearchParams): Promise<object> { | ||
public delete(path: string, config?: AxiosRequestConfig): Promise<object> { | ||
return this.axiosInstance | ||
.put(this.getPathWithParams(path, searchParams), { | ||
...payload | ||
}) | ||
.delete(path, config) | ||
.then(this.handleResponse) | ||
@@ -268,11 +239,2 @@ .catch(this.handleError); | ||
public delete(path: string, payload: {} = {}, searchParams?: SearchParams): Promise<object> { | ||
return this.axiosInstance | ||
.delete(this.getPathWithParams(path, searchParams), { | ||
data: { ...payload } | ||
}) | ||
.then(this.handleResponse) | ||
.catch(this.handleError); | ||
} | ||
/** | ||
@@ -279,0 +241,0 @@ * Return the first error message on the first field from the Api response |
@@ -30,4 +30,5 @@ import { AxiosRequestConfig, AxiosError } from 'axios'; | ||
apiBaseUrl?: string; | ||
headers?: AxiosRequestConfig['headers']; | ||
logger?: Logger; | ||
logErrorInterceptor?: (error: AxiosError) => Promise<AxiosError> | void; | ||
} |
Sorry, the diff of this file is not supported yet
40891
821