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

build(deps): bump akka-actor_2.12 from 2.5.9 to 2.5.16 in /concurrency/akka/akka-spring-boot #8

Closed
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
2166c9b
(1)修正参数的设置顺序
wx-chevalier Jul 9, 2017
9686054
添加实体类自动生成功能
wx-chevalier Jul 11, 2017
1640b76
Update README.md
wx-chevalier Jul 11, 2017
29ac5bc
Update README.md
wx-chevalier Jul 11, 2017
faa4ac7
(1)变更 lib 路径以方便进行编译
wx-chevalier Jul 11, 2017
0aac8ed
Merge branch 'master' of https://github.com/wxyyxc1992/swagger-decorator
wx-chevalier Jul 11, 2017
d1b9934
(1)将 declarative-validator 独立出来,成为单独的项目
wx-chevalier Jul 16, 2017
9073bed
(1)初始化创建 observer-x 库
wx-chevalier Jul 17, 2017
1a1dc69
修正 README.md
wx-chevalier Jul 18, 2017
4770a9e
(1)重命名本项目
wx-chevalier Jul 18, 2017
305ae9a
Update README.md
wx-chevalier Jul 18, 2017
cc0a113
Update README.en.md
wx-chevalier Jul 18, 2017
9189b65
(1)使用 Rollup 作为 observer-x 打包工具
wx-chevalier Jul 18, 2017
52fe388
Merge branch 'master' of https://github.com/wxyyxc1992/Modern-JavaScr…
wx-chevalier Jul 18, 2017
1ecaeb5
Update README.md
wx-chevalier Jul 19, 2017
e1f1bc4
Update README.md
wx-chevalier Jul 20, 2017
85e0f23
合并 Swagger-Decorator 的 PR
wx-chevalier Jul 23, 2017
3eda318
发布并且修正 observer-x 示例
wx-chevalier Jul 23, 2017
f1a8b41
refactor(src): 重命名 swagger-decorator 为 decorator-x
wx-chevalier Jul 25, 2017
dc41676
fet(demo): 为测试用例添加 GraphQL 支持
wx-chevalier Jul 25, 2017
d015ac2
refactor(fluent-fetcher): 将 fluent-fetcher 移动到现代 JavaScript 瑞士军刀中
wx-chevalier Jul 25, 2017
239e82b
refactor(fluent-fetcher): 升级依赖版本
wx-chevalier Aug 8, 2017
5e5924d
refactor(decorator-x):重构 Flow 支持
wx-chevalier Aug 8, 2017
4909587
refactor(observer-x):优化 Flow 支持
wx-chevalier Aug 8, 2017
18b65f9
refactor(*): 重构部分类型支持
wx-chevalier Aug 18, 2017
4b640cc
feature(*): Fix
wx-chevalier Sep 3, 2017
cf50ccf
重新命名文件内容
wx-chevalier Dec 17, 2017
7267bd5
Create Usage.md
wx-chevalier Jan 8, 2018
de41963
Update README.md
wx-chevalier Jan 8, 2018
07b9dc3
移入项目
wx-chevalier Feb 3, 2018
fbd3e91
feat: 更新 README
wx-chevalier Apr 28, 2018
86e2e1d
feat: update
wx-chevalier Apr 28, 2018
90c8dda
feat: update
wx-chevalier Apr 28, 2018
a75f8a1
feat: update
wx-chevalier May 14, 2018
a8cc844
refactor: 重新定位项目
wx-chevalier Nov 5, 2018
f4de99e
Create README.md
Nov 27, 2018
64dc741
feat: add glo
wx-chevalier Dec 26, 2018
c5844ec
Create README.md
Apr 23, 2019
3f6c2b4
Update README.md
Apr 23, 2019
fcdc8c5
build(deps): bump stringstream
dependabot[bot] Jun 21, 2019
4aae569
build(deps): bump stringstream from 0.0.5 to 0.0.6 in /js/x-fetch
dependabot[bot] Jun 21, 2019
d8dd492
Update README.md
Jun 24, 2019
f3c5ffd
Merge pull request #3 from wx-chevalier/dependabot/npm_and_yarn/js/x-…
Jun 24, 2019
1521ce7
Merge pull request #2 from wx-chevalier/dependabot/npm_and_yarn/js/ty…
Jun 24, 2019
1a22499
chore: housekeeping
wx-chevalier Aug 10, 2019
9bc5df0
chore: housekeeping
wx-chevalier Aug 10, 2019
14ca118
chore: housekeeping
wx-chevalier Aug 10, 2019
dd41345
chore: housekeeping
wx-chevalier Aug 15, 2019
fcb2818
Update README.md
Aug 19, 2019
ac498e4
chore: housekeeping
wx-chevalier Aug 29, 2019
6cd399f
chore: housekeeping
wx-chevalier Oct 29, 2019
35be94d
chore: housekeeping
wx-chevalier Dec 16, 2019
58f3bad
chore: housekeeping
wx-chevalier Dec 17, 2019
e81408f
chore: housekeeping
wx-chevalier Dec 19, 2019
b07f6a0
chore: housekeeping
wx-chevalier Feb 4, 2020
ae4a53d
build(deps): bump akka-actor_2.12 in /concurrency/akka/akka-spring-boot
dependabot[bot] Feb 4, 2020
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
Prev Previous commit
Next Next commit
修正 README.md
  • Loading branch information
