Skip to content

Commit cb22db9

Browse files
committed
Production ready
* Rename to @maxxton/angular-rest * Update to Angular 2.3.1 * Add collection format * Fix gulp build * Add tests * Update README.md
1 parent c664505 commit cb22db9

31 files changed

+1664
-3048
lines changed

README.md

+77-41
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,138 @@
11
# angular2-rest
2-
Angular2 HTTP client to consume RESTful services. Built on angular2/http with TypeScript.
3-
**Note:** this solutions is not production ready, it's in a very basic alpha state. Any ideas or contributions are very welcomed :)
2+
Angular2 HTTP client to consume RESTful services. Built on @angular/http with TypeScript.
3+
**Note:** Production Ready! (Well tested)
44

55
## Installation
66

77
```sh
8-
npm install angular2-rest
8+
npm install @maxxton/angular-rest
99
```
1010

1111
## Example
1212

1313
```ts
1414

15-
import {Request, Response} from 'angular2/http';
16-
import {RESTClient, GET, PUT, POST, DELETE, BaseUrl, Headers, DefaultHeaders, Path, Body, Query} from 'angular2-rest';
15+
import {Http, Request, Response} from '@angular/http';
16+
import {HttpClient, RESTClient, Client, GET, PUT, POST, DELETE, Headers, Path, Body, Query, Produces, MediaType} from '@maxxton/angular-rest';
1717

1818
import {Todo} from './models/Todo';
1919
import {SessionFactory} from './sessionFactory';
2020

2121
@Injectable()
22-
@BaseUrl("http://localhost:3000/api/")
23-
@DefaultHeaders({
24-
'Accept': 'application/json',
25-
'Content-Type': 'application/json'
22+
@Client({
23+
serviceId: 'todo-service',
24+
baseUrl: 'http://localhost:3000/api/',
25+
headers: {
26+
'content-type': 'application/json'
27+
}
2628
})
27-
export class TodoRESTClient extends RESTClient {
29+
export class TodoClient extends RestClient {
30+
31+
constructor(http:Http){
32+
super(<HttpClient>http);
33+
}
2834

29-
protected requestInterceptor(req: Request) {
35+
protected requestInterceptor(req: Request):void {
3036
if (SessionFactory.getInstance().isAuthenticated) {
3137
req.headers.append('jwt', SessionFactory.getInstance().credentials.jwt);
3238
}
3339
}
3440

35-
protected requestInterceptor(req: Response) {
36-
// do sg with responses
41+
protected responseInterceptor(res: Observable<Response>): Observable<any> {
42+
// do anything with responses
43+
return res;
3744
}
3845

39-
@GET("todo/")
40-
public getTodos( @Query("sort") sort?: string): Observable { return null; };
46+
@Get("todo/")
47+
@Produces(MediaType.JSON)
48+
public getTodos( @Query("page") page:number, @Query("size", {default: 20}) size?:number, @Query("sort") sort?: string): Observable<Todo[]> { return null; };
4149

42-
@GET("todo/{id}")
43-
public getTodoById( @Path("id") id: string): Observable { return null; };
50+
@Get("todo/{id}")
51+
@Map(resp => new Todo(resp.json()))
52+
public getTodoById( @Path("id") id: number): Observable<Todo>{ return null; };
4453

45-
@POST("todo")
46-
public postTodo( @Body todo: Todo): Observable { return null; };
54+
@Post("todo")
55+
@Headers({
56+
'content-type': 'application/json'
57+
})
58+
public postTodo( @Body todo: Todo): Observable<Response> { return null; };
4759

48-
@PUT("todo/{id}")
49-
public putTodoById( @Path("id") id: string, @Body todo: Todo): Observable { return null; };
60+
@Put("todo/{id}")
61+
public putTodoById( @Path("id") id: string, @Body todo: Todo): Observable<Response> { return null; };
5062

51-
@DELETE("todo/{id}")
52-
public deleteTodoById( @Path("id") id: string): Observable { return null; };
63+
@Delete("todo/{id}")
64+
public deleteTodoById( @Path("id") id: string): Observable<Response> { return null; };
5365

5466
}
5567
```
5668

5769
### Using it in your component
5870

59-
71+
**```app.module.ts```**
72+
``` ts
73+
@NgModule({
74+
declarations: [
75+
AppComponent
76+
],
77+
imports: [
78+
HttpModule
79+
],
80+
providers: [
81+
TodoClient
82+
],
83+
bootstrap: [AppComponent]
84+
})
85+
export class AppModule { }
86+
```
87+
**```todo.component.ts```**
6088
``` ts
6189
@Component({
6290
selector: 'to-do',
63-
viewProviders: [TodoRESTClient],
64-
})
65-
@View({
66-
templateUrl: 'components/to-do-template.html',
6791
})
6892
export class ToDoCmp {
6993

70-
constructor(todoRESTClient: TodoRESTClient) {
94+
constructor(private todoClient: TodoClient) {
7195
}
7296

73-
//Use todoRESTClient
97+
//Use todoClient
7498
}
7599
```
76100
## API Docs
77101

78102
### RESTClient
79103
#### Methods:
80-
- `getBaseUrl(): string`: returns the base url of RESTClient
81-
- `getDefaultHeaders(): Object`: returns the default headers of RESTClient in a key-value pair
104+
- `getServiceId(): string`: returns the serviceId of the RestClient
105+
- `getBaseUrl(): string`: returns the base url of RestClient
106+
- `getDefaultHeaders(): Object`: returns the default headers of RestClient in a key-value pair
82107

83108
### Class decorators:
84-
- `@BaseUrl(url: string)`
85-
- `@DefaultHeaders(headers: Object)`
109+
- `@Client(args:{serviceId?: string, baseUrl?: string, headers?: any})`
86110

87111
### Method decorators:
88-
- `@GET(url: String)`
89-
- `@POST(url: String)`
90-
- `@PUT(url: String)`
91-
- `@DELETE(url: String)`
112+
- `@Get(url: String)`
113+
- `@Post(url: String)`
114+
- `@Put(url: String)`
115+
- `@Patch(url: String)`
116+
- `@Delete(url: String)`
117+
- `@Head(url: String)`
92118
- `@Headers(headers: Object)`
93119

94120
### Parameter decorators:
95-
- `@Path(key: string)`
96-
- `@Query(key: string)`
97-
- `@Header(key: string)`
121+
- `@Path(name: string, value?:any|{value?:any})`
122+
- `@Query(name: string, value?:any|{value?:any,format?:string})`
123+
- `@Header(name: string, value?:any|{value?:any,format?:string})`
98124
- `@Body`
99125

126+
#### Collection Format
127+
Determines the format of the array if type array is used. (used for ``@Query`` and ``@Header``) Possible values are:
128+
* ``Format.CSV`` - comma separated values ``foo,bar``.
129+
* ``Format.SSV`` - space separated values ``foo bar``.
130+
* ``Format.TSV`` - tab separated values ``foo\tbar``.
131+
* ``Format.PIPES`` - pipe separated values ``foo|bar``.
132+
* ``Format.MULTI`` - corresponds to multiple parameter instances instead of multiple values for a single instance ``foo=bar&foo=baz``. This is valid only for parameters in "query" or "formData".
133+
134+
Default value is ``Format.CSV``.
135+
100136
# License
101137

102138
MIT

0 commit comments

Comments
 (0)