this.resti.get('/document')
.query('prefer','simple')
.callback(result=>console.log(result))
.send();
To install this library, run:
$ npm install ng4-resti --save
Add the NgResti to the list of imports of your Angular4 Module
// Import NgResti
import { NgResti } from 'ng4-resti';
@NgModule({
...
// Specify NgResti as an import
imports: [NgResti.forRoot()],
...
})
export class AppModule { }
import {RestiService} from 'ng4-resti';
@Injectable()
export class ApiService {
constructor(private api: RestiService) {
}
}
getUsers(){
this.api.get('/users') // request URL is /users (other available functions are .head, .options, .delete)
.callback(res=>console.log(res)) // callback that consumes the response
.send() // sends the request
}
getUsers(){
this.api.get('/users') // request URL is /users (other available functions are .head, .options, .delete)
.query('name','islam') // URL becomes /users?name=islam
.query('gender','male') // URL becomes /users?name=islam&gender=male
.segment('/search') // URL becomes /users/search?name=islam&gender=male
.header('Authorization','Bearer ...') // sets a header
.callback(res=>console.log(res)) // callback that consumes the response
.send() //sends the request
}
addUser(){
this.api.post('/users') // request URL is /users (other available functions are .put, .patch)
.callback(res=>console.log(res)) // callback that consumes the response
.send() // sends the request
}
addUser(){
// request URL is /users, the body is set to the provided object
//(other available functions are .put, .patch)
this.api.post('/users', {name:'islam',gender:'male'})
.body({'age':'27'}) // body is now merged with the {age:'27'}
.segment('/create') // URL becomes /users/create
.header('Authorization','Bearer ...') // sets a header
.callback(res=>console.log(res)) // callback when the request returns
.send() //sends the request, no need to subscribe to the result cause we specified a callback
}
typically a callback recieves the result (This can be a Response or any if a transform is applied see below).
sample(){
this.api.get('/users')
.callback((result)=> console.log(result))
.send();
}
Transforms helps converting you Response instance before passing it to the callback if present, otherwise the result of the transform is returned
sample(){
this.api.get('/users')
.transform((res: Response)=>{
return res.json();
})
.callback((result)=> console.log(result))
.send();
}
MIT © Islam Al-Rayan