From bec9f024a994e4b9bb256843d0e574f71cf05b36 Mon Sep 17 00:00:00 2001 From: George Adams Date: Sat, 7 Oct 2023 10:44:36 +0100 Subject: [PATCH] improve types definitions --- gatsby-config.ts | 69 +++++++++++++----------------------------------- gatsby-node.ts | 59 +++++++++-------------------------------- 2 files changed, 32 insertions(+), 96 deletions(-) diff --git a/gatsby-config.ts b/gatsby-config.ts index 4473d45f9..0977cfae7 100644 --- a/gatsby-config.ts +++ b/gatsby-config.ts @@ -8,72 +8,39 @@ import path from 'path' import adapter from 'gatsby-adapter-netlify' import locales from './locales/i18n' -interface SiteMetadata { - title: string; - description: string; - author: string; - siteUrl: string; - social: { - twitter: string - } -} - -interface AsciidocNode { - id: string; - document: { - title: string; - }; - fields: { - slug: string; - }; - html: string; -} +import type { GatsbyConfig } from 'gatsby' +import type { MDXPage, AsciidocPage } from './src/types' interface LocalSearchNormalizerArgs { data: { allAsciidoc: { - edges: Array<{ node: AsciidocNode }>; + edges: Array<{ node: AsciidocPage }>; }; }; } -interface MdxNode { - excerpt: string; - fields: { - slug: string; - postPath: string; - }; - frontmatter: { - date: string; - title: string; - }; -} +type SiteMetadata = GatsbyConfig['siteMetadata'] & { + siteUrl: string; +}; interface AllMdxQueryResult { - allMdx: { - totalCount: number; - edges: { - node: MdxNode; - }[]; - }; + allMdx: MDXPage; site: { siteMetadata: SiteMetadata; } } -const metadata: SiteMetadata = { - title: 'Adoptium', - description: 'Eclipse Adoptium provides prebuilt OpenJDK binaries ...', - author: 'Eclipse Adoptium', - siteUrl: 'https://adoptium.net', - social: { - twitter: 'Adoptium' - } -}; - -module.exports = { +const config: GatsbyConfig = { adapter: adapter(), - siteMetadata: metadata, + siteMetadata: { + title: 'Adoptium', + description: 'Eclipse Adoptium provides prebuilt OpenJDK binaries ...', + author: 'Eclipse Adoptium', + siteUrl: 'https://adoptium.net', + social: { + twitter: 'Adoptium' + } + }, plugins: [ 'gatsby-plugin-sitemap', { @@ -325,3 +292,5 @@ module.exports = { } ] } + +export default config diff --git a/gatsby-node.ts b/gatsby-node.ts index b517e0328..d3dbbe6c3 100644 --- a/gatsby-node.ts +++ b/gatsby-node.ts @@ -9,7 +9,7 @@ import { promisify } from 'util' import { createFilePath } from 'gatsby-source-filesystem' import locales from './locales/i18n' import authors from './src/json/authors.json' -import type { Actions, GatsbyNode, Node } from 'gatsby' +import type { GatsbyNode, Node } from 'gatsby' import { localizedSlug, findKey, removeTrailingSlash } from './src/util/gatsby-node-helpers' const exec = util.promisify(execChild) @@ -28,26 +28,6 @@ interface AdoptiumData { most_recent_feature_version: string; } -interface Page { - path: string; - component: string; - context: { - locale: string; - defaultGitSHA?: string | null; - language: string; - i18n: { - routed: boolean; - originalPath: string; - path: string; - language: string; - }; - }; -} - -interface Action { - createNodeField: (field: any) => void; -} - interface MdxNode extends Node { frontmatter: { date: string; @@ -58,25 +38,6 @@ interface MdxNode extends Node { } } -interface FileNode { - id: string; - relativePath: string; - absolutePath: string; -} - -interface CreateNodeArgs { - node: Node; - actions: Action; - getNode: (id: string) => FileNode; - getNodes: () => FileNode[]; -} - -interface CreatePageArgs { - page: Page; - actions: Actions; - getNodes: () => Node[]; -} - interface LocaleEntry { default?: boolean; locale: string; @@ -160,9 +121,14 @@ export const sourceNodes: GatsbyNode['sourceNodes'] = async ({ actions, createNo createNode(MostRecentFeatureVersion) } -export const onCreatePage = ({ page, actions, getNodes }: CreatePageArgs): void => { +export const onCreatePage: GatsbyNode['onCreatePage'] = async ({ page, actions, getNodes }) => { const { createPage, deletePage } = actions + // Throw error if page.context is undefined + if (!page.context) { + throw new Error('Error retrieving page context') + } + // Delete pages such as /about/index.de if (page.path.includes('index')) { return deletePage(page) @@ -200,12 +166,12 @@ export const onCreatePage = ({ page, actions, getNodes }: CreatePageArgs): void // This context also gets passed to the src/components/layout file // This should ensure that the locale is available on every page context: { - ...page.context, + ...(page.context ? page.context : {}), locale, defaultGitSHA, language: lang, i18n: { - ...page.context.i18n, + ...(page.context?.i18n ? page.context.i18n : {}), routed: true, originalPath: page.path, path: removeTrailingSlash(localizedPath), @@ -231,7 +197,7 @@ export const onCreatePage = ({ page, actions, getNodes }: CreatePageArgs): void }) } -export const onCreateNode = async ({ node, actions, getNode, getNodes }: CreateNodeArgs) => { +export const onCreateNode: GatsbyNode['onCreateNode'] = async ({ node, actions, getNode, getNodes }) => { const { createNodeField } = actions if (node.internal.type === 'Asciidoc') { @@ -240,9 +206,10 @@ export const onCreateNode = async ({ node, actions, getNode, getNodes }: CreateN // Handle the case where fetchFilePath is not found. throw new Error(`No file path found for node with parent ID: ${node.parent}`); } - const name = path.basename(fetchFilePath.relativePath, '.adoc') - const currentFileDir = path.dirname(fetchFilePath.absolutePath) + const name = path.basename(fetchFilePath.relativePath as string, '.adoc') + + const currentFileDir = path.dirname(fetchFilePath.absolutePath as string); const partialDir = path.join(currentFileDir, '_partials') // Check if post.name is "index" -- because that's the file for default language