diff --git a/packages/notion-utils/CHANGELOG.md b/packages/notion-utils/CHANGELOG.md index 50756cb..25b0c7e 100644 --- a/packages/notion-utils/CHANGELOG.md +++ b/packages/notion-utils/CHANGELOG.md @@ -1,5 +1,11 @@ # @julianjark/notion-utils +## 0.3.1 + +### Patch Changes + +- add getImageAssets to client + ## 0.3.0 ### Minor Changes diff --git a/packages/notion-utils/package.json b/packages/notion-utils/package.json index df224c4..7f7cfaf 100644 --- a/packages/notion-utils/package.json +++ b/packages/notion-utils/package.json @@ -1,6 +1,6 @@ { "name": "@julianjark/notion-utils", - "version": "0.3.0", + "version": "0.3.1", "description": "Helper functions for the Notion API", "author": "Julian Jark", "license": "ISC", diff --git a/packages/notion-utils/src/client.ts b/packages/notion-utils/src/client.ts index 0e89223..46e95db 100644 --- a/packages/notion-utils/src/client.ts +++ b/packages/notion-utils/src/client.ts @@ -7,6 +7,7 @@ import type { PartialDatabaseObjectResponse, PartialPageObjectResponse, } from "@notionhq/client/build/src/api-endpoints"; +import { getImageAssets } from "./image"; /** * Creates a simplified client for fetching data from Notion @@ -23,6 +24,8 @@ export const getClient = (token: string) => { getDatabase: getDatabase(notionClient), getBlocks: getBlocks(notionClient), getBlocksWithChildren: getBlocksWithChildren(notionClient), + + getImageAssets: getImageAssets(notionClient), }; }; diff --git a/packages/notion-utils/src/image.ts b/packages/notion-utils/src/image.ts index 723a12f..35997ca 100644 --- a/packages/notion-utils/src/image.ts +++ b/packages/notion-utils/src/image.ts @@ -6,22 +6,22 @@ export interface ImageAsset { src: string; alt: string; } -interface FetchImageAssetsConfig { +export interface GetImageAssetsConfig { titleProperty: string; srcProperty: string; altProperty: string; databaseId: string; } -export const fetchImageAssets = +export const getImageAssets = (client: NotionClient) => - async ( - names: T[], + async ( + names: ImageName[], { titleProperty, srcProperty, altProperty, databaseId, - }: FetchImageAssetsConfig + }: GetImageAssetsConfig ) => { const assets = await getDatabasePages(client)(databaseId, { filter: { @@ -35,7 +35,7 @@ export const fetchImageAssets = }); const images = assets.reduce((acc, asset) => { - const name = getTitle(asset) as T; + const name = getTitle(asset) as ImageName; const url = getFileUrl(srcProperty, asset); if (url === undefined) throw new Error(`no image resource for name ${name}`); @@ -45,7 +45,7 @@ export const fetchImageAssets = acc[name] = { src: url, alt }; return acc; - }, {} as Record); + }, {} as Record); assertContainsItems(names, images); return images;