From c2de1fc3476e83a0939b209663f37b0147204f52 Mon Sep 17 00:00:00 2001 From: Matteo Rigon Date: Thu, 21 Apr 2022 10:03:37 +0200 Subject: [PATCH] feat: add timeout option to config --- __tests__/config.spec.ts | 30 +++++++++++++++++++++++------- __tests__/request.spec.ts | 1 + __tests__/utils.ts | 5 +++++ src/config.ts | 8 +++++++- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/__tests__/config.spec.ts b/__tests__/config.spec.ts index 01b4a0f..1d4adf4 100644 --- a/__tests__/config.spec.ts +++ b/__tests__/config.spec.ts @@ -26,6 +26,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: defaultConfig.refreshTokens, @@ -40,6 +41,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: '', refreshTokens: defaultConfig.refreshTokens, @@ -55,6 +57,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: 'asd2', refreshTokens: defaultConfig.refreshTokens, @@ -70,6 +73,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: '', refreshTokens: false, @@ -86,6 +90,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: '', refreshTokens: false, @@ -103,6 +108,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: '', refreshTokens: false, @@ -126,6 +132,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: '', refreshTokens: true, @@ -150,6 +157,7 @@ describe('config', () => { }) expect(config).toEqual({ host: 'asdasd', + timeout: 5000, clientId: 'asd', clientSecret: '', refreshTokens: true, @@ -164,18 +172,25 @@ describe('config', () => { initConfig({ host: 'asdasd', - clientId: 'asd', - refreshTokens: true, - refreshTokensAttempts: 4, - cookies: { - customer_token: '', - }, + timeout: 10000, + }) + + expect(config).toEqual({ + host: 'asdasd', + timeout: 10000, + clientId: '', + clientSecret: '', + refreshTokens: defaultConfig.refreshTokens, + refreshTokensAttempts: defaultConfig.refreshTokensAttempts, + onRefreshError: defaultConfig.onRefreshError, + cookies: defaultConfig.cookies, }) }) - it('passes host to request', () => { + it('passes host and timeout to request', () => { initConfig({ host: 'asdasd', + timeout: 10000, clientId: 'asd', refreshTokens: true, refreshTokensAttempts: 4, @@ -185,6 +200,7 @@ describe('config', () => { }) expect(baseRequest.defaults.baseURL).toBe('asdasd') + expect(baseRequest.defaults.timeout).toBe(10000) }) }) diff --git a/__tests__/request.spec.ts b/__tests__/request.spec.ts index 8ae1a95..6d6d88f 100644 --- a/__tests__/request.spec.ts +++ b/__tests__/request.spec.ts @@ -5,6 +5,7 @@ describe('request', () => { const baseURL = 'http://www.google.com' initRequest({ host: baseURL, + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: false, diff --git a/__tests__/utils.ts b/__tests__/utils.ts index ebea9ab..cb9c42a 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -5,6 +5,7 @@ import customerResponse from './responses/customer.json' export const mockRequestWithConfig = (): AxiosInstance => { initRequest({ host: 'http://www.google.com', + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: false, @@ -23,6 +24,7 @@ export const mockRequestWithUri = (): AxiosInstance => { const baseURL = 'http://www.google.com' initRequest({ host: baseURL, + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: false, @@ -46,6 +48,7 @@ export const mockRequestWithResponse = ( const baseURL = 'http://www.google.com' initRequest({ host: baseURL, + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: false, @@ -78,6 +81,7 @@ export const mockRequestWithEcho = (): AxiosInstance => { const baseURL = 'http://www.google.com' initRequest({ host: baseURL, + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: false, @@ -104,6 +108,7 @@ export const mockRequestWithError = ( const baseURL = 'http://www.google.com' initRequest({ host: baseURL, + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: false, diff --git a/src/config.ts b/src/config.ts index 2f1cfbf..59d19af 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,6 +2,7 @@ import axios, { AxiosInstance } from 'axios' import { CommonPayloadAttributes } from './resource' export interface InternalConfig { host: string + timeout: number clientId: string clientSecret: string refreshTokens: boolean @@ -16,6 +17,7 @@ export interface InternalConfig { export interface Config { host: string + timeout?: number clientId?: string clientSecret?: string refreshTokens?: boolean @@ -32,6 +34,7 @@ export interface Config { export const defaultConfig: Readonly = { host: '', + timeout: 5000, clientId: '', clientSecret: '', refreshTokens: true, @@ -49,6 +52,7 @@ export const defaultConfig: Readonly = { export const config: InternalConfig = { host: defaultConfig.host, + timeout: defaultConfig.timeout, clientId: defaultConfig.clientId, clientSecret: defaultConfig.clientSecret, refreshTokens: defaultConfig.refreshTokens, @@ -75,6 +79,7 @@ export const __resetConfig = (): void => { export const initConfig = (providedConfig: Config): void => { config.host = providedConfig.host + config.timeout = providedConfig.timeout || 5000 config.clientId = providedConfig.clientId || '' config.clientSecret = providedConfig.clientSecret || '' config.refreshTokens = !!providedConfig.refreshTokens @@ -127,7 +132,7 @@ export const getConfig = (): Readonly => config export const baseRequest: AxiosInstance = axios.create({ baseURL: '', - timeout: 5000, + timeout: defaultConfig.timeout, headers: { Accept: 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json', @@ -136,6 +141,7 @@ export const baseRequest: AxiosInstance = axios.create({ export const initRequest = (config: InternalConfig): void => { baseRequest.defaults.baseURL = config.host + baseRequest.defaults.timeout = config.timeout } export const commonPayloadAttributes: (keyof CommonPayloadAttributes)[] = [