1
0
mirror of synced 2025-12-23 11:54:18 -05:00
Files
docs/middleware/contextualizers/rest.js
Peter Bengtsson 9a7a8a761e make lib/rest/index.js callable (#23214)
* Experiment with making the tarball smaller

Part of #1248

* try this

* stop debugging

* delete translations too

* delete heavy search indexes too

* push and popd

* try this hack

* delete but leave directory

* debug more

* faster delete of translations

* less loud

* async await

* async await

* no tree

* simplify

* experimenting more

* unfinished

* only the large files

* change order

* brotli with level 6

* cope better with decorated rest json files

* tidying

* keep images

* cleaning

* cleaning up

* refactored function

* try this

* better comment

* remove console logging

* more important changes

* make lib/rest/index.js callable

Part of #1177

* memoize

* remove console logging
2021-12-07 13:21:23 +00:00

44 lines
1.4 KiB
JavaScript

import path from 'path'
import getRest 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`
const category = req.pagePath
.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()
}