Skip to content

Commit d4a469d

Browse files
author
Денис
committed
Enhance OpenAPI documentation and examples; update server URL and test script
1 parent 68368fe commit d4a469d

File tree

6 files changed

+151
-23
lines changed

6 files changed

+151
-23
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
.idea/
2+
.idea/
3+
out/

README.md

Lines changed: 135 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
# openapi-jsonrpc-jsdoc
22

33
## Install
4+
45
```fish
56
npm i openapi-jsonrpc-jsdoc --save-dev
67
```
78

8-
## Example
9+
## Examples
910

1011
### Create JSON-RPC Method
12+
1113
```js
1214
// api/api-v1.js
1315
/**
14-
* @description Название API
15-
* @param {object} parameters - params
16-
* @param {string} parameters.id - id
17-
*/
16+
* @description Название API
17+
* @param {object} parameters - params
18+
* @param {string} parameters.id - id
19+
* @example
20+
* {
21+
* "@context": "https://www.w3.org/ns/activitystreams",
22+
* "type": "Note"
23+
* }
24+
*/
1825
module.exports = (parameters) => {
1926
return parameters.id;
2027
}
2128
```
2229

2330
### Run package
31+
2432
```js
2533
// index.js
2634
const fs = require('fs');
@@ -40,7 +48,7 @@ openapiJSONRpcJSDoc({
4048
```
4149

4250
<details>
43-
<summary>See result</summary>
51+
<summary>See Swagger result</summary>
4452

