链函数QQ
功能连锁容易
这个 Chain 分类为管理和执行职能链提供了一种效用。 链中的每个函数都可以可选地引用下个函数,从而能够灵活而可调和地进行执行. 这对诸如中间软件处理,数据转换,或以有条理的方式处理同步操作等情景特别有用.
目录
开始
运行以下命令开始使用 chain-functions 在您的项目中:
npm i @alessiofrittoli/chain-functions
或使用 pnpm
pnpm i @alessiofrittoli/chain-functions
发展
运行以下开始开发
nvm use
pnpm i
地方发展
运行以下
pnpm dev
这将生成未忽略的输出代码, 源图并启用源文件监视器 .
生产建设
运行以下来创建本地生产建筑
pnpm build
这将生成没有源代码的已修改输出代码 .
单位测试
通过运行下列程序运行所有定义的测试套件 :
# Run tests and watch file changes.
pnpm test:watch
# Run tests in a CI environment.
pnpm test:ci
进行覆盖测试
然后开始使用 HTTP 服务器服务覆盖文件 ./coverage 文件夹.
您可以在您第一次运行此命令时看到空白页 。 只需刷新浏览器即可看到更新 .
pnpm test:coverage:serve
捐款
献礼真受欢迎!
请参看 贡献文件 了解如何开始为该项目作出贡献的更多信息.
帮助更新此项目 GitHub 赞助商。 。 。 .
警卫
如果你认为你发现了安全漏洞 我们鼓励你 负责地披露此事,不公开问题我们将调查所有合法的报告。 电子邮件 [email protected] 披露任何安全弱点.
API 参考
Chain 类
用于管理和执行职能链的效用类别.
静态方法
Chain.functions()
递归性地执行一系列函数.
参数
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
chain | ChainFactory<T,U> | - 说吧 | 要执行的功能链 . |
这必须是一系列功能(ChainLink<T>),其中最后一个函数为 LastChainLink 类型。 见 类型 供进一步参考. | |||
index | number | 0 | (备选) 执行的起始索引 . |
掷出
Error 如果在指定的索引中找不到函数.
示例
import type { ChainLink, LastChainLink, ChainFactory } from '@alessiofrittoli/chain-functions/types'
import { Chain } from '@alessiofrittoli/chain-functions'
type ChainFunction = () => string
const function1: ChainLink<ChainFunction> = next => () => `1-${next()}`
const function2: ChainLink<ChainFunction> = next => () => `2-${next()}`
const function3: LastChainLink<ChainFunction> = () => () => 'end'
const chain: ChainFactory<ChainFunction> = [function1, function2, function3]
const result = Chain.functions(chain)()
console.log(result) // Output: '1-2-end'
Chain.isLast()
确定给定的函数是否是按键投出最后一个函数所需的链条中的最后一个函数 LastChainLink<U>。 。 。 .
这种方法主要用于内部 Chain.functions() 用于确定链式执行何时终止的方法.
参数
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
chain | ChainFactory<T,U> | - 说吧 | 职能链. 见 类型 供进一步参考. |
fn | ChainLink<T>|LastChainLink<U> | - 说吧 | 键入 Cast 函数 。 这既可以是常规链条链条,也可以是最后一个链条链条. 见 类型 供进一步参考. |
index | number | 0 | (备选) 当前函数索引 Chain.functions() 复相会相. |
回返
类型 : boolean
回返 true 如果给定的函数是链条中最后一个函数, false 否则.
类型
ChainFunction
代表任何可以作为链条的一部分引用的可调用函数.
这在内部用于打入其他类型 template 参数.
ChainLink<T extends ChainFunction = ChainFunction>
代表一个职能链中的单一环节.
参数
| 参数 | 类型 | 说明 |
|---|---|---|
next | T | 链中下个功能. 返回类型必须是类型 T。 。 。 . |
回返
类型 : T
可以被引用为链条一部分的函数.
LastChainLink<T extends ChainFunction = ChainFunction>
代表一个职能链中的最后一个环节。 与 ChainLink,它不接受 next 参数.
回返
类型 : T
可以被引用为链条中最后一步的函数.
ChainFactory<T extends ChainFunction = ChainFunction, U extends ChainFunction = T>
作为数组表示完整的函数链.
结构
- 可包含任何数量
ChainLink<T>函数. - 数组中最后一个元素必须是
LastChainLink<U>。 。 。 .
关键特性
- 链路链接功能高度自定义化.
- 链路链接功能可以是
async函数. - 最后一个链条链条可以返回不同的类型(
U除外T从标准ChainLink。 。 。 .
实例
导入库
// importing the main `Chain` class
// importing types
import type { ChainLink, LastChainLink, ChainFactory } from '@alessiofrittoli/chain-functions/types'
import { Chain } from '@alessiofrittoli/chain-functions'
基本用途
// define the chain link function type
type ChainFunction = () => string
// declare chain link functions
const function1: ChainLink<ChainFunction> = next => () => `1-${next()}`
const function2: ChainLink<ChainFunction> = next => () => `2-${next()}`
// declare the last chain function
const function3: LastChainLink<ChainFunction> = () => () => 'end'
// declare the chain array
const chain: ChainFactory<ChainFunction> = [function1, function2, function3]
// execute the chain array
const result = Chain.functions(chain)()
console.log(result) // Output: '1-2-end'
预用
type ChainFunctionProps = {
someProperty: string
firstFunction?: boolean
secondFunction?: boolean
thirdFunction?: boolean
}
// define the chain link function type
type ChainFunction = (props: ChainFunctionProps) => ChainFunctionProps
// declare chain link functions
const function1: ChainLink<ChainFunction> = next => props => {
// edit properties
props.someProperty = 'Edited by 1st function'
props.firstFunction = true
// call the next function in the chain
return next(props)
}
const function2: ChainLink<ChainFunction> = next => props => {
props.secondFunction = true
if (props.someProperty === 'Edited by 1st function') {
// stop chain execution if some condition is met.
return props
}
// call the next function in the chain
return next(props)
}
// declare the last chain function
const function3: LastChainLink<ChainFunction> = () => props => {
props.thirdFunction = true
return props
}
// declare the chain array
const chain: ChainFactory<ChainFunction> = [function1, function2, function3]
// declare the initial state
const initialState: ChainFunctionProps = {
someProperty: 'Initial value',
firstFunction: false,
secondFunction: false,
thirdFunction: false,
}
// execute the chain array with initial state
const result = Chain.functions(chain)(initialState)
console.log(result)
// Output: {
// someProperty : 'Edited by 1st function',
// firstFunction : true,
// secondFunction : true,
// thirdFunction : false,
// }
" Last ChainLink " 有自定义返回类型
type ChainFunction = () => string
type LastChainFunction = () => boolean
const function1: ChainLink<ChainFunction> = next => () => `1-${next()}`
const function2: ChainLink<ChainFunction> = next => () => `2-${next()}`
const function3: LastChainLink<LastChainFunction> = () => () => true
const chain: ChainFactory<ChainFunction, LastChainFunction> = [function1, function2, function3]
const result = Chain.functions(chain)()
console.log(result) // Outputs: '1-2-true'
" ChainLink " 职能与承诺
type ChainFunction = () => string | Promise<string>
const function1: ChainLink<ChainFunction> = next => async () => {
// simulate a long task running
await new Promise<void>(resolve => setTimeout(resolve, 5000))
return `1-${next()}`
}
// this function is executed once `function1` Promise get resolved.
const function2: ChainLink<ChainFunction> = next => () => `2-${next()}`
const function3: LastChainLink<ChainFunction> = () => () => 'end'
const chain: ChainFactory<ChainFunction> = [function1, function2, function3]
const result = Chain.functions(chain)() // `result` is now a promise
console.log(await result) // Outputs: '1-2-end'
Next.js 中间软件链
// src/middleware.ts
import type { ChainFactory, ChainLink, LastChainLink } from '@alessiofrittoli/chain-functions/types'
import { NextMiddleware, NextResponse } from 'next/server'
import { Chain } from '@alessiofrittoli/chain-functions'
type Middleware = ChainLink<NextMiddleware>
type LastMiddleware = () => NextResponse<unknown>
type MiddlewareFactory = ChainFactory<NextMiddleware, LastMiddleware>
const middleware1: Middleware = next => async (request, event) => {
const { nextUrl } = request
if (nextUrl === '...') {
const rewriteUrl = '...'
return NextResponse.rewrite(rewriteUrl)
}
return next(request, event)
}
const middleware2: Middleware = next => async (request, event) => {
const response = await next(request, event)
// do something with `response` returned by the next middleware.
// ...
return response
}
// ensures `NextResponse.next()` is called if no one stops the chain.
const lastMiddleware: LastChainLink<LastMiddleware> = () => () => NextResponse.next()
const middlewares: MiddlewareFactory = [middleware1, middleware2, lastMiddleware]
export const config = {
matcher: [
/**
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api/|_next|.*\\..*).*)',
],
}
// note that we do not execute the chain like in the previous examples since Next.js is responsible for the execution, providing `request` and `event` parameters to the `middleware` functions.
export default Chain.functions(middlewares)
由 所制
|
|
|