meta-api 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +2 -2
- data/README.md +32 -21
- data/config/locales/zh-CN.yml +11 -3
- data/docs//346/225/231/347/250/213.md +513 -171
- data/examples/rails_app/Gemfile.lock +1 -1
- data/lib/meta/application/execution.rb +27 -18
- data/lib/meta/application/metadata.rb +1 -1
- data/lib/meta/application/parameters.rb +17 -9
- data/lib/meta/application/route.rb +6 -8
- data/lib/meta/errors.rb +1 -1
- data/lib/meta/json_schema/builders/schema_builder_tool.rb +1 -1
- data/lib/meta/json_schema/schemas/base_schema.rb +2 -0
- data/lib/meta/json_schema/support/type_converter.rb +13 -11
- data/lib/meta/route_dsl/application_builder.rb +18 -10
- data/lib/meta/route_dsl/around_action_builder.rb +30 -3
- data/lib/meta/route_dsl/route_builder.rb +10 -4
- data/meta-api.gemspec +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4385e82bdaec80e15adc4b62b1259f30125720452e579d84a45c79471c2d9cb
|
4
|
+
data.tar.gz: f3b9baa683d4a757eabbcc9c13e86f133741f941f75c92bf8e28999d17f29653
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29e7fa33cae933c185cad5ea6bc5e0a9e460f549b5ea8bd12a6217b93e473d61a9cd5eaba2f803c28a5a55710a3668db80c42ad656b526cfb8604e50cdb42721
|
7
|
+
data.tar.gz: c7774b6feb64e9646017a7034082ab81b9fcf9a3928450edd725192671126a472941da314f64eb5c798a253eec6c82a99ff0701db6eacbda31cff42b6a605de0
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# 更新日志
|
2
2
|
|
3
|
+
## 0.0.6(2023 年 5 月 26 日)
|
4
|
+
|
5
|
+
1. 添加了 Meta::Execution#abort_execution! 方法。
|
6
|
+
2. 重新规范响应体的 application/json 设定,尽可能不过分设定。
|
7
|
+
3. 修复了若干实现上和文档的 bug.
|
8
|
+
|
9
|
+
## 0.0.5(2023 年 4 月 27 日)
|
10
|
+
|
11
|
+
1. 调整了 `around` 钩子的执行顺序,它与 `before` 钩子共同定义顺序。
|
12
|
+
2. 修复了若干 bug.
|
13
|
+
|
3
14
|
## 0.0.4(2023 年 4 月 18 日)
|
4
15
|
|
5
16
|
1. `Application` 添加 `.around` 宏。
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Meta 框架
|
2
2
|
|
3
|
-
Meta
|
3
|
+
Meta 框架是一个用于开发 Web API 的后端框架,采用 Ruby 语言编写。正如它的名字,它是用定义“元”信息的方式实现 API,同时生成一份符合 Open API 规格的接口文档。
|
4
4
|
|
5
5
|
> 有关框架名称的由来,阅读[框架的名称由来](docs/名称由来.md)。
|
6
6
|
|
7
7
|
## 脚手架
|
8
8
|
|
9
|
-
|
9
|
+
可直接使用我的[脚手架](https://github.com/yetrun/web-frame-example)项目上手体验:
|
10
10
|
|
11
11
|
```bash
|
12
12
|
$ git clone https://github.com/yetrun/web-frame-example.git
|
@@ -17,7 +17,7 @@ $ git clone https://github.com/yetrun/web-frame-example.git
|
|
17
17
|
在 Gemfile 中添加:
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
gem 'meta-api', '~> 0.0.
|
20
|
+
gem 'meta-api', '~> 0.0.5' # Meta 框架处于快速开发阶段,引入时应尽量固定版本
|
21
21
|
```
|
22
22
|
|
23
23
|
然后在 Ruby 代码中引用:
|
@@ -26,9 +26,9 @@ gem 'meta-api', '~> 0.0.3' # Meta 框架处于快速开发阶段,引入时应
|
|
26
26
|
require 'meta/api'
|
27
27
|
```
|
28
28
|
|
29
|
-
> 或者可嵌入到 Rails
|
29
|
+
> 或者可嵌入到 Rails 项目中使用,参考[为 Rails 项目带来参数验证效果](docs/Rails.md)。
|
30
30
|
|
31
|
-
##
|
31
|
+
## 快速一览
|
32
32
|
|
33
33
|
### 定义 API
|
34
34
|
|
@@ -52,12 +52,12 @@ class NotesAPI < Meta::Application
|
|
52
52
|
param :note, type: 'object', ref: NoteEntity
|
53
53
|
end
|
54
54
|
status 201 do
|
55
|
-
expose :note, type: 'object', ref: NoteEntity
|
55
|
+
expose :note, type: 'object', ref: NoteEntity.lock_scope('full')
|
56
56
|
end
|
57
57
|
action do
|
58
58
|
note = Note.create!(params[:note])
|
59
59
|
response.status = 201
|
60
|
-
render :note, note
|
60
|
+
render :note, note
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -67,7 +67,7 @@ class NotesAPI < Meta::Application
|
|
67
67
|
param :id, type: 'integer'
|
68
68
|
end
|
69
69
|
status 200 do
|
70
|
-
expose :note, type: 'object', ref: NoteEntity
|
70
|
+
expose :note, type: 'object', ref: NoteEntity.lock_scope('full')
|
71
71
|
end
|
72
72
|
action do
|
73
73
|
note = Note.find(params[:id])
|
@@ -81,7 +81,7 @@ class NotesAPI < Meta::Application
|
|
81
81
|
param :note, type: 'object', ref: NoteEntity
|
82
82
|
end
|
83
83
|
status 200 do
|
84
|
-
expose :note, type: 'object', ref: NoteEntity
|
84
|
+
expose :note, type: 'object', ref: NoteEntity.lock_scope('full')
|
85
85
|
end
|
86
86
|
action do
|
87
87
|
note = Note.find(params[:id])
|
@@ -92,6 +92,7 @@ class NotesAPI < Meta::Application
|
|
92
92
|
|
93
93
|
delete '/notes/:id' do
|
94
94
|
title '删除笔记'
|
95
|
+
status 204
|
95
96
|
action do
|
96
97
|
note = Note.find(params[:id])
|
97
98
|
note.destroy!
|
@@ -107,18 +108,12 @@ end
|
|
107
108
|
|
108
109
|
```ruby
|
109
110
|
class NoteEntity < Meta::Entity
|
110
|
-
property :id, type: 'integer', param: false
|
111
|
+
property :id, type: 'integer', param: false # 不作为参数传递
|
111
112
|
property :title, type: 'string'
|
112
|
-
property :content, type: 'string', render: { scope: 'full' }
|
113
|
+
property :content, type: 'string', render: { scope: 'full' } # 列表页接口不返回此字段
|
113
114
|
end
|
114
115
|
```
|
115
116
|
|
116
|
-
我们发现了一些特殊的定义:
|
117
|
-
|
118
|
-
- 标记 `id` 的 `param` 选项为 `false`,它不作为参数传递。
|
119
|
-
|
120
|
-
- 标记 `content` 在 `render` 下的 `scope`,当且仅当显示传递 `scope` 为 `false` 时才会渲染此字段。(对比 *查看笔记列表* 和 *查看笔记* 接口)
|
121
|
-
|
122
117
|
### 生成 API 文档
|
123
118
|
|
124
119
|
通过主动调用以下的方法可以生成 Open API 的规格文档:
|
@@ -127,20 +122,32 @@ end
|
|
127
122
|
NotesAPI.to_swagger_doc
|
128
123
|
```
|
129
124
|
|
130
|
-
该 Open API 文档是 JSON 格式,可以在 Swagger UI
|
125
|
+
该 Open API 文档是 JSON 格式,可以在 Swagger UI 下预览效果。如果也不乐意自己搭建 Swagger UI,可以直接使用在线的:
|
131
126
|
|
132
127
|
> http://openapi.yet.run/playground
|
133
128
|
|
134
129
|
### 将模块挂载在 Rack 下运行
|
135
130
|
|
136
|
-
API 模块同时也是一个 Rack 中间件,它可以挂载在 Rack
|
131
|
+
API 模块同时也是一个 Rack 中间件,它可以挂载在 Rack 下运行。假设以上文件分别位于 `notes_api.rb` 和 `note_entity.rb`,在项目下新建文件 `config.ru`,并写入:
|
137
132
|
|
138
133
|
```ruby
|
139
|
-
|
134
|
+
require 'meta/api'
|
135
|
+
require_relative 'notes_api'
|
136
|
+
require_relative 'note_entity'
|
137
|
+
|
138
|
+
# 将文档挂载到 /api_spec 锚点下
|
139
|
+
map '/api_spec' do
|
140
|
+
run ->(env) {
|
141
|
+
[200, { 'CONTENT_TYPE' => 'application/json' }, [JSON.generate(NotesAPI.to_swagger_doc)]]
|
142
|
+
}
|
143
|
+
end
|
140
144
|
|
145
|
+
# 启动 NotesAPI 中定义的接口
|
141
146
|
run NotesAPI
|
142
147
|
```
|
143
148
|
|
149
|
+
然后执行命令:`bundle exec rackup`,接口即可启动。
|
150
|
+
|
144
151
|
## 文档
|
145
152
|
|
146
153
|
- [教程](docs/教程.md)
|
@@ -148,7 +155,11 @@ run NotesAPI
|
|
148
155
|
|
149
156
|
## 支持
|
150
157
|
|
151
|
-
|
158
|
+
你可以通过以下途径获得支持:
|
159
|
+
|
160
|
+
1. 通过 GitHub 提交 [ISSUE](https://github.com/yetrun/web-frame/issues)
|
161
|
+
2. 通过 QQ 群(489579810)获得实时答疑
|
162
|
+
3. 对本项目提交 [Pull Request](https://github.com/yetrun/web-frame/pulls)
|
152
163
|
|
153
164
|
## License
|
154
165
|
|
data/config/locales/zh-CN.yml
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
zh-CN:
|
2
2
|
json_schema:
|
3
|
+
type_names:
|
4
|
+
basic: "基本类型"
|
5
|
+
array: "数组类型"
|
3
6
|
errors:
|
4
|
-
required:
|
5
|
-
format:
|
6
|
-
allowable:
|
7
|
+
required: "未提供"
|
8
|
+
format: "格式不正确"
|
9
|
+
allowable: "不在允许的值范围内"
|
10
|
+
type_convert:
|
11
|
+
basic: "类型转化失败,期望得到一个 `%{target_type}` 类型,但值 `%{value}` 无法转化"
|
12
|
+
array: "转化为数组类型时期望对象拥有 `to_a` 方法"
|
13
|
+
object: "类型转化失败,期望得到一个 `object` 类型,但值 `%{value}` 是一个%{real_type}"
|
14
|
+
unknown: "未知的目标类型 `%{target_type}`"
|