Skip to content

Commit

Permalink
feat: add part of swagger doc generation
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Nov 25, 2018
1 parent f093c18 commit b684a1f
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![Downloads](https://img.shields.io/npm/dm/types-as-schema.svg)](https://www.npmjs.com/package/types-as-schema)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fplantain-00%2Ftypes-as-schema%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/types-as-schema)

Genetate json scheme, protobuf file, graphQL/mongoose(alpha) schema and reasonml(alpha)/ocaml(alpha)/rust(alpha) types from typescript types.
Genetate json scheme, protobuf file, graphQL/mongoose(alpha) schema, reasonml(alpha)/ocaml(alpha)/rust(alpha) types and swagger(alhpa) doc from typescript types.

## supported types features

Expand Down Expand Up @@ -48,6 +48,7 @@ parameters | description
`--ocaml` | generated ocaml types file
`--rust` | generated rust types file
`--mongoose` | generated mongoose schema file
`--swagger` | generated swagger json file
`--debug` | generated file with debug information in it
`--watch` or `-w` | watch mode

Expand Down
2 changes: 1 addition & 1 deletion clean-scripts.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},
revStaticCommand,
{
default: 'node ./dist/index.js demo/cases.ts demo/case2.ts --json demo/ --debug demo/debug.json --protobuf demo/cases.proto --graphql-root-type demo/root-type.ts --graphql demo/cases.gql --reason demo/cases.re --ocaml demo/cases.ml --rust demo/cases.rs --mongoose demo/cases.mongoose.ts',
default: 'node ./dist/index.js demo/cases.ts demo/case2.ts --json demo/ --debug demo/debug.json --protobuf demo/cases.proto --graphql-root-type demo/root-type.ts --graphql demo/cases.gql --reason demo/cases.re --ocaml demo/cases.ml --rust demo/cases.rs --mongoose demo/cases.mongoose.ts --swagger demo/cases.swagger.json',
logTool: 'node ./dist/index.js demo/log-tool/types.ts --json demo/log-tool/ --debug demo/log-tool/debug.json --protobuf demo/log-tool/protocol.proto --graphql demo/log-tool/protocol.gql',
matchCalculator: 'node ./dist/index.js demo/match-calculator/types.ts --json demo/match-calculator/ --debug demo/match-calculator/debug.json',
baogame: 'node ./dist/index.js demo/baogame/common.ts --protobuf demo/baogame/protocol.proto --graphql demo/baogame/protocol.gql --debug demo/baogame/debug.json'
Expand Down
7 changes: 7 additions & 0 deletions demo/cases.gql
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ type Result3 {
result3: String!
}

type Pet {
id: Float
name: String!
photoUrls: [String]!
status: String!
}

type OuterType {
outerType: Float!
}
7 changes: 7 additions & 0 deletions demo/cases.ml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ type result3 = {
result3: string;
}

type pet = {
id: float option;
name: string;
photoUrls: string list;
status: string;
}

type outerType = {
outerType: float;
}
19 changes: 19 additions & 0 deletions demo/cases.mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,25 @@ export const result3Schema = {
},
}

export const petSchema = {
id: {
type: Schema.Types.Number,
required: false
},
name: {
type: Schema.Types.String,
required: true
},
photoUrls: {
type: Schema.Types.Mixed,
required: true
},
status: {
type: Schema.Types.Mixed,
required: true
},
}

