Skip to content

Commit 5f3805c

Browse files
committed
Add custom options object to HttpClient methods
1 parent 1b3aaba commit 5f3805c

3 files changed

Lines changed: 37 additions & 12 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+
- Add custom options object to HttpClient methods
67
- Add npm keywords
78
- [Issue #192] HttpClient must allow for custom headers
89

src/http/http-client.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ export class HttpClient {
3333
* @param data Data to send as query
3434
* @param bearer Bearer token to send
3535
* @param expect Array of status codes to expect
36+
* @param customOptions Additional request options
3637
* @return Returns a promise of HttpClientResponse
3738
*/
3839
public post(
3940
path: string,
4041
data: object,
4142
bearer: boolean | string = false,
42-
expect: number[] = [ 200, 201, 202, 204 ]
43+
expect: number[] = [ 200, 201, 202, 204 ],
44+
customOptions?: object
4345
): Promise<HttpClientResponse> {
44-
const options = {
46+
let options = {
4547
method: 'POST',
4648
url: `${this.url}:${this.port}${path}`,
4749
body: data,
@@ -56,6 +58,10 @@ export class HttpClient {
5658
options['headers'] = this.headers;
5759
}
5860

61+
if (customOptions) {
62+
options = Object.assign(options, options);
63+
}
64+
5965
return new Promise<HttpClientResponse>((accept, reject) => {
6066
this.request(options, (error, response, body) => {
6167
if (error) {
@@ -82,16 +88,18 @@ export class HttpClient {
8288
* @param data Data to send as query
8389
* @param bearer Bearer token to send
8490
* @param expect Array of status codes to expect
91+
* @param customOptions Additional request options
8592
* @return Returns a promise of HttpClientResponse
8693
*/
8794
public get(
8895
path: string,
8996
data: boolean | Object = false,
9097
bearer: boolean | string = false,
91-
expect: number[] = [ 200, 202 ]
98+
expect: number[] = [ 200, 202 ],
99+
customOptions?: object
92100
): Promise<HttpClientResponse> {
93101
return new Promise<HttpClientResponse>((accept, reject) => {
94-
const options = {
102+
let options = {
95103
method: 'GET',
96104
url: `${this.url}:${this.port}${path}`,
97105
json: true,
@@ -106,6 +114,10 @@ export class HttpClient {
106114
options['headers'] = this.headers;
107115
}
108116

117+
if (customOptions) {
118+
options = Object.assign(options, options);
119+
}
120+
109121
this.request(options, (error, response, body) => {
110122
if (error) {
111123
reject(new HttpClientResponse(response, error));
@@ -131,15 +143,17 @@ export class HttpClient {
131143
* @param data Data to send as query
132144
* @param bearer Bearer token to send
133145
* @param expect Array of status codes to expect
146+
* @param customOptions Additional request options
134147
* @return Returns a promise of HttpClientResponse
135148
*/
136149
public patch(
137150
path: string,
138151
data: object,
139152
bearer: boolean | string = false,
140-
expect: number[] = [ 200, 201, 202, 204 ]
153+
expect: number[] = [ 200, 201, 202, 204 ],
154+
customOptions?: object
141155
): Promise<HttpClientResponse> {
142-
const options = {
156+
let options = {
143157
method: 'PATCH',
144158
url: `${this.url}:${this.port}${path}`,
145159
body: data,
@@ -154,6 +168,10 @@ export class HttpClient {
154168
options['headers'] = this.headers;
155169
}
156170

171+
if (customOptions) {
172+
options = Object.assign(options, options);
173+
}
174+
157175
return new Promise<HttpClientResponse>((accept, reject) => {
158176
this.request(options, (error, response, body) => {
159177
if (error) {
@@ -180,15 +198,17 @@ export class HttpClient {
180198
* @param data Data to send as query
181199
* @param bearer Bearer token to send
182200
* @param expect Array of status codes to expect
201+
* @param customOptions Additional request options
183202
* @return Returns a promise of HttpClientResponse
184203
*/
185204
public delete(
186205
path: string,
187206
bearer: boolean | string = false,
188-
expect: number[] = [ 200, 202, 204 ]
207+
expect: number[] = [ 200, 202, 204 ],
208+
customOptions?: object
189209
): Promise<HttpClientResponse> {
190210
return new Promise<HttpClientResponse>((accept, reject) => {
191-
const options = {
211+
let options = {
192212
method: 'DELETE',
193213
url: `${this.url}:${this.port}${path}`
194214
};
@@ -201,6 +221,10 @@ export class HttpClient {
201221
options['headers'] = this.headers;
202222
}
203223

224+
if (customOptions) {
225+
options = Object.assign(options, options);
226+
}
227+
204228
this.request(options, (error, response, body) => {
205229
if (error) {
206230
reject(new HttpClientResponse(response, error));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('[HTTP] HTTP Client', () => {
2929

3030
it('can get', async () => {
3131
const result: void | HttpClientResponse = await http
32-
.get('/', {}, 'Bearer: test', [ 404 ])
32+
.get('/', {}, 'Bearer: test', [ 404 ], {})
3333
.catch((error) => fail(JSON.stringify(error)));
3434

3535
if (result) {
@@ -52,7 +52,7 @@ describe('[HTTP] HTTP Client', () => {
5252

5353
it('can post', async () => {
5454
const result: void | HttpClientResponse = await http
55-
.post('/', {}, 'Bearer: test', [ 404 ])
55+
.post('/', {}, 'Bearer: test', [ 404 ], {})
5656
.catch((error) => fail(JSON.stringify(error)));
5757

5858
if (result) {
@@ -75,7 +75,7 @@ describe('[HTTP] HTTP Client', () => {
7575

7676
it('can patch', async () => {
7777
const result: void | HttpClientResponse = await http
78-
.patch('/', {}, 'Bearer: test', [ 404 ])
78+
.patch('/', {}, 'Bearer: test', [ 404 ], {})
7979
.catch((error) => fail(JSON.stringify(error)));
8080

8181
if (result) {
@@ -98,7 +98,7 @@ describe('[HTTP] HTTP Client', () => {
9898

9999
it('can delete', async () => {
100100
const result: void | HttpClientResponse = await http
101-
.delete('/', 'Bearer: test', [ 404 ])
101+
.delete('/', 'Bearer: test', [ 404 ], {})
102102
.catch((error) => fail(JSON.stringify(error)));
103103

104104
if (result) {

0 commit comments

Comments
 (0)