Skip to content

Commit

Permalink
Updating to check apiVersion is a real apiVersion (github#33058)
Browse files Browse the repository at this point in the history
Co-authored-by: Rachael Sewell <rachmari@github.com>
  • Loading branch information
gracepark and rachmari authored Nov 29, 2022
1 parent 1a77ceb commit 53f5470
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
15 changes: 11 additions & 4 deletions components/RestRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ export function RestRedirect() {
const router = useRouter()
const { currentVersion } = useVersion()
const { allVersions } = useMainContext()
const apiVersions = allVersions[currentVersion].apiVersions
const latestApiVersion = allVersions[currentVersion].latestApiVersion
const validApiVersions = allVersions[currentVersion].apiVersions
const isReleaseVersioned = allVersions[currentVersion].apiVersions.length > 0
const latestValidApiVersion = allVersions[currentVersion].latestApiVersion
const queryApiVersion = router.query.apiVersion

useEffect(() => {
if (allVersions[currentVersion].apiVersions.length > 0 && !router.query.apiVersion) {
if (
isReleaseVersioned &&
(!queryApiVersion || !validApiVersions.includes(queryApiVersion as string))
) {
const versionCookie = Cookies.get(API_VERSION_COOKIE_NAME)
const date =
versionCookie && apiVersions.includes(versionCookie) ? versionCookie : latestApiVersion
versionCookie && validApiVersions.includes(versionCookie)
? versionCookie
: latestValidApiVersion
const hash = router.asPath.split('#')[1]
const [asPathRoot, asPathQuery = ''] = router.asPath.split('#')[0].split('?')
const params = new URLSearchParams(asPathQuery)
Expand Down
13 changes: 9 additions & 4 deletions components/sidebar/ApiVersionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ export const ApiVersionPicker = ({ variant, width }: Props) => {
const { t } = useTranslation(['products'])
const basePath = router.asPath.split('#')[0].split('?')[0]
// Get current date from cookie, query path, or lastly set it to latest rest version date
const currentDate =
router.query.apiVersion && typeof router.query.apiVersion === 'string'
? router.query.apiVersion
: allVersions[currentVersion].latestApiVersion
const isValidApiVersion =
(router.query.apiVersion &&
typeof router.query.apiVersion === 'string' &&
allVersions[currentVersion].apiVersions.includes(router.query.apiVersion)) ||
false

const currentDate = (
isValidApiVersion ? router.query.apiVersion : allVersions[currentVersion].latestApiVersion
) as string

const currentDateDisplayText =
currentDate === allVersions[currentVersion].latestApiVersion
Expand Down
6 changes: 4 additions & 2 deletions pages/[versionId]/rest/[category]/[subcategory].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
const currentVersion = context.params!.versionId as string
const currentLanguage = req.context.currentLanguage as string
const allVersions = req.context.allVersions
const apiVersion = context.query.apiVersion || allVersions[currentVersion].latestApiVersion

const queryApiVersion = context.query.apiVersion
const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion)
? queryApiVersion
: allVersions[currentVersion].latestApiVersion
// For pages with category level only operations like /rest/billing, we set
// the subcategory's value to be the category for the call to getRest()
if (!subCategory) {
Expand Down
5 changes: 4 additions & 1 deletion pages/[versionId]/rest/[category]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
const currentVersion = context.params!.versionId as string
const currentLanguage = req.context.currentLanguage as string
const allVersions = req.context.allVersions
const apiVersion = context.query.apiVersion || allVersions[currentVersion].latestApiVersion
const queryApiVersion = context.query.apiVersion
const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion)
? queryApiVersion
: allVersions[currentVersion].latestApiVersion

// For pages with category level only operations like /rest/billing, we set
// the subcategory's value to be the category for the call to getRest()
Expand Down

0 comments on commit 53f5470

Please sign in to comment.