openapi-jsonrpc-jsdoc 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +56 -27
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -43,6 +43,14 @@ function resolveSchemaFromTypeNames(names) {
43
43
  items = {type: 'string'};
44
44
  break;
45
45
  }
46
+ case 'Array.<bigint>': {
47
+ type = 'array';
48
+ items = {
49
+ type: 'integer',
50
+ format: 'int64',
51
+ };
52
+ break;
53
+ }
46
54
  case 'Array.<number>': {
47
55
  type = 'array';
48
56
  items = {type: 'number'};
@@ -250,12 +258,11 @@ export default async function openapiJsonrpcJsdoc({
250
258
 
251
259
  const schema = {
252
260
  post: {
253
- operationId: filename,
261
+ operationId: apiName,
254
262
  deprecated: module.deprecated || false,
255
- summary: `/${apiName}`,
263
+ summary: module.summary ?? `/${apiName}`,
256
264
  description: module.description,
257
265
  tags: Array.from(tags),
258
- parameters: [],
259
266
  responses: {
260
267
  '200': {
261
268
  description: 'OK',
@@ -307,9 +314,12 @@ export default async function openapiJsonrpcJsdoc({
307
314
  },
308
315
  };
309
316
  if (module.params) {
310
- let exampleJSON = null;
317
+ const prevObject = {};
311
318
  if (module.examples?.length) {
312
- exampleJSON = JSON.parse(module.examples[0]);
319
+ const exampleJSON = JSON.parse(module.examples[0]);
320
+ if (exampleJSON) {
321
+ prevObject['default'] = exampleJSON;
322
+ }
313
323
  }
314
324
 
315
325
  const propertiesParameters = module.params.reduce(
@@ -320,15 +330,16 @@ export default async function openapiJsonrpcJsdoc({
320
330
  if (!parameter.type) {
321
331
  throw new Error('JSDoc parameter error: ' + apiName);
322
332
  }
323
- // todo поддержать не только object поле в аргументе функции
324
- if (parameter.type.names[0] === 'object') {
333
+ // если главный параметр объявлен как объект - пропускаем его
334
+ if (parameter.type.names.every(name => name.toLowerCase() === 'object')) {
325
335
  return accumulator;
326
336
  }
337
+ const isPlain = !parameter.name.includes('.');
327
338
 
328
339
  const name = extractName(parameter.name);
329
- accumulator.properties[name] = accumulator.properties[name] ?? {};
340
+ const prop = {};
330
341
  const description = parameter.description;
331
- const defaultValue = parameter.defaultvalue;
342
+ let defaultValue = parameter.defaultvalue;
332
343
 
333
344
  const {
334
345
  items,
@@ -339,45 +350,63 @@ export default async function openapiJsonrpcJsdoc({
339
350
  nullable,
340
351
  } = resolveSchemaFromTypeNames(parameter.type.names)
341
352
  if (!parameter.optional) {
353
+ if (!accumulator.required) {
354
+ accumulator.required = [];
355
+ }
342
356
  accumulator.required.push(name);
343
357
  }
344
358
  if (nullable) {
345
- accumulator.properties[name].nullable = nullable;
359
+ prop.nullable = nullable;
346
360
  }
347
- if (defaultValue) {
348
- accumulator.properties[name].default = defaultValue;
361
+ if (defaultValue !== undefined) {
362
+ // fix for array type if it is not properly closed in JSDoc
363
+ if (typeof defaultValue === 'string' && defaultValue.startsWith('[') && !defaultValue.endsWith(']')) {
364
+ defaultValue += ']';
365
+ }
366
+ prop.default = JSON.parse(defaultValue);
349
367
  }
350
368
  if (constant) {
351
- accumulator.properties[name].const = constant;
369
+ prop.const = constant;
352
370
  }
353
371
  if (format) {
354
- accumulator.properties[name].format = format;
372
+ prop.format = format;
355
373
  }
356
374
  if (enumData) {
357
- accumulator.properties[name].enum = enumData;
375
+ prop.enum = enumData;
358
376
  }
359
377
  if (type) {
360
- accumulator.properties[name].type = type;
378
+ prop.type = type;
361
379
  }
362
380
  if (description) {
363
- accumulator.properties[name].description = description;
381
+ prop.description = description;
364
382
  }
365
383
  if (items) {
366
- accumulator.properties[name].items = items;
384
+ prop.items = items;
367
385
  }
386
+ if (isPlain) {
387
+ accumulator = Object.assign(accumulator, prop);
388
+ } else if (accumulator.properties) {
389
+ accumulator.properties[name] = prop;
390
+ } else {
391
+ accumulator.properties = {
392
+ [name]: prop,
393
+ };
394
+ }
395
+
368
396
  return accumulator;
369
397
  },
370
- {
371
- title: 'Parameters',
372
- type: 'object',
373
- 'default': exampleJSON,
374
- required: [],
375
- properties: {},
376
- },
398
+ prevObject,
377
399
  );
378
- if (exampleJSON !== null) {
400
+ if (Object.keys(propertiesParameters).length) {
379
401
  const schemaPostJsdoc = schema.post.requestBody.content['application/json'].schema;
380
- schemaPostJsdoc.properties.params = propertiesParameters;
402
+ if (propertiesParameters.properties) {
403
+ schemaPostJsdoc.properties.params = {
404
+ type: 'object',
405
+ ...propertiesParameters,
406
+ };
407
+ } else {
408
+ schemaPostJsdoc.properties.params = propertiesParameters;
409
+ }
381
410
  }
382
411
  }
383
412
  temporaryDocument.paths[`${api}${apiName}`] = schema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-jsonrpc-jsdoc",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Transform JSDoc-annotated JSON-RPC 2.0 methods into OpenAPI specifications.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",