1 |
|
2 | 'use strict'
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const path = require('path')
|
12 |
|
13 | const handlers = require('./handlers.js')
|
14 | const readRoutes = require('./routes/read.js')
|
15 |
|
16 | function getHandlerConfig (
|
17 | routeConfig /* : RouteConfiguration */,
|
18 | method /* : string */
|
19 | ) /* : Promise<HandlerConfiguration> */ {
|
20 | return handlers.getHandler(routeConfig.module, method)
|
21 | .then((handler) => ({
|
22 | handler,
|
23 | params: routeConfig.params || {}
|
24 | }))
|
25 | }
|
26 |
|
27 | function getRouteConfig (
|
28 | cwd /* : string */,
|
29 | route /* : string */
|
30 | ) /* : Promise<RouteConfiguration> */ {
|
31 | return readRoutes(cwd)
|
32 | .then((routeConfigs) => handlers.findRouteConfig(route, routeConfigs))
|
33 | .then((routeConfig) => {
|
34 | routeConfig.module = path.resolve(cwd, routeConfig.module)
|
35 | wipeRouteFromRequireCache(routeConfig)
|
36 | return routeConfig
|
37 | })
|
38 | }
|
39 |
|
40 | function wipeRouteFromRequireCache (
|
41 | routeConfig /* : RouteConfiguration */
|
42 | ) /* : void */ {
|
43 |
|
44 | if (routeConfig && require.cache[routeConfig.module]) {
|
45 | delete require.cache[routeConfig.module]
|
46 | }
|
47 | }
|
48 |
|
49 | module.exports = {
|
50 | getHandlerConfig,
|
51 | getRouteConfig,
|
52 | wipeRouteFromRequireCache
|
53 | }
|