| | import { readFile, writeFile } from 'fs/promises' |
| |
|
| | const STATIC_REDIRECTS = 'src/rest/data/client-side-rest-api-redirects.json' |
| | const REST_API_OVERRIDES = 'src/rest/lib/rest-api-overrides.json' |
| |
|
| | interface OperationUrl { |
| | originalUrl: string |
| | category: string |
| | subcategory?: string |
| | } |
| |
|
| | interface RestApiOverrides { |
| | operationUrls: Record<string, OperationUrl> |
| | sectionUrls: Record<string, string> |
| | } |
| |
|
| | interface RedirectMap { |
| | [oldUrl: string]: string |
| | } |
| |
|
| | |
| | |
| | export async function syncRestRedirects(): Promise<void> { |
| | const clientSideRedirects = await getClientSideRedirects() |
| |
|
| | await writeFile(STATIC_REDIRECTS, JSON.stringify(clientSideRedirects, null, 2), 'utf8') |
| | console.log(`✅ Wrote ${STATIC_REDIRECTS}`) |
| | } |
| |
|
| | |
| | |
| | async function getClientSideRedirects(): Promise<RedirectMap> { |
| | const { operationUrls, sectionUrls }: RestApiOverrides = JSON.parse( |
| | await readFile(REST_API_OVERRIDES, 'utf8'), |
| | ) |
| |
|
| | const operationRedirects: RedirectMap = {} |
| | for (const value of Object.values(operationUrls)) { |
| | const oldUrl = value.originalUrl.replace('/rest/reference', '/rest') |
| | const anchor = oldUrl.split('#')[1] |
| | const subcategory = value.subcategory |
| | const redirectTo = subcategory |
| | ? `/rest/${value.category}/${subcategory}#${anchor}` |
| | : `/rest/${value.category}#${anchor}` |
| | operationRedirects[oldUrl] = redirectTo |
| | } |
| | const redirects: RedirectMap = { |
| | ...operationRedirects, |
| | ...sectionUrls, |
| | } |
| | return redirects |
| | } |
| |
|