Middleware builders for data fetching, conditional execution, and template rendering.
This file provides a set of reusable middleware functions and utility functions that can be composed together to create custom workflows for a web application. Includes functions for fetching data from a dataset, conditionally executing middleware, and rendering templates with validation.
- Source:
Members
(static, constant) FetchOneFallbackPolicy
Collection of fallback policies for the fetchOneFn
middleware.
The policy is enacted when zero records is returned from the data source.
- Source:
(static, constant) handleRejections
Express 4.x does not handle promise rejections in middleware on its own. The {@fetchOne} and {@fetchMany} middleware handle that case but for any other async code you can wrap it in this middleware to ensure rejections don't end up unhandled.
Methods
(static) fetchIf(condition, fetchFn, elseFnopt) → {function}
Returns a middleware that will fetch data if condition is satisfied,
invoke elseFn
otherwise.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
condition |
function | Predicate function that takes a request object and returns a boolean |
|
fetchFn |
function | Fetch middleware function |
|
elseFn |
function |
<optional> |
Optional function to call if condition is not met |
Returns:
Middleware function
- Type
- function
(static) fetchMany(context) → {function}
Fetches a collection of records and stores them in req
under key specified by result
entry.
Parameters:
Name | Type | Description |
---|---|---|
context |
module:middleware~QueryContext | Configuration object |
Returns:
Middleware function
- Type
- function
(static) fetchManyFn(req, res, next)
Middleware. Attempts to fetch a collection of data from datasette.
this
needs { query( {req, params } ) => any, result: string, dataset?: FetchParams | (req) => string }
Parameters:
Name | Type | Description |
---|---|---|
req |
* | |
res |
* | |
next |
* |
(static) fetchManyFromAllDatasets(context) → {function}
Fetches a collection of records from all dataset databases and stores them in req
under key specified by result
entry.
Parameters:
Name | Type | Description |
---|---|---|
context |
module:middleware~QueryContext | Configuration object |
Returns:
Middleware function
- Type
- function
(static) fetchOne(context) → {function}
Fetches a single entity and stores it in req
under key specified by result
entry.
Use fallbackPolicy
to handle zero record responses differently than the default 404 response.
See FetchOneFallbackPolicy
Parameters:
Name | Type | Description |
---|---|---|
context |
module:middleware~QueryContext | Configuration object |
Returns:
Middleware function
- Type
- function
(static) fetchOneFromAllDatasets(context) → {function}
Fetches a single record from each dataset databases and stores them in req
under key specified by result
entry.
Parameters:
Name | Type | Description |
---|---|---|
context |
module:middleware~QueryContext | Configuration object |
Returns:
Middleware function
- Type
- function
(static) parallel(middlewares) → {function}
Returns a middleware that invokes the passed middlewares in parallel.
Usage: when all sub-middlewre can be fetched independently, but we can't accept a partial success (e.g. we require all middlewares to succeed).
Parameters:
Name | Type | Description |
---|---|---|
middlewares |
Array.<function()> | Array of middleware functions |
Returns:
Express middleware function that returns a Promise
- Type
- function
(static) renderTemplate(context) → {function}
Validates and renders the template.
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
context |
Object | Configuration object Properties
|
Returns:
Express middleware function
- Type
- function
(static) renderTemplateFn(req, res, next) → {void}
Middleware. Validates and renders the template.
this
needs: { templateParams(req), template, handlerName }
Parameters:
Name | Type | Description |
---|---|---|
req |
Object | Express request object |
res |
Object | Express response object |
next |
function | Express next function |
Returns:
- Type
- void
(static) validateAndRender(res, name, params) → {string}
Looks up schema for name in templateSchema
(defaults to any()), validates and renders the template.
Parameters:
Name | Type | Description |
---|---|---|
res |
module:express~Response | response object |
name |
string | |
params |
* | template params |
Returns:
- Type
- string
(async, inner) fetchIfFn(req, res, next)
Middleware. Does a conditional fetch. Optionally invokes else
if condition is false.
this
needs: { fetchFn, condition: (req) => boolean, else?: (req) => void }
fetchFn
should be a middleware fn. Can be async.
Parameters:
Name | Type | Description |
---|---|---|
req |
* | |
res |
* | |
next |
* |
(async, inner) fetchManyFromAllDatasetsFn(req, res, next) → {Promise.<*>}
Fetches data from all available datasets and stores the result in the request object.
Parameters:
Name | Type | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
req |
Object | The request object. Properties
|
||||||||||||||||
res |
Object | The response object. |
||||||||||||||||
next |
function | The next middleware function in the chain. |
Throws:
-
If an error occurs while fetching data from any of the datasets.
- Type
- Error
Returns:
- Type
- Promise.<*>
(async, inner) fetchOneFn(req, res, next)
Middleware. Attempts to fetch data from datasette and short-circuits with 404 when data for given query does not exist. Meant to be used to fetch singular records.
this
needs { query({ req, params }) => any, result: string, dataset?: FetchParams | (req) => string, fallbackPolicy: (req, res, next) => void }
where the result
is the key under which result of the query will be stored in req
Parameters:
Name | Type | Description |
---|---|---|
req |
* | |
res |
* | |
next |
* |
- Source:
(async, inner) fetchOneFromAllDatasetsFn(req, res, next)
Middleware. Fetches one result from all available datasets.
This function runs a query on each available dataset, catches any errors that may occur, and then compiles the results into a single object. The result object is then attached to the request object.
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
req |
Object | The request object. Properties
|
|||||||||||||||
res |
Object | The response object. |
|||||||||||||||
next |
function | The next middleware function in the stack. |
Throws:
-
If any of the queries fail.
- Type
- Error
(async, inner) parallelFn(req, res, next) → {Promise.<undefined>}
Returns a middleware that executes the given sub-middlewares in parallel and waits for all of them to complete.
Parameters:
Name | Type | Description |
---|---|---|
req |
Object | Express request object |
res |
Object | Express response object |
next |
function | Express next function |
Returns:
- Type
- Promise.<undefined>
Type Definitions
QueryContext
Type:
- Object
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
query |
function | Function that takes req and params and returns query object |
|
result |
string | Key to store result under in req |
|
dataset |
Symbol |
<optional> |
how to get the dataset name, see |
context.fallbackPolicy |
function |
<optional> |
Custom fallback policy for zero results |