Skip to content

Commit

Permalink
feat: generate paths
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Nov 25, 2018
1 parent 51b4adc commit f0bdf1c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ boolean[]:
+ `@param {string} name`: set argument `name: String!`
+ `@param {string} [name]`: set argument `name: String`

## swagger doc only

+ `@method get`: set api method
+ `@path /pet/{id}`: set api url

## number type alias

```ts
Expand Down
5 changes: 3 additions & 2 deletions demo/cases.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"operationId": "getPetById",
"parameters": [
{
"name": "petId",
"name": "id",
"required": true,
"type": "integer"
"in": "query",
"type": "number"
}
],
"responses": {
Expand Down
61 changes: 39 additions & 22 deletions src/swagger-doc-generator.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { TypeDeclaration } from './utils'
import { TypeDeclaration, Type } from './utils'

export function generateSwaggerDoc(_typeDeclarations: TypeDeclaration[]) {
const result = {
swagger: '2.0',
paths: {
'/pet/{id}': {
get: {
operationId: 'getPetById',
parameters: [
{
name: 'petId',
required: true,
type: 'integer'
}
],
responses: {
200: {
schema: {
$ref: '#/definitions/Pet'
}
}
export function generateSwaggerDoc(typeDeclarations: TypeDeclaration[]) {
const paths: { [path: string]: { [method: string]: any } } = {}
for (const typeDeclaration of typeDeclarations) {
if (typeDeclaration.kind === 'function'
&& typeDeclaration.path
&& typeDeclaration.method) {
if (!paths[typeDeclaration.path]) {
paths[typeDeclaration.path] = {}
}
paths[typeDeclaration.path][typeDeclaration.method] = {
operationId: typeDeclaration.name,
parameters: typeDeclaration.parameters.map((parameter) => ({
name: parameter.name,
required: !parameter.optional,
in: 'query',
...getType(parameter.type)
})),
responses: {
200: {
...getType(typeDeclaration.type)
}
}
}
},
}
}
const result = {
swagger: '2.0',
paths,
definitions: {
Pet: {
type: 'object',
Expand Down Expand Up @@ -60,3 +64,16 @@ export function generateSwaggerDoc(_typeDeclarations: TypeDeclaration[]) {
}
return JSON.stringify(result, null, 2)
}

function getType(type: Type) {
if (type.kind === 'reference') {
return {
schema: {
$ref: `#/definitions/${type.name}`
}
}
}
return {
type: type.kind
}
}

0 comments on commit f0bdf1c

Please sign in to comment.