* 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 )
| 25 | * @returns {object} A dictionary of endpoint request handler factories |
| 26 | */ |
| 27 | function 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 | |
| 59 | module.exports = { |
| 60 | generate: generateEndpointFactories, |
no test coverage detected