UNPKG

1.25 kBJavaScriptView Raw
1/* @flow */
2'use strict'
3
4/* ::
5import type {
6 HandlerConfiguration,
7 RouteConfiguration
8} from '../types.js'
9*/
10
11const path = require('path')
12
13const handlers = require('./handlers.js')
14const readRoutes = require('./routes/read.js')
15
16function 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
27function 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
40function wipeRouteFromRequireCache (
41 routeConfig /* : RouteConfiguration */
42) /* : void */ {
43 // property names in require.cache are absolute paths
44 if (routeConfig && require.cache[routeConfig.module]) {
45 delete require.cache[routeConfig.module]
46 }
47}
48
49module.exports = {
50 getHandlerConfig,
51 getRouteConfig,
52 wipeRouteFromRequireCache
53}