This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
/
index.js
178 lines (149 loc) · 4.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// @flow
import { createRoutes, formatPattern } from "react-router"
import colors from "chalk"
import urlJoin from "url-join"
import arrayUnique from "../../_utils/array-unique"
const defaultConsole = console.log
const flattenRoute = (route) => {
// @todo remove the default route.path, user should use IndexRoute instead?
let routesUrls = route.path ? [ route.path ] : []
if (route.indexRoute) {
routesUrls.push(route.path)
}
if (route.childRoutes) {
const root = route.path
route.childRoutes.forEach((route) => {
routesUrls = [
...routesUrls,
...flattenRoute(route).map((r) => root ? urlJoin(root, r) : r),
]
})
}
return routesUrls
}
// only simple types will be accepted
// array<string | number> | string | number
// other will be skipped
const paramsListFromCollection = (collection) => {
const params = {}
collection.forEach((item) => {
if (!item.head) {
return
}
Object.keys(item.head).forEach((key) => {
// array<string | number>
if (
Array.isArray(item.head[key]) &&
item.head[key].length &&
(
typeof item.head[key][0] === "string" ||
typeof item.head[key][0] === "number"
)
) {
const k = key
// categories => category
.replace(/ies$/, "y")
// tags => tag
.replace(/s$/, "")
if (!params[k]) {
params[k] = []
}
params[k] = [
...params[k],
...item.head[key],
]
}
// string, number
if (
typeof item.head[key] === "string" ||
typeof item.head[key] === "number"
) {
if (!params[key]) {
params[key] = []
}
params[key].push(item.head[key])
}
})
})
params.splat = collection.map((item) => item.__url)
return params
}
const createUrlsFromParamsReplacementInUrl = (url, params, log) => {
// don't compute anything if url doesn't seems to have dynamic parameters
// react-router url params are like ``:that`` (or splat *)
if (url.indexOf(":") === -1 && url.indexOf("*") === -1) {
return [ url ]
}
const urls = []
const possibleErrorEnd = `parameter for path "${ url }"`
let missingKeys = []
let nonMissingKeys = []
Object.keys(params).forEach((paramName) => {
params[paramName].forEach((paramValue) => {
const urlParams = { [paramName]: paramValue }
try {
const hydratedUrl = formatPattern(url, urlParams)
urls.push(hydratedUrl)
nonMissingKeys.push(paramName)
}
catch (e) {
// log(paramName, e.message)
const matches = e.message.match(/Missing \"(.*)\" parameter for path/)
if (matches && matches[1]) {
missingKeys.push(matches[1])
}
if (
e.message.indexOf("Missing") === 0 &&
e.message.lastIndexOf(possibleErrorEnd) ===
(e.message.length - possibleErrorEnd.length - 1)
) {
throw e
}
}
})
})
nonMissingKeys = arrayUnique(nonMissingKeys)
missingKeys = arrayUnique(missingKeys).filter(
(key) => nonMissingKeys.indexOf(key) === -1
)
if (missingKeys.length) {
log("⚠️ " + colors.red(
"It looks like some parameters can't be mapped to create routes: ",
missingKeys.map((key) => ":" + key).join(", "),
))
}
// @todo improve the algorithm to avoid duplicates,
// we will probably get better perfs
return arrayUnique(urls.sort())
}
const hydrateRoutesUrls = (routesUrls, collection, log) => {
const paramsList = paramsListFromCollection(collection)
return routesUrls.reduce((acc, url) => {
return [
...acc,
...createUrlsFromParamsReplacementInUrl(url, paramsList, log),
]
}, [])
}
export default (
routes: React$Element<any>,
collection: PhenomicCollection,
// for testing
log: Function = defaultConsole,
): Array<string> => {
const flattenedRoutes = arrayUnique(
createRoutes(routes).reduce((acc, r) => [ ...acc, ...flattenRoute(r) ], [])
)
if (flattenedRoutes.filter((url) => url.indexOf("*") > -1).length > 1) {
throw new Error(
"Phenomic can only handle one splat (*) in react-router routes. \n" +
"You must use only one splat. If you have a specific need, do not "+
"hesitate to open an issue at " +
"https://github.com/MoOx/phenomic/issues/new"
)
}
const normalizedRoutes = flattenedRoutes.map(
(route) => "/" + route.replace(/^\/+/, "").replace(/\/+$/, "")
)
return hydrateRoutesUrls(normalizedRoutes, collection, log)
}