This library is designed to make it easy for your Angular2 application to request specific resources from a WordPress install.
## Abstract
Services:
WpService
One service for all operations (Access/Authenticate/Configure).
Components:
<wp-collection>
Inputs: args, endpoint - Output: collection response.<wp-model>
Inputs: id, args, endpoint - Output: model response.
Helper Classes:
WpPost
Useful class for posts and pages (contains functions for accessing embedded posts).WpUser
Interface for user response.WpQueryArgs
Class for creating query arguments for collection/model.WpEndpoint
List of default endpoints and their addresses.
Default Endpoints are : posts
, pages
, users
, categories
, tags
, taxonomies
, statuses
, comments
, media
WpService
├── config
| ├── baseUrl: string
| ├── timeOut: number
| ├── errors: Observable
| ├── loading: Observable
|
├── discover(url): Observable
├── link(url): Observable
├── collection()
| ├── endpoint(ep)
| ├── get(args?): Observable
| ├── more(): Observable
├── model()
| ├── endpoint($ep)
| ├── get(id, args?): Observable
| ├── add(body): Observable
| ├── update(id, body): Observable
| ├── delete(id): Observable
├── auth()
| ├── basic(username, password): Observable
| ├── cookies(): Observable
| ├── logout()
### Initialization
Set your WordPress base url in the root component
constructor(wpService: WpService){
}
ngOnInit(){
wpService.config.baseUrl = "http://localhost/wordpress";
wpService.config.loading.subscribe(...);
wpService.config.errors.subscribe(...);
}
See [Initializing example](/examples/Initilizing WP Service.ts).
After initializing, you can either consume the library as a service, or as a component.
**For collection:**
A basic example of fetching 6 embedded posts:
import {WpService,WpEndpoint,WpQueryArgs,CollectionResponse} from "ng2-wp-api";
.
.
var endpoint = WpEndpoint.posts;
var args = new WpQueryArgs({
perPage: 6,
embed: true
});
this.wpService.collection()
.endpoint(endpoint)
.get(args)
.subscribe((res: CollectionResponse) => {
this.data = res.data;
this.pagination = res.pagination;
});
See [WpService Collection example](/examples/Collection using the service.ts)
**For collection:**
<wp-collection [args]="args" [endpoint]="endpoint" (response)="wpResponse($event)">
</wp-collection>
WpCollection component gets a new response automatically when the input [args]
value has set/changed.
See [Collection Component example](/examples/Collection using the component.ts)
NOTE: Make sure enableProdMode()
is called to avoid onChanges
error.
## Authentication
this.wpService.auth().basic('username', 'password').subscribe(
(loggedInUser: WpUser)=> {
console.log(loggedInUser);
});
this.wpService.auth().cookies().subscribe(
(loggedInUser: WpUser)=> {
console.log(loggedInUser);
});
Direct Link
If you want to do a GET
request for any URL, Use WpService.link(url).subscribe(...)
to get the advantage of error and loading notifiers.