Skip to content

Commit f6a662c

Browse files
committed
Added RequestOptionsArgs to HTTP-Wrapper, fixes #68
1 parent 634d431 commit f6a662c

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ this._tokenService.updatePassword({
277277
## HTTP Service Wrapper
278278
`Angular2TokenService` wraps all standard Angular2 Http Service calls for authentication and token processing.
279279
If `apiPath` is configured it gets added in front of path.
280-
- `get(path: string): Observable<Response>`
281-
- `post(path: string, data: any): Observable<Response>`
282-
- `put(path: string, data: any): Observable<Response>`
283-
- `delete(path: string): Observable<Response>`
284-
- `patch(path: string, data: any): Observable<Response>`
285-
- `head(path: string): Observable<Response>`
286-
- `options(path: string): Observable<Response>`
280+
- `get(url: string, options?: RequestOptionsArgs): Observable<Response>`
281+
- `post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>`
282+
- `put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>`
283+
- `delete(url: string, options?: RequestOptionsArgs): Observable<Response>`
284+
- `patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>`
285+
- `head(url: string, options?: RequestOptionsArgs): Observable<Response>`
286+
- `options(url: string, options?: RequestOptionsArgs): Observable<Response>`
287287
288288
#### Example:
289289
```javascript

src/angular2-token.service.ts

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -248,56 +248,56 @@ export class Angular2TokenService implements CanActivate {
248248
}
249249

250250
// Standard HTTP requests
251-
get(path: string): Observable<Response> {
252-
return this.request({
253-
method: RequestMethod.Get,
254-
url: this._constructApiPath() + path
255-
});
251+
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
252+
return this.request(this._mergeRequestOptionsArgs({
253+
url: this._constructApiPath() + url,
254+
method: RequestMethod.Get
255+
}, options));
256256
}
257257

258-
post(path: string, data: any): Observable<Response> {
259-
return this.request({
258+
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
259+
return this.request(this._mergeRequestOptionsArgs({
260+
url: this._constructApiPath() + url,
260261
method: RequestMethod.Post,
261-
url: this._constructApiPath() + path,
262-
body: data
263-
});
262+
body: body
263+
}, options));
264264
}
265265

266-
put(path: string, data: any): Observable<Response> {
267-
return this.request({
266+
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
267+
return this.request(this._mergeRequestOptionsArgs({
268+
url: this._constructApiPath() + url,
268269
method: RequestMethod.Put,
269-
url: this._constructApiPath() + path,
270-
body: data
271-
});
270+
body: body
271+
}, options));
272272
}
273273

274-
delete(path: string): Observable<Response> {
275-
return this.request({
276-
method: RequestMethod.Delete,
277-
url: this._constructApiPath() + path
278-
});
274+
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
275+
return this.request(this._mergeRequestOptionsArgs({
276+
url: this._constructApiPath() + url,
277+
method: RequestMethod.Delete
278+
}, options));
279279
}
280280

281-
patch(path: string, data: any): Observable<Response> {
282-
return this.request({
281+
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
282+
return this.request(this._mergeRequestOptionsArgs({
283+
url: this._constructApiPath() + url,
283284
method: RequestMethod.Patch,
284-
url: this._constructApiPath() + path,
285-
body: data
286-
});
285+
body: body
286+
}, options));
287287
}
288288

289-
head(path: string): Observable<Response> {
289+
head(path: string, options?: RequestOptionsArgs): Observable<Response> {
290290
return this.request({
291291
method: RequestMethod.Head,
292292
url: this._constructApiPath() + path
293293
});
294294
}
295295

296-
options(path: string): Observable<Response> {
297-
return this.request({
298-
method: RequestMethod.Options,
299-
url: this._constructApiPath() + path
300-
});
296+
options(url: string, options?: RequestOptionsArgs): Observable<Response> {
297+
return this.request(this._mergeRequestOptionsArgs({
298+
url: this._constructApiPath() + url,
299+
method: RequestMethod.Options
300+
}, options));
301301
}
302302

303303
// Construct and send Http request
@@ -331,6 +331,16 @@ export class Angular2TokenService implements CanActivate {
331331
return response;
332332
}
333333

334+
private _mergeRequestOptionsArgs(options: RequestOptionsArgs, addOptions?: RequestOptionsArgs): RequestOptionsArgs {
335+
336+
let returnOptions: RequestOptionsArgs = options;
337+
338+
if (options)
339+
(<any>Object).assign(returnOptions, addOptions);
340+
341+
return returnOptions;
342+
}
343+
334344
// Check if response is complete and newer, then update storage
335345
private _handleResponse(response: Observable<Response>) {
336346
response.subscribe(res => {

0 commit comments

Comments
 (0)