Skip to content

Commit 3c13882

Browse files
authored
Merge pull request #1 from manudss/version6
Version 6.1
2 parents 9e0dd6a + 207b1aa commit 3c13882

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+13519
-48
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules
2+
/test-api/node_modules/
3+
/dist

.metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"skipMetadataEmit": true,
3+
"skipTemplateCodegen": true
4+
}

README.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# ngx-http-rest
1+
# ngx-http-annotations
22

33
This library allows to interact with rest api in your angular app.
44
It contains:
55

66
- Annotations for http methods (@GET, @POST, @PUT, @DELETE, @OPTIONS, @HEAD, @PATCH)
77
- Annotations for adding headers, setting produces results and intercepting response
8-
- Params anotations
8+
- Params annotations
9+
10+
forked from : https://github.com/Mixalloff/ngx-http-rest
911

1012
### Installation
1113

1214
Install through npm:
1315

1416
```sh
15-
$ npm install ngx-http-rest --save
17+
$ npm install ngx-http-annotations --save
1618
```
1719

1820

@@ -23,7 +25,7 @@ Example of using library.
2325
1) Plug the HttpRestModule into your AppModule
2426

2527
```typescript
26-
import { HttpRestModule } from 'ngx-http-rest';
28+
import { HttpRestModule } from 'ngx-http-annotations';
2729
import { NgModule } from '@angular/core';
2830

2931
@NgModule({
@@ -39,7 +41,7 @@ export class AppModule {
3941

4042

4143
```typescript
42-
import { HttpRestService, GET, Path, PathParam, QueryParam, QueryParams } from 'ngx-http-rest';
44+
import { HttpRestService, GET, POST, DELETE, Path, PathParam, Body, QueryParam, QueryParams, ResponseObservable } from 'ngx-http-annotations';
4345
import { Injectable } from '@angular/core';
4446
import RestConfig from 'app/core/configs/rest.config';
4547

