Resource (REST) service for Angular 2+.
Did you miss the ngResource service from angular1.x in angular2+? Then you have come to the right place.
npm install --save ng-restly
- Create a service and extend it from ResourceService
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ResourceService } from 'ng-restly';
@Injectable()
export class PostService extends ResourceService {
constructor(protected http: HttpClient) {
this.url = 'http://api.example.com/posts/:id';
super(http);
}
}
- Inject your service wherever you would like to take the benefits of ng-restly
import { PostService } from './post.service';
export class PostComponent implements OnInit {
constructor(private postService: PostService) {}
post: any; // Best Practice: Create and use an Interface as type (ie: PostInterface)
ngOnInit() {
this.postService
.get({ id: 123 })
.subscribe(post => this.post = post);
}
}
See full documentation here.
ng-restly v1.0.0 supports the deprecated and soon to be removed @angular/http module. If you are using version below 4.4.0 of angular you have to install v1.0.0 of ng-restly.
npm install --save ng-restly@1.0.0
See full documentation for v1.0.0 here.
Run npm run test
to execute the unit tests via Karma. This will help you to run and debug your code if you wish to contribute to the development of this library.
Enjoy 😃