|
1 | 1 | # 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) |
4 | 4 |
|
5 | 5 | ## Installation
|
6 | 6 |
|
7 | 7 | ```sh
|
8 |
| -npm install angular2-rest |
| 8 | +npm install @maxxton/angular-rest |
9 | 9 | ```
|
10 | 10 |
|
11 | 11 | ## Example
|
12 | 12 |
|
13 | 13 | ```ts
|
14 | 14 |
|
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'; |
17 | 17 |
|
18 | 18 | import {Todo} from './models/Todo';
|
19 | 19 | import {SessionFactory} from './sessionFactory';
|
20 | 20 |
|
21 | 21 | @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 | + } |
26 | 28 | })
|
27 |
| -export class TodoRESTClient extends RESTClient { |
| 29 | +export class TodoClient extends RestClient { |
| 30 | + |
| 31 | + constructor(http:Http){ |
| 32 | + super(<HttpClient>http); |
| 33 | + } |
28 | 34 |
|
29 |
| - protected requestInterceptor(req: Request) { |
| 35 | + protected requestInterceptor(req: Request):void { |
30 | 36 | if (SessionFactory.getInstance().isAuthenticated) {
|
31 | 37 | req.headers.append('jwt', SessionFactory.getInstance().credentials.jwt);
|
32 | 38 | }
|
33 | 39 | }
|
34 | 40 |
|
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; |
37 | 44 | }
|
38 | 45 |
|
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; }; |
41 | 49 |
|
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; }; |
44 | 53 |
|
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; }; |
47 | 59 |
|
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; }; |
50 | 62 |
|
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; }; |
53 | 65 |
|
54 | 66 | }
|
55 | 67 | ```
|
56 | 68 |
|
57 | 69 | ### Using it in your component
|
58 | 70 |
|
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```** |
60 | 88 | ``` ts
|
61 | 89 | @Component({
|
62 | 90 | selector: 'to-do',
|
63 |
| - viewProviders: [TodoRESTClient], |
64 |
| -}) |
65 |
| -@View({ |
66 |
| - templateUrl: 'components/to-do-template.html', |
67 | 91 | })
|
68 | 92 | export class ToDoCmp {
|
69 | 93 |
|
70 |
| - constructor(todoRESTClient: TodoRESTClient) { |
| 94 | + constructor(private todoClient: TodoClient) { |
71 | 95 | }
|
72 | 96 |
|
73 |
| - //Use todoRESTClient |
| 97 | + //Use todoClient |
74 | 98 | }
|
75 | 99 | ```
|
76 | 100 | ## API Docs
|
77 | 101 |
|
78 | 102 | ### RESTClient
|
79 | 103 | #### 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 |
82 | 107 |
|
83 | 108 | ### Class decorators:
|
84 |
| -- `@BaseUrl(url: string)` |
85 |
| -- `@DefaultHeaders(headers: Object)` |
| 109 | +- `@Client(args:{serviceId?: string, baseUrl?: string, headers?: any})` |
86 | 110 |
|
87 | 111 | ### 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)` |
92 | 118 | - `@Headers(headers: Object)`
|
93 | 119 |
|
94 | 120 | ### 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})` |
98 | 124 | - `@Body`
|
99 | 125 |
|
| 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 | + |
100 | 136 | # License
|
101 | 137 |
|
102 | 138 | MIT
|
0 commit comments