MCPcopy
hub / github.com/github/docs / RestRedirect

Function RestRedirect

components/RestRedirect.tsx:12–41  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10// This component allows us to set the URL Param for the REST API Calendar Date version
11// We set a cookie as well to remember what calendar date version the user is on
12export function RestRedirect() {
13 const router = useRouter()
14 const { currentVersion } = useVersion()
15 const { allVersions } = useMainContext()
16 const validApiVersions = allVersions[currentVersion].apiVersions
17 const isReleaseVersioned = allVersions[currentVersion].apiVersions.length > 0
18 const latestValidApiVersion = allVersions[currentVersion].latestApiVersion
19 const queryApiVersion = router.query.apiVersion
20
21 useEffect(() => {
22 if (
23 isReleaseVersioned &&
24 (!queryApiVersion || !validApiVersions.includes(queryApiVersion as string))
25 ) {
26 const versionCookie = Cookies.get(API_VERSION_COOKIE_NAME)
27 const date =
28 versionCookie && validApiVersions.includes(versionCookie)
29 ? versionCookie
30 : latestValidApiVersion
31 const hash = router.asPath.split('#')[1]
32 const [asPathRoot, asPathQuery = ''] = router.asPath.split('#')[0].split('?')
33 const params = new URLSearchParams(asPathQuery)
34
35 params.set('apiVersion', date)
36 const url = `/${router.locale}${asPathRoot}?${params}${hash ? '#' + hash : ''}`
37 router.replace(url)
38 }
39 }, [router.asPath, currentVersion])
40 return null
41}

Callers

nothing calls this directly

Calls 2

useVersionFunction · 0.90
useMainContextFunction · 0.90

Tested by

no test coverage detected