1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/middleware/contextualizers/rest.js
James M. Greene c940dcd98b Middleware overhaul! (#18218)
* Middleware overhaul!

- Remove unnecessary 'async' keywords from middleware functions
- Ensure all middleware functions we create have names
- Wrap the method contents of all async middleware functions in a try-catch+next(error) pattern

* Use asyncMiddleware wrapper instead of try-catch+next(error) pattern

* Remove unnecessary try-catch+next(error) pattern from context middleware
2021-03-11 01:12:41 +00:00

36 lines
1.2 KiB
JavaScript

const path = require('path')
const rest = require('../../lib/rest')
const removeFPTFromPath = require('../../lib/remove-fpt-from-path')
module.exports = function restContext (req, res, next) {
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.path.includes('rest/reference')) return next()
// e.g. the `activity` from `/en/rest/reference/activity#events`
const category = req.path
.split('rest/reference')[1]
.replace(/^\//, '') // remove leading slash
.split('/')[0]
// 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()
}