Skip to content

Commit

Permalink
fix(interceptor): Intercept responses, fix URI encoding
Browse files Browse the repository at this point in the history
Closes issues #4 & #5
  • Loading branch information
Hugh Boylan committed Dec 12, 2016
1 parent 3005eb9 commit b2ce45d
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 18 deletions.
12 changes: 11 additions & 1 deletion demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {DemoService, Post} from './demo.service';
template: `
<h3>Sample Posts</h3>
<form (ngSubmit)="onSubmit()" *ngIf="!isLoading">
<form (ngSubmit)="onSubmit()" *ngIf="!isLoading" style="margin-bottom: 10px">
<div class="form-group">
<label for="userId">User ID</label>
<input type="text" class="form-control" id="userId" name="userId" placeholder="User ID" [(ngModel)]="demoPost.userId">
Expand All @@ -22,6 +22,8 @@ import {DemoService, Post} from './demo.service';
<button type="submit" class="btn btn-primary">Create</button>
</form>
<button class="btn btn-info" (click)="onGetUserPosts()">Get User {{demoPost.userId}} posts</button>
<table class="table table-hover">
<thead>
<tr><th>#</th><th>User</th><th>Title</th><th>Body</th></tr>
Expand Down Expand Up @@ -58,4 +60,12 @@ export class Demo implements OnInit {
this.demoPost = new Post(1, 'Demo Post', 'Lorem Ipsum');
})
}

onGetUserPosts() {
this.isLoading = true
this.service.getPosts(this.demoPost.userId).subscribe(posts => {
this.isLoading = false
this.posts = posts
})
}
}
8 changes: 6 additions & 2 deletions demo/demo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ export class DemoService extends RESTClient {
}

@GET('/posts')
@Produces<Post[]>()
public getPosts(@Query('userId') userId?: number): Observable<Post[]> {
@Produces<Post[]>((res: Response) => {
res.headers.forEach((values: string[], name: string) => {
console.log(name, '=', values)
})
})
public getPosts(@Query('$userId') userId?: number): Observable<Post[]> {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">ng2-http</a>
<a class="navbar-brand" href="https://www.npmjs.com/package/ng2-http">ng2-http</a>
</div>
<ul class="nav navbar-nav hidden-xs">
<li><a href="#demo">Demo</a></li>
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/src/rest.service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export declare function Headers(headersDef: any): (target: RESTClient, propertyK
/**
* Defines the type(s) that the responses can produce
*/
export declare function Produces<T>(): (target: RESTClient, propertyKey: string, descriptor: any) => any;
export declare function Produces<T>(interceptor?: (res: Response) => void): (target: RESTClient, propertyKey: string, descriptor: any) => any;
/**
* Path variable of a method's url, type: string
* @param {string} key - path key to bind value
Expand Down
9 changes: 7 additions & 2 deletions dist/esm/src/rest.service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/src/rest.service.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b2ce45d

Please sign in to comment.