-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgatsby-node.ts
133 lines (122 loc) Β· 3.57 KB
/
gatsby-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { config } from 'dotenv';
config();
import { CreatePagesArgs } from 'gatsby';
import path from 'node:path';
import {
FooterEntry,
FooterEntryMetaItem,
HeaderEntry,
HeaderEntryMetaItem,
HomePageEntry,
HomePageEntryMetaItem,
LegalPageEntry,
RecipeEntry,
RecipeEntryMetaItem,
RecipesPageEntry,
RecipesPageEntryMetaItem,
} from './bcms/types/ts';
import { Client } from '@thebcms/client';
import {
HomePageContent,
LegalPageContent,
RecipePageContent,
RecipesPageContent,
} from './src/types';
import { recipeToLight } from './src/utils/recipe';
const bcms = new Client(
process.env.BCMS_ORG_ID || '',
process.env.BCMS_INSTANCE_ID || '',
{
id: process.env.BCMS_API_KEY_ID || '',
secret: process.env.BCMS_API_KEY_SECRET || '',
},
{
injectSvg: true,
},
);
export const createPages = async ({
actions: { createPage },
}: CreatePagesArgs) => {
const header = (await bcms.entry.getBySlug(
'header',
'header',
)) as HeaderEntry;
const headerMeta = header.meta.en as HeaderEntryMetaItem;
const footer = (await bcms.entry.getBySlug(
'footer',
'footer',
)) as FooterEntry;
const footerMeta = footer.meta.en as FooterEntryMetaItem;
// HOME
const homeTemplate = path.resolve('./src/templates/home.tsx');
const homePageEntry = (await bcms.entry.getBySlug(
'home',
'home-page',
)) as HomePageEntry;
const homePageMeta = homePageEntry.meta.en as HomePageEntryMetaItem;
const recipeEntries = (await bcms.entry.getAll('recipe')) as RecipeEntry[];
const lightRecipes = recipeEntries.map((e) => {
return recipeToLight(e);
});
createPage({
path: `/`,
component: homeTemplate,
context: {
header: headerMeta,
footer: footerMeta,
meta: homePageMeta,
recipes: lightRecipes,
bcms: bcms.getConfig(),
} as HomePageContent,
});
// LEGAL
const legalTemplate = path.resolve('./src/templates/legal.tsx');
const legalPageEntries = (await bcms.entry.getAll(
'legal-page',
)) as LegalPageEntry[];
createPage({
path: `/legal`,
component: legalTemplate,
context: {
header: headerMeta,
footer: footerMeta,
entries: legalPageEntries,
bcms: bcms.getConfig(),
} as LegalPageContent,
});
// RECIPES
const recipesTemplate = path.resolve('./src/templates/recipes.tsx');
const recipesPageEntry = (await bcms.entry.getBySlug(
'recipes',
'recipes-page',
)) as RecipesPageEntry;
const recipesPageMeta = recipesPageEntry.meta
.en as RecipesPageEntryMetaItem;
createPage({
path: `/recipes`,
component: recipesTemplate,
context: {
header: headerMeta,
footer: footerMeta,
meta: recipesPageMeta,
recipes: lightRecipes,
bcms: bcms.getConfig(),
} as RecipesPageContent,
});
// RECIPE
const recipeTemplate = path.resolve('./src/templates/recipe.tsx');
recipeEntries.forEach((recipe) => {
const meta = recipe.meta.en as RecipeEntryMetaItem;
createPage({
path: `/recipes/${meta.slug}`,
component: recipeTemplate,
context: {
header: headerMeta,
footer: footerMeta,
meta,
recipes: lightRecipes,
bcms: bcms.getConfig(),
} as RecipePageContent,
});
});
};