MCPcopy Index your code
hub / github.com/WP-API/node-wpapi / generateEndpointFactories

Function generateEndpointFactories

lib/endpoint-factories.js:27–57  ·  view source on GitHub ↗

* Given an array of route definitions and a specific namespace for those routes, * recurse through the node tree representing all possible routes within the * provided namespace to define path value setters (and corresponding property * validators) for all possible variants of each resource's API

( routesByNamespace )

Source from the content-addressed store, hash-verified

25 * @returns {object} A dictionary of endpoint request handler factories
26 */
27function generateEndpointFactories( routesByNamespace ) {
28
29 return objectReduce( routesByNamespace, ( namespaces, routeDefinitions, namespace ) => {
30
31 // Create
32 namespaces[ namespace ] = objectReduce( routeDefinitions, ( handlers, routeDef, resource ) => {
33
34 const handlerSpec = createResourceHandlerSpec( routeDef, resource );
35
36 const EndpointRequest = createEndpointRequest( handlerSpec, resource, namespace );
37
38 // "handler" object is now fully prepared; create the factory method that
39 // will instantiate and return a handler instance
40 handlers[ resource ] = function( options ) {
41 return new EndpointRequest( {
42 ...this._options,
43 ...options,
44 } );
45 };
46
47 // Expose the constructor as a property on the factory function, so that
48 // auto-generated endpoint request constructors may be further customized
49 // when needed
50 handlers[ resource ].Ctor = EndpointRequest;
51
52 return handlers;
53 }, {} );
54
55 return namespaces;
56 }, {} );
57}
58
59module.exports = {
60 generate: generateEndpointFactories,

Callers 2

wpapi.jsFile · 0.85
registerRouteFunction · 0.85

Calls 1

createEndpointRequestFunction · 0.85

Tested by

no test coverage detected