forked from WP-API/node-wpapi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathendpoint-request.js
More file actions
72 lines (61 loc) · 2.26 KB
/
Copy pathendpoint-request.js
File metadata and controls
72 lines (61 loc) · 2.26 KB
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
/**
* @module endpoint-request
*/
'use strict';
const WPRequest = require( './constructors/wp-request' );
const mixins = require( './mixins' );
const applyMixin = require( './util/apply-mixin' );
/**
* Create an endpoint request handler constructor for a specific resource tree
*
* @method create
* @param {Object} handlerSpec A resource handler specification object
* @param {String} resource The root resource of requests created from the returned factory
* @param {String} namespace The namespace string for the returned factory's handlers
* @returns {Function} A constructor inheriting from {@link WPRequest}
*/
function createEndpointRequest( handlerSpec, resource, namespace ) {
// Create the constructor function for this endpoint
class EndpointRequest extends WPRequest {
constructor( options ) {
super( options );
/**
* Semi-private instance property specifying the available URL path options
* for this endpoint request handler, keyed by ascending whole numbers.
*
* @property _levels
* @type {object}
* @private
*/
this._levels = handlerSpec._levels;
// Configure handler for this endpoint's root URL path & set namespace
this
.setPathPart( 0, resource )
.namespace( namespace );
}
}
// Mix in all available shortcut methods for GET request query parameters that
// are valid within this endpoint tree
if ( typeof handlerSpec._getArgs === 'object' ) {
Object.keys( handlerSpec._getArgs ).forEach( ( supportedQueryParam ) => {
const mixinsForParam = mixins[ supportedQueryParam ];
// Only proceed if there is a mixin available AND the specified mixins will
// not overwrite any previously-set prototype method
if ( typeof mixinsForParam === 'object' ) {
Object.keys( mixinsForParam ).forEach( ( methodName ) => {
applyMixin( EndpointRequest.prototype, methodName, mixinsForParam[ methodName ] );
} );
}
} );
}
Object.keys( handlerSpec._setters ).forEach( ( setterFnName ) => {
// Only assign setter functions if they do not overwrite preexisting methods
if ( ! EndpointRequest.prototype[ setterFnName ] ) {
EndpointRequest.prototype[ setterFnName ] = handlerSpec._setters[ setterFnName ];
}
} );
return EndpointRequest;
}
module.exports = {
create: createEndpointRequest,
};