* Create and return a handler for an arbitrary WP REST API endpoint. * * The first two parameters mirror `register_rest_route` in the REST API * codebase: * * @memberof! WPAPI# * @param {string} namespace A namespace string, e.g. 'myplugin/v1' * @param {string} restBase A
( namespace, restBase, options = {} )
| 21 | * @returns {Function} An endpoint handler factory function for the specified route |
| 22 | */ |
| 23 | function registerRoute( namespace, restBase, options = {} ) { |
| 24 | // Support all methods until requested to do otherwise |
| 25 | let supportedMethods = [ 'head', 'get', 'patch', 'put', 'post', 'delete' ]; |
| 26 | |
| 27 | if ( Array.isArray( options.methods ) ) { |
| 28 | // Permit supported methods to be specified as an array |
| 29 | supportedMethods = options.methods.map( method => method.trim().toLowerCase() ); |
| 30 | } else if ( typeof options.methods === 'string' ) { |
| 31 | // Permit a supported method to be specified as a string |
| 32 | supportedMethods = [ options.methods.trim().toLowerCase() ]; |
| 33 | } |
| 34 | |
| 35 | // Ensure that if GET is supported, then HEAD is as well, and vice-versa |
| 36 | if ( supportedMethods.indexOf( 'get' ) !== -1 && supportedMethods.indexOf( 'head' ) === -1 ) { |
| 37 | supportedMethods.push( 'head' ); |
| 38 | } else if ( supportedMethods.indexOf( 'head' ) !== -1 && supportedMethods.indexOf( 'get' ) === -1 ) { |
| 39 | supportedMethods.push( 'get' ); |
| 40 | } |
| 41 | |
| 42 | const fullRoute = namespace |
| 43 | // Route should always have preceding slash |
| 44 | .replace( /^[\s/]*/, '/' ) |
| 45 | // Route should always be joined to namespace with a single slash |
| 46 | .replace( /[\s/]*$/, '/' ) + restBase.replace( /^[\s/]*/, '' ); |
| 47 | |
| 48 | const routeObj = {}; |
| 49 | routeObj[ fullRoute ] = { |
| 50 | namespace: namespace, |
| 51 | methods: supportedMethods, |
| 52 | }; |
| 53 | |
| 54 | // Go through the same steps used to bootstrap the client to parse the |
| 55 | // provided route out into a handler request method |
| 56 | const routeTree = buildRouteTree( routeObj ); |
| 57 | // Parse the mock route object into endpoint factories |
| 58 | const endpointFactories = generateEndpointFactories( routeTree )[ namespace ]; |
| 59 | const EndpointRequest = endpointFactories[ Object.keys( endpointFactories )[ 0 ] ].Ctor; |
| 60 | |
| 61 | if ( options && options.params ) { |
| 62 | options.params.forEach( ( param ) => { |
| 63 | // Only accept string parameters |
| 64 | if ( typeof param !== 'string' ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // If the parameter can be mapped to a mixin, apply that mixin |
| 69 | if ( typeof mixins[ param ] === 'object' ) { |
| 70 | Object.keys( mixins[ param ] ).forEach( ( key ) => { |
| 71 | applyMixin( EndpointRequest.prototype, key, mixins[ param ][ key ] ); |
| 72 | } ); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Attempt to create a simple setter for any parameters for which |
| 77 | // we do not already have a custom mixin |
| 78 | applyMixin( EndpointRequest.prototype, param, paramSetter( param ) ); |
| 79 | } ); |
| 80 | } |
no test coverage detected