Skip to content

Commit

Permalink
refactor our types and interfaces files to be a bit more organized
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed May 19, 2022
1 parent 0bc9bcf commit b81d89f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
12 changes: 6 additions & 6 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import child_process from "child_process"
import { createFilePath } from "gatsby-source-filesystem"
import type { GatsbyNode } from "gatsby"

import type { TContext } from "./types"
import type { AllMdxQuery } from "./src/types/schema"
import type { Context } from "./src/types"
import type { AllMdxQuery } from "./src/interfaces"

import mergeTranslations from "./src/scripts/mergeTranslations"
import copyContributors from "./src/scripts/copyContributors"
Expand Down Expand Up @@ -184,7 +184,7 @@ export const onCreateNode: GatsbyNode["onCreateNode"] = async ({
}
}

export const createPages: GatsbyNode<any, TContext>["createPages"] = async ({
export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
graphql,
actions,
reporter,
Expand Down Expand Up @@ -289,7 +289,7 @@ export const createPages: GatsbyNode<any, TContext>["createPages"] = async ({
}
}

createPage<TContext>({
createPage<Context>({
path: slug,
component: path.resolve(`src/templates/${template}.js`),
context: {
Expand Down Expand Up @@ -358,7 +358,7 @@ export const createPages: GatsbyNode<any, TContext>["createPages"] = async ({
// Add additional context to translated pages
// Only ran when creating component pages
// https://www.gatsbyjs.com/docs/creating-and-modifying-pages/#pass-context-to-pages
export const onCreatePage: GatsbyNode<any, TContext>["onCreatePage"] = async ({
export const onCreatePage: GatsbyNode<any, Context>["onCreatePage"] = async ({
page,
actions,
}) => {
Expand All @@ -373,7 +373,7 @@ export const onCreatePage: GatsbyNode<any, TContext>["onCreatePage"] = async ({
page.context.language
)
deletePage(page)
createPage<TContext>({
createPage<Context>({
...page,
context: {
...page.context,
Expand Down
4 changes: 4 additions & 0 deletions src/types/schema.ts → src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface Messages {
[key: string]: string
}

export interface AllMdxQuery {
node: {
fields: {
Expand Down
12 changes: 6 additions & 6 deletions types.ts → src/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Lang } from "./src/utils/languages"
import { IMessages } from "./src/utils/flattenMessages"
import type { Messages } from "./interfaces"
import type { Lang } from "./utils/languages"

export type TIntl = {
export type Intl = {
language: Lang
languages: Array<Lang>
defaultLanguage: Lang
messages: IMessages
messages: Messages
routed: boolean
originalPath: string
redirect: boolean
}

export type TContext = {
export type Context = {
slug: string
relativePath: string
intl: TIntl
intl: Intl
language?: string
isOutdated: boolean
isContentEnglish?: boolean
Expand Down
8 changes: 3 additions & 5 deletions src/utils/flattenMessages.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export interface IMessages {
[key: string]: string
}
import type { Messages } from "../interfaces"

// same function from 'gatsby-plugin-intl'
const flattenMessages = (nestedMessages: IMessages, prefix = ""): IMessages => {
return Object.keys(nestedMessages).reduce<IMessages>((messages, key) => {
const flattenMessages = (nestedMessages: Messages, prefix = ""): Messages => {
return Object.keys(nestedMessages).reduce<Messages>((messages, key) => {
let value = nestedMessages[key]
let prefixedKey = prefix ? `${prefix}.${key}` : key

Expand Down
5 changes: 3 additions & 2 deletions src/utils/getMessages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from "fs"

import flattenMessages, { IMessages } from "./flattenMessages"
import flattenMessages from "./flattenMessages"

import type { Messages } from "../interfaces"
import type { Lang } from "./languages"

// same function from 'gatsby-plugin-intl'
const getMessages = (path: string, language: Lang): IMessages => {
const getMessages = (path: string, language: Lang): Messages => {
try {
const messages = JSON.parse(
fs.readFileSync(`${path}/${language}.json`, "utf8")
Expand Down

0 comments on commit b81d89f

Please sign in to comment.