Skip to content

Commit 99c04f9

Browse files
committed
refactor: user a helper for filling the default config in one place
1 parent 24b3e43 commit 99c04f9

File tree

3 files changed

+89
-53
lines changed

3 files changed

+89
-53
lines changed

source/gatsby-node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ import { name } from '#.';
1919
import { Notion } from '#client';
2020
import { getDatabases, getPages } from '#plugin';
2121
import { NodeManager } from '#node';
22+
import { normaliseConfig } from '#plugin';
2223

2324
import type { GatsbyNode } from 'gatsby';
2425

25-
import { PluginConfig } from '#plugin';
26-
2726
/* eslint-disable jsdoc/require-param, jsdoc/require-returns */
2827

2928
/** Define a schema for the options using Joi to validate the options users pass to the plugin. */
@@ -53,8 +52,9 @@ export const onPreBootstrap: NonNullable<GatsbyNode['onPreBootstrap']> = async (
5352

5453
export const sourceNodes: NonNullable<GatsbyNode['sourceNodes']> = async (
5554
args,
56-
pluginConfig: PluginConfig,
55+
partialConfig,
5756
) => {
57+
const pluginConfig = normaliseConfig(partialConfig);
5858
const client = new Notion(pluginConfig);
5959

6060
// getting entries from notion

source/plugin.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ export interface PluginConfig extends PluginOptions, NotionOptions {
2828
pages?: string[];
2929
}
3030

31+
interface FullPluginConfig extends PluginConfig {
32+
databases: string[];
33+
pages: string[];
34+
}
35+
36+
/**
37+
* fill in the missing config with defaults
38+
* @param config pluginConfig passed from the plugin options
39+
* @returns a complete config
40+
*/
41+
export function normaliseConfig(
42+
config: Partial<PluginConfig>,
43+
): FullPluginConfig {
44+
const databases = [
45+
...(config.databases ?? []),
46+
...(process.env['GATSBY_NOTION_DATABASES']?.split(/, +/) ?? []),
47+
].filter(
48+
// no empty id
49+
(id) => !!id,
50+
);
51+
52+
const pages = [
53+
...(config.pages ?? []),
54+
...(process.env['GATSBY_NOTION_PAGES']?.split(/, +/) ?? []),
55+
].filter(
56+
// no empty id
57+
(id) => !!id,
58+
);
59+
60+
return { ...config, databases, pages, plugins: [] };
61+
}
62+
3163
/**
3264
* gat relevant databases from Notion
3365
* @param client a Notion client
@@ -36,16 +68,11 @@ export interface PluginConfig extends PluginOptions, NotionOptions {
3668
*/
3769
export async function getDatabases(
3870
client: Notion,
39-
pluginConfig: PluginConfig,
71+
pluginConfig: FullPluginConfig,
4072
): Promise<FullDatabase[]> {
4173
const databases: FullDatabase[] = [];
4274

43-
const databaseIDs = [
44-
...(pluginConfig.databases ?? []),
45-
...(process.env['GATSBY_NOTION_DATABASES']?.split(/, +/) ?? []),
46-
];
47-
48-
for (const databaseID of databaseIDs) {
75+
for (const databaseID of pluginConfig.databases) {
4976
const database = await client.getDatabase(databaseID);
5077
databases.push(database);
5178
}
@@ -61,16 +88,11 @@ export async function getDatabases(
6188
*/
6289
export async function getPages(
6390
client: Notion,
64-
pluginConfig: PluginConfig,
91+
pluginConfig: FullPluginConfig,
6592
): Promise<FullPage[]> {
6693
const pages: FullPage[] = [];
6794

68-
const pageIDs = [
69-
...(pluginConfig.pages ?? []),
70-
...(process.env['GATSBY_NOTION_PAGES']?.split(/, +/) ?? []),
71-
];
72-
73-
for (const pageID of pageIDs) {
95+
for (const pageID of pluginConfig.pages) {
7496
pages.push(await client.getPage(pageID));
7597
}
7698

spec/plugin.spec.ts

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,72 @@
1414
*/
1515

1616
import { Notion } from '#client';
17-
import { getDatabases, getPages } from '#plugin';
17+
import { getDatabases, getPages, normaliseConfig } from '#plugin';
1818
import { mockDatabase, mockPage } from './mock';
1919

2020
const client = new Notion({ token: 'token' });
2121

22+
describe('fn:normaliseConfig', () => {
23+
const env = { ...process.env };
24+
afterEach(() => (process.env = { ...env }));
25+
26+
it('combine options from the environment variables as well', () => {
27+
process.env.GATSBY_NOTION_DATABASES = 'database_env_1, database_env_2';
28+
process.env.GATSBY_NOTION_PAGES = 'page_env_1, page_env_2';
29+
30+
const normalisedConfig = normaliseConfig({
31+
databases: ['database_options'],
32+
pages: ['page_options'],
33+
});
34+
35+
expect(normalisedConfig.databases).toEqual([
36+
'database_options',
37+
'database_env_1',
38+
'database_env_2',
39+
]);
40+
41+
expect(normalisedConfig.pages).toEqual([
42+
'page_options',
43+
'page_env_1',
44+
'page_env_2',
45+
]);
46+
});
47+
});
48+
2249
describe('fn:getDatabases', () => {
2350
it('return nothing if no database is supplied', async () => {
24-
expect(await getDatabases(client, { plugins: [] })).toEqual([]);
51+
expect(await getDatabases(client, normaliseConfig({}))).toEqual([]);
2552
});
2653

27-
it('return a combined list of databases from the options and environment variables', async () => {
28-
mockDatabase('database_from_options');
29-
mockDatabase('database_from_env_1');
30-
mockDatabase('database_from_env_2');
54+
it('return databases from Notion API', async () => {
55+
mockDatabase('database');
3156

32-
process.env['GATSBY_NOTION_DATABASES'] =
33-
'database_from_env_1, database_from_env_2';
34-
35-
const databases = await getDatabases(client, {
36-
plugins: [],
37-
databases: ['database_from_options'],
38-
});
39-
expect(databases.length).toEqual(3);
40-
expect(databases.map((database) => database.id)).toEqual([
41-
'database_from_options',
42-
'database_from_env_1',
43-
'database_from_env_2',
44-
]);
57+
const databases = await getDatabases(
58+
client,
59+
normaliseConfig({
60+
databases: ['database'],
61+
}),
62+
);
63+
expect(databases.length).toEqual(1);
64+
expect(databases.map((database) => database.id)).toEqual(['database']);
4565
});
4666
});
4767

4868
describe('fn:getPages', () => {
4969
it('return nothing if no page is supplied', async () => {
50-
expect(await getPages(client, { plugins: [] })).toEqual([]);
70+
expect(await getPages(client, normaliseConfig({}))).toEqual([]);
5171
});
5272

53-
it('return a combined list of pages from the options and environment variables', async () => {
54-
mockPage('page_from_options');
55-
mockPage('page_from_env_1');
56-
mockPage('page_from_env_2');
57-
58-
process.env['GATSBY_NOTION_PAGES'] = 'page_from_env_1, page_from_env_2';
73+
it('return pages from Notion API', async () => {
74+
mockPage('page');
5975

60-
const pages = await getPages(client, {
61-
plugins: [],
62-
pages: ['page_from_options'],
63-
});
64-
expect(pages.length).toEqual(3);
65-
expect(pages.map((page) => page.id)).toEqual([
66-
'page_from_options',
67-
'page_from_env_1',
68-
'page_from_env_2',
69-
]);
76+
const pages = await getPages(
77+
client,
78+
normaliseConfig({
79+
pages: ['page'],
80+
}),
81+
);
82+
expect(pages.length).toEqual(1);
83+
expect(pages.map((page) => page.id)).toEqual(['page']);
7084
});
7185
});

0 commit comments

Comments
 (0)