Refactor routing in App component to enhance navigation and improve error handling by integrating dynamic routes and updating the NotFound route.
This commit is contained in:
7
node_modules/trough/index.d.ts
generated
vendored
Normal file
7
node_modules/trough/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
export type Callback = import('./lib/index.js').Callback;
|
||||
export type Middleware = import('./lib/index.js').Middleware;
|
||||
export type Pipeline = import('./lib/index.js').Pipeline;
|
||||
export type Run = import('./lib/index.js').Run;
|
||||
export type Use = import('./lib/index.js').Use;
|
||||
export { trough, wrap } from "./lib/index.js";
|
||||
//# sourceMappingURL=index.d.ts.map
|
1
node_modules/trough/index.d.ts.map
generated
vendored
Normal file
1
node_modules/trough/index.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"uBACa,OAAO,gBAAgB,EAAE,QAAQ;yBACjC,OAAO,gBAAgB,EAAE,UAAU;uBACnC,OAAO,gBAAgB,EAAE,QAAQ;kBACjC,OAAO,gBAAgB,EAAE,GAAG;kBAC5B,OAAO,gBAAgB,EAAE,GAAG"}
|
9
node_modules/trough/index.js
generated
vendored
Normal file
9
node_modules/trough/index.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @typedef {import('./lib/index.js').Callback} Callback
|
||||
* @typedef {import('./lib/index.js').Middleware} Middleware
|
||||
* @typedef {import('./lib/index.js').Pipeline} Pipeline
|
||||
* @typedef {import('./lib/index.js').Run} Run
|
||||
* @typedef {import('./lib/index.js').Use} Use
|
||||
*/
|
||||
|
||||
export {trough, wrap} from './lib/index.js'
|
105
node_modules/trough/lib/index.d.ts
generated
vendored
Normal file
105
node_modules/trough/lib/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* @typedef {(error?: Error | null | undefined, ...output: Array<any>) => void} Callback
|
||||
* Callback.
|
||||
*
|
||||
* @typedef {(...input: Array<any>) => any} Middleware
|
||||
* Ware.
|
||||
*
|
||||
* @typedef Pipeline
|
||||
* Pipeline.
|
||||
* @property {Run} run
|
||||
* Run the pipeline.
|
||||
* @property {Use} use
|
||||
* Add middleware.
|
||||
*
|
||||
* @typedef {(...input: Array<any>) => void} Run
|
||||
* Call all middleware.
|
||||
*
|
||||
* Calls `done` on completion with either an error or the output of the
|
||||
* last middleware.
|
||||
*
|
||||
* > 👉 **Note**: as the length of input defines whether async functions get a
|
||||
* > `next` function,
|
||||
* > it’s recommended to keep `input` at one value normally.
|
||||
|
||||
*
|
||||
* @typedef {(fn: Middleware) => Pipeline} Use
|
||||
* Add middleware.
|
||||
*/
|
||||
/**
|
||||
* Create new middleware.
|
||||
*
|
||||
* @returns {Pipeline}
|
||||
* Pipeline.
|
||||
*/
|
||||
export function trough(): Pipeline;
|
||||
/**
|
||||
* Wrap `middleware` into a uniform interface.
|
||||
*
|
||||
* You can pass all input to the resulting function.
|
||||
* `callback` is then called with the output of `middleware`.
|
||||
*
|
||||
* If `middleware` accepts more arguments than the later given in input,
|
||||
* an extra `done` function is passed to it after that input,
|
||||
* which must be called by `middleware`.
|
||||
*
|
||||
* The first value in `input` is the main input value.
|
||||
* All other input values are the rest input values.
|
||||
* The values given to `callback` are the input values,
|
||||
* merged with every non-nullish output value.
|
||||
*
|
||||
* * if `middleware` throws an error,
|
||||
* returns a promise that is rejected,
|
||||
* or calls the given `done` function with an error,
|
||||
* `callback` is called with that error
|
||||
* * if `middleware` returns a value or returns a promise that is resolved,
|
||||
* that value is the main output value
|
||||
* * if `middleware` calls `done`,
|
||||
* all non-nullish values except for the first one (the error) overwrite the
|
||||
* output values
|
||||
*
|
||||
* @param {Middleware} middleware
|
||||
* Function to wrap.
|
||||
* @param {Callback} callback
|
||||
* Callback called with the output of `middleware`.
|
||||
* @returns {Run}
|
||||
* Wrapped middleware.
|
||||
*/
|
||||
export function wrap(middleware: Middleware, callback: Callback): Run;
|
||||
/**
|
||||
* Callback.
|
||||
*/
|
||||
export type Callback = (error?: Error | null | undefined, ...output: Array<any>) => void;
|
||||
/**
|
||||
* Ware.
|
||||
*/
|
||||
export type Middleware = (...input: Array<any>) => any;
|
||||
/**
|
||||
* Pipeline.
|
||||
*/
|
||||
export type Pipeline = {
|
||||
/**
|
||||
* Run the pipeline.
|
||||
*/
|
||||
run: Run;
|
||||
/**
|
||||
* Add middleware.
|
||||
*/
|
||||
use: Use;
|
||||
};
|
||||
/**
|
||||
* Call all middleware.
|
||||
*
|
||||
* Calls `done` on completion with either an error or the output of the
|
||||
* last middleware.
|
||||
*
|
||||
* > 👉 **Note**: as the length of input defines whether async functions get a
|
||||
* > `next` function,
|
||||
* > it’s recommended to keep `input` at one value normally.
|
||||
*/
|
||||
export type Run = (...input: Array<any>) => void;
|
||||
/**
|
||||
* Add middleware.
|
||||
*/
|
||||
export type Use = (fn: Middleware) => Pipeline;
|
||||
//# sourceMappingURL=index.d.ts.map
|
1
node_modules/trough/lib/index.d.ts.map
generated
vendored
Normal file
1
node_modules/trough/lib/index.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH;;;;;GAKG;AACH,0BAHa,QAAQ,CAoEpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,iCAPW,UAAU,YAEV,QAAQ,GAEN,GAAG,CAuEf;;;;gCAzMqB,KAAK,GAAG,IAAI,GAAG,SAAS,aAAa,MAAM,GAAG,CAAC,KAAK,IAAI;;;;oCAGtD,MAAM,GAAG,CAAC,KAAK,GAAG;;;;;;;;SAK5B,GAAG;;;;SAEH,GAAG;;;;;;;;;;;;6BAGO,MAAM,GAAG,CAAC,KAAK,IAAI;;;;uBAWzB,UAAU,KAAK,QAAQ"}
|
206
node_modules/trough/lib/index.js
generated
vendored
Normal file
206
node_modules/trough/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,206 @@
|
||||
// To do: remove `void`s
|
||||
// To do: remove `null` from output of our APIs, allow it as user APIs.
|
||||
|
||||
/**
|
||||
* @typedef {(error?: Error | null | undefined, ...output: Array<any>) => void} Callback
|
||||
* Callback.
|
||||
*
|
||||
* @typedef {(...input: Array<any>) => any} Middleware
|
||||
* Ware.
|
||||
*
|
||||
* @typedef Pipeline
|
||||
* Pipeline.
|
||||
* @property {Run} run
|
||||
* Run the pipeline.
|
||||
* @property {Use} use
|
||||
* Add middleware.
|
||||
*
|
||||
* @typedef {(...input: Array<any>) => void} Run
|
||||
* Call all middleware.
|
||||
*
|
||||
* Calls `done` on completion with either an error or the output of the
|
||||
* last middleware.
|
||||
*
|
||||
* > 👉 **Note**: as the length of input defines whether async functions get a
|
||||
* > `next` function,
|
||||
* > it’s recommended to keep `input` at one value normally.
|
||||
|
||||
*
|
||||
* @typedef {(fn: Middleware) => Pipeline} Use
|
||||
* Add middleware.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create new middleware.
|
||||
*
|
||||
* @returns {Pipeline}
|
||||
* Pipeline.
|
||||
*/
|
||||
export function trough() {
|
||||
/** @type {Array<Middleware>} */
|
||||
const fns = []
|
||||
/** @type {Pipeline} */
|
||||
const pipeline = {run, use}
|
||||
|
||||
return pipeline
|
||||
|
||||
/** @type {Run} */
|
||||
function run(...values) {
|
||||
let middlewareIndex = -1
|
||||
/** @type {Callback} */
|
||||
const callback = values.pop()
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('Expected function as last argument, not ' + callback)
|
||||
}
|
||||
|
||||
next(null, ...values)
|
||||
|
||||
/**
|
||||
* Run the next `fn`, or we’re done.
|
||||
*
|
||||
* @param {Error | null | undefined} error
|
||||
* @param {Array<any>} output
|
||||
*/
|
||||
function next(error, ...output) {
|
||||
const fn = fns[++middlewareIndex]
|
||||
let index = -1
|
||||
|
||||
if (error) {
|
||||
callback(error)
|
||||
return
|
||||
}
|
||||
|
||||
// Copy non-nullish input into values.
|
||||
while (++index < values.length) {
|
||||
if (output[index] === null || output[index] === undefined) {
|
||||
output[index] = values[index]
|
||||
}
|
||||
}
|
||||
|
||||
// Save the newly created `output` for the next call.
|
||||
values = output
|
||||
|
||||
// Next or done.
|
||||
if (fn) {
|
||||
wrap(fn, next)(...output)
|
||||
} else {
|
||||
callback(null, ...output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {Use} */
|
||||
function use(middelware) {
|
||||
if (typeof middelware !== 'function') {
|
||||
throw new TypeError(
|
||||
'Expected `middelware` to be a function, not ' + middelware
|
||||
)
|
||||
}
|
||||
|
||||
fns.push(middelware)
|
||||
return pipeline
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap `middleware` into a uniform interface.
|
||||
*
|
||||
* You can pass all input to the resulting function.
|
||||
* `callback` is then called with the output of `middleware`.
|
||||
*
|
||||
* If `middleware` accepts more arguments than the later given in input,
|
||||
* an extra `done` function is passed to it after that input,
|
||||
* which must be called by `middleware`.
|
||||
*
|
||||
* The first value in `input` is the main input value.
|
||||
* All other input values are the rest input values.
|
||||
* The values given to `callback` are the input values,
|
||||
* merged with every non-nullish output value.
|
||||
*
|
||||
* * if `middleware` throws an error,
|
||||
* returns a promise that is rejected,
|
||||
* or calls the given `done` function with an error,
|
||||
* `callback` is called with that error
|
||||
* * if `middleware` returns a value or returns a promise that is resolved,
|
||||
* that value is the main output value
|
||||
* * if `middleware` calls `done`,
|
||||
* all non-nullish values except for the first one (the error) overwrite the
|
||||
* output values
|
||||
*
|
||||
* @param {Middleware} middleware
|
||||
* Function to wrap.
|
||||
* @param {Callback} callback
|
||||
* Callback called with the output of `middleware`.
|
||||
* @returns {Run}
|
||||
* Wrapped middleware.
|
||||
*/
|
||||
export function wrap(middleware, callback) {
|
||||
/** @type {boolean} */
|
||||
let called
|
||||
|
||||
return wrapped
|
||||
|
||||
/**
|
||||
* Call `middleware`.
|
||||
* @this {any}
|
||||
* @param {Array<any>} parameters
|
||||
* @returns {void}
|
||||
*/
|
||||
function wrapped(...parameters) {
|
||||
const fnExpectsCallback = middleware.length > parameters.length
|
||||
/** @type {any} */
|
||||
let result
|
||||
|
||||
if (fnExpectsCallback) {
|
||||
parameters.push(done)
|
||||
}
|
||||
|
||||
try {
|
||||
result = middleware.apply(this, parameters)
|
||||
} catch (error) {
|
||||
const exception = /** @type {Error} */ (error)
|
||||
|
||||
// Well, this is quite the pickle.
|
||||
// `middleware` received a callback and called it synchronously, but that
|
||||
// threw an error.
|
||||
// The only thing left to do is to throw the thing instead.
|
||||
if (fnExpectsCallback && called) {
|
||||
throw exception
|
||||
}
|
||||
|
||||
return done(exception)
|
||||
}
|
||||
|
||||
if (!fnExpectsCallback) {
|
||||
if (result && result.then && typeof result.then === 'function') {
|
||||
result.then(then, done)
|
||||
} else if (result instanceof Error) {
|
||||
done(result)
|
||||
} else {
|
||||
then(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call `callback`, only once.
|
||||
*
|
||||
* @type {Callback}
|
||||
*/
|
||||
function done(error, ...output) {
|
||||
if (!called) {
|
||||
called = true
|
||||
callback(error, ...output)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call `done` with one value.
|
||||
*
|
||||
* @param {any} [value]
|
||||
*/
|
||||
function then(value) {
|
||||
done(null, value)
|
||||
}
|
||||
}
|
21
node_modules/trough/license
generated
vendored
Normal file
21
node_modules/trough/license
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
77
node_modules/trough/package.json
generated
vendored
Normal file
77
node_modules/trough/package.json
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
{
|
||||
"name": "trough",
|
||||
"version": "2.2.0",
|
||||
"description": "`trough` is middleware",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"middleware",
|
||||
"ware"
|
||||
],
|
||||
"repository": "wooorm/trough",
|
||||
"bugs": "https://github.com/wooorm/trough/issues",
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/wooorm"
|
||||
},
|
||||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
|
||||
"contributors": [
|
||||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
|
||||
],
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"exports": "./index.js",
|
||||
"files": [
|
||||
"lib/",
|
||||
"index.d.ts.map",
|
||||
"index.d.ts",
|
||||
"index.js"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"c8": "^9.0.0",
|
||||
"prettier": "^3.0.0",
|
||||
"remark-cli": "^11.0.0",
|
||||
"remark-preset-wooorm": "^9.0.0",
|
||||
"type-coverage": "^2.0.0",
|
||||
"typescript": "^5.0.0",
|
||||
"xo": "^0.56.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --build --clean && tsc --build && type-coverage",
|
||||
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
|
||||
"prepack": "npm run build && npm run format",
|
||||
"test": "npm run build && npm run format && npm run test-coverage",
|
||||
"test-api": "node --conditions development test.js",
|
||||
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
|
||||
},
|
||||
"prettier": {
|
||||
"bracketSpacing": false,
|
||||
"singleQuote": true,
|
||||
"semi": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "none",
|
||||
"useTabs": false
|
||||
},
|
||||
"remarkConfig": {
|
||||
"plugins": [
|
||||
"remark-preset-wooorm"
|
||||
]
|
||||
},
|
||||
"typeCoverage": {
|
||||
"atLeast": 100,
|
||||
"detail": true,
|
||||
"strict": true,
|
||||
"ignoreCatch": true,
|
||||
"#": "some nessecary `any`s",
|
||||
"ignoreFiles": [
|
||||
"lib/index.js",
|
||||
"lib/index.d.ts"
|
||||
]
|
||||
},
|
||||
"xo": {
|
||||
"prettier": true,
|
||||
"rules": {
|
||||
"capitalized-comments": "off"
|
||||
}
|
||||
}
|
||||
}
|
494
node_modules/trough/readme.md
generated
vendored
Normal file
494
node_modules/trough/readme.md
generated
vendored
Normal file
@@ -0,0 +1,494 @@
|
||||
# trough
|
||||
|
||||
[![Build][badge-build-image]][badge-build-url]
|
||||
[![Coverage][badge-coverage-image]][badge-coverage-url]
|
||||
[![Downloads][badge-downloads-image]][badge-downloads-url]
|
||||
[![Size][badge-size-image]][badge-size-url]
|
||||
|
||||
`trough` is middleware.
|
||||
|
||||
## Contents
|
||||
|
||||
* [What is this?](#what-is-this)
|
||||
* [When should I use this?](#when-should-i-use-this)
|
||||
* [Install](#install)
|
||||
* [Use](#use)
|
||||
* [API](#api)
|
||||
* [`trough()`](#trough-1)
|
||||
* [`wrap(middleware, callback)`](#wrapmiddleware-callback)
|
||||
* [`Callback`](#callback)
|
||||
* [`Middleware`](#middleware)
|
||||
* [`Pipeline`](#pipeline)
|
||||
* [`Run`](#run)
|
||||
* [`Use`](#use-1)
|
||||
* [Compatibility](#compatibility)
|
||||
* [Security](#security)
|
||||
* [Contribute](#contribute)
|
||||
* [License](#license)
|
||||
|
||||
## What is this?
|
||||
|
||||
`trough` is like [`ware`][github-segmentio-ware] with less sugar.
|
||||
Middleware functions can also change the input of the next.
|
||||
|
||||
The word **trough** (`/trôf/`) means a channel used to convey a liquid.
|
||||
|
||||
## When should I use this?
|
||||
|
||||
You can use this package when you’re building something that accepts “plugins”,
|
||||
which are functions, that can be sync or async, promises or callbacks.
|
||||
|
||||
## Install
|
||||
|
||||
This package is [ESM only][github-gist-esm].
|
||||
In Node.js (version 16+),
|
||||
install with [npm][npm-install]:
|
||||
|
||||
```sh
|
||||
npm install trough
|
||||
```
|
||||
|
||||
In Deno with [`esm.sh`][esm-sh]:
|
||||
|
||||
```js
|
||||
import {trough, wrap} from 'https://esm.sh/trough@2'
|
||||
```
|
||||
|
||||
In browsers with [`esm.sh`][esm-sh]:
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import {trough, wrap} from 'https://esm.sh/trough@2?bundle'
|
||||
</script>
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
```js
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import process from 'node:process'
|
||||
import {trough} from 'trough'
|
||||
|
||||
const pipeline = trough()
|
||||
.use(function (fileName) {
|
||||
console.log('Checking… ' + fileName)
|
||||
})
|
||||
.use(function (fileName) {
|
||||
return path.join(process.cwd(), fileName)
|
||||
})
|
||||
.use(function (filePath, next) {
|
||||
fs.stat(filePath, function (error, stats) {
|
||||
next(error, {filePath, stats})
|
||||
})
|
||||
})
|
||||
.use(function (ctx, next) {
|
||||
if (ctx.stats.isFile()) {
|
||||
fs.readFile(ctx.filePath, next)
|
||||
} else {
|
||||
next(new Error('Expected file'))
|
||||
}
|
||||
})
|
||||
|
||||
pipeline.run('readme.md', console.log)
|
||||
pipeline.run('node_modules', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
Checking… readme.md
|
||||
Checking… node_modules
|
||||
Error: Expected file
|
||||
at ~/example.js:22:12
|
||||
at wrapped (~/node_modules/trough/index.js:111:16)
|
||||
at next (~/node_modules/trough/index.js:62:23)
|
||||
at done (~/node_modules/trough/index.js:145:7)
|
||||
at ~/example.js:15:7
|
||||
at FSReqCallback.oncomplete (node:fs:199:5)
|
||||
null <Buffer 23 20 74 72 6f 75 67 68 0a 0a 5b 21 5b 42 75 69 6c 64 5d 5b 62 75 69 6c 64 2d 62 61 64 67 65 5d 5d 5b 62 75 69 6c 64 5d 0a 5b 21 5b 43 6f 76 65 72 61 ... 7994 more bytes>
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
This package exports the identifiers
|
||||
[`trough`][api-trough] and
|
||||
[`wrap`][api-wrap].
|
||||
There is no default export.
|
||||
|
||||
It exports the [TypeScript][] types
|
||||
[`Callback`][api-callback],
|
||||
[`Middleware`][api-middleware],
|
||||
[`Pipeline`][api-pipeline],
|
||||
[`Run`][api-run],
|
||||
and [`Use`][api-use].
|
||||
|
||||
### `trough()`
|
||||
|
||||
Create new middleware.
|
||||
|
||||
###### Parameters
|
||||
|
||||
There are no parameters.
|
||||
|
||||
###### Returns
|
||||
|
||||
[`Pipeline`][api-pipeline].
|
||||
|
||||
### `wrap(middleware, callback)`
|
||||
|
||||
Wrap `middleware` into a uniform interface.
|
||||
|
||||
You can pass all input to the resulting function.
|
||||
`callback` is then called with the output of `middleware`.
|
||||
|
||||
If `middleware` accepts more arguments than the later given in input,
|
||||
an extra `done` function is passed to it after that input,
|
||||
which must be called by `middleware`.
|
||||
|
||||
The first value in `input` is the main input value.
|
||||
All other input values are the rest input values.
|
||||
The values given to `callback` are the input values,
|
||||
merged with every non-nullish output value.
|
||||
|
||||
* if `middleware` throws an error,
|
||||
returns a promise that is rejected,
|
||||
or calls the given `done` function with an error,
|
||||
`callback` is called with that error
|
||||
* if `middleware` returns a value or returns a promise that is resolved,
|
||||
that value is the main output value
|
||||
* if `middleware` calls `done`,
|
||||
all non-nullish values except for the first one (the error) overwrite the
|
||||
output values
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `middleware` ([`Middleware`][api-middleware])
|
||||
— function to wrap
|
||||
* `callback` ([`Callback`][api-callback])
|
||||
— callback called with the output of `middleware`
|
||||
|
||||
###### Returns
|
||||
|
||||
Wrapped middleware ([`Run`][api-run]).
|
||||
|
||||
### `Callback`
|
||||
|
||||
Callback function (TypeScript type).
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `error` (`Error`, optional)
|
||||
— error, if any
|
||||
* `...output` (`Array<unknown>`, optional)
|
||||
— output values
|
||||
|
||||
###### Returns
|
||||
|
||||
Nothing (`undefined`).
|
||||
|
||||
### `Middleware`
|
||||
|
||||
A middleware function called with the output of its predecessor (TypeScript
|
||||
type).
|
||||
|
||||
###### Synchronous
|
||||
|
||||
If `fn` returns or throws an error,
|
||||
the pipeline fails and `done` is called with that error.
|
||||
|
||||
If `fn` returns a value (neither `null` nor `undefined`),
|
||||
the first `input` of the next function is set to that value
|
||||
(all other `input` is passed through).
|
||||
|
||||
The following example shows how returning an error stops the pipeline:
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function (thing) {
|
||||
return new Error('Got: ' + thing)
|
||||
})
|
||||
.run('some value', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
Error: Got: some value
|
||||
at ~/example.js:5:12
|
||||
…
|
||||
```
|
||||
|
||||
The following example shows how throwing an error stops the pipeline:
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function (thing) {
|
||||
throw new Error('Got: ' + thing)
|
||||
})
|
||||
.run('more value', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
Error: Got: more value
|
||||
at ~/example.js:5:11
|
||||
…
|
||||
```
|
||||
|
||||
The following example shows how the first output can be modified:
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function (thing) {
|
||||
return 'even ' + thing
|
||||
})
|
||||
.run('more value', 'untouched', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
null 'even more value' 'untouched'
|
||||
```
|
||||
|
||||
###### Promise
|
||||
|
||||
If `fn` returns a promise,
|
||||
and that promise rejects,
|
||||
the pipeline fails and `done` is called with the rejected value.
|
||||
|
||||
If `fn` returns a promise,
|
||||
and that promise resolves with a value (neither `null` nor `undefined`),
|
||||
the first `input` of the next function is set to that value (all other `input`
|
||||
is passed through).
|
||||
|
||||
The following example shows how rejecting a promise stops the pipeline:
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function (thing) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
reject('Got: ' + thing)
|
||||
})
|
||||
})
|
||||
.run('thing', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
Got: thing
|
||||
```
|
||||
|
||||
The following example shows how the input isn’t touched by resolving to `null`.
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function () {
|
||||
return new Promise(function (resolve) {
|
||||
setTimeout(function () {
|
||||
resolve(null)
|
||||
}, 100)
|
||||
})
|
||||
})
|
||||
.run('Input', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
null 'Input'
|
||||
```
|
||||
|
||||
###### Asynchronous
|
||||
|
||||
If `fn` accepts one more argument than the given `input`,
|
||||
a `next` function is given (after the input).
|
||||
`next` must be called, but doesn’t have to be called async.
|
||||
|
||||
If `next` is given a value (neither `null` nor `undefined`) as its first
|
||||
argument,
|
||||
the pipeline fails and `done` is called with that value.
|
||||
|
||||
If `next` is given no value (either `null` or `undefined`) as the first
|
||||
argument,
|
||||
all following non-nullish values change the input of the following
|
||||
function,
|
||||
and all nullish values default to the `input`.
|
||||
|
||||
The following example shows how passing a first argument stops the pipeline:
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function (thing, next) {
|
||||
next(new Error('Got: ' + thing))
|
||||
})
|
||||
.run('thing', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
Error: Got: thing
|
||||
at ~/example.js:5:10
|
||||
```
|
||||
|
||||
The following example shows how more values than the input are passed.
|
||||
|
||||
```js
|
||||
import {trough} from 'trough'
|
||||
|
||||
trough()
|
||||
.use(function (thing, next) {
|
||||
setTimeout(function () {
|
||||
next(null, null, 'values')
|
||||
}, 100)
|
||||
})
|
||||
.run('some', console.log)
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
null 'some' 'values'
|
||||
```
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `...input` (`Array<any>`, optional)
|
||||
— input values
|
||||
|
||||
###### Returns
|
||||
|
||||
Output, promise, etc (`any`).
|
||||
|
||||
### `Pipeline`
|
||||
|
||||
Pipeline (TypeScript type).
|
||||
|
||||
###### Properties
|
||||
|
||||
* `run` ([`Run`][api-run])
|
||||
— run the pipeline
|
||||
* `use` ([`Use`][api-use])
|
||||
— add middleware
|
||||
|
||||
### `Run`
|
||||
|
||||
Call all middleware (TypeScript type).
|
||||
|
||||
Calls `done` on completion with either an error or the output of the
|
||||
last middleware.
|
||||
|
||||
> 👉 **Note**: as the length of input defines whether async functions get a
|
||||
> `next` function,
|
||||
> it’s recommended to keep `input` at one value normally.
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `...input` (`Array<any>`, optional)
|
||||
— input values
|
||||
* `done` ([`Callback`][api-callback])
|
||||
— callback called when done
|
||||
|
||||
###### Returns
|
||||
|
||||
Nothing (`undefined`).
|
||||
|
||||
### `Use`
|
||||
|
||||
Add middleware (TypeScript type).
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `middleware` ([`Middleware`][api-middleware])
|
||||
— middleware function
|
||||
|
||||
###### Returns
|
||||
|
||||
Current pipeline ([`Pipeline`][api-pipeline]).
|
||||
|
||||
## Compatibility
|
||||
|
||||
This projects is compatible with maintained versions of Node.js.
|
||||
|
||||
When we cut a new major release,
|
||||
we drop support for unmaintained versions of Node.
|
||||
This means we try to keep the current release line,
|
||||
`trough@2`,
|
||||
compatible with Node.js 12.
|
||||
|
||||
## Security
|
||||
|
||||
This package is safe.
|
||||
|
||||
## Contribute
|
||||
|
||||
Yes please!
|
||||
See [How to Contribute to Open Source][open-source-guide-contribute].
|
||||
|
||||
## License
|
||||
|
||||
[MIT][file-license] © [Titus Wormer][wooorm]
|
||||
|
||||
<!-- Definitions -->
|
||||
|
||||
[api-callback]: #callback
|
||||
|
||||
[api-middleware]: #middleware
|
||||
|
||||
[api-pipeline]: #pipeline
|
||||
|
||||
[api-run]: #run
|
||||
|
||||
[api-trough]: #trough
|
||||
|
||||
[api-use]: #use
|
||||
|
||||
[api-wrap]: #wrapmiddleware-callback
|
||||
|
||||
[badge-build-image]: https://github.com/wooorm/trough/workflows/main/badge.svg
|
||||
|
||||
[badge-build-url]: https://github.com/wooorm/trough/actions
|
||||
|
||||
[badge-coverage-image]: https://img.shields.io/codecov/c/github/wooorm/trough.svg
|
||||
|
||||
[badge-coverage-url]: https://codecov.io/github/wooorm/trough
|
||||
|
||||
[badge-downloads-image]: https://img.shields.io/npm/dm/trough.svg
|
||||
|
||||
[badge-downloads-url]: https://www.npmjs.com/package/trough
|
||||
|
||||
[badge-size-image]: https://img.shields.io/bundlejs/size/trough
|
||||
|
||||
[badge-size-url]: https://bundlejs.com/?q=trough
|
||||
|
||||
[npm-install]: https://docs.npmjs.com/cli/install
|
||||
|
||||
[esm-sh]: https://esm.sh
|
||||
|
||||
[file-license]: license
|
||||
|
||||
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
||||
|
||||
[github-segmentio-ware]: https://github.com/segmentio/ware
|
||||
|
||||
[open-source-guide-contribute]: https://opensource.guide/how-to-contribute/
|
||||
|
||||
[typescript]: https://www.typescriptlang.org
|
||||
|
||||
[wooorm]: https://wooorm.com
|
Reference in New Issue
Block a user