From b7a9faac04a32672af92e517c34c8ac3baa456de Mon Sep 17 00:00:00 2001 From: Martin Lagrange Date: Tue, 18 May 2021 17:05:23 -0700 Subject: [PATCH] feat: add Api Base URL option to client --- src/create-client.ts | 4 +++- tests/unit/create-client.test.ts | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/create-client.ts b/src/create-client.ts index 07d6295..2ce866c 100644 --- a/src/create-client.ts +++ b/src/create-client.ts @@ -13,7 +13,9 @@ export const clientConstructor = ({ token, ...options }: Typeform.ClientArg = {} ...otherArgs } = args - const requestUrl = buildUrlWithParams(`${API_BASE_URL}${url}`, params) + const { apiBaseUrl } = options + const requestApiBaseUrl = apiBaseUrl || API_BASE_URL + const requestUrl = buildUrlWithParams(`${requestApiBaseUrl}${url}`, params) const { headers = {} } = options const authorization = token ? { Authorization: `bearer ${token}` } : {} diff --git a/tests/unit/create-client.test.ts b/tests/unit/create-client.test.ts index dc3d765..1785bb1 100644 --- a/tests/unit/create-client.test.ts +++ b/tests/unit/create-client.test.ts @@ -68,3 +68,23 @@ test('falsy values should be passed', () => { } expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=0&b=0') }) + +test('Specify apiBaseUrl', async () => { + + + const rainbowApi = 'https://api.rainbow.typeform.com' + + const clientWithApiBaseUrl = clientConstructor({ + token: 'abc', + apiBaseUrl: rainbowApi + }) + + await clientWithApiBaseUrl.request({ + url: '/forms', + method: 'get', + headers: { + Accepts: 'application/json' + } + }) + expect(axios.history.get[0].url).toBe(`${rainbowApi}/forms`) +}) \ No newline at end of file