@@ -111,7 +111,7 @@ function WPAPI( options ) {
111
111
* }
112
112
*
113
113
* // Delegate to default transport if no cached data was found
114
- * return WPAPI .transport.get( wpreq ).then(function( result ) {
114
+ * return this.constructor .transport.get( wpreq ).then(function( result ) {
115
115
* cache[ wpreq ] = result;
116
116
* return result;
117
117
* });
@@ -138,10 +138,10 @@ WPAPI.prototype.transport = function( transport ) {
138
138
// Local reference to avoid need to reference via `this` inside forEach
139
139
const _options = this . _options ;
140
140
141
- // Create the default transport if it does not exist
141
+ // Attempt to use the default transport if no override was provided
142
142
if ( ! _options . transport ) {
143
- _options . transport = WPAPI . transport ?
144
- Object . create ( WPAPI . transport ) :
143
+ _options . transport = this . constructor . transport ?
144
+ Object . create ( this . constructor . transport ) :
145
145
{ } ;
146
146
}
147
147
@@ -155,47 +155,6 @@ WPAPI.prototype.transport = function( transport ) {
155
155
return this ;
156
156
} ;
157
157
158
- /**
159
- * Convenience method for making a new WPAPI instance
160
- *
161
- * @example
162
- * These are equivalent:
163
- *
164
- * var wp = new WPAPI({ endpoint: 'http://my.blog.url/wp-json' });
165
- * var wp = WPAPI.site( 'http://my.blog.url/wp-json' );
166
- *
167
- * `WPAPI.site` can take an optional API root response JSON object to use when
168
- * bootstrapping the client's endpoint handler methods: if no second parameter
169
- * is provided, the client instance is assumed to be using the default API
170
- * with no additional plugins and is initialized with handlers for only those
171
- * default API routes.
172
- *
173
- * @example
174
- * These are equivalent:
175
- *
176
- * // {...} means the JSON output of http://my.blog.url/wp-json
177
- * var wp = new WPAPI({
178
- * endpoint: 'http://my.blog.url/wp-json',
179
- * json: {...}
180
- * });
181
- * var wp = WPAPI.site( 'http://my.blog.url/wp-json', {...} );
182
- *
183
- * @memberof ! WPAPI
184
- * @static
185
- * @param {String } endpoint The URI for a WP-API endpoint
186
- * @param {Object } routes The "routes" object from the JSON object returned
187
- * from the root API endpoint of a WP site, which should
188
- * be a dictionary of route definition objects keyed by
189
- * the route's regex pattern
190
- * @returns {WPAPI } A new WPAPI instance, bound to the provided endpoint
191
- */
192
- WPAPI . site = function ( endpoint , routes ) {
193
- return new WPAPI ( {
194
- endpoint : endpoint ,
195
- routes : routes ,
196
- } ) ;
197
- } ;
198
-
199
158
/**
200
159
* Generate a request against a completely arbitrary endpoint, with no assumptions about
201
160
* or mutation of path, filtering, or query parameters. This request is not restricted to
@@ -392,28 +351,43 @@ WPAPI.prototype.namespace = function( namespace ) {
392
351
} ;
393
352
394
353
/**
395
- * Take an arbitrary WordPress site, deduce the WP REST API root endpoint, query
396
- * that endpoint, and parse the response JSON. Use the returned JSON response
397
- * to instantiate a WPAPI instance bound to the provided site.
354
+ * Convenience method for making a new WPAPI instance for a given API root
355
+ *
356
+ * @example
357
+ * These are equivalent:
358
+ *
359
+ * var wp = new WPAPI({ endpoint: 'http://my.blog.url/wp-json' });
360
+ * var wp = WPAPI.site( 'http://my.blog.url/wp-json' );
361
+ *
362
+ * `WPAPI.site` can take an optional API root response JSON object to use when
363
+ * bootstrapping the client's endpoint handler methods: if no second parameter
364
+ * is provided, the client instance is assumed to be using the default API
365
+ * with no additional plugins and is initialized with handlers for only those
366
+ * default API routes.
367
+ *
368
+ * @example
369
+ * These are equivalent:
370
+ *
371
+ * // {...} means the JSON output of http://my.blog.url/wp-json
372
+ * var wp = new WPAPI({
373
+ * endpoint: 'http://my.blog.url/wp-json',
374
+ * json: {...}
375
+ * });
376
+ * var wp = WPAPI.site( 'http://my.blog.url/wp-json', {...} );
398
377
*
399
378
* @memberof ! WPAPI
400
379
* @static
401
- * @param {string } url A URL within a REST API-enabled WordPress website
402
- * @returns {Promise } A promise that resolves to a configured WPAPI instance bound
403
- * to the deduced endpoint, or rejected if an endpoint is not found or the
404
- * library is unable to parse the provided endpoint.
380
+ * @param {String } endpoint The URI for a WP-API endpoint
381
+ * @param {Object } routes The "routes" object from the JSON object returned
382
+ * from the root API endpoint of a WP site, which should
383
+ * be a dictionary of route definition objects keyed by
384
+ * the route's regex pattern
385
+ * @returns {WPAPI } A new WPAPI instance, bound to the provided endpoint
405
386
*/
406
- WPAPI . discover = ( url ) => {
407
- // Use WPAPI.site to make a request using the defined transport
408
- const req = WPAPI . site ( url ) . root ( ) . param ( 'rest_route' , '/' ) ;
409
- return req . get ( ) . then ( ( apiRootJSON ) => {
410
- const routes = apiRootJSON . routes ;
411
- return new WPAPI ( {
412
- // Derive the endpoint from the self link for the / root
413
- endpoint : routes [ '/' ] . _links . self ,
414
- // Bootstrap returned WPAPI instance with the discovered routes
415
- routes : routes ,
416
- } ) ;
387
+ WPAPI . site = ( endpoint , routes ) => {
388
+ return new WPAPI ( {
389
+ endpoint : endpoint ,
390
+ routes : routes ,
417
391
} ) ;
418
392
} ;
419
393
0 commit comments