Skip to content

Commit

Permalink
Merge pull request #72 from Typeform/Client-Api_Base_URL_Feature
Browse files Browse the repository at this point in the history
feat: add Api Base URL option to client
  • Loading branch information
Martin Lagrange authored May 19, 2021
2 parents 866075c + b7a9faa commit 195eac2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/create-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}` } : {}
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/create-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})

0 comments on commit 195eac2

Please sign in to comment.