Skip to content

Commit eae282a

Browse files
committed
Allow customization of the PageFunction return type
1 parent 2fd3a2b commit eae282a

File tree

7 files changed

+32
-27
lines changed

7 files changed

+32
-27
lines changed

index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@ import { DomStackAggregateError } from './lib/helpers/dom-stack-aggregate-error.
3030
*/
3131

3232
/**
33-
* @template {Record<string, any>} T
33+
* @template {Record<string, any>} T - The type of variables passed to the layout function
3434
* @typedef {LayoutFunction<T>} LayoutFunction
3535
*/
3636

3737
/**
38-
* @template {Record<string, any>} T
38+
* @template {Record<string, any>} T - The type of variables for the post vars function
3939
* @typedef {PostVarsFunction<T>} PostVarsFunction
4040
*/
4141

4242
/**
43-
* @template {Record<string, any>} T
44-
* @typedef {PageFunction<T>} PageFunction
43+
* @template {Record<string, any>} T - The type of variables passed to the page function
44+
* @template [U=any] U - The return type of the page function (defaults to any)
45+
* @typedef {PageFunction<T, U>} PageFunction
4546
*/
4647

4748
/**
48-
* @template {Record<string, any>} T
49+
* @template {Record<string, any>} T - The type of variables for the template function
4950
* @typedef {TemplateFunction<T>} TemplateFunction
5051
*/
5152

5253
/**
53-
* @template {Record<string, any>} T
54+
* @template {Record<string, any>} T - The type of variables for the template async iterator
5455
* @typedef {TemplateAsyncIterator<T>} TemplateAsyncIterator
5556
*/
5657

@@ -69,7 +70,7 @@ const DEFAULT_IGNORES = /** @type {const} */ ([
6970
])
7071

7172
/**
72-
* @template {DomStackOpts} [CurrentOpts=DomStackOpts]
73+
* @template {DomStackOpts} [CurrentOpts=DomStackOpts] - The type of options for the DomStack instance
7374
*/
7475
export class DomStack {
7576
/** @type {string} */ #src = ''

lib/build-pages/page-builders/html/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import Handlebars from 'handlebars'
88

99
/**
1010
* Build all of the bundles using esbuild.
11-
* @template {Record<string, any>} T
12-
* @type {PageBuilderType<T>}
11+
* @template {Record<string, any>} T - The type of variables for the page
12+
* @type {PageBuilderType<T, string>}
1313
*/
1414
export async function htmlBuilder ({ pageInfo }) {
1515
assert(pageInfo.type === 'html', 'html builder requires a "html" page type')

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import assert from 'node:assert'
66

77
/**
88
* Build all of the bundles using esbuild.
9-
* @template {Record<string, any>} T
10-
* @type {PageBuilderType<T>}
9+
* @template {Record<string, any>} T - The type of variables for the page
10+
* @template [U=any] U - The return type of the pageLayout function
11+
* @type {PageBuilderType<T, U>}
1112
*/
1213
export async function jsBuilder ({ pageInfo }) {
1314
assert(pageInfo.type === 'js', 'js page builder requires "js" page type')

lib/build-pages/page-builders/md/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let md = null
1313

1414
/**
1515
* Build all of the bundles using esbuild.
16-
* @template {Record<string, any>} T
17-
* @type {PageBuilderType<T>}
16+
* @template {Record<string, any>} T - The type of variables for the page
17+
* @type {PageBuilderType<T, string>}
1818
*/
1919
export async function mdBuilder ({ pageInfo, options }) {
2020
assert(pageInfo.type === 'md', 'md builder requires an "md" page type')

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { writeFile, mkdir } from 'fs/promises'
2020
* pageLayout functions Can be used to type a name.layout.js file
2121
*
2222
* @async
23-
* @template {Record<string, any>} T
23+
* @template {Record<string, any>} T - The type of variables passed to the page function
24+
* @template [U=any] U - The return type of the page function (defaults to any)
2425
* @callback PageFunction
2526
* @param {object} params - The parameters for the pageLayout.
2627
* @param {T} params.vars - All default, global, layout, page, and builder vars shallow merged.
@@ -29,24 +30,26 @@ import { writeFile, mkdir } from 'fs/promises'
2930
* @param {PageInfo} params.page - Info about the current page
3031
* @param {PageData<T>[]} params.pages - An array of info about every page
3132
* @param {Object<string, string>} [params.workers] - Map of worker names to their output paths
32-
* @returns {Promise<any> | any} The rendered inner page thats compatible with its matched layout
33+
* @returns {Promise<U> | U} The rendered inner page thats compatible with its matched layout
3334
*/
3435

3536
/**
36-
* @template {Record<string, any>} T
37+
* @template {Record<string, any>} T - The type of variables for the page
38+
* @template [U=any] U - The return type of the pageLayout function
3739
* @typedef PageBuilderResult
3840
* @property {object} vars - Any variables resolved by the builder
39-
* @property {PageFunction<T>} pageLayout - The function that returns the rendered page
41+
* @property {PageFunction<T, U>} pageLayout - The function that returns the rendered page
4042
*/
4143

4244
/**
43-
* @template {Record<string, any>} T
45+
* @template {Record<string, any>} T - The type of variables for the page
46+
* @template [U=any] U - The return type of the pageLayout function
4447
* @callback PageBuilderType
4548
*
4649
* @param {object} params
4750
* @param {PageInfo} params.pageInfo
4851
* @param {BuilderOptions} [params.options]
49-
* @returns {Promise<PageBuilderResult<T>>} - The results of the build step.
52+
* @returns {Promise<PageBuilderResult<T, U>>} - The results of the build step.
5053
*/
5154

5255
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { writeFile, mkdir } from 'fs/promises'
1414
/**
1515
* Callback for rendering a template.
1616
*
17-
* @template {Record<string, any>} T
17+
* @template {Record<string, any>} T - The type of variables for the template
1818
* @callback TemplateFunction
1919
* @param {object} params - The parameters for the template.
2020
* @param {T} params.vars - All of the site globalVars.
@@ -25,13 +25,13 @@ import { writeFile, mkdir } from 'fs/promises'
2525
*/
2626

2727
/**
28-
* @template {Record<string, any>} T
28+
* @template {Record<string, any>} T - The type of variables for the template function parameters
2929
* @typedef {Parameters<TemplateFunction<T>>} TemplateFunctionParams
3030
*/
3131

3232
/**
3333
* Callback for rendering a template with an async iterator.
34-
* @template T
34+
* @template T - The type of variables for the template async iterator
3535
* @callback TemplateAsyncIterator
3636
* @param {TemplateFunctionParams<T>[0]} params - Parameters of the template function.
3737
* @returns {AsyncIterable<TemplateOutputOverride>}
@@ -47,7 +47,7 @@ import { writeFile, mkdir } from 'fs/promises'
4747

4848
/**
4949
* The template builder renders templates agains the globalVars variables
50-
* @template {Record<string, any>} T
50+
* @template {Record<string, any>} T - The type of global variables for the template builder
5151
* @param {object} params
5252
* @param {string} params.src - The src path of the site build.
5353
* @param {string} params.dest - The dest path of the site build.

lib/build-pages/page-data.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import pretty from 'pretty'
1818
*
1919
* @async
2020
* @function
21-
* @template T
21+
* @template T - The type of variables for the layout
2222
* @param {string} layoutPath - The string path to the layout ESM module.
2323
* @returns {Promise<LayoutFunction<T>>} The resolved layout exported as default from the module.
2424
*/
@@ -31,7 +31,7 @@ export async function resolveLayout (layoutPath) {
3131
/**
3232
* Callback for rendering a layout.
3333
*
34-
* @template T
34+
* @template T - The type of variables passed to the layout function
3535
* @callback LayoutFunction
3636
* @param {object} params - The parameters for the layout.
3737
* @param {T} params.vars - All default, global, layout, page, and builder vars shallow merged.
@@ -48,7 +48,7 @@ export async function resolveLayout (layoutPath) {
4848
* postVars functions Can be used to generate page vars but access all page data
4949
*
5050
* @async
51-
* @template {Record<string, any>} T
51+
* @template {Record<string, any>} T - The type of variables for the page
5252
* @callback PostVarsFunction
5353
* @param {object} params - The parameters for the pageLayout.
5454
* @param {T} params.vars - All default, global, layout, page, and builder vars shallow merged.
@@ -62,7 +62,7 @@ export async function resolveLayout (layoutPath) {
6262

6363
/**
6464
* Represents the data for a page.
65-
* @template {Record<string, any>} T
65+
* @template {Record<string, any>} T - The type of variables for the page data
6666
*/
6767
export class PageData {
6868
/** @type {PageInfo} */ pageInfo

0 commit comments

Comments
 (0)