|
1 | 1 | wp-api-angularjs
|
2 | 2 | ================
|
3 | 3 |
|
4 |
| -AngularJS services to consume [WP-API v2](http://v2.wp-api.org/) |
| 4 | +Angular2 services to consume [WP-API v2](http://v2.wp-api.org/) (< 2.5kb gziped) |
5 | 5 |
|
6 |
| -## Documentation |
7 |
| - |
8 |
| -<http://shprink.github.io/wp-api-angularjs/> |
| 6 | +If you want to use AngularJS v1, here is the latest version: [v2.0.0-rc3](https://github.com/shprink/wp-api-angularjs/tree/v2.0.0-rc3) |
9 | 7 |
|
10 | 8 | ## Installation
|
11 | 9 |
|
12 |
| -### npm |
| 10 | +### Dependencies |
13 | 11 |
|
14 |
| -``` |
15 |
| -npm install wp-api-angularjs |
16 |
| -``` |
| 12 | +make sure you have those packages installed: |
17 | 13 |
|
18 |
| -then import the dist file |
| 14 | +- '@angular/core' |
| 15 | +- '@angular/http' |
| 16 | +- 'rxjs' |
19 | 17 |
|
20 |
| -``` |
21 |
| -# ES5 |
22 |
| -require('wp-api-angularjs'); |
| 18 | +### via npm |
23 | 19 |
|
24 |
| -# or ES6 |
25 |
| -import 'wp-api-angularjs'; |
| 20 | +```shell |
| 21 | +npm install wp-api-angularjs@v3.0.x |
26 | 22 | ```
|
27 | 23 |
|
28 |
| -### Bower |
| 24 | +## Bootstrap |
29 | 25 |
|
30 |
| -``` |
31 |
| -bower install shprink/wp-api-angularjs |
32 |
| -``` |
33 | 26 |
|
34 |
| -## Setup |
| 27 | +```js |
| 28 | +import { |
| 29 | + WPAPI_PROVIDERS, |
| 30 | + defaultWpApi |
| 31 | +} from 'wp-api-angularjs'; |
35 | 32 |
|
36 |
| -To setup wp-api-angularjs in your app: |
| 33 | +import {App} from './app'; |
| 34 | + |
| 35 | +bootstrap(App, [ |
| 36 | + WPAPI_PROVIDERS, |
| 37 | + defaultWpApi({ |
| 38 | + baseUrl: "http://YOUR_DOMAIN/wp-json/", |
| 39 | + namespace: '/wp/v2' // (optional, default: '/wp/v2') |
| 40 | + }) |
| 41 | +]); |
37 | 42 |
|
38 | 43 | ```
|
39 |
| -var app = angular.module('app', ['wp-api-angularjs']); |
40 | 44 |
|
41 |
| -app.config(function (WpApiProvider) { |
42 |
| - WpApiProvider.setBaseUrl('http://www.example.com/wp-json/'); |
43 |
| -}); |
| 45 | +## API |
44 | 46 |
|
45 |
| -app.controller('PostsController', ['$scope', '$wpApiPosts', PostsController]); |
| 47 | +Every method return an Obervable. If you want to get a Promise you will need to add the rxjs `toPromise` operator: |
46 | 48 |
|
47 |
| -function PostsController($scope, $wpApiPosts) { |
48 |
| - $scope.posts = []; |
| 49 | +```js |
| 50 | +import 'rxjs/add/operator/toPromise'; |
49 | 51 |
|
50 |
| - $wpApiPosts.getList({ |
51 |
| - page: 1, |
52 |
| - per_page: 10 |
53 |
| - }).then(function (posts) { |
54 |
| - $scope.posts = posts.data; |
55 |
| - }); |
| 52 | +class Test { |
| 53 | + constructor(private wpApiPosts: WpApiPosts) { |
| 54 | + this.wpApiPosts.getList() |
| 55 | + .toPromise() |
| 56 | + .then(response => response.json()) |
| 57 | + .then(body => {}) |
| 58 | + .catch(error => {}) |
| 59 | + } |
56 | 60 | }
|
57 | 61 |
|
| 62 | +``` |
58 | 63 |
|
| 64 | +### RequestOptionsArgs |
| 65 | + |
| 66 | +Every request can have an optional [`RequestOptionsArgs`](https://angular.io/docs/ts/latest/api/http/index/RequestOptionsArgs-interface.html) object. |
| 67 | + |
| 68 | +```js |
| 69 | +class RequestOptionsArgs { |
| 70 | + url : string |
| 71 | + method : string|RequestMethod |
| 72 | + search : string|URLSearchParams |
| 73 | + headers : Headers |
| 74 | + body : any |
| 75 | + withCredentials : boolean |
| 76 | +} |
59 | 77 | ```
|
60 | 78 |
|
61 |
| -## Authentication |
| 79 | +This is where you can add query string to your request or change the headers. |
| 80 | + |
| 81 | +### WpApiPosts |
| 82 | + |
| 83 | +```ts |
| 84 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 85 | + get(postId, options?: RequestOptionsArgs): Observable<Response>; |
| 86 | + create(body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 87 | + update(postId, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 88 | + delete(postId, options?: RequestOptionsArgs): Observable<Response>; |
| 89 | + getMetaList(postId, options?: RequestOptionsArgs): Observable<Response>; |
| 90 | + getMeta(postId, metaId, options?: RequestOptionsArgs): Observable<Response>; |
| 91 | + getRevisionList(postId, options?: RequestOptionsArgs): Observable<Response>; |
| 92 | + getRevision(postId, revisionId, options?: RequestOptionsArgs): Observable<Response>; |
| 93 | + getCategoryList(postId, options?: RequestOptionsArgs): Observable<Response>; |
| 94 | + getCategory(postId, categoryId, options?: RequestOptionsArgs): Observable<Response>; |
| 95 | + getTagList(postId, options?: RequestOptionsArgs): Observable<Response>; |
| 96 | + getTag(postId, tagId, options?: RequestOptionsArgs): Observable<Response>; |
| 97 | +``` |
| 98 | + |
| 99 | +### WpApiPages |
| 100 | + |
| 101 | +```ts |
| 102 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 103 | + get(pageId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 104 | + create(body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 105 | + update(pageId: number, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 106 | + delete(pageId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 107 | + getMetaList(pageId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 108 | + getMeta(pageId: number, metaId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 109 | + getRevisionList(pageId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 110 | + getRevision(pageId: number, revisionId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 111 | +``` |
62 | 112 |
|
63 |
| -This library only supports basic auth. OAuth1 not being suitable for JS clients (it would mean exposing key and password out of the open) |
| 113 | +### WpApiComments |
64 | 114 |
|
65 |
| -### Basic auth |
| 115 | +```ts |
| 116 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 117 | + get(commentId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 118 | + create(body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 119 | + update(commentId: number, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 120 | + delete(commentId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 121 | +``` |
| 122 | +
|
| 123 | +### WpApiTypes |
66 | 124 |
|
67 |
| -Basic auth is only secured to use if used during the app run time and used with a secured connection to your Blog (via SSL). |
| 125 | +```ts |
| 126 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 127 | + get(postType: string, options?: RequestOptionsArgs): Observable<Response>; |
| 128 | +``` |
68 | 129 |
|
69 |
| -#### During run time |
| 130 | +### WpApiMedia |
70 | 131 |
|
71 |
| -Make sure your WP-API runs with an SSL certificate (https) otherwise this will expose your credentials at every request. |
| 132 | +```ts |
| 133 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 134 | + get(mediaId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 135 | + create(body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 136 | + update(mediaId: number, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 137 | + delete(mediaId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 138 | +``` |
72 | 139 |
|
73 |
| -Display a form for users to connect and use the following code to register credentials: |
| 140 | +### WpApiUsers |
74 | 141 |
|
| 142 | +```ts |
| 143 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 144 | + me(options?: RequestOptionsArgs): Observable<Response>; |
| 145 | + get(userId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 146 | + create(body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 147 | + update(userId: number, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 148 | + delete(userId: number, options?: RequestOptionsArgs): Observable<Response>; |
75 | 149 | ```
|
76 |
| -.controller(function(WpApi){ |
77 |
| - WpApi.setBasicCredentials(<login>, <password>); |
78 |
| -}); |
| 150 | +
|
| 151 | +### WpApiTaxonomies |
| 152 | +
|
| 153 | +```ts |
| 154 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 155 | + get(taxonomiesType: string, options?: RequestOptionsArgs): Observable<Response>; |
79 | 156 | ```
|
80 | 157 |
|
81 |
| -#### During configuration |
| 158 | +### WpApiStatuses |
| 159 | +
|
| 160 | +```ts |
| 161 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 162 | + get(statusesName: string, options?: RequestOptionsArgs): Observable<Response>; |
| 163 | +``` |
82 | 164 |
|
83 |
| -You can also set basic credentials during the configuration but use this should only be used for testing as it embed credentials in the application code. |
| 165 | +### WpApiTerms |
84 | 166 |
|
| 167 | +`taxonomiesType` can be `tags`, `categories` and more. |
| 168 | +
|
| 169 | +```ts |
| 170 | + getList(taxonomiesType: string, options?: RequestOptionsArgs): Observable<Response>; |
| 171 | + get(taxonomiesType: string, termId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 172 | + create(taxonomiesType: string, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 173 | + update(taxonomiesType: string, termId: number, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 174 | + delete(taxonomiesType: string, termId: number, options?: RequestOptionsArgs): Observable<Response>; |
85 | 175 | ```
|
86 |
| -.config(function(WpApiProvider){ |
87 |
| - WpApiProvider.setBasicCredentials(<login>, <password>); |
88 |
| -}); |
| 176 | +
|
| 177 | +### WpApiCustom |
| 178 | +
|
| 179 | +```ts |
| 180 | + getList(options?: RequestOptionsArgs): Observable<Response>; |
| 181 | + get(customId: number, options?: RequestOptionsArgs): Observable<Response>; |
| 182 | + create(body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 183 | + update(customId: number, body: any, options?: RequestOptionsArgs): Observable<Response>; |
| 184 | + delete(customId: number, options?: RequestOptionsArgs): Observable<Response>; |
89 | 185 | ```
|
90 | 186 |
|
| 187 | +## Authentication |
| 188 | +
|
| 189 | +TO BE DEFINED |
| 190 | +
|
91 | 191 | ## Contribute
|
92 | 192 |
|
93 |
| -``` |
| 193 | +```shell |
94 | 194 | npm install
|
95 | 195 | cp config.dist.json config.json
|
96 | 196 |
|
|
0 commit comments