Skip to content

Commit

Permalink
feat: add timeout option to config
Browse files Browse the repository at this point in the history
  • Loading branch information
reegodev committed Apr 21, 2022
1 parent 5c43df6 commit c2de1fc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
30 changes: 23 additions & 7 deletions __tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: defaultConfig.refreshTokens,
Expand All @@ -40,6 +41,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: '',
refreshTokens: defaultConfig.refreshTokens,
Expand All @@ -55,6 +57,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: 'asd2',
refreshTokens: defaultConfig.refreshTokens,
Expand All @@ -70,6 +73,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: '',
refreshTokens: false,
Expand All @@ -86,6 +90,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: '',
refreshTokens: false,
Expand All @@ -103,6 +108,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: '',
refreshTokens: false,
Expand All @@ -126,6 +132,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: '',
refreshTokens: true,
Expand All @@ -150,6 +157,7 @@ describe('config', () => {
})
expect(config).toEqual({
host: 'asdasd',
timeout: 5000,
clientId: 'asd',
clientSecret: '',
refreshTokens: true,
Expand All @@ -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,
Expand All @@ -185,6 +200,7 @@ describe('config', () => {
})

expect(baseRequest.defaults.baseURL).toBe('asdasd')
expect(baseRequest.defaults.timeout).toBe(10000)
})
})

Expand Down
1 change: 1 addition & 0 deletions __tests__/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('request', () => {
const baseURL = 'http://www.google.com'
initRequest({
host: baseURL,
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: false,
Expand Down
5 changes: 5 additions & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,6 +24,7 @@ export const mockRequestWithUri = (): AxiosInstance => {
const baseURL = 'http://www.google.com'
initRequest({
host: baseURL,
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: false,
Expand All @@ -46,6 +48,7 @@ export const mockRequestWithResponse = (
const baseURL = 'http://www.google.com'
initRequest({
host: baseURL,
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: false,
Expand Down Expand Up @@ -78,6 +81,7 @@ export const mockRequestWithEcho = (): AxiosInstance => {
const baseURL = 'http://www.google.com'
initRequest({
host: baseURL,
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: false,
Expand All @@ -104,6 +108,7 @@ export const mockRequestWithError = (
const baseURL = 'http://www.google.com'
initRequest({
host: baseURL,
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: false,
Expand Down
8 changes: 7 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,7 @@ export interface InternalConfig {

export interface Config {
host: string
timeout?: number
clientId?: string
clientSecret?: string
refreshTokens?: boolean
Expand All @@ -32,6 +34,7 @@ export interface Config {

export const defaultConfig: Readonly<InternalConfig> = {
host: '',
timeout: 5000,
clientId: '',
clientSecret: '',
refreshTokens: true,
Expand All @@ -49,6 +52,7 @@ export const defaultConfig: Readonly<InternalConfig> = {

export const config: InternalConfig = {
host: defaultConfig.host,
timeout: defaultConfig.timeout,
clientId: defaultConfig.clientId,
clientSecret: defaultConfig.clientSecret,
refreshTokens: defaultConfig.refreshTokens,
Expand All @@ -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
Expand Down Expand Up @@ -127,7 +132,7 @@ export const getConfig = (): Readonly<InternalConfig> => 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',
Expand All @@ -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)[] = [
Expand Down

0 comments on commit c2de1fc

Please sign in to comment.