wx-chevalier committed Jul 18, 2017
commit 1a1dc69ec1c50f37cb3b80319355958bb435eb2d
105 changes: 99 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

![](https://coding.net/u/hoteam/p/Cache/git/raw/master/2017/6/1/noun_28890_cc.png)
[![npm version](https://badge.fury.io/js/swagger-decorator.svg)](https://badge.fury.io/js/swagger-decorator)

中文版本 | [English Version](https://github.com/wxyyxc1992/swagger-decorator/blob/master/README.en.md)

> [基于 swagger-decorator 的自动实体类构建与 Swagger 接口文档生成](),对于不反感使用注解的项目中利用 swagger-decorator 添加合适的实体类或者接口类注解,从而实现支持嵌套地实体类校验与生成、Sequelize 等 ORM 模型生成、基于 Swagger 的接口文档生成等等功能。

> [基于 swagger-decorator 的自动实体类构建与 Swagger 接口文档生成](https://zhuanlan.zhihu.com/p/27941329)是笔者对于开源项目[ swagger-decorator ](https://github.com/wxyyxc1992/swagger-decorator)的描述,对于不反感使用注解的项目中利用 swagger-decorator 添加合适的实体类或者接口类注解,从而实现支持嵌套地实体类校验与生成、Sequelize 等 ORM 模型生成、基于 Swagger 的接口文档生成等等功能。如果有对 JavaScript 语法使用尚存不明的可以参考[ JavaScript 学习与实践资料索引](https://parg.co/bMI)或者[现代 JavaScript 开发:语法基础与实践技巧](https://parg.co/b1c)系列文章。

# swagger-decorator: 一处注解,多处使用

Expand Down Expand Up @@ -33,6 +37,8 @@ import {

# 实体类注解

swagger-decorator 的核心 API 即是对于实体类的注解,该注解不会改变实体类的任何属性表现,只是会将注解限定的属性特性记录在内置的 innerEntityObject 单例中以供后用。属性注解 entityProperty 的方法说明如下:

```javascript
/**
* Description 创建某个属性的描述
Expand All @@ -59,6 +65,8 @@ export function entityProperty({
}) {}
```

简单的用户实体类注解如下,这里的数据类型 type 支持 Swagger 默认的字符格式的类型描述,也支持直接使用 JavaScript 类名或者 JavaScript 数组。

```javascript
// @flow

Expand Down Expand Up @@ -114,7 +122,7 @@ export default class UserProperty {

```

数据类型支持
Swagger 内置数据类型定义

| Common Name | [`type`](http://swagger.io/specification/#dataTypeType) | [`format`](http://swagger.io/specification/#dataTypeFormat) | Comments |
| ----------- | ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
Expand All @@ -132,6 +140,9 @@ export default class UserProperty {


## 实例生成与校验

实体类定义完毕之后,我们首先可以使用 instantiate 函数为实体类生成实例;不同于直接使用 new 关键字创建,instantiate 能够根据指定属性的数据类型或者格式进行校验,同时能够迭代生成嵌套地子对象。

```javascript
/**
* Description 从实体类中生成对象,并且进行数据校验;注意,这里会进行递归生成,即对实体类对象同样进行生成
Expand All @@ -150,6 +161,8 @@ export function instantiate(
): Object {}
```

这里为了方便描述使用 Jest 测试用例说明不同的使用场景:

```javascript
describe("测试实体类实例化函数", () => {
test("测试 User 类实例化校验", () => {
Expand Down Expand Up @@ -229,6 +242,8 @@ describe("测试嵌套实例生成", () => {

## Sequelize 模型生成

[Sequelize](http://docs.sequelizejs.com/) 是 Node.js 应用中常用的 ORM 框架,swagger-decorator 提供了 generateSequelizeModel 函数以方便从实体类中利用现有的信息生成 Sequelize 对象模型;generateSequelizeModel 的第一个参数输入实体类,第二个参数输入需要覆写的模型属性,第三个参数设置额外属性,譬如是否需要将驼峰命名转化为下划线命名等等。

```javascript
const originUserSequelizeModel = generateSequelizeModel(
User,
Expand Down Expand Up @@ -259,14 +274,91 @@ UserSequelizeModel.findAll({
});
```

## 实体类转化与生成工具
## 从 Flow 类型声明中自动生成注解

笔者习惯使用 Flow 作为 JavaScript 的静态类型检测工具,因此笔者添加了 flowToDecorator 函数以自动地从 Flow 声明的类文件中提取出类型信息;内部原理参考[现代 JavaScript 开发:语法基础与实践技巧](https://parg.co/b1c) 一书中的[ JavaScript 语法树与代码转化](https://parg.co/baS)章节。该函数的使用方式为:
```
// @flow

import { flowToDecorator } from '../../../../src/transform/entity/flow/flow';

test('测试从 Flow 中提取出数据类型并且转化为 Swagger 接口类', () => {
flowToDecorator('./TestEntity.js', './TestEntity.transformed.js').then(
codeStr => {
console.log(codeStr);
},
err => {
console.error(err);
}
);
});

```
这里对应的 TestEntity 为:
```
// @flow

import AnotherEntity from "./AnotherEntity";

class Entity {
// Comment
stringProperty: string = 0;

classProperty: Entity = null;

rawProperty;

@entityProperty({
type: "string",
description: "this is property description",
required: true
})
decoratedProperty;
}

```
转化后的实体类为:
```
// @flow

import { entityProperty } from 'swagger-decorator';

import AnotherEntity from './AnotherEntity';

class Entity {
// Comment
@entityProperty({
type: 'string',
required: false,
description: 'Comment'
})
stringProperty: string = 0;

@entityProperty({
type: Entity,
required: false
})
classProperty: Entity = null;

### 从 Flow 类型声明中自动生成注解
@entityProperty({
type: 'string',
required: false
})
rawProperty;

@entityProperty({
type: 'string',
description: 'this is property description',
required: true
})
decoratedProperty;
}

```

# 接口注解与 Swagger 文档生成

对于 Swagger 文档规范可以参考[ OpenAPI Specification ](http://swagger.io/specification/),而对于 swagger-decorator 的实际使用可以参考本项目的[使用示例]()或者[ 基于 Koa2 的 Node.js 应用模板 ](https://parg.co/bvx)。
对于 Swagger 文档规范可以参考[ OpenAPI Specification ](http://swagger.io/specification/),而对于 swagger-decorator 的实际使用可以参考本项目的[使用示例](https://parg.co/bac)或者[ 基于 Koa2 的 Node.js 应用模板 ](https://parg.co/bvx)。


## 封装路由对象
Expand Down Expand Up @@ -320,6 +412,7 @@ export default class UserController extends UserControllerDoc {
}
}
```
在 UserController 中是负责具体的业务实现,为了避免过多的注解文档对于代码可读性的干扰,笔者建议是将除了路径与描述之外的信息放置到父类中声明;swagger-decorator 会自动从某个接口类的直接父类中提取出同名方法的描述文档。

```javascript

Expand Down Expand Up @@ -357,7 +450,7 @@ export default class UserControllerDoc {

```

## 应用运行
## 运行应用

- run your application and open swagger docs (PS. swagger-decorator contains Swagger UI):
```text
Expand Down