From 21be2857d9842e2df771bfd35fc0e36487ef3e91 Mon Sep 17 00:00:00 2001 From: Guo Yunhe Date: Sun, 18 Mar 2018 19:33:21 +0200 Subject: [PATCH] wpapi: class support custom methods though without return type --- types/wpapi/index.d.ts | 17 ++++++++++++++--- types/wpapi/wpapi-tests.ts | 14 ++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/types/wpapi/index.d.ts b/types/wpapi/index.d.ts index ae079b0edc..cbaee37286 100644 --- a/types/wpapi/index.d.ts +++ b/types/wpapi/index.d.ts @@ -27,9 +27,6 @@ declare class WPAPI { */ static discover(url: string): Promise; - /** 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 { diff --git a/types/wpapi/wpapi-tests.ts b/types/wpapi/wpapi-tests.ts index 7e414daecc..f990ba09cb 100644 --- a/types/wpapi/wpapi-tests.ts +++ b/types/wpapi/wpapi-tests.ts @@ -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 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) => { /* ... */ }); });