From bac1912b66c6f8b1e734afbc6c73f1b8f52d8d4e Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 5 Dec 2021 18:43:26 +0800 Subject: [PATCH] docs: update api doc --- docs/reference/application.md | 15 ++++-- docs/reference/context.md | 79 ++++++++++++++++++++++++++++++ docs/reference/filter-operators.md | 4 +- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/docs/reference/application.md b/docs/reference/application.md index cf6b2131d5..384e0b883a 100644 --- a/docs/reference/application.md +++ b/docs/reference/application.md @@ -19,9 +19,7 @@ class Application { ##### Examples ```ts -app.db.on('xxx', () => { - -}); +app.db.close(); ``` ## app.resourcer @@ -92,13 +90,22 @@ app.i18n.t('Hello'); 等同于 app.resourcer.registerActions() +```ts +app.actions({ + async test(ctx, next) { + ctx.body = 'hello world'; + await next(); + }, +}); +``` + ## app.resource() 待完善 等同于 app.resourcer.define() ## app.parse() -等同于 app.cli.parse() +等同于 app.cli.parseAsync() ## app.load() 待完善 diff --git a/docs/reference/context.md b/docs/reference/context.md index 4dfaae59a3..a170d63e0c 100644 --- a/docs/reference/context.md +++ b/docs/reference/context.md @@ -26,6 +26,85 @@ async (ctx, next) { ## ctx.action 待完善 +### action.params + +资源标识定位 + +- `associatedName` +- `associatedIndex` +- `resourceName` +- `resourceIndex` + +request body + +- `values` bodyparser 之后的 body + +request query + +- `filter` +- `fields` +- `pageSize` +- `page` +- `sort` +- 其他参数 + +### action.mergeParams(); + +```ts +interface mergeParams { + (params: ActionParams, strategies?: MergeStrategies): void; +} + +type MergeStrategyType = 'merge' | 'deepMerge' | 'overwrite' | 'andMerge' | 'orMerge' | 'intersect' | 'union'; +type MergeStrategyFunc = (x: any, y: any) => any; +type MergeStrategy = MergeStrategyType | MergeStrategyFunc; + +interface MergeStrategies { + [key: string]: MergeStrategy; +} +``` + +合并策略 + +- `merge` 浅合并 +- `deepMerge` 深层合并(默认) +- `andMerge` and 合并,用于 filter 参数 +- `orMerge` or 合并,用于 filter 参数 +- `overwrite` 覆盖 +- `intersect` 交集,用于 array 类型,也支持逗号间隔的字符串数组 +- `union` 并集,去重,用于 array 类型,也支持逗号间隔的字符串数组 + +特定参数的默认合并策略 + +```ts +{ + filter: 'andMerge', + fields: 'intersect', + appends: 'union', + except: 'union', + whitelist: 'intersect', + blacklist: 'union', + sort: 'overwrite', +} +``` + +示例 + +```ts +ctx.action.mergeParams({ + filter: { b: 'b1' }, + fields: 'a1,b1,c1', + key1: 'abcdef', +}, { + // filter 参数采用 orMerge 策略 + filter: 'orMerge', + // fields 参数采用 intersect 策略 + fields: 'intersect', + // key1 自定义函数 + key1: (x, y) => y.split(''), +}); +``` + ## ctx.i18n app.i18n 的 cloneInstance。详情见 [I18next API](https://www.i18next.com/overview/api) diff --git a/docs/reference/filter-operators.md b/docs/reference/filter-operators.md index 8918ec946a..9261b3f2aa 100644 --- a/docs/reference/filter-operators.md +++ b/docs/reference/filter-operators.md @@ -25,7 +25,7 @@ toc: menu - empty - notEmpty -## select +## enum - eq - ne @@ -34,7 +34,7 @@ toc: menu - empty - notEmpty -## multipleSelect +## array - match - notMatch