Skip to content

Commit db01eec

Browse files
committed
[Issue #192] HttpClient must allow for custom headers
1 parent cc37ac3 commit db01eec

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [3.3.0] Nov-13-2019
44

55
### Additions
6+
- [Issue #192] HttpClient must allow for custom headers
67

78
### Fixes
89
- Node 12 Upgrade

src/http/http-client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export class HttpClient {
2424
// Server PORT
2525
public port: number | string = process.env.PORT;
2626

27+
// Global headers
28+
public headers?: Object;
29+
2730
/**
2831
* Send a POST http request to the server
2932
* @param path Path to send to (e.g. /users)
@@ -48,6 +51,10 @@ export class HttpClient {
4851
if (bearer) {
4952
options['auth'] = { bearer: `${bearer}` };
5053
}
54+
55+
if (this.headers) {
56+
options['headers'] = this.headers;
57+
}
5158

5259
return new Promise<HttpClientResponse>((accept, reject) => {
5360
this.request(options, (error, response, body) => {
@@ -94,6 +101,10 @@ export class HttpClient {
94101
if (bearer) {
95102
options['auth'] = { bearer: `${bearer}` };
96103
}
104+
105+
if (this.headers) {
106+
options['headers'] = this.headers;
107+
}
97108

98109
this.request(options, (error, response, body) => {
99110
if (error) {
@@ -138,6 +149,10 @@ export class HttpClient {
138149
if (bearer) {
139150
options['auth'] = { bearer: `${bearer}` };
140151
}
152+
153+
if (this.headers) {
154+
options['headers'] = this.headers;
155+
}
141156

142157
return new Promise<HttpClientResponse>((accept, reject) => {
143158
this.request(options, (error, response, body) => {
@@ -181,6 +196,10 @@ export class HttpClient {
181196
if (bearer) {
182197
options['auth'] = { bearer: `${bearer}` };
183198
}
199+
200+
if (this.headers) {
201+
options['headers'] = this.headers;
202+
}
184203

185204
this.request(options, (error, response, body) => {
186205
if (error) {

test/spec/api/http/http-client.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { pointy } from '../../../../src/';
44
import { HttpClientResponse } from '../../../../src/http/http-client-response';
55
const http = pointy.http;
66

7+
http.headers = {
8+
'CustomHeader': 'CustomValue'
9+
};
10+
711
/**
812
* HTTP Client
913
* pointyapi/http

0 commit comments

Comments
 (0)