forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathearly-access-paths.js
33 lines (27 loc) · 1.12 KB
/
early-access-paths.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
const { chain } = require('lodash')
let paths
// This middleware finds all pages with `hidden: true` frontmatter
// and responds with a JSON array of all requests paths (and redirects) that lead to those pages.
// Requesting this path from EARLY_ACCESS_HOSTNAME will respond with an array of Early Access paths.
// Requesting this path from docs.github.com (production) will respond with an empty array (no Early Access paths).
module.exports = async (req, res, next) => {
if (req.path !== '/early-access-paths.json') return next()
if (
!req.headers ||
!req.headers['early-access-shared-secret'] ||
req.headers['early-access-shared-secret'] !== process.env.EARLY_ACCESS_SHARED_SECRET
) {
return res.status(401).send({ error: '401 Unauthorized' })
}
paths = paths || chain(req.context.pages)
.filter(page => page.hidden && page.languageCode === 'en')
.map(page => {
const permalinks = page.permalinks.map(permalink => permalink.href)
const redirects = Object.keys(page.redirects)
return permalinks.concat(redirects)
})
.flatten()
.uniq()
.value()
return res.json(paths)
}