From cbf3a793d5ac1dd7063ed3f073a359a030acef6e Mon Sep 17 00:00:00 2001 From: Dominic Dagradi Date: Thu, 16 Aug 2018 21:37:15 +0000 Subject: [PATCH] sdk/node: support setting additional request headers This is useful for manually manipulating the name-set header for certain application cases. Closes #4741 Author: Dominic Dagradi Date: Thu Aug 16 14:36:28 2018 -0700 upstream:ee67d832493b68a97d8aada774c1bd53a45f53f7 --- CHANGELOG.md | 5 +++++ src/connection.ts | 19 ++++++++++++++----- test/connection.ts | 8 ++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 281898d..0be875e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Sequence Node SDK changelog +## 2.2.1 (??????) + +* Add support for setting custom HTTP request headers at + the client level. + ## 2.2.1 (20180814) * Added support for setting a custom API URL when run outside diff --git a/src/connection.ts b/src/connection.ts index 9c63618..b8c1be2 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -95,6 +95,7 @@ export class Connection { public credential?: string public baseUrl: string public agent?: Agent + public customHeaders: { [key: string]: string } public ledgerName: string private ledgerUrl: string private deadline: number @@ -112,6 +113,7 @@ export class Connection { this.ledgerName = ledgerName this.credential = credential this.agent = agent + this.customHeaders = {} } /** @@ -142,11 +144,16 @@ export class Connection { return this.requestRaw( this.ledgerUrl + path, body, - Object.assign({}, headers, { - Credential: this.credential, - 'Idempotency-Key': uuid.v4(), - 'Name-Set': 'camel', - }), + Object.assign( + {}, + headers, + { + Credential: this.credential, + 'Idempotency-Key': uuid.v4(), + 'Name-Set': 'camel', + }, + this.customHeaders + ), reqId ) } @@ -263,7 +270,9 @@ export class Connection { } if (resp.status / 100 === 2) { Object.defineProperty(body, '_rawResponse', { writable: true }) + Object.defineProperty(body, '_rawRequest', { writable: true }) body._rawResponse = resp + body._rawRequest = req return camelize(body) } diff --git a/test/connection.ts b/test/connection.ts index 339ad71..b07e908 100644 --- a/test/connection.ts +++ b/test/connection.ts @@ -26,4 +26,12 @@ describe('Connection', () => { expect((client.connection as any).deadline).to.equal(initial) }) + + it('passes additional headers to the api when set', async () => { + const client = testHelpers.constructClient() + client.connection.customHeaders = {Foo: 'bar'} + + const resp = await client.stats.get() + expect(resp._rawRequest.headers.Foo).to.equal('bar') + }) })