Skip to content

Commit

Permalink
refactor: Create Notion client singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
botmaster committed Feb 9, 2024
1 parent 33cc2eb commit 9e44e78
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
22 changes: 22 additions & 0 deletions server/NotionClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Client } from '@notionhq/client';

export class NotionClient {
private static instance: NotionClient;
private readonly client: Client;

private constructor() {
const config = useRuntimeConfig();
this.client = new Client({ auth: config.notionApiKey });
}

public static getInstance(): NotionClient {
if (!NotionClient.instance)
NotionClient.instance = new NotionClient();

return NotionClient.instance;
}

public getClient() {
return this.client;
}
}
7 changes: 2 additions & 5 deletions server/api/notion-database-info.get.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Client } from '@notionhq/client';

// Get the runtime config
const config = useRuntimeConfig();
import { NotionClient } from '~/server/NotionClient';

// Initialize Notion Client
const notion = new Client({ auth: config.notionApiKey });
const notion = NotionClient.getInstance().getClient();

export default defineEventHandler(async (event) => {
// Get Query
Expand Down
6 changes: 2 additions & 4 deletions server/api/notion-page-image.post.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Client } from '@notionhq/client';

const config = useRuntimeConfig();
import { NotionClient } from '~/server/NotionClient';

// Initialize Notion Client
const notion = new Client({ auth: config.notionApiKey });
const notion = NotionClient.getInstance().getClient();

// Cache the image url
const imageUrlCache = new Map<string, string>();
Expand Down
6 changes: 2 additions & 4 deletions server/api/notion-page-list.post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client } from '@notionhq/client';
import { NotionClient } from '~/server/NotionClient';

export interface INotionArticle {
title: string
Expand All @@ -16,10 +16,8 @@ export interface INotionArticle {
}
}

const config = useRuntimeConfig();

// Initialize Notion Client
const notion = new Client({ auth: config.notionApiKey });
const notion = NotionClient.getInstance().getClient();

// Define the event handler
export default defineEventHandler(async (event) => {
Expand Down

0 comments on commit 9e44e78

Please sign in to comment.