Paldom / angular2-rest

Angular2 HTTP client to consume RESTful services. Built with TypeScript.
MIT License
248 stars 72 forks source link

alignment with ng2 interfaces #2

Closed Discountrobot closed 8 years ago

Discountrobot commented 8 years ago

Here is an update that mostly replaces/adds ng2 interfaces.

Note: This commit is meant as a discussion on how the component should align with the built-in interfaces

Additionally this commit:

import {RESTClient, GET, PUT, POST, DELETE, BaseUrl, Headers, DefaultHeaders, Path, Body, Query} from './angular-rest'
import {Injectable, Inject} from 'angular2/angular2'
import {Observable} from 'angular2/core'
import {Http, Request, Response} from 'angular2/Http'
import {IFilter} from './loopback-interfaces'

@Injectable()
@BaseUrl("http://localhost:3000/api")
@DefaultHeaders({
  'Accept': 'application/json',
  'Content-Type': 'application/json'
})
export class TodoRESTClient extends RESTClient {

  constructor( @Inject(Http) protected http: Http) {
    super(http)
  }

  protected requestInterceptor(req: Request) {
    req.headers.set('hello', 'world')
  }

  protected responseInterceptor(res: Response) {
    return res.json()
  }

  @GET("/")
  public find( @Query("filter") filter?: IFilter): Observable { return null }

  @GET("/{id}")
  public findById( @Path("id") id: string): Observable { return null }

  @DELETE("/{id}")
  public deleteById( @Path("id") id: string): Observable { return null }

  @GET("/{id}/exists")
  public exists( @Path("id") id: string): Observable { return null }

  @POST("/")
  public create( @Body data: any): Observable { return null; };

}
Paldom commented 8 years ago

Thanks mate, this is awesome! :+1:

Paldom commented 8 years ago

In the example, I'd rather use typed Observables, but its up to the user :)