Skip to content

Commit

Permalink
Changed .sendHttpRequest() to accept RequestOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
neroniaky committed Sep 20, 2016
1 parent a2f4a5d commit 0a6b2de
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 86 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## v0.1.12
**Breaking changes:**
- Changed paramater type of `sendHttpRequest(requestOptions: RequestOptions)`
- `sendHttpRequest()` does not add `apiPath` to path anymore

## v0.1.8 & v0.1.9
**Features:**
- Updated Dependencies to Angular2 Final
Expand Down
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,14 @@ this._tokenService.updatePassword(
## HTTP Service Wrapper
`Angular2TokenService` wraps all standard Angular2 Http Service calls for authentication and token processing.
- `get(path: string, requestOptions?: RequestOptions): Observable<Response>`
- `post(path: string, data: any, requestOptions?: RequestOptions): Observable<Response>`
- `put(path: string, data: any, requestOptions?: RequestOptions): Observable<Response>`
- `delete(path: string, requestOptions?: RequestOptions): Observable<Response>`
- `patch(path: string, data: any, requestOptions?: RequestOptions): Observable<Response>`
- `head(path: string, requestOptions?: RequestOptions): Observable<Response>`
- `options(path: string, requestOptions?: RequestOptions): Observable<Response>`
If `apiPath` is configured it gets added in front of path.
- `get(path: string): Observable<Response>`
- `post(path: string, data: any): Observable<Response>`
- `put(path: string, data: any): Observable<Response>`
- `delete(path: string): Observable<Response>`
- `patch(path: string, data: any): Observable<Response>`
- `head(path: string): Observable<Response>`
- `options(path: string): Observable<Response>`
#### Example:
```javascript
Expand All @@ -246,18 +247,18 @@ this._tokenService.get('my-resource/1').map(res => res.json()).subscribe(
```
### .sendHttpRequest()
More customized requests can be send with the `.sendHttpRequest()`-function. It is used by all Http wrappers.
More customized requests can be send with the `.sendHttpRequest()`-function. It accepts the RequestOptions-Class.
More information can be found in the Angular2 API Reference [here](https://angular.io/docs/ts/latest/api/http/index/RequestOptions-class.html).
`sendHttpRequest(options: HttpRequestOptions): Observable<Response>`
`sendHttpRequest(options: RequestOptions): Observable<Response>`
#### Example:
```javascript
this.sendHttpRequest({
requestMethod: RequestMethod.Post,
path: 'my-resource/1',
data: mydata
requestOptions: myRequestOptions
});
this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Post,
url: 'my-resource/1',
data: mydata
}));
```
## Multiple User Types
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "angular2-token",
"version": "0.1.11",
"version": "0.1.12",
"description": "Angular2 service for token based authentication",
"main": "./angular2-token.js",
"typings": "./angular2-token.d.ts",
"scripts": {
"test": "karma start",
"build": "rm -rf lib && tsc"
"build": "rm -rf lib && tsc",
"prepublish": "npm run build"
},
"keywords": [
"angular2",
Expand Down
12 changes: 0 additions & 12 deletions src/angular2-token.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import {
RequestMethod,
RequestOptions
} from '@angular/http';

export interface UserType {
name: string;
path: string;
Expand Down Expand Up @@ -38,11 +33,4 @@ export interface Angular2TokenOptions {
userTypes?: UserType[];

oAuthPaths?: OAuthPaths;
}

export interface HttpRequestOptions {
requestMethod: RequestMethod,
path: string,
body?: any,
requestOptions?: RequestOptions
}
102 changes: 45 additions & 57 deletions src/angular2-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import 'rxjs/add/operator/share';
import {
UserType,
AuthData,
Angular2TokenOptions,
HttpRequestOptions
Angular2TokenOptions
} from './angular2-token.model';

@Injectable()
Expand Down Expand Up @@ -196,67 +195,60 @@ export class Angular2TokenService implements CanActivate {
}

// Standard HTTP requests
get(path: string, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Get,
path: path,
requestOptions: requestOptions
});
get(path: string): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Get,
url: this._constructApiPath() + path
}));
}

post(path: string, data: any, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Post,
path: path,
body: data,
requestOptions: requestOptions
});
post(path: string, data: any): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Post,
url: this._constructApiPath() + path,
body: data
}));
}

put(path: string, data: any, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Put,
path: path,
body: data,
requestOptions: requestOptions
});
put(path: string, data: any): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Put,
url: this._constructApiPath() + path,
body: data
}));
}

delete(path: string, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Delete,
path: path,
requestOptions: requestOptions
});
delete(path: string): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Delete,
url: this._constructApiPath() + path
}));
}

patch(path: string, data: any, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Patch,
path: path,
body: data,
requestOptions: requestOptions
});
patch(path: string, data: any): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Patch,
url: this._constructApiPath() + path,
body: data
}));
}

head(path: string, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Head,
path: path,
requestOptions: requestOptions
});
head(path: string): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Head,
url: this._constructApiPath() + path
}));
}

options(path: string, requestOptions?: RequestOptions): Observable<Response> {
return this.sendHttpRequest({
requestMethod: RequestMethod.Options,
path: path,
requestOptions: requestOptions
});
options(path: string): Observable<Response> {
return this.sendHttpRequest(new RequestOptions({
method: RequestMethod.Options,
url: this._constructApiPath() + path
}));
}

// Construct and send Http request
sendHttpRequest(options: HttpRequestOptions): Observable<Response> {
sendHttpRequest(requestOptions: RequestOptions): Observable<Response> {

let headers: Headers;
let baseRequestOptions: RequestOptions;
Expand All @@ -266,6 +258,7 @@ export class Angular2TokenService implements CanActivate {
if (this._currentAuthData != null)
headers = new Headers({
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
'Accept': 'application/json',
'access-token': this._currentAuthData.accessToken,
'client': this._currentAuthData.client,
'expiry': this._currentAuthData.expiry,
Expand All @@ -274,22 +267,17 @@ export class Angular2TokenService implements CanActivate {
});
else
headers = new Headers({
'Content-Type': 'application/json' // ToDo: Add to RequestOptions if available
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
'Accept': 'application/json'
});

// Construct Default Request Options
baseRequestOptions = new RequestOptions({
method: options.requestMethod,
url: this._constructApiPath() + options.path,
headers: headers,
body: options.body
headers: headers
})

// Merge standard and custom RequestOptions
if (options.requestOptions != null)
mergedRequestOptions = baseRequestOptions.merge(options.requestOptions);
else
mergedRequestOptions = baseRequestOptions;
mergedRequestOptions = baseRequestOptions.merge(requestOptions);

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

Expand Down

0 comments on commit 0a6b2de

Please sign in to comment.