Skip to content

Commit

Permalink
refactor: user a helper for filling the default config in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jul 14, 2021
1 parent 24b3e43 commit 99c04f9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 53 deletions.
6 changes: 3 additions & 3 deletions source/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import { name } from '#.';
import { Notion } from '#client';
import { getDatabases, getPages } from '#plugin';
import { NodeManager } from '#node';
import { normaliseConfig } from '#plugin';

import type { GatsbyNode } from 'gatsby';

import { PluginConfig } from '#plugin';

/* eslint-disable jsdoc/require-param, jsdoc/require-returns */

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

export const sourceNodes: NonNullable<GatsbyNode['sourceNodes']> = async (
args,
pluginConfig: PluginConfig,
partialConfig,
) => {
const pluginConfig = normaliseConfig(partialConfig);
const client = new Notion(pluginConfig);

// getting entries from notion
Expand Down
50 changes: 36 additions & 14 deletions source/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ export interface PluginConfig extends PluginOptions, NotionOptions {
pages?: string[];
}

interface FullPluginConfig extends PluginConfig {
databases: string[];
pages: string[];
}

/**
* fill in the missing config with defaults
* @param config pluginConfig passed from the plugin options
* @returns a complete config
*/
export function normaliseConfig(
config: Partial<PluginConfig>,
): FullPluginConfig {
const databases = [
...(config.databases ?? []),
...(process.env['GATSBY_NOTION_DATABASES']?.split(/, +/) ?? []),
].filter(
// no empty id
(id) => !!id,
);

const pages = [
...(config.pages ?? []),
...(process.env['GATSBY_NOTION_PAGES']?.split(/, +/) ?? []),
].filter(
// no empty id
(id) => !!id,
);

return { ...config, databases, pages, plugins: [] };
}

/**
* gat relevant databases from Notion
* @param client a Notion client
Expand All @@ -36,16 +68,11 @@ export interface PluginConfig extends PluginOptions, NotionOptions {
*/
export async function getDatabases(
client: Notion,
pluginConfig: PluginConfig,
pluginConfig: FullPluginConfig,
): Promise<FullDatabase[]> {
const databases: FullDatabase[] = [];

const databaseIDs = [
...(pluginConfig.databases ?? []),
...(process.env['GATSBY_NOTION_DATABASES']?.split(/, +/) ?? []),
];

for (const databaseID of databaseIDs) {
for (const databaseID of pluginConfig.databases) {
const database = await client.getDatabase(databaseID);
databases.push(database);
}
Expand All @@ -61,16 +88,11 @@ export async function getDatabases(
*/
export async function getPages(
client: Notion,
pluginConfig: PluginConfig,
pluginConfig: FullPluginConfig,
): Promise<FullPage[]> {
const pages: FullPage[] = [];

const pageIDs = [
...(pluginConfig.pages ?? []),
...(process.env['GATSBY_NOTION_PAGES']?.split(/, +/) ?? []),
];

for (const pageID of pageIDs) {
for (const pageID of pluginConfig.pages) {
pages.push(await client.getPage(pageID));
}

Expand Down
86 changes: 50 additions & 36 deletions spec/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,72 @@
*/

import { Notion } from '#client';
import { getDatabases, getPages } from '#plugin';
import { getDatabases, getPages, normaliseConfig } from '#plugin';
import { mockDatabase, mockPage } from './mock';

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

describe('fn:normaliseConfig', () => {
const env = { ...process.env };
afterEach(() => (process.env = { ...env }));

it('combine options from the environment variables as well', () => {
process.env.GATSBY_NOTION_DATABASES = 'database_env_1, database_env_2';
process.env.GATSBY_NOTION_PAGES = 'page_env_1, page_env_2';

const normalisedConfig = normaliseConfig({
databases: ['database_options'],
pages: ['page_options'],
});

expect(normalisedConfig.databases).toEqual([
'database_options',
'database_env_1',
'database_env_2',
]);

expect(normalisedConfig.pages).toEqual([
'page_options',
'page_env_1',
'page_env_2',
]);
});
});

describe('fn:getDatabases', () => {
it('return nothing if no database is supplied', async () => {
expect(await getDatabases(client, { plugins: [] })).toEqual([]);
expect(await getDatabases(client, normaliseConfig({}))).toEqual([]);
});

it('return a combined list of databases from the options and environment variables', async () => {
mockDatabase('database_from_options');
mockDatabase('database_from_env_1');
mockDatabase('database_from_env_2');
it('return databases from Notion API', async () => {
mockDatabase('database');

process.env['GATSBY_NOTION_DATABASES'] =
'database_from_env_1, database_from_env_2';

const databases = await getDatabases(client, {
plugins: [],
databases: ['database_from_options'],
});
expect(databases.length).toEqual(3);
expect(databases.map((database) => database.id)).toEqual([
'database_from_options',
'database_from_env_1',
'database_from_env_2',
]);
const databases = await getDatabases(
client,
normaliseConfig({
databases: ['database'],
}),
);
expect(databases.length).toEqual(1);
expect(databases.map((database) => database.id)).toEqual(['database']);
});
});

describe('fn:getPages', () => {
it('return nothing if no page is supplied', async () => {
expect(await getPages(client, { plugins: [] })).toEqual([]);
expect(await getPages(client, normaliseConfig({}))).toEqual([]);
});

it('return a combined list of pages from the options and environment variables', async () => {
mockPage('page_from_options');
mockPage('page_from_env_1');
mockPage('page_from_env_2');

process.env['GATSBY_NOTION_PAGES'] = 'page_from_env_1, page_from_env_2';
it('return pages from Notion API', async () => {
mockPage('page');

const pages = await getPages(client, {
plugins: [],
pages: ['page_from_options'],
});
expect(pages.length).toEqual(3);
expect(pages.map((page) => page.id)).toEqual([
'page_from_options',
'page_from_env_1',
'page_from_env_2',
]);
const pages = await getPages(
client,
normaliseConfig({
pages: ['page'],
}),
);
expect(pages.length).toEqual(1);
expect(pages.map((page) => page.id)).toEqual(['page']);
});
});

0 comments on commit 99c04f9

Please sign in to comment.