Skip to content

Commit

Permalink
[fetch] Remove all Node dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Xennis committed Jun 4, 2024
1 parent e21b9cf commit f2c2d97
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ jobs:
run: pnpm run format:check
- name: Build packages
run: pnpm --filter './packages/*' run build
#- name: Build examples
# run: pnpm --filter './examples/*' run build
32 changes: 31 additions & 1 deletion examples/nextjs/src/lib/fetchers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { unstable_cache } from "next/cache"
import { fetchBlocksChildren, IconResponse } from "@react-notion-cms/render"
import { downloadImage, fetchDatabasePages, propsPlainTexts } from "@react-notion-cms/fetch"
import { fetchDatabasePages, propsPlainTexts } from "@react-notion-cms/fetch"
import { Client } from "@notionhq/client"
import type { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints"
import path from "node:path"
import { statSync, writeFileSync } from "node:fs"

import nextConfig from "../../next.config.mjs"

Expand Down Expand Up @@ -59,6 +61,34 @@ export const getCachedPageContent = unstable_cache(
["cms-page"],
)

const downloadImage = async (dir: string, url: string, meta: { blockId: string; lastEditedTime: Date }) => {
const fileUrl = new URL(url)
const originalFileName = fileUrl.pathname.substring(fileUrl.pathname.lastIndexOf("/") + 1)
const originalFileExtension = originalFileName.split(".").pop()
const newFileName = path.join(dir, `${meta.blockId}.${originalFileExtension}`)

let savedLastEditedTime: Date | null = null
try {
const stat = statSync(newFileName)
savedLastEditedTime = stat.mtime
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
console.debug(`${newFileName} not found`)
} else {
console.warn(`${newFileName}: ${error}`)
}
}
// Avoid download the file again and again
if (savedLastEditedTime === null || meta.lastEditedTime > savedLastEditedTime) {
const resp = await fetch(fileUrl)
const blob = await resp.blob()
writeFileSync(newFileName, new DataView(await blob.arrayBuffer()))
console.info(`downloaded image ${newFileName} of block ${meta.blockId}`)
}

return newFileName
}

const downloadImageToPublicDir = async (url: string, meta: { blockId: string; lastEditedTime: Date }) => {
const localImage = await downloadImage("public/cms", url, meta)
return localImage.replace("public", nextConfig.basePath ?? "")
Expand Down
30 changes: 0 additions & 30 deletions packages/fetch/src/image.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./fetch"
export * from "./image"
export * from "./properties"

0 comments on commit f2c2d97

Please sign in to comment.