forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.js
48 lines (37 loc) · 1.64 KB
/
rest.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
import path from 'path'
import getRest, { restRepoCategoryExceptions } from '../../lib/rest/index.js'
import removeFPTFromPath from '../../lib/remove-fpt-from-path.js'
// Global cache to avoid calling getRest() more than once
let rest = null
export default async function restContext(req, res, next) {
// Bail early because these are pointless to contextualize
if (req.path.startsWith('/_next/static')) return next()
if (!rest) {
rest = await getRest()
}
req.context.rest = rest
// link to include in `Works with GitHub Apps` notes
// e.g. /ja/rest/reference/apps or /en/enterprise/2.20/user/rest/reference/apps
req.context.restGitHubAppsLink = removeFPTFromPath(
path.join('/', req.context.currentLanguage, req.context.currentVersion, '/developers/apps')
)
// ignore requests to non-REST reference paths
if (!req.pagePath.includes('rest/reference')) return next()
// e.g. the `activity` from `/en/rest/reference/activity#events`
let category = req.pagePath
.split('rest/reference')[1]
.replace(/^\//, '') // remove leading slash
.split('/')[0]
// override the category if the category is in the restRepoCategoryExceptions list
if (restRepoCategoryExceptions.includes(category)) {
category = 'repos'
}
// ignore empty strings or bare `/`
if (!category || category.length < 2) return next()
const operationsForCurrentProduct = req.context.rest.operations[req.context.currentVersion] || []
// find all operations with a category matching the current path
req.context.currentRestOperations = operationsForCurrentProduct.filter(
(operation) => operation.category === category
)
return next()
}