Skip to content

Commit

Permalink
sdk/node: support setting additional request headers
Browse files Browse the repository at this point in the history
This is useful for manually manipulating the name-set header
for certain application cases.

Closes #4741

Author: Dominic Dagradi <[email protected]>
Date: Thu Aug 16 14:36:28 2018 -0700
upstream:ee67d832493b68a97d8aada774c1bd53a45f53f7
  • Loading branch information
dominic authored and iampogo committed Aug 16, 2018
1 parent ac97d7f commit cbf3a79
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 14 additions & 5 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -112,6 +113,7 @@ export class Connection {
this.ledgerName = ledgerName
this.credential = credential
this.agent = agent
this.customHeaders = {}
}

/**
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions test/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit cbf3a79

Please sign in to comment.