Skip to content

Repository files navigation

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

npm install angular2-rest

Example

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'})exportclassTodoRESTClientextendsRESTClient{protectedrequestInterceptor(req: Request){if(SessionFactory.getInstance().isAuthenticated){req.headers.append('jwt',SessionFactory.getInstance().credentials.jwt);}}protectedrequestInterceptor(req: Response){// do sg with responses}
@GET("todo/")publicgetTodos( @Query("sort")sort?: string): Observable{returnnull;};
@GET("todo/{id}")publicgetTodoById( @Path("id")id: string): Observable{returnnull;};
@POST("todo")publicpostTodo( @Bodytodo: Todo): Observable{returnnull;};
@PUT("todo/{id}")publicputTodoById( @Path("id")id: string, @Bodytodo: Todo): Observable{returnnull;};
@DELETE("todo/{id}")publicdeleteTodoById( @Path("id")id: string): Observable{returnnull;};}

Using it in your component

@Component({selector: 'to-do',viewProviders: [TodoRESTClient],})
@View({templateUrl: 'components/to-do-template.html',})exportclassToDoCmp{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

About

Angular2 HTTP client to consume RESTful services. Built with TypeScript.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages