Skip to content

Commit 0e1939a

Browse files
committed
Enable customization of LayoutFunction types
1 parent eae282a commit 0e1939a

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ import { DomStackAggregateError } from './lib/helpers/dom-stack-aggregate-error.
3131

3232
/**
3333
* @template {Record<string, any>} T - The type of variables passed to the layout function
34-
* @typedef {LayoutFunction<T>} LayoutFunction
34+
* @template [U=any] U - The return type of the page function (defaults to any)
35+
* @template [V=string] V - The return type of the layout function (defaults to string)
36+
* @typedef {LayoutFunction<T, U, V>} LayoutFunction
3537
*/
3638

3739
/**
3840
* @template {Record<string, any>} T - The type of variables for the post vars function
39-
* @typedef {PostVarsFunction<T>} PostVarsFunction
41+
* @template [U=any] U - The return type of the page function (defaults to any)
42+
* @template [V=string] V - The return type of the layout function (defaults to string)
43+
* @typedef {PostVarsFunction<T, U, V>} PostVarsFunction
4044
*/
4145

4246
/**

lib/build-pages/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ const __dirname = import.meta.dirname
3737
*/
3838

3939
/**
40-
* @template T
40+
* @template T - The type of variables for the layout
41+
* @template [U=any] U - The return type of the page function (defaults to any)
42+
* @template [V=string] V - The return type of the layout function (defaults to string)
4143
* @typedef ResolvedLayout
42-
* @property {LayoutFunction<T>} render - The layout function
44+
* @property {LayoutFunction<T, U, V>} render - The layout function
4345
* @property {string} name - The name of the layout
4446
* @property {string | null} layoutStylePath - The string path to the layout style
4547
* @property {string | null} layoutClientPath - The string path to the layout client
@@ -126,7 +128,7 @@ export async function buildPagesDirect (src, dest, siteData, _opts) {
126128
}),
127129
])
128130

129-
/** @type {ResolvedLayout<object>[]} */
131+
/** @type {ResolvedLayout<object, any, string>[]} */
130132
const resolvedLayoutResults = await pMap(Object.values(siteData.layouts), async (layout) => {
131133
const render = await resolveLayout(layout.filepath)
132134
return {

lib/build-pages/page-builders/page-writer.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { writeFile, mkdir } from 'fs/promises'
1313

1414
/**
1515
* @template {Record<string, any>} T
16-
* @typedef {PageData<T>} PageData
16+
* @template [U=any] U - The return type of the page function (defaults to any)
17+
* @template [V=string] V - The return type of the layout function (defaults to string)
18+
* @typedef {PageData<T, U, V>} PageData
1719
*/
1820

1921
/**
@@ -28,7 +30,7 @@ import { writeFile, mkdir } from 'fs/promises'
2830
* @param {string[]} [params.scripts] - Array of script URLs to include.
2931
* @param {string[]} [params.styles] - Array of stylesheet URLs to include.
3032
* @param {PageInfo} params.page - Info about the current page
31-
* @param {PageData<T>[]} params.pages - An array of info about every page
33+
* @param {PageData<T, U, string>[]} params.pages - An array of info about every page
3234
* @param {Object<string, string>} [params.workers] - Map of worker names to their output paths
3335
* @returns {Promise<U> | U} The rendered inner page thats compatible with its matched layout
3436
*/
@@ -55,11 +57,13 @@ import { writeFile, mkdir } from 'fs/promises'
5557
/**
5658
* Handles rendering and writing a page to disk
5759
* @template {Record<string, any>} T
60+
* @template [U=any] U - The return type of the page function (defaults to any)
61+
* @template [V=string] V - The return type of the layout function (defaults to string)
5862
* @param {object} params
5963
* @param {string} params.src - The src folder.
6064
* @param {string} params.dest - The dest folder.
61-
* @param {PageData<T>} params.page - The PageInfo object of the current page
62-
* @param {PageData<T>[]} params.pages - The PageInfo[] array of all pages
65+
* @param {PageData<T, U, V>} params.page - The PageInfo object of the current page
66+
* @param {PageData<T, U, V>[]} params.pages - The PageInfo[] array of all pages
6367
*/
6468
export async function pageWriter ({
6569
dest,

lib/build-pages/page-builders/template-builder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { writeFile, mkdir } from 'fs/promises'
1919
* @param {object} params - The parameters for the template.
2020
* @param {T} params.vars - All of the site globalVars.
2121
* @param {TemplateInfo} params.template - Info about the current template
22-
* @param {PageData<T>[]} params.pages - An array of info about every page
22+
* @param {PageData<T, any, string>[]} params.pages - An array of info about every page
2323
* @returns {Promise<string | TemplateOutputOverride | TemplateOutputOverride[]>}
2424
* } - The results of a template build
2525
*/
@@ -53,7 +53,7 @@ import { writeFile, mkdir } from 'fs/promises'
5353
* @param {string} params.dest - The dest path of the site build.
5454
* @param {T} params.globalVars - The resolvedGlobal vars object.
5555
* @param {TemplateInfo} params.template - The TemplateInfo of the template.
56-
* @param {PageData<T>[]} params.pages - The array of PageData object.
56+
* @param {PageData<T, any, string>[]} params.pages - The array of PageData object.
5757
*/
5858
export async function templateBuilder ({
5959
dest,

lib/build-pages/page-data.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import pretty from 'pretty'
1919
* @async
2020
* @function
2121
* @template T - The type of variables for the layout
22+
* @template [U=any] U - The return type of the page function (defaults to any)
23+
* @template [V=string] V - The return type of the layout function (defaults to string)
2224
* @param {string} layoutPath - The string path to the layout ESM module.
23-
* @returns {Promise<LayoutFunction<T>>} The resolved layout exported as default from the module.
25+
* @returns {Promise<LayoutFunction<T, U, V>>} The resolved layout exported as default from the module.
2426
*/
2527
export async function resolveLayout (layoutPath) {
2628
const { default: layout } = await import(layoutPath)
@@ -32,41 +34,47 @@ export async function resolveLayout (layoutPath) {
3234
* Callback for rendering a layout.
3335
*
3436
* @template T - The type of variables passed to the layout function
37+
* @template [U=any] U - The return type of the page function (defaults to any)
38+
* @template [V=string] V - The return type of the layout function (defaults to string)
3539
* @callback LayoutFunction
3640
* @param {object} params - The parameters for the layout.
3741
* @param {T} params.vars - All default, global, layout, page, and builder vars shallow merged.
3842
* @param {string[]} [params.scripts] - Array of script URLs to include.
3943
* @param {string[]} [params.styles] - Array of stylesheet URLs to include.
4044
* @param {any} params.children - The children content, either as a string or a render function.
4145
* @param {PageInfo} params.page - Info about the current page
42-
* @param {PageData<T>[]} params.pages - An array of info about every page
46+
* @param {PageData<T, U, V>[]} params.pages - An array of info about every page
4347
* @param {Object<string, string>} [params.workers] - Map of worker names to their output paths
44-
* @returns {Promise<string> | string} The rendered HTML string.
48+
* @returns {Promise<V> | V} The rendered content.
4549
*/
4650

4751
/**
4852
* postVars functions Can be used to generate page vars but access all page data
4953
*
5054
* @async
5155
* @template {Record<string, any>} T - The type of variables for the page
56+
* @template [U=any] U - The return type of the page function (defaults to any)
57+
* @template [V=string] V - The return type of the layout function (defaults to string)
5258
* @callback PostVarsFunction
5359
* @param {object} params - The parameters for the pageLayout.
5460
* @param {T} params.vars - All default, global, layout, page, and builder vars shallow merged.
5561
* @param {string[]} [params.scripts] - Array of script URLs to include.
5662
* @param {string[]} [params.styles] - Array of stylesheet URLs to include.
5763
* @param {PageInfo} params.page - Info about the current page
58-
* @param {PageData<T>[]} params.pages - An array of info about every page
64+
* @param {PageData<T, U, V>[]} params.pages - An array of info about every page
5965
* @param {Object<string, string>} [params.workers] - Map of worker names to their output paths
6066
* @returns {Promise<T>} The rendered postVars
6167
*/
6268

6369
/**
6470
* Represents the data for a page.
6571
* @template {Record<string, any>} T - The type of variables for the page data
72+
* @template [U=any] U - The return type of the page function (defaults to any)
73+
* @template [V=string] V - The return type of the layout function (defaults to string)
6674
*/
6775
export class PageData {
6876
/** @type {PageInfo} */ pageInfo
69-
/** @type {ResolvedLayout<T> | null | undefined} */ layout
77+
/** @type {ResolvedLayout<T, U, V> | null | undefined} */ layout
7078
/** @type {object} */ globalVars
7179
/** @type {object?} */ pageVars = null
7280
/** @type {function?} */ postVars = null
@@ -139,7 +147,7 @@ export class PageData {
139147
}
140148

141149
/**
142-
* @type {PostVarsFunction<T>}
150+
* @type {PostVarsFunction<T, U, V>}
143151
*/
144152
async #renderPostVars ({ vars, styles, scripts, pages, page, workers }) {
145153
if (!this.#initialized) throw new Error('Initialize PageData before accessing renderPostVars')
@@ -163,7 +171,7 @@ export class PageData {
163171
/**
164172
* [init description]
165173
* @param {object} params - Parameters required to initialize
166-
* @param {Record<string,ResolvedLayout<T>>} params.layouts - The array of ResolvedLayouts
174+
* @param {Record<string,ResolvedLayout<T, U, V>>} params.layouts - The array of ResolvedLayouts
167175
*/
168176
async init ({ layouts }) {
169177
if (this.#initialized) return
@@ -232,7 +240,7 @@ export class PageData {
232240
/**
233241
* Render the inner contents of a page.
234242
* @param {object} params The params required to render the page
235-
* @param {PageData<T>[]} params.pages An array of initialized PageDatas.
243+
* @param {PageData<T, U, V>[]} params.pages An array of initialized PageDatas.
236244
*/
237245
async renderInnerPage ({ pages }) {
238246
if (!this.#initialized) throw new Error('Must be initialized before rendering inner pages')
@@ -248,7 +256,7 @@ export class PageData {
248256
/**
249257
* Render the full contents of a page with its layout
250258
* @param {object} params The params required to render the page
251-
* @param {PageData<T>[]} params.pages An array of initialized PageDatas.
259+
* @param {PageData<T, U, V>[]} params.pages An array of initialized PageDatas.
252260
*/
253261
async renderFullPage ({ pages }) {
254262
if (!this.#initialized) throw new Error('Must be initialized before rendering full pages')

0 commit comments

Comments
 (0)