-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
93 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { deflateSync } from 'node:zlib'; | ||
|
||
import { VERCEL_REVALIDATE } from '@/next.constants.mjs'; | ||
import { defaultLocale } from '@/next.locales.mjs'; | ||
import type { GitHubApiFile } from '@/types'; | ||
import { getGitHubApiDocsUrl } from '@/util/gitHubUtils'; | ||
import { parseRichTextIntoPlainText } from '@/util/stringUtils'; | ||
|
||
const getPathnameForApiFile = (name: string) => | ||
`api/${name.replace('.md', '.html')}`; | ||
|
||
// This is the Route Handler for the `GET` method which handles the request | ||
// for a digest and metadata of all API pages from the Node.js Website | ||
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers | ||
export const GET = async () => { | ||
const gitHubApiResponse = await fetch(getGitHubApiDocsUrl('main')); | ||
|
||
return gitHubApiResponse.json().then((apiDocsFiles: Array<GitHubApiFile>) => { | ||
// maps over each api file and get the download_url, fetch the content and deflates it | ||
const mappedApiFiles = apiDocsFiles.map( | ||
async ({ name, path: filename, download_url }) => { | ||
const apiFileResponse = await fetch(download_url); | ||
|
||
// Retrieves the content as a raw text string | ||
const source = await apiFileResponse.text(); | ||
|
||
// Removes empty/blank lines or lines just with spaces and trims each line | ||
// from leading and trailing paddings/spaces | ||
const cleanedContent = parseRichTextIntoPlainText(source); | ||
|
||
const deflatedSource = deflateSync(cleanedContent).toString('base64'); | ||
|
||
return { | ||
filename, | ||
pathname: getPathnameForApiFile(name), | ||
content: deflatedSource, | ||
}; | ||
} | ||
); | ||
|
||
return Promise.all(mappedApiFiles).then(Response.json); | ||
}); | ||
}; | ||
|
||
// This function generates the static paths that come from the dynamic segments | ||
// `[locale]/next-data/api-data/` and returns an array of all available static paths | ||
// This is used for ISR static validation and generation | ||
export const generateStaticParams = async () => [ | ||
{ locale: defaultLocale.code }, | ||
]; | ||
|
||
// Enforces that only the paths from `generateStaticParams` are allowed, giving 404 on the contrary | ||
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams | ||
export const dynamicParams = false; | ||
|
||
// Enforces that this route is used as static rendering | ||
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic | ||
export const dynamic = 'force-static'; | ||
|
||
// Ensures that this endpoint is invalidated and re-executed every X minutes | ||
// so that when new deployments happen, the data is refreshed | ||
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate | ||
export const revalidate = VERCEL_REVALIDATE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface GitHubApiFile { | ||
name: string; | ||
path: string; | ||
sha: string; | ||
size: number; | ||
url: string; | ||
html_url: string; | ||
git_url: string; | ||
download_url: string; | ||
type: 'file' | 'dir'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import GitHubSlugger from 'github-slugger'; | ||
|
||
export const githubProfileAvatarUrl = (username: string): string => | ||
export const getGitHubAvatarUrl = (username: string): string => | ||
`https://avatars.githubusercontent.com/${username}`; | ||
|
||
export const createGitHubSlug = () => { | ||
export const createGitHubSlugger = () => { | ||
const githubSlugger = new GitHubSlugger(); | ||
|
||
return (text: string) => githubSlugger.slug(text); | ||
}; | ||
|
||
export const getGitHubEditPageUrl = (filename: string) => | ||
export const getGitHubBlobUrl = (filename: string) => | ||
`https://github.com/nodejs/nodejs.org/blob/main/pages/en/${filename}`; | ||
|
||
export const getGitHubApiDocsUrl = (ref: string) => | ||
`https://api.github.com/repos/nodejs/node/contents/doc/api?ref=${ref}`; |