diff --git a/lib/Client/index.ts b/lib/Client/index.ts index 843c296..2b0fa08 100644 --- a/lib/Client/index.ts +++ b/lib/Client/index.ts @@ -1,12 +1,12 @@ import { State } from '@voiceflow/runtime'; -import axios, { AxiosInstance } from 'axios'; +import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; import _cloneDeep from 'lodash/cloneDeep'; import { RequestContext, ResponseContext } from '@/lib/types'; import { adaptResponseContext, extractAudioStep } from './adapters'; -export type ClientConfig = { variables?: Partial; endpoint: string; versionID: string }; +export type ClientConfig = { variables?: Partial; endpoint: string; versionID: string; axiosConfig?: AxiosRequestConfig }; export class Client = Record> { private axios: AxiosInstance; @@ -17,8 +17,8 @@ export class Client = Record> { private initVariables: Partial | undefined; - constructor({ variables, endpoint, versionID }: ClientConfig) { - this.axios = axios.create({ baseURL: endpoint }); + constructor({ variables, endpoint, versionID, axiosConfig }: ClientConfig) { + this.axios = axios.create({ ...axiosConfig, baseURL: endpoint }); this.initVariables = variables; this.versionID = versionID; diff --git a/lib/RuntimeClientFactory/index.ts b/lib/RuntimeClientFactory/index.ts index afba684..084cbca 100644 --- a/lib/RuntimeClientFactory/index.ts +++ b/lib/RuntimeClientFactory/index.ts @@ -1,4 +1,5 @@ import { State } from '@voiceflow/runtime'; +import { AxiosRequestConfig } from 'axios'; import Client from '@/lib/Client'; import RuntimeClient from '@/lib/RuntimeClient'; @@ -12,6 +13,7 @@ export type FactoryConfig = { endpoint?: string; dataConfig?: DataConfig; variables?: Partial; + axiosConfig?: AxiosRequestConfig; }; export class RuntimeClientFactory = Record> { @@ -21,12 +23,12 @@ export class RuntimeClientFactory = Record) { + constructor({ versionID, endpoint = DEFAULT_ENDPOINT, dataConfig, variables, axiosConfig }: FactoryConfig) { if (variables) { validateVarMerge(variables); } - this.client = new Client({ variables, endpoint, versionID }); + this.client = new Client({ variables, endpoint, versionID, axiosConfig }); this.defaultState = { stack: [], storage: {}, variables: { ...variables } }; this.dataConfig = { diff --git a/tests/lib/RuntimeClientFactory/index.unit.ts b/tests/lib/RuntimeClientFactory/index.unit.ts index c117907..548c28c 100644 --- a/tests/lib/RuntimeClientFactory/index.unit.ts +++ b/tests/lib/RuntimeClientFactory/index.unit.ts @@ -32,19 +32,31 @@ describe('RuntimeClientFactory', () => { it('constructor', () => { const { client } = createRuntimeClientFactory(); - expect(client.args).to.eql([[{ versionID: VERSION_ID, endpoint: DEFAULT_ENDPOINT, variables: undefined }]]); + expect(client.args).to.eql([[{ versionID: VERSION_ID, endpoint: DEFAULT_ENDPOINT, variables: undefined, axiosConfig: undefined }]]); }); it('variables', () => { const { client } = createRuntimeClientFactory({ variables: 'foo' as any }); - expect(client.args).to.eql([[{ versionID: VERSION_ID, endpoint: DEFAULT_ENDPOINT, variables: 'foo' }]]); + expect(client.args).to.eql([[{ versionID: VERSION_ID, endpoint: DEFAULT_ENDPOINT, variables: 'foo', axiosConfig: undefined }]]); }); - it('optional', () => { - const { client } = createRuntimeClientFactory({ variables: 'foo' as any, versionID: 'bar', endpoint: 'x', dataConfig: 'y' as any }); + it('axiosConfig', () => { + const { client } = createRuntimeClientFactory({ axiosConfig: 'foo' as any }); + + expect(client.args).to.eql([[{ versionID: VERSION_ID, endpoint: DEFAULT_ENDPOINT, variables: undefined, axiosConfig: 'foo' }]]); + }); - expect(client.args).to.eql([[{ versionID: 'bar', endpoint: 'x', variables: 'foo' }]]); + it('optional', () => { + const { client } = createRuntimeClientFactory({ + variables: 'foo' as any, + versionID: 'bar', + endpoint: 'x', + dataConfig: 'y' as any, + axiosConfig: 'bar' as any, + }); + + expect(client.args).to.eql([[{ versionID: 'bar', endpoint: 'x', variables: 'foo', axiosConfig: 'bar' }]]); }); it('does not accept invalid variables', () => {