Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: pr#2023 #181

Merged
merged 2 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/Errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ content type 不能是一个空字符串。

同 `$id` 的 schema 已经存在。

<a name="FST_ERR_SCH_NOT_PRESENT"></a>
#### FST_ERR_SCH_NOT_PRESENT
<a name="FST_ERR_SCH_VALIDATION_BUILD"></a>
#### FST_ERR_SCH_VALIDATION_BUILD

不存在 `$id` 为提供的值的 schema。
用于校验路由的 JSON schema 不合法

<a name="FST_ERR_SCH_BUILD"></a>
#### FST_ERR_SCH_BUILD
<a name="FST_ERR_SCH_SERIALIZATION_BUILD"></a>
#### FST_ERR_SCH_SERIALIZATION_BUILD

某个路由的 JSON schema 不合法。
用于序列化响应的 JSON schema 不合法。

<a name="FST_ERR_PROMISE_NOT_FULLFILLED"></a>
#### FST_ERR_PROMISE_NOT_FULLFILLED
Expand Down
11 changes: 6 additions & 5 deletions docs/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ fastify.route(options)
* `params`:校验 url 参数。
* `response`:过滤并生成用于响应的 schema,能帮助提升 10-20% 的吞吐量。
* `attachValidation`:当 schema 校验出错时,将一个 `validationError` 对象添加到请求中,否则错误将被发送给错误处理函数。
* `onRequest(request, reply, done)`: 每当接收到一个请求时触发的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#onrequest)。可以是一个函数数组。
* `preParsing(request, reply, done)`: 解析请求前调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#preparsing)。可以是一个函数数组。
* `onRequest(request, reply, done)`每当接收到一个请求时触发的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#onrequest)。可以是一个函数数组。
* `preParsing(request, reply, done)`解析请求前调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#preparsing)。可以是一个函数数组。
* `preValidation(request, reply, done)`:在共享的 `preValidation` 钩子之后执行的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#prevalidation),在路由层进行认证等场景中会有用处。可以是一个函数数组。
* `preHandler(request, reply, done)`:处理请求之前调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#prehandler)。可以是一个函数数组。
* `preSerialization(request, reply, payload, done)`:序列化之前调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#preserialization)。可以是一个函数数组。
* `onSend(request, reply, payload, done)`: 响应即将发送前调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#route-hooks)。可以是一个函数数组。
* `onResponse(request, reply, done)`: 当响应发送后调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#onresponse)。因此,在这个函数内部,不允许再向客户端发送数据。可以是一个函数数组。
* `onSend(request, reply, payload, done)`响应即将发送前调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#route-hooks)。可以是一个函数数组。
* `onResponse(request, reply, done)`当响应发送后调用的[函数](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md#onresponse)。因此,在这个函数内部,不允许再向客户端发送数据。可以是一个函数数组。
* `handler(request, reply)`:处理请求的函数。
* `schemaCompiler(schema)`:生成校验 schema 的函数。请看[这里](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-compiler)。
* `validatorCompiler(method, url, httpPart, schema)`:生成校验请求的 schema 的函数。详见[验证与序列化](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-validator)。
* `serializerCompiler(method, url, httpPart, schema)`:生成序列化响应的 schema 的函数。详见[验证与序列化](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-serializer)。
* `bodyLimit`:一个以字节为单位的整形数,默认值为 `1048576` (1 MiB),防止默认的 JSON 解析器解析超过此大小的请求主体。你也可以通过 `fastify(options)`,在首次创建 Fastify 实例时全局设置该值。
* `logLevel`:设置日志级别。详见下文。
* `logSerializers`:设置当前路由的日志序列化器。
Expand Down
39 changes: 27 additions & 12 deletions docs/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,16 @@ fastify.register(function (instance, opts, done) {

<a name="add-schema"></a>
#### addSchema
`fastify.addSchema(schemaObj)`,向 Fastify 实例添加可共用的 schema,用于验证数据。你可以通过该 schema 的 id 在应用的任意位置使用它。<br/>
请看[验证和序列化](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md)一文中的[范例](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#shared-schema)。
`fastify.addSchema(schemaObj)`,向 Fastify 实例添加 JSON schema。你可以通过 `$ref` 关键字在应用的任意位置使用它。<br/>
更多内容,请看[验证和序列化](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md)。

<a name="get-schemas"></a>
#### getSchemas
`fastify.getSchemas()`,返回一个对象,包含所有通过 `addSchema` 添加的 schema,对象的键是 JSON schema 的 `$id`。

<a name="get-schema"></a>
#### getSchema
`fastify.getSchema(id)`,返回通过 `addSchema` 添加的拥有匹配 `id` 的 schema,未找到则返回 `undefined`。

<a name="set-reply-serializer"></a>
#### setReplySerializer
Expand All @@ -619,24 +627,31 @@ fastify.setReplySerializer(function (payload, statusCode){
})
```

<a name="set-schema-compiler"></a>
#### setSchemaCompiler
为所有的路由设置 schema 编译器 (schema compiler),请看[这里](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-compiler)了解更多信息。
<a name="set-validator-compiler"></a>
#### setValidatorCompiler
为所有的路由设置 schema 校验编译器 (validator compiler)。详见 [#schema-validator](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-validator)。

<a name="set-serializer-resolver"></a>
#### setSerializerCompiler
为所有的路由设置 schema 序列化编译器 (serializer compiler)。详见 [#schema-serializer](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-serializer)。
**注:** [`setReplySerializer`](#set-reply-serializer) 有更高的优先级!

<a name="set-schema-resolver"></a>
#### setSchemaResolver
为所有的路由设置 schema `$ref` 解析器 (schema `$ref` resolver),请看[这里](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-resolver)了解更多信息。
<a name="validator-compiler"></a>
#### validatorCompiler
该属性用于获取 schema 校验器。未设置校验器时,在服务器启动前,该值是 `null`,之后是一个签名为 `function (method, url, httpPart, schema)` 的函数。该函数将 `schema` 参数编译为一个校验数据的函数,并返回生成的函数。
`schema` 参数能访问到所有通过 [`.addSchema`](#add-schema) 添加的共用 schema。

<a name="schema-compiler"></a>
#### schemaCompiler
`setSchemaCompiler` 方法的简写。用于设置 schema 编译器函数,也可用于返回全部路由的 schema 编译器。
<a name="serializer-compiler"></a>
#### serializerCompiler
该属性用于获取 schema 序列化器。未设置序列化器时,在服务器启动前,该值是 `null`,之后是一个签名为 `function (method, url, httpPart, schema)` 的函数。该函数将 `schema` 参数编译为一个校验数据的函数,并返回生成的函数。
`schema` 参数能访问到所有通过 [`.addSchema`](#add-schema) 添加的共用 schema。

<a name="set-not-found-handler"></a>
#### setNotFoundHandler

`fastify.setNotFoundHandler(handler(request, reply))`:为 404 状态 (not found) 设置处理函数 (handler)。向 `fastify.register()` 传递不同的 [`prefix` 选项](https://github.com/fastify/docs-chinese/blob/master/docs/Plugins.md#route-prefixing-option),就可以为不同的插件设置不同的处理函数。这些处理函数被视为常规的路由处理函数,因此它们的请求会经历一个完整的 [Fastify 生命周期](https://github.com/fastify/docs-chinese/blob/master/docs/Lifecycle.md#lifecycle)。

你也可以为 404 处理函数注册一个 [preValidation](https://www.fastify.io/docs/latest/Hooks/#route-hooks) 或 [preHandler](https://www.fastify.io/docs/latest/Hooks/#route-hooks) 钩子。
你也可以为 404 处理函数注册 [`preValidation`](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md/#route-hooks) 或 [`preHandler`](https://github.com/fastify/docs-chinese/blob/master/docs/Hooks.md/#route-hooks) 钩子。

```js
fastify.setNotFoundHandler({
Expand Down
Loading