-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
wpapi-tests.ts
103 lines (91 loc) · 2.9 KB
/
wpapi-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Initialize the client
import * as WPAPI from "wpapi";
const wp = new WPAPI({ endpoint: "http://src.wordpress-develop.dev/wp-json" });
// Callbacks
wp.posts().get((err: Error, data: any) => {
if (err) {
// handle err
}
// do something with the returned posts
});
// Promises
wp
.posts()
.then((data: any) => {
// do something with the returned posts
})
.catch((err: Error) => {
// handle error
});
// Auto-discover
const apiPromise = WPAPI.discover("http://my-site.com");
apiPromise.then(site => {
// If default routes were detected, they are now available
site.posts().then((posts: any[]) => {}); // etc
// If custom routes were detected, they can be accessed via .namespace()
// Custom routes have different methods to generate requests, so .authors()
// does not necessarily exist. You have use force type <WPRequest> or 'as
// Request'
(site.namespace("myplugin/v1").authors() as WPAPI.WPRequest).then(
(authors: any[]) => {
/* ... */
}
);
// Namespaces can be saved out to variables:
const myplugin = site.namespace("myplugin/v1");
myplugin
.authors()
.id(7)
.then((author: any) => {
/* ... */
});
});
// Authenticating with Auto-Discovery
const apiPromise2 = WPAPI.discover("http://my-site.com").then(site => {
return site.auth({
username: "admin",
password: "always use secure passwords"
});
});
apiPromise2.then(site => {
// site is now configured to use authentication
});
// You must authenticate to be able to POST (create) a post
const wp2 = new WPAPI({
endpoint: "http://your-site.com/wp-json",
// This assumes you are using basic auth, as described further below
username: "someusername",
password: "password"
});
wp2
.posts()
.create({
// "title" and "content" are the only required properties
title: "Your Post Title",
content: "Your post content",
// Post will be created as a draft by default if a specific "status"
// is not specified
status: "publish"
})
.then((response: any) => {
// "response" will hold all properties of your newly-created post,
// including the unique `id` the post was assigned on creation
});
// You must authenticate to be able to PUT (update) a post
// .id() must be used to specify the post we are updating
wp2
.posts()
.id(2501)
.update({
// Update the title
title: "A Better Title",
// Set the post live (assuming it was "draft" before)
status: "publish"
})
.then((response: any) => {});
// Custom routes
const site = new WPAPI({ endpoint: "http://www.yoursite.com/wp-json" });
const myCustomResource = site.registerRoute("myplugin/v1", "/author/(?P<id>)");
myCustomResource()
.id(17)
.then((response: any) => {}); // => myplugin/v1/author/17