@@ -71,7 +73,6 @@ export class SomeRestService extends HttpRestService {
7173
getGoodsItemById(@PathParam('id') itemId: number): any {}
7274

7375
@GET
74-
@Interceptor(SomeRestService.logInterceptor) /* Set response interceptor */
7576
@Path('/:id/child/:childId') /* Few path params */
7677
getChildrenOfSomeGoods(@PathParam('id') id: number,
7778
@PathParam('childId') childId: number
@@ -83,14 +84,19 @@ export class SomeRestService extends HttpRestService {
8384
createGoods(@Body /* Body of POST request */ goodsObject: GoodsItem): any {}
8485

8586
@DELETE
86-
@NoResponse /* This method doesn`t process the body of response */
8787
@Path('/:id')
8888
removeGoodsById(@PathParam('id') itemId: number): any {}
89-
90-
private static logInterceptor(response: any) {
91-
console.log(response);
89+
90+
@GET
91+
@Path('posts')
92+
/**
93+
* getPostForUserId(3, 2) : call the the url /posts?userId=2 and only take 3 results
94+
*/
95+
public getPostForUserId(number: number, @QueryParam('userId') userId: number, @ResponseObservable res: Observable<any> = undefined): Observable<any> {
96+
return res.pipe(map((response) => response.slice(0, number)));
9297
}
9398

99+
94100
}
95101
```
96102

@@ -125,10 +131,27 @@ Available annotations:
125131
- @Path - set path of url for request. Combined class @Path annotation value and current method @Path. Path params passed with ":". For example @Path('/someurl/:someParam')
126132
- @Headers - set headers for request (if annotate class, then all class methods getting this headers. method Headers merge with class Headers)
127133
- @Produces - setting expected response type. By default Reponse transformed by .json() method
128-
- @NoResponse (alias for @Produces(null)) - if expected empty response body, you need to set that annotation
129-
- @DefaultResponse (alias for @Produces(Response)) - response doesn`t transformed with .json() method. Returned pure Response object
130-
3) Parameters
134+
- @Observes - setting http observes.
135+
3) Parameters
131136
- @PathParam (or @Path) - pass current parameter by name to collected url. Example: someFunc(@PathParam('id') itemId: number) {}
132137
- @Body - pass body object into request. Ex.: someMethod(@Body bodyObject: any){}
133138
- @QueryParam - pass single query parameters into request. Ex.: someMethod(@QueryParam('a') a: any, @QueryParam('b') b: any) {}. someMethod(1, 2) -> ..requested_url..?a=1&b=2
134-
- @QueryParams - pass object with few query params. Ex.: someMethod(@QueryParams queryObj: any){}. someMethod({x: 1, y: 2, z: 3}) -> ..requested_url..?x=1&y=2&z=3
139+
- @QueryParams - pass object with few query params. Ex.: someMethod(@QueryParams queryObj: any){}. someMethod({x: 1, y: 2, z: 3}) -> ..requested_url..?x=1&y=2&z=3
140+
- @ResponseObservable - specify in witch function params, the response observable will be added. Ex.: someMethod(@ResponseObservable res: Observable<any> = undefined){ /* transform request */ return res; }. need to initialise as undefined to pass compile error, and return a response.
141+
142+
143+
#### Transform response with all rxjs function
144+
145+
By adding the parameters @ResponseObservable you can specify, where add the observable response,
146+
147+
```typescript
148+
149+
@GET
150+
@Path('posts')
151+
/**
152+
* getPostForUserId(3, 2) : call the the url /posts?userId=2 and only take 3 results
153+
*/
154+
public getPostForUserId(number: number, @QueryParam('userId') userId: number, @ResponseObservable res: Observable<any> = undefined): Observable<any> {
155+
return res.pipe(map((response) => response.slice(0, number)));
156+
}
157+
```

dist/ngx-http-annotations/README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# ngx-http-annotations
2+
3+
This library allows to interact with rest api in your angular app.
4+
It contains:
5+
6+
- Annotations for http methods (@GET, @POST, @PUT, @DELETE, @OPTIONS, @HEAD, @PATCH)
7+
- Annotations for adding headers, setting produces results and intercepting response
8+
- Params annotations
9+
10+
### Installation
11+
12+
Install through npm:
13+
14+
```sh
15+
$ npm install ngx-http-annotations --save
16+
```
17+
18+
19+
### Development
20+
21+
Example of using library.
22+
23+
1) Plug the HttpRestModule into your AppModule
24+
25+
```typescript
26+
import { HttpRestModule } from 'ngx-http-annotations';
27+
import { NgModule } from '@angular/core';
28+
29+
@NgModule({
30+
imports: [
31+
HttpRestModule
32+
]
33+
})
34+
export class AppModule {
35+
}
36+
```
37+
38+
2) Create a service to work with rest api. Inherit it from HttpRestService from 'ngx-http-annotations'. Put annotations on the class, methods and params.
39+
40+
41+
```typescript
42+
import { HttpRestService, GET, Path, PathParam, QueryParam, QueryParams } from 'ngx-http-annotations';
43+
import { Injectable } from '@angular/core';
44+
import RestConfig from 'app/core/configs/rest.config';
45+
46+
interface GoodsItem {
47+
id: number,
48+
name: string,
49+
price: number,
50+
sales?: boolean;
51+
desc?: string;
52+
children?: Array<GoodsItem>;
53+
}
54+
55+
@Injectable()
56+
@Headers({
57+
'someHeader1': 'headerValue1',
58+
'someHeader2': 'headerValue2'
59+
})
60+
@Path(`/test/goods`)
61+
export class SomeRestService extends HttpRestService {
62+
63+
@GET
64+
getGoods(@QueryParams /* Object with queryParams { [name: string]: [value: any] } */ queryObj?: any): any {}
65+
66+
@GET
67+
getGoodsBySomeParam(@QueryParam('sales') /* ...?sales= */ isSold: boolean): any {}
68+
69+
@GET
70+
@Path('/:id')
71+
getGoodsItemById(@PathParam('id') itemId: number): any {}
72+
73+
@GET
74+
@Path('/:id/child/:childId') /* Few path params */
75+
getChildrenOfSomeGoods(@PathParam('id') id: number,
76+
@PathParam('childId') childId: number
77+
@QueryParam('sales') isSold: boolean,
78+
@QueryParam('someParam') some: any): any {}
79+
80+
@POST
81+
@Path('/create')
82+
createGoods(@Body /* Body of POST request */ goodsObject: GoodsItem): any {}
83+
84+
@DELETE
85+
@Path('/:id')
86+
removeGoodsById(@PathParam('id') itemId: number): any {}
87+
88+
89+
}
90+
```
91+
92+
3) Call the request method from component
93+
94+
```typescript
95+
import { Component, OnInit } from '@angular/core';
96+
import { SomeRestService } from './some-rest.service';
97+
98+
@Component({
99+
selector: 'some-test-component',
100+
templateUrl: './test-component.component.html',
101+
styleUrls: ['./test-component.component.css'],
102+
providers: [SomeRestService]
103+
})
104+
export class TestComponent implements OnInit {
105+
constructor(private someRestService: SomeRestService){}
106+
107+
ngOnInit() {
108+
this.someRestService
109+
.getGoods()
110+
.subscribe( goods => console.log(goods) );
111+
}
112+
}
113+
```
114+
115+
### Description
116+
Available annotations:
117+
1) Request methods
118+
@GET, @POST, @PUT, @DELETE, @OPTIONS, @HEAD, @PATCH - marks methods implementing the corresponding requests
119+
2) Added settings
120+
- @Path - set path of url for request. Combined class @Path annotation value and current method @Path. Path params passed with ":". For example @Path('/someurl/:someParam')
121+
- @Headers - set headers for request (if annotate class, then all class methods getting this headers. method Headers merge with class Headers)
122+
- @Produces - setting expected response type. By default Reponse transformed by .json() method
123+
- @Observes - setting http observes.
124+
3) Parameters
125+
- @PathParam (or @Path) - pass current parameter by name to collected url. Example: someFunc(@PathParam('id') itemId: number) {}
126+
- @Body - pass body object into request. Ex.: someMethod(@Body bodyObject: any){}
127+
- @QueryParam - pass single query parameters into request. Ex.: someMethod(@QueryParam('a') a: any, @QueryParam('b') b: any) {}. someMethod(1, 2) -> ..requested_url..?a=1&b=2
128+
- @QueryParams - pass object with few query params. Ex.: someMethod(@QueryParams queryObj: any){}. someMethod({x: 1, y: 2, z: 3}) -> ..requested_url..?x=1&y=2&z=3
129+
130+
131+
### Credits
132+
133+
Originaly created by "Mixalloff"
134+
forked from https://github.com/Mixalloff/ngx-http-rest
135+
136+
Updated by Manudss.
137+

0 commit comments

Comments
 (0)