From f2c2d976999d4afaa491cc0c21042217db0a08b7 Mon Sep 17 00:00:00 2001 From: Xennis Date: Tue, 4 Jun 2024 18:08:23 +0200 Subject: [PATCH] [fetch] Remove all Node dependencies --- .github/workflows/ci.yml | 2 ++ examples/nextjs/src/lib/fetchers.ts | 32 ++++++++++++++++++++++++++++- packages/fetch/src/image.ts | 30 --------------------------- packages/fetch/src/index.ts | 1 - 4 files changed, 33 insertions(+), 32 deletions(-) delete mode 100644 packages/fetch/src/image.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b54cb1a..ad857a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/examples/nextjs/src/lib/fetchers.ts b/examples/nextjs/src/lib/fetchers.ts index 9474ab0..9f89ef5 100644 --- a/examples/nextjs/src/lib/fetchers.ts +++ b/examples/nextjs/src/lib/fetchers.ts @@ -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" @@ -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 ?? "") diff --git a/packages/fetch/src/image.ts b/packages/fetch/src/image.ts deleted file mode 100644 index 4a058ad..0000000 --- a/packages/fetch/src/image.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { statSync, writeFileSync } from "node:fs" -import * as path from "node:path" - -export 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 -} diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index de613f1..310038c 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -1,3 +1,2 @@ export * from "./fetch" -export * from "./image" export * from "./properties"