wpapi: class support custom methods though without return type

This commit is contained in:
Guo Yunhe
2018-03-18 19:33:21 +02:00
parent c59f06c21f
commit 21be2857d9
2 changed files with 22 additions and 9 deletions
+14 -3
View File
@@ -27,9 +27,6 @@ declare class WPAPI {
*/
static discover(url: string): Promise<WPAPI>;
/** Start a request of plugin authors endpoint */
authors(): WPAPI.WPRequest;
/** Start a request against /categories endpoint */
categories(): WPAPI.WPRequest;
@@ -163,6 +160,13 @@ declare class WPAPI {
* @param url The URL to request
*/
url(url: string): WPAPI.WPRequest;
/**
* An API client can define its parameter methods, like .authors(), .cart(),
* .products(). They are usually decided by WPAPI namespaces configuration
* object. They have WPRequest return type.
*/
[customRoutesMethod: string]: any;
}
/*~ If you want to expose types from your module as well, you can
@@ -411,6 +415,13 @@ declare namespace WPAPI {
* the regular expression the API uses to goven that segment.
*/
validatePath(): WPRequest;
/**
* A request can define its parameter methods, like .id(), .date(),
* .author(). They are usually decided by WPAPI routes configuration
* object.
*/
[customParamsMethod: string]: any;
}
export interface WPAPIOptions extends Credentials {
+8 -6
View File
@@ -28,19 +28,21 @@ apiPromise.then(site => {
site.posts().then((posts: Object[]) => {}); // etc
// If custom routes were detected, they can be accessed via .namespace()
site
.namespace("myplugin/v1")
.authors()
.then((authors: Object[]) => {
// 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: Object[]) => {
.then((author: any) => {
/* ... */
});
});