export const outerTypeSchema = {
outerType: {
type: Schema.Types.Number,
Expand Down
7 changes: 7 additions & 0 deletions demo/cases.proto
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ message Result3 {
string result3 = 1;
}

message Pet {
double id = 1;
string name = 2;
repeated string photoUrls = 3;
string status = 4;
}

message OuterType {
double outerType = 1;
}
8 changes: 8 additions & 0 deletions demo/cases.re
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ type result3 = {
result3: string,
};

type pet = {
.
id: option(float),
name: string,
photoUrls: list(string),
status: string,
};

type outerType = {
.
outerType: float,
Expand Down
8 changes: 8 additions & 0 deletions demo/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ pub struct Result3 {
#[serde(rename = "result3")] pub result_3: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Pet {
pub id: Option<f32>,
pub name: String,
#[serde(rename = "photoUrls")] pub photo_urls: Vec<String>,
pub status: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct OuterType {
#[serde(rename = "outerType")] pub outer_type: f32,
Expand Down
57 changes: 57 additions & 0 deletions demo/cases.swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"swagger": "2.0",
"paths": {
"/pet/{petId}": {
"get": {
"operationId": "getPetById",
"parameters": [
{
"name": "petId",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Pet"
}
}
}
}
}
},
"definitions": {
"Pet": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"photoUrls": {
"type": "array",
"items": {
"type": "string"
}
},
"status": {
"type": "string",
"enum": [
"available",
"pending",
"sold"
]
}
},
"required": [
"name",
"photoUrls",
"status"
],
"additionalProperties": false
}
}
}
13 changes: 13 additions & 0 deletions demo/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,16 @@ type Result2 = Result3
interface Result3 {
result3: string
}

interface Pet {
id?: number
name: string
photoUrls: string[]
status: 'available' | 'pending' | 'sold'
}

/**
* @method get
* @path "/pet/{id}"
*/
export declare function getPetById(petId: number): Promise<Pet>
74 changes: 74 additions & 0 deletions demo/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -3073,6 +3073,80 @@
"character": 0
}
},
{
"kind": "object",
"name": "Pet",
"members": [
{
"name": "id",
"type": {
"kind": "number",
"type": "number",
"position": {
"file": "demo/cases.ts",
"line": 402,
"character": 7
}
},
"optional": true
},
{
"name": "name",
"type": {
"kind": "string",
"position": {
"file": "demo/cases.ts",
"line": 403,
"character": 8
}
}
},
{
"name": "photoUrls",
"type": {
"kind": "array",
"type": {
"kind": "string",
"position": {
"file": "demo/cases.ts",
"line": 404,
"character": 13
}
},
"position": {
"file": "demo/cases.ts",
"line": 404,
"character": 13
}
}
},
{
"name": "status",
"type": {
"kind": "enum",
"type": "string",
"name": "string",
"enums": [
"available",
"pending",
"sold"
],
"position": {
"file": "demo/cases.ts",
"line": 405,
"character": 10
}
}
}
],
"minProperties": 3,
"maxProperties": 4,
"position": {
"file": "demo/cases.ts",
"line": 401,
"character": 0
}
},
{
"kind": "object",
"name": "OuterType",
Expand Down
13 changes: 13 additions & 0 deletions demo/root-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ export interface Result3<TContext = any> {
result3: string
}

export interface Pet<TContext = any> {
id?: number
name: string
photoUrls: Array<string>
status: string
}

export interface OuterType<TContext = any> {
outerType: number
}
Expand Down Expand Up @@ -532,6 +539,12 @@ export interface ApolloResolvers<TContext = any> {
Result3?: {
result3?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
},
Pet?: {
id?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
name?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
photoUrls?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
status?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
},
OuterType?: {
outerType?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
},
Expand Down
3 changes: 2 additions & 1 deletion online/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
.ocaml-types,
.rust-types,
.mongoose-schema,
.graphql-root-type {
.graphql-root-type,
.swagger-doc {
display: block;
padding: 9.5px;
margin: 3px 0 10px 3px;
Expand Down
1 change: 1 addition & 0 deletions online/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<pre class="rust-types" v-if="selectedOption === 'rust types'">{{rustTypes}}</pre>
<pre class="mongoose-schema" v-if="selectedOption === 'mongoose schema'">{{mongooseSchema}}</pre>
<pre class="graphql-root-type" v-if="selectedOption === 'graphql root type'">{{graphqlRootType}}</pre>
<pre class="swagger-doc" v-if="selectedOption === 'swagger doc'">{{swaggerDoc}}</pre>
</div>
</div>
4 changes: 4 additions & 0 deletions online/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class App extends Vue {
rustTypes = ''
mongooseSchema = ''
graphqlRootType = ''
swaggerDoc = ''
private innerSource = localStorage.getItem(localStorageKey) || demoCasesTs
private jsonSchemas: { entry: string; content: string }[] = []

Expand Down Expand Up @@ -75,6 +76,9 @@ export class App extends Vue {

this.graphqlRootType = generator.generateGraphqlRootType('.')
this.options.push('graphql root type')

this.swaggerDoc = generator.generateSwaggerDoc()
this.options.push('swagger doc')
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion online/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,22 @@ type Result2 = Result3
interface Result3 {
result3: string
}
interface Pet {
id?: number
name: string
photoUrls: string[]
status: 'available' | 'pending' | 'sold'
}
/**
* @method get
* @path "/pet/{id}"
*/
export declare function getPetById(petId: number): Promise<Pet>
`
// @ts-ignore
export function indexTemplateHtml(this: App) {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"app"},[_c('textarea',{directives:[{name:"model",rawName:"v-model",value:(_vm.source),expression:"source"}],staticClass:"source",domProps:{"value":(_vm.source)},on:{"input":function($event){if($event.target.composing){ return; }_vm.source=$event.target.value}}}),_vm._v(" "),_c('div',{staticClass:"result"},[_c('button',{on:{"click":function($event){_vm.generate()}}},[_vm._v("generate")]),_vm._v(" "),_c('div',{staticClass:"options"},[_c('select',{directives:[{name:"model",rawName:"v-model",value:(_vm.selectedOption),expression:"selectedOption"}],on:{"change":function($event){var $$selectedVal = Array.prototype.filter.call($event.target.options,function(o){return o.selected}).map(function(o){var val = "_value" in o ? o._value : o.value;return val}); _vm.selectedOption=$event.target.multiple ? $$selectedVal : $$selectedVal[0]}}},_vm._l((_vm.options),function(option){return _c('option',{key:option,domProps:{"value":option}},[_vm._v(_vm._s(option))])}))]),_vm._v(" "),(_vm.selectedOption === 'protobuf')?_c('pre',{staticClass:"protobuf"},[_vm._v(_vm._s(_vm.protobuf))]):_vm._e(),_vm._v(" "),(_vm.jsonSchema)?_c('pre',{staticClass:"json-schema"},[_vm._v(_vm._s(_vm.jsonSchema))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'graphql schema')?_c('pre',{staticClass:"graphql-schema"},[_vm._v(_vm._s(_vm.graphqlSchema))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'reason types')?_c('pre',{staticClass:"reason-types"},[_vm._v(_vm._s(_vm.reasonTypes))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'ocaml types')?_c('pre',{staticClass:"ocaml-types"},[_vm._v(_vm._s(_vm.ocamlTypes))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'rust types')?_c('pre',{staticClass:"rust-types"},[_vm._v(_vm._s(_vm.rustTypes))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'mongoose schema')?_c('pre',{staticClass:"mongoose-schema"},[_vm._v(_vm._s(_vm.mongooseSchema))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'graphql root type')?_c('pre',{staticClass:"graphql-root-type"},[_vm._v(_vm._s(_vm.graphqlRootType))]):_vm._e()])])}
export function indexTemplateHtml(this: App) {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"app"},[_c('textarea',{directives:[{name:"model",rawName:"v-model",value:(_vm.source),expression:"source"}],staticClass:"source",domProps:{"value":(_vm.source)},on:{"input":function($event){if($event.target.composing){ return; }_vm.source=$event.target.value}}}),_vm._v(" "),_c('div',{staticClass:"result"},[_c('button',{on:{"click":function($event){_vm.generate()}}},[_vm._v("generate")]),_vm._v(" "),_c('div',{staticClass:"options"},[_c('select',{directives:[{name:"model",rawName:"v-model",value:(_vm.selectedOption),expression:"selectedOption"}],on:{"change":function($event){var $$selectedVal = Array.prototype.filter.call($event.target.options,function(o){return o.selected}).map(function(o){var val = "_value" in o ? o._value : o.value;return val}); _vm.selectedOption=$event.target.multiple ? $$selectedVal : $$selectedVal[0]}}},_vm._l((_vm.options),function(option){return _c('option',{key:option,domProps:{"value":option}},[_vm._v(_vm._s(option))])}))]),_vm._v(" "),(_vm.selectedOption === 'protobuf')?_c('pre',{staticClass:"protobuf"},[_vm._v(_vm._s(_vm.protobuf))]):_vm._e(),_vm._v(" "),(_vm.jsonSchema)?_c('pre',{staticClass:"json-schema"},[_vm._v(_vm._s(_vm.jsonSchema))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'graphql schema')?_c('pre',{staticClass:"graphql-schema"},[_vm._v(_vm._s(_vm.graphqlSchema))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'reason types')?_c('pre',{staticClass:"reason-types"},[_vm._v(_vm._s(_vm.reasonTypes))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'ocaml types')?_c('pre',{staticClass:"ocaml-types"},[_vm._v(_vm._s(_vm.ocamlTypes))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'rust types')?_c('pre',{staticClass:"rust-types"},[_vm._v(_vm._s(_vm.rustTypes))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'mongoose schema')?_c('pre',{staticClass:"mongoose-schema"},[_vm._v(_vm._s(_vm.mongooseSchema))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'graphql root type')?_c('pre',{staticClass:"graphql-root-type"},[_vm._v(_vm._s(_vm.graphqlRootType))]):_vm._e(),_vm._v(" "),(_vm.selectedOption === 'swagger doc')?_c('pre',{staticClass:"swagger-doc"},[_vm._v(_vm._s(_vm.swaggerDoc))]):_vm._e()])])}
// @ts-ignore
export var indexTemplateHtmlStatic = [ ]
// tslint:enable
5 changes: 5 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { generateRustTypes } from './rust-type-generator'
import { generateMongooseSchema } from './mongoose-schema-generator'
import { TypeDeclaration } from './utils'
import { generateGraphqlRootType } from './graphql-root-type-generator'
import { generateSwaggerDoc } from './swagger-doc-generator'

export class Generator {
declarations: TypeDeclaration[] = []
Expand Down Expand Up @@ -49,6 +50,10 @@ export class Generator {
generateMongooseSchema() {
return generateMongooseSchema(this.declarations)
}

generateSwaggerDoc() {
return generateSwaggerDoc(this.declarations)
}
}

export { ArrayDefinition, ObjectDefinition, UndefinedDefinition }

0 comments on commit b684a1f

Please sign in to comment.