Skip to content

Commit

Permalink
add getImageAssets to client
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuxu committed Jun 8, 2023
1 parent a8ca591 commit 23494f1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/notion-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @julianjark/notion-utils

## 0.3.1

### Patch Changes

- add getImageAssets to client

## 0.3.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/notion-utils/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions packages/notion-utils/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +24,8 @@ export const getClient = (token: string) => {
getDatabase: getDatabase(notionClient),
getBlocks: getBlocks(notionClient),
getBlocksWithChildren: getBlocksWithChildren(notionClient),

getImageAssets: getImageAssets(notionClient),
};
};

Expand Down
14 changes: 7 additions & 7 deletions packages/notion-utils/src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T extends string>(
names: T[],
async <ImageName extends string>(
names: ImageName[],
{
titleProperty,
srcProperty,
altProperty,
databaseId,
}: FetchImageAssetsConfig
}: GetImageAssetsConfig
) => {
const assets = await getDatabasePages(client)(databaseId, {
filter: {
Expand All @@ -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}`);
Expand All @@ -45,7 +45,7 @@ export const fetchImageAssets =

acc[name] = { src: url, alt };
return acc;
}, {} as Record<T, ImageAsset>);
}, {} as Record<ImageName, ImageAsset>);
assertContainsItems(names, images);

return images;
Expand Down

0 comments on commit 23494f1

Please sign in to comment.