4553
```json
4654
{
@@ -57,29 +65,46 @@ openapiJSONRpcJSDoc({
5765
"javascript"
5866
],
5967
"info": {
60-
"version": "1.0.0",
61-
"title": "project-name",
62-
"description": "project-description"
68+
"version": "1.0.4",
69+
"title": "openapi-jsonrpc-jsdoc",
70+
"description": "OpenAPI generator"
6371
},
6472
"servers": [
6573
{
66-
"url": "0.0.0.0:8080"
74+
"url": "http://0.0.0.0:9000"
6775
}
6876
],
6977
"paths": {
70-
"/api/api-v1": {
78+
"/api/v1": {
7179
"post": {
72-
"operationId": "api-v1.js",
80+
"operationId": "v1.js",
7381
"deprecated": false,
74-
"summary": "/api-v1",
82+
"summary": "/v1",
7583
"description": "Название API",
7684
"tags": [
7785
"JSONRPC"
7886
],
7987
"parameters": [],
8088
"responses": {
8189
"200": {
82-
"description": "OK"
90+
"description": "OK",
91+
"content": {
92+
"application/json": {
93+
"schema": {
94+
"type": "object"
95+
}
96+
}
97+
}
98+
},
99+
"default": {
100+
"description": "unexpected error",
101+
"content": {
102+
"application/json": {
103+
"schema": {
104+
"$ref": "#/components/schemas/Error"
105+
}
106+
}
107+
}
83108
}
84109
},
85110
"requestBody": {
@@ -96,8 +121,8 @@ openapiJSONRpcJSDoc({
96121
"properties": {
97122
"method": {
98123
"type": "string",
99-
"default": "api-v1",
100-
"description": "API method api-v1"
124+
"default": "v1",
125+
"description": "API method v1"
101126
},
102127
"id": {
103128
"type": "integer",
@@ -113,7 +138,14 @@ openapiJSONRpcJSDoc({
113138
"params": {
114139
"title": "Parameters",
115140
"type": "object",
141+
"default": {
142+
"@context": "https://www.w3.org/ns/activitystreams",
143+
"type": "Note"
144+
},
116145
"required": [
146+
"method",
147+
"id",
148+
"jsonrpc",
117149
"id"
118150
],
119151
"properties": {
@@ -129,6 +161,72 @@ openapiJSONRpcJSDoc({
129161
}
130162
}
131163
}
164+
},
165+
"/api/v2": {
166+
"post": {
167+
"operationId": "v2.js",
168+
"deprecated": true,
169+
"summary": "/v2",
170+
"description": "Название API 2",
171+
"tags": [
172+
"JSONRPC"
173+
],
174+
"parameters": [],
175+
"responses": {
176+
"200": {
177+
"description": "OK",
178+
"content": {
179+
"application/json": {
180+
"schema": {
181+
"type": "object"
182+
}
183+
}
184+
}
185+
},
186+
"default": {
187+
"description": "unexpected error",
188+
"content": {
189+
"application/json": {
190+
"schema": {
191+
"$ref": "#/components/schemas/Error"
192+
}
193+
}
194+
}
195+
}
196+
},
197+
"requestBody": {
198+
"content": {
199+
"application/json": {
200+
"schema": {
201+
"type": "object",
202+
"required": [
203+
"method",
204+
"id",
205+
"jsonrpc"
206+
],
207+
"properties": {
208+
"method": {
209+
"type": "string",
210+
"default": "v2",
211+
"description": "API method v2"
212+
},
213+
"id": {
214+
"type": "integer",
215+
"default": 1,
216+
"format": "int32",
217+
"description": "Request ID"
218+
},
219+
"jsonrpc": {
220+
"type": "string",
221+
"default": "2.0",
222+
"description": "JSON-RPC Version (2.0)"
223+
}
224+
}
225+
}
226+
}
227+
}
228+
}
229+
}
132230
}
133231
},
134232
"components": {
@@ -137,6 +235,27 @@ openapiJSONRpcJSDoc({
137235
"type": "http",
138236
"scheme": "digest"
139237
}
238+
},
239+
"schemas": {
240+
"Error": {
241+
"required": [
242+
"error",
243+
"id",
244+
"jsonrpc"
245+
],
246+
"properties": {
247+
"id": {
248+
"type": "integer",
249+
"format": "int32"
250+
},
251+
"error": {
252+
"type": "object"
253+
},
254+
"jsonrpc": {
255+
"type": "string"
256+
}
257+
}
258+
}
140259
}
141260
},
142261
"security": [

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ async function openapiJsonrpcJsdoc({ files, securitySchemes = {}, packageUrl, se
55
files,
66
package: packageUrl,
77
access: 'public',
8+
encoding: 'utf8',
89
module: true,
910
undocumented: false,
1011
sort: 'scope',
12+
allowUnknownTags: true,
13+
dictionaries: ['jsdoc'],
1114
hierarchy: true,
1215
});
1316
const temporaryDocument = {
@@ -143,7 +146,6 @@ async function openapiJsonrpcJsdoc({ files, securitySchemes = {}, packageUrl, se
143146
...accumulator.properties,
144147
[name]: {
145148
type,
146-
// default: ...,// fixme: настроить выдачу default значения по JSON-DOC
147149
description,
148150
},
149151
};
@@ -152,6 +154,7 @@ async function openapiJsonrpcJsdoc({ files, securitySchemes = {}, packageUrl, se
152154
{
153155
title: 'Parameters',
154156
type: 'object',
157+
'default': module.examples.length ? JSON.parse(module.examples[0]) : null,
155158
required: ['method', 'id', 'jsonrpc'],
156159
properties: {},
157160
},
@@ -168,4 +171,4 @@ async function openapiJsonrpcJsdoc({ files, securitySchemes = {}, packageUrl, se
168171
return temporaryDocument;
169172
}
170173

171-
module.exports = openapiJsonrpcJsdoc;
174+
module.exports = openapiJsonrpcJsdoc;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "OpenAPI generator",
55
"main": "index.js",
66
"scripts": {
7-
"test": "ava test/*.test.js"
7+
"test": "NODE_NO_WARNINGS=1 ava test/*.test.js"
88
},
99
"repository": {
1010
"type": "git",

test/api/v1.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
* @description Название API
33
* @param {object} parameters - params
44
* @param {string} parameters.id - id
5+
* @example
6+
* {
7+
* "@context": "https://www.w3.org/ns/activitystreams",
8+
* "type": "Note"
9+
* }
510
*/
611
module.exports = (parameters) => {
712
return parameters.id;
8-
};
13+
};

test/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ test.before(async t => {
4646
},
4747
servers: [
4848
{
49-
url: '0.0.0.0:' + port,
49+
url: 'http://0.0.0.0:' + port,
5050
},
5151
],
52-
packageUrl: './package.json',
52+
packageUrl: path.resolve('./package.json'),
5353
files: './test/api/*.js',
5454
});
5555
});

0 commit comments

Comments
 (0)