Skip to content

Commit 538e577

Browse files
author
svolkov
committed
feat: @summary, @description comments at each route; fix: @description (parse all lines); fix: parsing schema without routes; internal: add debug script
1 parent 4aef428 commit 538e577

21 files changed

+224
-59
lines changed

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch via npm",
9+
"type": "node",
10+
"request": "launch",
11+
"cwd": "${workspaceFolder}",
12+
"runtimeExecutable": "npm",
13+
"runtimeArgs": ["run-script", "generate:debug"],
14+
"port": 9229
15+
}
16+
]
17+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"cli": "node index.js -p ./tests/schemas/v3.0/personal-api-example.json -o ./tests/generated/v3.0/ -n personal-api-example.ts",
77
"generate": "node tests/generate.js",
8+
"generate:debug": "node --nolazy --inspect-brk=9229 tests/generate.js",
89
"validate": "node tests/validate.js"
910
},
1011
"author": "acacode",

src/routes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const getRouteName = (operationId, method, route, moduleName) => {
3131
}
3232

3333
const parseRoutes = (routes, parsedSchemas) =>
34-
Object.entries(routes)
34+
_.entries(routes)
3535
.reduce((routes, [route, requestInfoByMethodsMap]) => {
3636
parameters = _.get(requestInfoByMethodsMap, 'parameters');
3737

@@ -119,11 +119,11 @@ const parseRoutes = (routes, parsedSchemas) =>
119119
const comments = [
120120
tags && tags.length && `@tags ${tags.join(', ')}`,
121121
`@name ${routeName}`,
122-
(description || summary) && `@description ${_.replace(summary || description, /\n/g, '')}`,
122+
summary && `@summary ${summary}`,
123123
`@request ${_.upperCase(method)}:${route}`,
124124
// requestBody && requestBody.description && `@body ${requestBody.description}`,
125-
hasSecurity && `@security true`,
126-
// ...responsesInfos,
125+
hasSecurity && `@secure`,
126+
description && `@description ${_.replace(description, /\n/g, '. ')}`,
127127
].filter(Boolean);
128128

129129
return {

src/schema.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const _ = require("lodash");
22
const { inlineExtraFormatters } = require("./typeFormatters");
33

4+
const jsTypes = ['number', 'boolean', 'string', 'object'];
5+
const jsEmptyTypes = ['null', 'undefined'];
6+
47
const findSchemaType = schema => {
58
if (schema.enum) return 'enum';
69
if (schema.properties) return 'object';
@@ -46,7 +49,6 @@ const complexSchemaParsers = {
4649
},
4750
'allOf': (schema) => {
4851
// T1 & T2
49-
// const { description, }
5052
return _.map(schema.allOf, complexTypeGetter).join(' & ')
5153
},
5254
'anyOf': (schema) => {
@@ -55,16 +57,18 @@ const complexSchemaParsers = {
5557
return `${combined.join(' | ')}` + (combined.length > 1 ? ` | (${combined.join(' & ')})` : '');
5658
},
5759
// TODO
58-
// 'not': (schema) => {
59-
// // TODO
60-
// }
60+
'not': (schema) => {
61+
// TODO
62+
}
6163
}
6264

6365
const getComplexType = (schema) => {
6466
if (schema.oneOf) return 'oneOf';
6567
if (schema.allOf) return 'allOf';
6668
if (schema.anyOf) return 'anyOf';
67-
// if (schema.not) return 'not';
69+
if (schema.not) return 'not';
70+
71+
throw new Error("Uknown complex type")
6872
}
6973

7074
const schemaParsers = {
@@ -102,6 +106,7 @@ const schemaParsers = {
102106
},
103107
'complex': (schema, typeName) => {
104108
const complexType = getComplexType(schema);
109+
105110
return {
106111
type: 'type',
107112
typeIdentifier: 'type',

src/swagger.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ const getSwaggerObject = (pathToSwagger, urlToSwagger) =>
3636
rbname: "requestBodyName",
3737
}, function(err, options){
3838
const swaggerSchema = _.get(err, 'options.openapi', _.get(options, 'openapi'))
39-
40-
if (!swaggerSchema && err) throw new Error(err)
39+
if (!swaggerSchema && err) {
40+
throw new Error(err)
41+
}
4142
resolve(swaggerSchema)
4243
});
4344
} else {

tests/generated/v2.0/api-with-examples.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ export class Api<SecurityDataType> {
9393

9494
/**
9595
* @name listVersionsv2
96-
* @description List API versions
96+
* @summary List API versions
9797
* @request GET:/
98+
* @description multiple line 1. multiple line 2. multiple line 3.
9899
*/
99100
listVersionsv2 = (params?: RequestParams) =>
100101
this.request<any>(`/`, "GET", params, null)
@@ -104,7 +105,7 @@ export class Api<SecurityDataType> {
104105

105106
/**
106107
* @name getVersionDetailsv2
107-
* @description Show API version details
108+
* @summary Show API version details
108109
* @request GET:/v2
109110
*/
110111
getVersionDetailsv2: (params?: RequestParams) =>

tests/generated/v2.0/petstore-expanded.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,35 @@ export class Api<SecurityDataType> {
117117

118118
/**
119119
* @name findPets
120-
* @description Returns all pets from the system that the user has access toNam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
121120
* @request GET:/pets
121+
* @description Returns all pets from the system that the user has access to. Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.. . Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien..
122122
*/
123123
findPets: (query: { tags?: string[], limit?: number }, params?: RequestParams) =>
124124
this.request<Pet[]>(`/pets${this.addQueryParams(query)}`, "GET", params, null),
125125

126126

127127
/**
128128
* @name addPet
129-
* @description Creates a new pet in the store. Duplicates are allowed
130129
* @request POST:/pets
130+
* @description Creates a new pet in the store. Duplicates are allowed
131131
*/
132132
addPet: (pet: NewPet, params?: RequestParams) =>
133133
this.request<Pet>(`/pets`, "POST", params, pet),
134134

135135

136136
/**
137137
* @name find pet by id
138-
* @description Returns a user based on a single ID, if the user does not have access to the pet
139138
* @request GET:/pets/{id}
139+
* @description Returns a user based on a single ID, if the user does not have access to the pet
140140
*/
141141
findPetById: (id: number, params?: RequestParams) =>
142142
this.request<Pet>(`/pets/${id}`, "GET", params, null),
143143

144144

145145
/**
146146
* @name deletePet
147-
* @description deletes a single pet based on the ID supplied
148147
* @request DELETE:/pets/{id}
148+
* @description deletes a single pet based on the ID supplied
149149
*/
150150
deletePet: (id: number, params?: RequestParams) =>
151151
this.request<any>(`/pets/${id}`, "DELETE", params, null),

tests/generated/v2.0/petstore-minimal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export class Api<SecurityDataType> {
101101

102102
/**
103103
* @name get
104-
* @description Returns all pets from the system that the user has access to
105104
* @request GET:/pets
105+
* @description Returns all pets from the system that the user has access to
106106
*/
107107
get: (params?: RequestParams) =>
108108
this.request<Pet[]>(`/pets`, "GET", params, null),

tests/generated/v2.0/petstore-simple.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,35 @@ export class Api<SecurityDataType> {
117117

118118
/**
119119
* @name findPets
120-
* @description Returns all pets from the system that the user has access to
121120
* @request GET:/pets
121+
* @description Returns all pets from the system that the user has access to
122122
*/
123123
findPets: (query: { tags?: string[], limit?: number }, params?: RequestParams) =>
124124
this.request<Pet[]>(`/pets${this.addQueryParams(query)}`, "GET", params, null),
125125

126126

127127
/**
128128
* @name addPet
129-
* @description Creates a new pet in the store. Duplicates are allowed
130129
* @request POST:/pets
130+
* @description Creates a new pet in the store. Duplicates are allowed
131131
*/
132132
addPet: (pet: NewPet, params?: RequestParams) =>
133133
this.request<Pet>(`/pets`, "POST", params, pet),
134134

135135

136136
/**
137137
* @name findPetById
138-
* @description Returns a user based on a single ID, if the user does not have access to the pet
139138
* @request GET:/pets/{id}
139+
* @description Returns a user based on a single ID, if the user does not have access to the pet
140140
*/
141141
findPetById: (id: number, params?: RequestParams) =>
142142
this.request<Pet>(`/pets/${id}`, "GET", params, null),
143143

144144

145145
/**
146146
* @name deletePet
147-
* @description deletes a single pet based on the ID supplied
148147
* @request DELETE:/pets/{id}
148+
* @description deletes a single pet based on the ID supplied
149149
*/
150150
deletePet: (id: number, params?: RequestParams) =>
151151
this.request<any>(`/pets/${id}`, "DELETE", params, null),

tests/generated/v2.0/petstore-with-external-docs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,35 @@ export class Api<SecurityDataType> {
117117

118118
/**
119119
* @name findPets
120-
* @description Returns all pets from the system that the user has access to
121120
* @request GET:/pets
121+
* @description Returns all pets from the system that the user has access to
122122
*/
123123
findPets: (query: { tags?: string[], limit?: number }, params?: RequestParams) =>
124124
this.request<Pet[]>(`/pets${this.addQueryParams(query)}`, "GET", params, null),
125125

126126

127127
/**
128128
* @name addPet
129-
* @description Creates a new pet in the store. Duplicates are allowed
130129
* @request POST:/pets
130+
* @description Creates a new pet in the store. Duplicates are allowed
131131
*/
132132
addPet: (pet: NewPet, params?: RequestParams) =>
133133
this.request<Pet>(`/pets`, "POST", params, pet),
134134

135135

136136
/**
137137
* @name findPetById
138-
* @description Returns a user based on a single ID, if the user does not have access to the pet
139138
* @request GET:/pets/{id}
139+
* @description Returns a user based on a single ID, if the user does not have access to the pet
140140
*/
141141
findPetById: (id: number, params?: RequestParams) =>
142142
this.request<Pet>(`/pets/${id}`, "GET", params, null),
143143

144144

145145
/**
146146
* @name deletePet
147-
* @description deletes a single pet based on the ID supplied
148147
* @request DELETE:/pets/{id}
148+
* @description deletes a single pet based on the ID supplied
149149
*/
150150
deletePet: (id: number, params?: RequestParams) =>
151151
this.request<any>(`/pets/${id}`, "DELETE", params, null),

0 commit comments

Comments
 (0)