Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export async function fetchMdxFromGitLabVersioned(
- env: { GITLAB_TOKEN: string; MDX_CACHE?: KVNamespace },
- projectId: number,
- filePath: string,
- branch = "main",
- ): Promise<{ mdxText: string; commitSha: string }> {
- const cacheKey = `mdx:${projectId}:${filePath}:${branch}`
- // 1) Attempt to read from KV if available
- let cached: { mdxText: string; commitSha: string } | null = null
- if (env.MDX_CACHE) {
- try {
- const cachedText = await env.MDX_CACHE.get(cacheKey, { type: "text" })
- if (cachedText) {
- cached = JSON.parse(cachedText) as { mdxText: string; commitSha: string }
- console.log("Cache hit for key:", cacheKey)
- return cached
- }
- } catch (err) {
- console.warn("KV get failed, continuing without cache:", err)
- }
- }
- // 2) Fetch latest commit SHA from GitLab
- const branchRes = await fetch(
- `https://gitlab.com/api/v4/projects/${projectId}/repository/branches/${encodeURIComponent(branch)}`,
- {
- headers: { "PRIVATE-TOKEN": env.GITLAB_TOKEN },
- },
- )
- if (!branchRes.ok) {
- throw new Error(`GitLab branch API error: ${branchRes.status}`)
- }
- const branchData = await branchRes.json<{ commit: { id: string } }>()
- const commitSha = branchData.commit.id
- // 3) Fetch raw file content from GitLab
- const fileUrl =
- `https://gitlab.com/api/v4/projects/${projectId}/repository/files/` +
- `${encodeURIComponent(filePath)}/raw?ref=${encodeURIComponent(branch)}`
- const fileRes = await fetch(fileUrl, {
- headers: { "PRIVATE-TOKEN": env.GITLAB_TOKEN },
- })
- if (!fileRes.ok) {
- throw new Error(`GitLab file API error: ${fileRes.status}`)
- }
- const mdxText = await fileRes.text()
- const data = { mdxText, commitSha }
- // 4) Write to KV if available
- if (env.MDX_CACHE) {
- try {
- console.log("Attempting to save to KV with key:", cacheKey)
- await env.MDX_CACHE.put(cacheKey, JSON.stringify(data), {
- expirationTtl: 3600,
- })
- const verification = await env.MDX_CACHE.get(cacheKey, { type: "text" })
- if (verification) {
- console.log("Cache successfully updated and verified for key:", cacheKey)
- } else {
- console.warn("Cache write verification failed - data may not have been saved")
- }
- } catch (err) {
- console.error("KV put failed with error:", err)
- console.error("Error details:", {
- message: err instanceof Error ? err.message : String(err),
- cacheKey,
- dataSize: JSON.stringify(data).length,
- })
- }
- } else {
- console.warn("MDX_CACHE binding not available - skipping cache operations")
- }
- console.log("Fetched new data from GitLab for key:", cacheKey)
- return data
- }
Advertisement
Add Comment
Please, Sign In to add comment