Skip to content

Commit 0a6b2de

Browse files
committed
Changed .sendHttpRequest() to accept RequestOptions
1 parent a2f4a5d commit 0a6b2de

File tree

5 files changed

+69
-86
lines changed

5 files changed

+69
-86
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## v0.1.12
4+
**Breaking changes:**
5+
- Changed paramater type of `sendHttpRequest(requestOptions: RequestOptions)`
6+
- `sendHttpRequest()` does not add `apiPath` to path anymore
7+
38
## v0.1.8 & v0.1.9
49
**Features:**
510
- Updated Dependencies to Angular2 Final

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ this._tokenService.updatePassword(
229229
230230
## HTTP Service Wrapper
231231
`Angular2TokenService` wraps all standard Angular2 Http Service calls for authentication and token processing.
232-
- `get(path: string, requestOptions?: RequestOptions): Observable<Response>`
233-
- `post(path: string, data: any, requestOptions?: RequestOptions): Observable<Response>`
234-
- `put(path: string, data: any, requestOptions?: RequestOptions): Observable<Response>`
235-
- `delete(path: string, requestOptions?: RequestOptions): Observable<Response>`
236-
- `patch(path: string, data: any, requestOptions?: RequestOptions): Observable<Response>`
237-
- `head(path: string, requestOptions?: RequestOptions): Observable<Response>`
238-
- `options(path: string, requestOptions?: RequestOptions): Observable<Response>`
232+
If `apiPath` is configured it gets added in front of path.
233+
- `get(path: string): Observable<Response>`
234+
- `post(path: string, data: any): Observable<Response>`
235+
- `put(path: string, data: any): Observable<Response>`
236+
- `delete(path: string): Observable<Response>`
237+
- `patch(path: string, data: any): Observable<Response>`
238+
- `head(path: string): Observable<Response>`
239+
- `options(path: string): Observable<Response>`
239240
240241
#### Example:
241242
```javascript
@@ -246,18 +247,18 @@ this._tokenService.get('my-resource/1').map(res => res.json()).subscribe(
246247
```
247248
248249
### .sendHttpRequest()
249-
More customized requests can be send with the `.sendHttpRequest()`-function. It is used by all Http wrappers.
250+
More customized requests can be send with the `.sendHttpRequest()`-function. It accepts the RequestOptions-Class.
251+
More information can be found in the Angular2 API Reference [here](https://angular.io/docs/ts/latest/api/http/index/RequestOptions-class.html).
250252
251-
`sendHttpRequest(options: HttpRequestOptions): Observable<Response>`
253+
`sendHttpRequest(options: RequestOptions): Observable<Response>`
252254
253255
#### Example:
254256
```javascript
255-
this.sendHttpRequest({
256-
requestMethod: RequestMethod.Post,
257-
path: 'my-resource/1',
258-
data: mydata
259-
requestOptions: myRequestOptions
260-
});
257+
this.sendHttpRequest(new RequestOptions({
258+
method: RequestMethod.Post,
259+
url: 'my-resource/1',
260+
data: mydata
261+
}));
261262
```
262263
263264
## Multiple User Types

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "angular2-token",
3-
"version": "0.1.11",
3+
"version": "0.1.12",
44
"description": "Angular2 service for token based authentication",
55
"main": "./angular2-token.js",
66
"typings": "./angular2-token.d.ts",
77
"scripts": {
88
"test": "karma start",
9-
"build": "rm -rf lib && tsc"
9+
"build": "rm -rf lib && tsc",
10+
"prepublish": "npm run build"
1011
},
1112
"keywords": [
1213
"angular2",

src/angular2-token.model.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import {
2-
RequestMethod,
3-
RequestOptions
4-
} from '@angular/http';
5-
61
export interface UserType {
72
name: string;
83
path: string;
@@ -38,11 +33,4 @@ export interface Angular2TokenOptions {
3833
userTypes?: UserType[];
3934

4035
oAuthPaths?: OAuthPaths;
41-
}
42-
43-
export interface HttpRequestOptions {
44-
requestMethod: RequestMethod,
45-
path: string,
46-
body?: any,
47-
requestOptions?: RequestOptions
4836
}

src/angular2-token.service.ts

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import 'rxjs/add/operator/share';
1515
import {
1616
UserType,
1717
AuthData,
18-
Angular2TokenOptions,
19-
HttpRequestOptions
18+
Angular2TokenOptions
2019
} from './angular2-token.model';
2120

2221
@Injectable()
@@ -196,67 +195,60 @@ export class Angular2TokenService implements CanActivate {
196195
}
197196

198197
// Standard HTTP requests
199-
get(path: string, requestOptions?: RequestOptions): Observable<Response> {
200-
return this.sendHttpRequest({
201-
requestMethod: RequestMethod.Get,
202-
path: path,
203-
requestOptions: requestOptions
204-
});
198+
get(path: string): Observable<Response> {
199+
return this.sendHttpRequest(new RequestOptions({
200+
method: RequestMethod.Get,
201+
url: this._constructApiPath() + path
202+
}));
205203
}
206204

207-
post(path: string, data: any, requestOptions?: RequestOptions): Observable<Response> {
208-
return this.sendHttpRequest({
209-
requestMethod: RequestMethod.Post,
210-
path: path,
211-
body: data,
212-
requestOptions: requestOptions
213-
});
205+
post(path: string, data: any): Observable<Response> {
206+
return this.sendHttpRequest(new RequestOptions({
207+
method: RequestMethod.Post,
208+
url: this._constructApiPath() + path,
209+
body: data
210+
}));
214211
}
215212

216-
put(path: string, data: any, requestOptions?: RequestOptions): Observable<Response> {
217-
return this.sendHttpRequest({
218-
requestMethod: RequestMethod.Put,
219-
path: path,
220-
body: data,
221-
requestOptions: requestOptions
222-
});
213+
put(path: string, data: any): Observable<Response> {
214+
return this.sendHttpRequest(new RequestOptions({
215+
method: RequestMethod.Put,
216+
url: this._constructApiPath() + path,
217+
body: data
218+
}));
223219
}
224220

225-
delete(path: string, requestOptions?: RequestOptions): Observable<Response> {
226-
return this.sendHttpRequest({
227-
requestMethod: RequestMethod.Delete,
228-
path: path,
229-
requestOptions: requestOptions
230-
});
221+
delete(path: string): Observable<Response> {
222+
return this.sendHttpRequest(new RequestOptions({
223+
method: RequestMethod.Delete,
224+
url: this._constructApiPath() + path
225+
}));
231226
}
232227

233-
patch(path: string, data: any, requestOptions?: RequestOptions): Observable<Response> {
234-
return this.sendHttpRequest({
235-
requestMethod: RequestMethod.Patch,
236-
path: path,
237-
body: data,
238-
requestOptions: requestOptions
239-
});
228+
patch(path: string, data: any): Observable<Response> {
229+
return this.sendHttpRequest(new RequestOptions({
230+
method: RequestMethod.Patch,
231+
url: this._constructApiPath() + path,
232+
body: data
233+
}));
240234
}
241235

242-
head(path: string, requestOptions?: RequestOptions): Observable<Response> {
243-
return this.sendHttpRequest({
244-
requestMethod: RequestMethod.Head,
245-
path: path,
246-
requestOptions: requestOptions
247-
});
236+
head(path: string): Observable<Response> {
237+
return this.sendHttpRequest(new RequestOptions({
238+
method: RequestMethod.Head,
239+
url: this._constructApiPath() + path
240+
}));
248241
}
249242

250-
options(path: string, requestOptions?: RequestOptions): Observable<Response> {
251-
return this.sendHttpRequest({
252-
requestMethod: RequestMethod.Options,
253-
path: path,
254-
requestOptions: requestOptions
255-
});
243+
options(path: string): Observable<Response> {
244+
return this.sendHttpRequest(new RequestOptions({
245+
method: RequestMethod.Options,
246+
url: this._constructApiPath() + path
247+
}));
256248
}
257249

258250
// Construct and send Http request
259-
sendHttpRequest(options: HttpRequestOptions): Observable<Response> {
251+
sendHttpRequest(requestOptions: RequestOptions): Observable<Response> {
260252

261253
let headers: Headers;
262254
let baseRequestOptions: RequestOptions;
@@ -266,6 +258,7 @@ export class Angular2TokenService implements CanActivate {
266258
if (this._currentAuthData != null)
267259
headers = new Headers({
268260
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
261+
'Accept': 'application/json',
269262
'access-token': this._currentAuthData.accessToken,
270263
'client': this._currentAuthData.client,
271264
'expiry': this._currentAuthData.expiry,
@@ -274,22 +267,17 @@ export class Angular2TokenService implements CanActivate {
274267
});
275268
else
276269
headers = new Headers({
277-
'Content-Type': 'application/json' // ToDo: Add to RequestOptions if available
270+
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
271+
'Accept': 'application/json'
278272
});
279273

280274
// Construct Default Request Options
281275
baseRequestOptions = new RequestOptions({
282-
method: options.requestMethod,
283-
url: this._constructApiPath() + options.path,
284-
headers: headers,
285-
body: options.body
276+
headers: headers
286277
})
287278

288279
// Merge standard and custom RequestOptions
289-
if (options.requestOptions != null)
290-
mergedRequestOptions = baseRequestOptions.merge(options.requestOptions);
291-
else
292-
mergedRequestOptions = baseRequestOptions;
280+
mergedRequestOptions = baseRequestOptions.merge(requestOptions);
293281

294282
let response = this._http.request(new Request(mergedRequestOptions)).share();
295283

0 commit comments

Comments
 (0)