Guest User

Untitled

a guest
Sep 11th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. export async function fetchMdxFromGitLabVersioned(
  2. env: { GITLAB_TOKEN: string; MDX_CACHE?: KVNamespace },
  3. projectId: number,
  4. filePath: string,
  5. branch = "main",
  6. ): Promise<{ mdxText: string; commitSha: string }> {
  7. const cacheKey = `mdx:${projectId}:${filePath}:${branch}`
  8.  
  9. // 1) Attempt to read from KV if available
  10. let cached: { mdxText: string; commitSha: string } | null = null
  11. if (env.MDX_CACHE) {
  12. try {
  13. const cachedText = await env.MDX_CACHE.get(cacheKey, { type: "text" })
  14. if (cachedText) {
  15. cached = JSON.parse(cachedText) as { mdxText: string; commitSha: string }
  16. console.log("Cache hit for key:", cacheKey)
  17. return cached
  18. }
  19. } catch (err) {
  20. console.warn("KV get failed, continuing without cache:", err)
  21. }
  22. }
  23.  
  24. // 2) Fetch latest commit SHA from GitLab
  25. const branchRes = await fetch(
  26. `https://gitlab.com/api/v4/projects/${projectId}/repository/branches/${encodeURIComponent(branch)}`,
  27. {
  28. headers: { "PRIVATE-TOKEN": env.GITLAB_TOKEN },
  29. },
  30. )
  31. if (!branchRes.ok) {
  32. throw new Error(`GitLab branch API error: ${branchRes.status}`)
  33. }
  34. const branchData = await branchRes.json<{ commit: { id: string } }>()
  35. const commitSha = branchData.commit.id
  36.  
  37. // 3) Fetch raw file content from GitLab
  38. const fileUrl =
  39. `https://gitlab.com/api/v4/projects/${projectId}/repository/files/` +
  40. `${encodeURIComponent(filePath)}/raw?ref=${encodeURIComponent(branch)}`
  41. const fileRes = await fetch(fileUrl, {
  42. headers: { "PRIVATE-TOKEN": env.GITLAB_TOKEN },
  43. })
  44. if (!fileRes.ok) {
  45. throw new Error(`GitLab file API error: ${fileRes.status}`)
  46. }
  47. const mdxText = await fileRes.text()
  48.  
  49. const data = { mdxText, commitSha }
  50.  
  51. // 4) Write to KV if available
  52. if (env.MDX_CACHE) {
  53. try {
  54. console.log("Attempting to save to KV with key:", cacheKey)
  55.  
  56. await env.MDX_CACHE.put(cacheKey, JSON.stringify(data), {
  57. expirationTtl: 3600,
  58. })
  59.  
  60. const verification = await env.MDX_CACHE.get(cacheKey, { type: "text" })
  61. if (verification) {
  62. console.log("Cache successfully updated and verified for key:", cacheKey)
  63. } else {
  64. console.warn("Cache write verification failed - data may not have been saved")
  65. }
  66. } catch (err) {
  67. console.error("KV put failed with error:", err)
  68. console.error("Error details:", {
  69. message: err instanceof Error ? err.message : String(err),
  70. cacheKey,
  71. dataSize: JSON.stringify(data).length,
  72. })
  73. }
  74. } else {
  75. console.warn("MDX_CACHE binding not available - skipping cache operations")
  76. }
  77.  
  78. console.log("Fetched new data from GitLab for key:", cacheKey)
  79.  
  80. return data
  81. }
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment