* 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
( handlerSpec, resource, namespace )
| 18 | * @returns {Function} A constructor inheriting from {@link WPRequest} |
| 19 | */ |
| 20 | function createEndpointRequest( handlerSpec, resource, namespace ) { |
| 21 | |
| 22 | // Create the constructor function for this endpoint |
| 23 | class EndpointRequest extends WPRequest { |
| 24 | constructor( options ) { |
| 25 | super( options ); |
| 26 | |
| 27 | /** |
| 28 | * Semi-private instance property specifying the available URL path options |
| 29 | * for this endpoint request handler, keyed by ascending whole numbers. |
| 30 | * |
| 31 | * @property _levels |
| 32 | * @type {object} |
| 33 | * @private |
| 34 | */ |
| 35 | this._levels = handlerSpec._levels; |
| 36 | |
| 37 | // Configure handler for this endpoint's root URL path & set namespace |
| 38 | this |
| 39 | .setPathPart( 0, resource ) |
| 40 | .namespace( namespace ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Mix in all available shortcut methods for GET request query parameters that |
| 45 | // are valid within this endpoint tree |
| 46 | if ( typeof handlerSpec._getArgs === 'object' ) { |
| 47 | Object.keys( handlerSpec._getArgs ).forEach( ( supportedQueryParam ) => { |
| 48 | const mixinsForParam = mixins[ supportedQueryParam ]; |
| 49 | |
| 50 | // Only proceed if there is a mixin available AND the specified mixins will |
| 51 | // not overwrite any previously-set prototype method |
| 52 | if ( typeof mixinsForParam === 'object' ) { |
| 53 | Object.keys( mixinsForParam ).forEach( ( methodName ) => { |
| 54 | applyMixin( EndpointRequest.prototype, methodName, mixinsForParam[ methodName ] ); |
| 55 | } ); |
| 56 | } |
| 57 | } ); |
| 58 | } |
| 59 | |
| 60 | Object.keys( handlerSpec._setters ).forEach( ( setterFnName ) => { |
| 61 | // Only assign setter functions if they do not overwrite preexisting methods |
| 62 | if ( ! EndpointRequest.prototype[ setterFnName ] ) { |
| 63 | EndpointRequest.prototype[ setterFnName ] = handlerSpec._setters[ setterFnName ]; |
| 64 | } |
| 65 | } ); |
| 66 | |
| 67 | return EndpointRequest; |
| 68 | } |
| 69 | |
| 70 | module.exports = { |
| 71 | create: createEndpointRequest, |
no outgoing calls
no test coverage detected