diff --git a/packages/http/src/Client.ts b/packages/http/src/Client.ts index 8d3099db..d56f7694 100644 --- a/packages/http/src/Client.ts +++ b/packages/http/src/Client.ts @@ -1,4 +1,4 @@ -import axios, {AxiosInstance, AxiosRequestConfig} from 'axios' +import axios, { AxiosInstance, AxiosRequestConfig } from 'axios' import * as rax from 'retry-axios' import qs from 'qs' import Network from './Network' @@ -47,6 +47,7 @@ interface Options { name?: string userAgent?: string retry?: number + headers?: Record } export default class Client { @@ -64,6 +65,7 @@ export default class Client { this.network = network this.axios = axios.create({ baseURL: this.network.endpoint, + headers: options?.headers, }) this.name = options?.name this.userAgent = options?.userAgent diff --git a/packages/http/src/__tests__/Client.spec.ts b/packages/http/src/__tests__/Client.spec.ts index 6dfb0656..97bba031 100644 --- a/packages/http/src/__tests__/Client.spec.ts +++ b/packages/http/src/__tests__/Client.spec.ts @@ -1,6 +1,5 @@ import nock from 'nock' -import Client from '..' -import { Network } from '../' +import Client, { Network } from '..' test('exposes a client instance with default options', () => { const prodUrl = 'https://api.helium.io' @@ -51,6 +50,26 @@ describe('get', () => { }) }) +it('client passes default headers to GET request', async () => { + const nameHeader = 'name-header-test' + const userAgentHeader = 'user-agent-test' + const network = 'helium' + nock('https://api.helium.io') + .get('/v1/greeting') + .reply(200, { + greeting: 'hello', + }) + + const opts = { name: nameHeader, userAgent: userAgentHeader, headers: { network } } + const client = new Client(Network.production, opts) + const response = await client.get('/greeting') + + expect(response.data.greeting).toBe('hello') + expect(response.config.headers['x-client-name']).toBe(nameHeader) + expect(response.config.headers['User-Agent']).toBe(userAgentHeader) + expect(response.config.headers.network).toBe(network) +}) + describe('post', () => { it('creates a POST request to the full url', async () => { nock('https://api.helium.io').post('/v1/greeting', { greeting: 'hello' }).reply(200, { @@ -77,6 +96,24 @@ describe('post', () => { expect(response.config.headers['x-client-name']).toBe(nameHeader) expect(response.config.headers['User-Agent']).toBe(userAgentHeader) }) + + it('client passes default headers to POST request', async () => { + const network = 'helium' + const nameHeader = 'name-header-test' + const userAgentHeader = 'user-agent-test' + nock('https://api.helium.io').post('/v1/greeting', { greeting: 'hello' }).reply(200, { + response: 'hey there!', + }) + const opts = { name: nameHeader, userAgent: userAgentHeader, headers: { network } } + const client = new Client(Network.production, opts) + const params = { greeting: 'hello' } + const response = await client.post('/greeting', params) + + expect(response.data.response).toBe('hey there!') + expect(response.config.headers['x-client-name']).toBe(nameHeader) + expect(response.config.headers['User-Agent']).toBe(userAgentHeader) + expect(response.config.headers.network).toBe(network) + }) }) describe('retry logic', () => {