Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Paldom/angular2-rest
Angular2 HTTP client to consume RESTful services. Built with TypeScript.
https://github.com/Paldom/angular2-rest
Last synced: 2 months ago
JSON representation
Angular2 HTTP client to consume RESTful services. Built with TypeScript.
- Host: GitHub
- URL: https://github.com/Paldom/angular2-rest
- Owner: Paldom
- License: mit
- Created: 2015-12-06T18:41:13.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-12T10:59:18.000Z (about 6 years ago)
- Last Synced: 2024-04-29T08:21:52.281Z (9 months ago)
- Language: TypeScript
- Size: 169 KB
- Stars: 248
- Watchers: 24
- Forks: 72
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-angular-components - angular2-rest - Angular2 HTTP client to consume RESTful services. Built with TypeScript. (Uncategorized / Uncategorized)
- awesome-angular-components - angular2-rest - Angular2 HTTP client to consume RESTful services. Built with TypeScript. (Uncategorized / Uncategorized)
README
# angular2-rest
Angular2 HTTP client to consume RESTful services. Built on angular2/http with TypeScript.
**Note:** this solutions is not production ready, it's in a very basic alpha state. Any ideas or contributions are very welcomed :)## Installation
```sh
npm install angular2-rest
```## Example
```ts
import {Request, Response} from 'angular2/http';
import {RESTClient, GET, PUT, POST, DELETE, BaseUrl, Headers, DefaultHeaders, Path, Body, Query} from 'angular2-rest';import {Todo} from './models/Todo';
import {SessionFactory} from './sessionFactory';@Injectable()
@BaseUrl("http://localhost:3000/api/")
@DefaultHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
export class TodoRESTClient extends RESTClient {protected requestInterceptor(req: Request) {
if (SessionFactory.getInstance().isAuthenticated) {
req.headers.append('jwt', SessionFactory.getInstance().credentials.jwt);
}
}
protected requestInterceptor(req: Response) {
// do sg with responses
}@GET("todo/")
public getTodos( @Query("sort") sort?: string): Observable { return null; };@GET("todo/{id}")
public getTodoById( @Path("id") id: string): Observable { return null; };@POST("todo")
public postTodo( @Body todo: Todo): Observable { return null; };@PUT("todo/{id}")
public putTodoById( @Path("id") id: string, @Body todo: Todo): Observable { return null; };@DELETE("todo/{id}")
public deleteTodoById( @Path("id") id: string): Observable { return null; };}
```### Using it in your component
``` ts
@Component({
selector: 'to-do',
viewProviders: [TodoRESTClient],
})
@View({
templateUrl: 'components/to-do-template.html',
})
export class ToDoCmp {constructor(todoRESTClient: TodoRESTClient) {
}
//Use todoRESTClient
}
```
## API Docs### RESTClient
#### Methods:
- `getBaseUrl(): string`: returns the base url of RESTClient
- `getDefaultHeaders(): Object`: returns the default headers of RESTClient in a key-value pair### Class decorators:
- `@BaseUrl(url: string)`
- `@DefaultHeaders(headers: Object)`### Method decorators:
- `@GET(url: String)`
- `@POST(url: String)`
- `@PUT(url: String)`
- `@DELETE(url: String)`
- `@Headers(headers: Object)`### Parameter decorators:
- `@Path(key: string)`
- `@Query(key: string)`
- `@Header(key: string)`
- `@Body`# License
MIT