Angular2 HTTP client to consume RESTful services. Built on @angular/http with TypeScript.
Note: Production Ready! (Well tested)
echo @maxxton:registry=https://npm.maxxton.com > .npmrc
npm install @maxxton/angular-rest --save
import {Http, Request, Response} from '@angular/http';
import {HttpClient, RESTClient, Client, GET, PUT, POST, DELETE, Headers, Path, Body, Query, Produces, MediaType} from '@maxxton/angular-rest';
import {Todo} from './models/Todo';
import {SessionFactory} from './sessionFactory';
@Injectable()
@Client({
serviceId: 'todo-service',
baseUrl: 'http://localhost:3000/api/',
headers: {
'content-type': 'application/json'
}
})
export class TodoClient extends RestClient {
constructor(http:Http){
super(<HttpClient>http);
}
protected requestInterceptor(req: Request):void {
if (SessionFactory.getInstance().isAuthenticated) {
req.headers.append('jwt', SessionFactory.getInstance().credentials.jwt);
}
}
protected responseInterceptor(res: Observable<Response>): Observable<any> {
// do anything with responses
return res;
}
@Get("todo/")
@Produces(MediaType.JSON)
public getTodos( @Query("page") page:number, @Query("size", {default: 20}) size?:number, @Query("sort") sort?: string): Observable<Todo[]> { return null; };
@Get("todo/{id}")
@Map(resp => new Todo(resp.json()))
public getTodoById( @Path("id") id: number): Observable<Todo>{ return null; };
@Post("todo")
@Headers({
'content-type': 'application/json'
})
public postTodo( @Body todo: Todo): Observable<Response> { return null; };
@Put("todo/{id}")
public putTodoById( @Path("id") id: string, @Body todo: Todo): Observable<Response> { return null; };
@Delete("todo/{id}")
public deleteTodoById( @Path("id") id: string): Observable<Response> { return null; };
}
app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
HttpModule
],
providers: [
TodoClient
],
bootstrap: [AppComponent]
})
export class AppModule { }
todo.component.ts
@Component({
selector: 'to-do',
})
export class ToDoCmp {
constructor(private todoClient: TodoClient) {
}
//Use todoClient
}
getServiceId(): string
: returns the serviceId of the RestClientgetBaseUrl(): string
: returns the base url of RestClientgetDefaultHeaders(): Object
: returns the default headers of RestClient in a key-value pair
@Client(args:{serviceId?: string, baseUrl?: string, headers?: any})
@Get(url: String)
@Post(url: String)
@Put(url: String)
@Patch(url: String)
@Delete(url: String)
@Head(url: String)
@Headers(headers: Object)
@Map(mapper:(resp : any)=>any)
@OnEmit(emitter:(resp : Observable<any>)=>Observable<any>)
@Path(name: string, value?:any|{value?:any})
@Query(name: string, value?:any|{value?:any,format?:string})
@Header(name: string, value?:any|{value?:any,format?:string})
@Body
Determines the format of the array if type array is used. (used for @Query
and @Header
) Possible values are:
Format.CSV
- comma separated valuesfoo,bar
.Format.SSV
- space separated valuesfoo bar
.Format.TSV
- tab separated valuesfoo\tbar
.Format.PIPES
- pipe separated valuesfoo|bar
.Format.MULTI
- corresponds to multiple parameter instances instead of multiple values for a single instancefoo=bar&foo=baz
. This is valid only for parameters in "query" or "formData".
Default value is Format.CSV
.
MIT