@grandlinex/swagger-mate 1.3.1 → 1.3.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.
@@ -2,15 +2,104 @@ import { CoreEntity } from '@grandlinex/core';
2
2
  import { HttpStatusTypes } from '../Meta/SwaggerTypesStatic.js';
3
3
  import { SKey, SSchemaEl, SwaggerContent, SwaggerRPathConfResponse, SwaggerRPathReqBody } from '../Meta/SwaggerTypes.js';
4
4
  export default class SPathUtil {
5
+ /**
6
+ * Generates a default response mapping for the specified HTTP status types.
7
+ *
8
+ * @param {HttpStatusTypes[]} types - The HTTP status types for which default responses should be created.
9
+ * @return {SwaggerRPathConfResponse} An object mapping each provided status type to its default response definition. */
5
10
  static defaultResponse(...types: HttpStatusTypes[]): SwaggerRPathConfResponse;
11
+ /**
12
+ * Creates a request body definition for JSON content type using the provided schema.
13
+ *
14
+ * @param {SSchemaEl} schema - The JSON schema used for validating the request body.
15
+ * @return {SwaggerRPathReqBody} A Swagger path request body object specifying application/json content type with the provided schema.
16
+ */
6
17
  static jsonBody(schema: SSchemaEl): SwaggerRPathReqBody;
18
+ /**
19
+ * Builds a Swagger request body for `multipart/form-data` requests.
20
+ *
21
+ * @param {SSchemaEl} [schema] Optional schema describing the form data.
22
+ * If omitted, a default schema with a single binary `file` field is provided.
23
+ * @return {SwaggerRPathReqBody} Swagger request body definition with
24
+ * `multipart/form-data` content and the supplied or default schema. */
7
25
  static formBody(schema?: SSchemaEl): SwaggerRPathReqBody;
26
+ /**
27
+ * Generates a Swagger content definition for the provided entity.
28
+ *
29
+ * @param {T} entity - The entity instance to derive the Swagger schema from.
30
+ * @param {boolean} [list] - When true, the schema will be wrapped in an array type, representing a list of entities.
31
+ *
32
+ * @returns {SwaggerContent|undefined} The Swagger content object for the entity, or `undefined` if the entity does not have a schema.
33
+ */
8
34
  static entityContent<T extends CoreEntity>(entity: T, list?: boolean): SwaggerContent | undefined;
35
+ /**
36
+ * @template T extends CoreEntity
37
+ * @param {T} entity - The entity instance for which to build the response configuration.
38
+ * @param {boolean} [list] - Indicates whether the response should represent a list of entities.
39
+ * @param {boolean} [create] - Indicates whether the response corresponds to a creation operation (status code 201); otherwise 200.
40
+ * @returns {SwaggerRPathConfResponse} The Swagger response configuration object containing the appropriate status code and content.
41
+ */
9
42
  static entityResponse<T extends CoreEntity>(entity: T, list?: boolean, create?: boolean): SwaggerRPathConfResponse;
43
+ /**
44
+ * Builds a JSON schema reference path for the given component name.
45
+ *
46
+ * @param {string} inp - The name of the schema component.
47
+ * @return {string} The JSON reference path formatted as `#/components/schemas/<inp>`.
48
+ */
10
49
  static schemaPath(inp: string): string;
50
+ /**
51
+ * Creates a Swagger request body definition that references a schema.
52
+ *
53
+ * @param {string | CoreEntity} $ref
54
+ * Either the string reference to a schema or a `CoreEntity` instance whose
55
+ * class name will be used to build the reference path.
56
+ * @param {boolean} list
57
+ * If true, the referenced schema is wrapped in an array; otherwise the
58
+ * schema is used directly.
59
+ * @returns {SwaggerRPathReqBody}
60
+ * The request body object containing the appropriate content and schema
61
+ * configuration.
62
+ */
63
+ static refRequest($ref: string | CoreEntity, list: boolean): SwaggerRPathReqBody;
64
+ /**
65
+ * Creates a Swagger response configuration for a given HTTP status code.
66
+ *
67
+ * @param {HttpStatusTypes} code - The primary HTTP status code for */
11
68
  static refResponse(code: HttpStatusTypes, $ref: string | CoreEntity, list: boolean, ...addCodes: HttpStatusTypes[]): SwaggerRPathConfResponse;
69
+ /**
70
+ * Builds a Swagger response configuration object for a given HTTP status code and schema.
71
+ *
72
+ * @param {HttpStatusTypes} code - The primary HTTP status code for the response.
73
+ * @param {SSchemaEl} schema - The JSON schema definition for the response body.
74
+ * @param {boolean} list - If true, the schema is wrapped in an array for list responses.
75
+ * @param {...HttpStatusTypes} addCodes - Additional HTTP status codes for default responses.
76
+ * @return {SwaggerRPathConfResponse} The constructed response configuration object.
77
+ */
12
78
  static jsonResponse(code: HttpStatusTypes, schema: SSchemaEl, list: boolean, ...addCodes: HttpStatusTypes[]): SwaggerRPathConfResponse;
79
+ /**
80
+ * Generates a JSON schema representation from a CoreEntity instance.
81
+ *
82
+ * This method inspects the entity's metadata to construct a schema object
83
+ * describing the entity's shape. The resulting schema contains:
84
+ * - `type`: always `"object"`.
85
+ * - `description`: a string indicating the entity name.
86
+ * - `required`: an array of property names that are defined on the entity.
87
+ * - `properties`: an object mapping each property name to an object that
88
+ * includes the resolved database type and its nullability.
89
+ *
90
+ * If no metadata is found for the provided entity, the method returns `undefined`.
91
+ *
92
+ * @param {T} entity - The entity instance for which to create a schema.
93
+ * @returns {SSchemaEl | undefined} The generated schema object, or `undefined`
94
+ * if the entity's metadata could not be retrieved.
95
+ */
13
96
  static schemaFromEntity<T extends CoreEntity>(entity: T): SSchemaEl | undefined;
97
+ /**
98
+ * Generates a content schema object for the given entity. The schema contains a description derived from the entity metadata and a JSON content schema based on the entity's structure.
99
+ *
100
+ * @param {T} entity - The entity instance for which to generate the content schema. The generic type `T` must extend {@link CoreEntity}.
101
+ * @returns {{ description: string; content: { 'application/json': { schema: SSchemaEl } }; } | undefined} An object containing the content schema, or `undefined` if no metadata is available for the entity.
102
+ */
14
103
  static contentSchemaFromEntity<T extends CoreEntity>(entity: T): {
15
104
  description: string;
16
105
  content: {
@@ -20,9 +109,91 @@ export default class SPathUtil {
20
109
  };
21
110
  } | undefined;
22
111
  /**
23
- * generate global schema
24
- * @param e
112
+ * Generates a mapping from entity names to their corresponding schema objects.
113
+ *
114
+ * @param {CoreEntity[]} e The entities for which schema entries should be generated.
115
+ * @return {SKey<SSchemaEl>} An object whose keys are entity names and values are the schemas derived from those entities.
25
116
  */
26
117
  static schemaEntryGen(...e: CoreEntity[]): SKey<SSchemaEl>;
118
+ /**
119
+ * Builds a JSON schema representation for an entity view that includes both the
120
+ * entity data and its related entity map.
121
+ *
122
+ * @param entity The primary entity used to construct the `dat` portion of the schema.
123
+ * @param entityMap The related entity map used to construct the `join_map` portion of the schema.
124
+ * @returns A {@link SSchemaEl} object schema with properties `i`, `dat`, and `join_map`.
125
+ */
27
126
  static schemaFromEntityView<A extends CoreEntity, B extends CoreEntity>(entity: A, entityMap: B): SSchemaEl;
127
+ /**
128
+ * Extends an entity schema object by merging additional schema options.
129
+ *
130
+ * @param {CoreEntity} entity
131
+ * The entity for which the schema should be extended.
132
+ *
133
+ * @param {...Object} options
134
+ * One or more objects defining schema extensions. Each object may contain:
135
+ * - `key` (string): The property key to add or extend.
136
+ * - `list` (boolean, optional): Indicates whether the property is a list.
137
+ * - `entity` (CoreEntity, optional): The entity type for the property.
138
+ * - `schema` (SSchemaEl, optional): A custom schema definition for the property.
139
+ * - `required` (boolean, optional): Whether the property is required.
140
+ *
141
+ * @return {SSchemaEl}
142
+ * The resulting schema element. If a single property is returned by
143
+ * `extendEntitySchema`, its schema is returned directly; otherwise an
144
+ * object schema with a type of `'object'` is returned.
145
+ */
146
+ static extendEntitySchemaObject(entity: CoreEntity, ...options: {
147
+ key: string;
148
+ list?: boolean;
149
+ entity?: CoreEntity;
150
+ schema?: SSchemaEl;
151
+ required?: boolean;
152
+ }[]): SSchemaEl;
153
+ /**
154
+ * Extends the schema of a given {@link CoreEntity} with additional properties.
155
+ *
156
+ * @param {CoreEntity} entity
157
+ * The entity whose schema will be extended.
158
+ *
159
+ * @param {...{
160
+ * key: string,
161
+ * list?: boolean,
162
+ * entity?: CoreEntity,
163
+ * schema?: SSchemaEl,
164
+ * required?: boolean
165
+ * }} options
166
+ * One or more option objects specifying the extensions to apply. Each option
167
+ * may provide either a direct schema (`schema`) or an entity reference
168
+ * (`entity`). The `list` flag indicates whether the property should be
169
+ * represented as an array of the provided schema. The `required` flag
170
+ * adds the property to the schema’s required list.
171
+ *
172
+ * @returns {SKey<SSchemaEl>}
173
+ * An object containing the updated schema for the entity, keyed by the
174
+ * entity’s name. If the entity metadata cannot be found, an empty
175
+ * object is returned.
176
+ */
177
+ static extendEntitySchema(entity: CoreEntity, ...options: {
178
+ key: string;
179
+ list?: boolean;
180
+ entity?: CoreEntity;
181
+ schema?: SSchemaEl;
182
+ required?: boolean;
183
+ }[]): SKey<SSchemaEl>;
184
+ /**
185
+ * Reduces the entity schema to a single schema element or a generic object.
186
+ *
187
+ * @param entity The entity whose schema should be reduced.
188
+ * @param keys Optional list of keys to include in the reduced schema. If omitted, all keys are considered.
189
+ * @return Returns the schema element of the sole key if only one key is present; otherwise, returns a generic object schema with type `'object'`. */
190
+ static reduceEntitySchemaObject<T extends CoreEntity>(entity: T, ...keys: (keyof T)[]): SSchemaEl;
191
+ /**
192
+ * Creates a reduced version of an entity's schema by excluding specified properties.
193
+ *
194
+ * @param {CoreEntity} entity - The entity whose schema is to be processed.
195
+ * @param {...string} keys - Property names to remove from the schema's `properties` and `required` lists.
196
+ *
197
+ * @returns */
198
+ static reduceEntitySchema<T extends CoreEntity>(entity: T, ...keys: (keyof T)[]): SKey<SSchemaEl>;
28
199
  }
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const core_1 = require("@grandlinex/core");
7
7
  const SUtilMap_js_1 = __importDefault(require("./SUtilMap.js"));
8
+ const SwaggerTypes_js_1 = require("../Meta/SwaggerTypes.js");
8
9
  function resolveDBType(dType) {
9
10
  switch (dType) {
10
11
  case 'int':
@@ -30,6 +31,11 @@ function resolveDBType(dType) {
30
31
  }
31
32
  }
32
33
  class SPathUtil {
34
+ /**
35
+ * Generates a default response mapping for the specified HTTP status types.
36
+ *
37
+ * @param {HttpStatusTypes[]} types - The HTTP status types for which default responses should be created.
38
+ * @return {SwaggerRPathConfResponse} An object mapping each provided status type to its default response definition. */
33
39
  static defaultResponse(...types) {
34
40
  const res = {};
35
41
  types.forEach((el) => {
@@ -37,6 +43,12 @@ class SPathUtil {
37
43
  });
38
44
  return res;
39
45
  }
46
+ /**
47
+ * Creates a request body definition for JSON content type using the provided schema.
48
+ *
49
+ * @param {SSchemaEl} schema - The JSON schema used for validating the request body.
50
+ * @return {SwaggerRPathReqBody} A Swagger path request body object specifying application/json content type with the provided schema.
51
+ */
40
52
  static jsonBody(schema) {
41
53
  return {
42
54
  content: {
@@ -46,6 +58,13 @@ class SPathUtil {
46
58
  },
47
59
  };
48
60
  }
61
+ /**
62
+ * Builds a Swagger request body for `multipart/form-data` requests.
63
+ *
64
+ * @param {SSchemaEl} [schema] Optional schema describing the form data.
65
+ * If omitted, a default schema with a single binary `file` field is provided.
66
+ * @return {SwaggerRPathReqBody} Swagger request body definition with
67
+ * `multipart/form-data` content and the supplied or default schema. */
49
68
  static formBody(schema) {
50
69
  return {
51
70
  content: {
@@ -64,6 +83,14 @@ class SPathUtil {
64
83
  },
65
84
  };
66
85
  }
86
+ /**
87
+ * Generates a Swagger content definition for the provided entity.
88
+ *
89
+ * @param {T} entity - The entity instance to derive the Swagger schema from.
90
+ * @param {boolean} [list] - When true, the schema will be wrapped in an array type, representing a list of entities.
91
+ *
92
+ * @returns {SwaggerContent|undefined} The Swagger content object for the entity, or `undefined` if the entity does not have a schema.
93
+ */
67
94
  static entityContent(entity, list) {
68
95
  const schema = this.schemaFromEntity(entity);
69
96
  if (!schema) {
@@ -85,6 +112,13 @@ class SPathUtil {
85
112
  },
86
113
  };
87
114
  }
115
+ /**
116
+ * @template T extends CoreEntity
117
+ * @param {T} entity - The entity instance for which to build the response configuration.
118
+ * @param {boolean} [list] - Indicates whether the response should represent a list of entities.
119
+ * @param {boolean} [create] - Indicates whether the response corresponds to a creation operation (status code 201); otherwise 200.
120
+ * @returns {SwaggerRPathConfResponse} The Swagger response configuration object containing the appropriate status code and content.
121
+ */
88
122
  static entityResponse(entity, list, create) {
89
123
  const code = create ? '201' : '200';
90
124
  const an = {};
@@ -94,9 +128,58 @@ class SPathUtil {
94
128
  };
95
129
  return an;
96
130
  }
131
+ /**
132
+ * Builds a JSON schema reference path for the given component name.
133
+ *
134
+ * @param {string} inp - The name of the schema component.
135
+ * @return {string} The JSON reference path formatted as `#/components/schemas/<inp>`.
136
+ */
97
137
  static schemaPath(inp) {
98
138
  return `#/components/schemas/${inp}`;
99
139
  }
140
+ /**
141
+ * Creates a Swagger request body definition that references a schema.
142
+ *
143
+ * @param {string | CoreEntity} $ref
144
+ * Either the string reference to a schema or a `CoreEntity` instance whose
145
+ * class name will be used to build the reference path.
146
+ * @param {boolean} list
147
+ * If true, the referenced schema is wrapped in an array; otherwise the
148
+ * schema is used directly.
149
+ * @returns {SwaggerRPathReqBody}
150
+ * The request body object containing the appropriate content and schema
151
+ * configuration.
152
+ */
153
+ static refRequest($ref, list) {
154
+ const t = typeof $ref === 'string'
155
+ ? { $ref }
156
+ : {
157
+ $ref: this.schemaPath(core_1.XUtil.getEntityNames($ref).className),
158
+ };
159
+ if (list) {
160
+ return {
161
+ content: {
162
+ 'application/json': {
163
+ schema: {
164
+ type: 'array',
165
+ items: t,
166
+ },
167
+ },
168
+ },
169
+ };
170
+ }
171
+ return {
172
+ content: {
173
+ 'application/json': {
174
+ schema: t,
175
+ },
176
+ },
177
+ };
178
+ }
179
+ /**
180
+ * Creates a Swagger response configuration for a given HTTP status code.
181
+ *
182
+ * @param {HttpStatusTypes} code - The primary HTTP status code for */
100
183
  static refResponse(code, $ref, list, ...addCodes) {
101
184
  const an = {
102
185
  ...this.defaultResponse(...addCodes),
@@ -131,6 +214,15 @@ class SPathUtil {
131
214
  }
132
215
  return an;
133
216
  }
217
+ /**
218
+ * Builds a Swagger response configuration object for a given HTTP status code and schema.
219
+ *
220
+ * @param {HttpStatusTypes} code - The primary HTTP status code for the response.
221
+ * @param {SSchemaEl} schema - The JSON schema definition for the response body.
222
+ * @param {boolean} list - If true, the schema is wrapped in an array for list responses.
223
+ * @param {...HttpStatusTypes} addCodes - Additional HTTP status codes for default responses.
224
+ * @return {SwaggerRPathConfResponse} The constructed response configuration object.
225
+ */
134
226
  static jsonResponse(code, schema, list, ...addCodes) {
135
227
  const an = {
136
228
  ...this.defaultResponse(...addCodes),
@@ -160,6 +252,23 @@ class SPathUtil {
160
252
  }
161
253
  return an;
162
254
  }
255
+ /**
256
+ * Generates a JSON schema representation from a CoreEntity instance.
257
+ *
258
+ * This method inspects the entity's metadata to construct a schema object
259
+ * describing the entity's shape. The resulting schema contains:
260
+ * - `type`: always `"object"`.
261
+ * - `description`: a string indicating the entity name.
262
+ * - `required`: an array of property names that are defined on the entity.
263
+ * - `properties`: an object mapping each property name to an object that
264
+ * includes the resolved database type and its nullability.
265
+ *
266
+ * If no metadata is found for the provided entity, the method returns `undefined`.
267
+ *
268
+ * @param {T} entity - The entity instance for which to create a schema.
269
+ * @returns {SSchemaEl | undefined} The generated schema object, or `undefined`
270
+ * if the entity's metadata could not be retrieved.
271
+ */
163
272
  static schemaFromEntity(entity) {
164
273
  const schema = {
165
274
  type: 'object',
@@ -184,6 +293,12 @@ class SPathUtil {
184
293
  });
185
294
  return schema;
186
295
  }
296
+ /**
297
+ * Generates a content schema object for the given entity. The schema contains a description derived from the entity metadata and a JSON content schema based on the entity's structure.
298
+ *
299
+ * @param {T} entity - The entity instance for which to generate the content schema. The generic type `T` must extend {@link CoreEntity}.
300
+ * @returns {{ description: string; content: { 'application/json': { schema: SSchemaEl } }; } | undefined} An object containing the content schema, or `undefined` if no metadata is available for the entity.
301
+ */
187
302
  static contentSchemaFromEntity(entity) {
188
303
  const meta = (0, core_1.getEntityMeta)(entity);
189
304
  if (!meta) {
@@ -199,8 +314,10 @@ class SPathUtil {
199
314
  };
200
315
  }
201
316
  /**
202
- * generate global schema
203
- * @param e
317
+ * Generates a mapping from entity names to their corresponding schema objects.
318
+ *
319
+ * @param {CoreEntity[]} e The entities for which schema entries should be generated.
320
+ * @return {SKey<SSchemaEl>} An object whose keys are entity names and values are the schemas derived from those entities.
204
321
  */
205
322
  static schemaEntryGen(...e) {
206
323
  const out = {};
@@ -212,6 +329,14 @@ class SPathUtil {
212
329
  });
213
330
  return out;
214
331
  }
332
+ /**
333
+ * Builds a JSON schema representation for an entity view that includes both the
334
+ * entity data and its related entity map.
335
+ *
336
+ * @param entity The primary entity used to construct the `dat` portion of the schema.
337
+ * @param entityMap The related entity map used to construct the `join_map` portion of the schema.
338
+ * @returns A {@link SSchemaEl} object schema with properties `i`, `dat`, and `join_map`.
339
+ */
215
340
  static schemaFromEntityView(entity, entityMap) {
216
341
  return {
217
342
  type: 'object',
@@ -224,5 +349,136 @@ class SPathUtil {
224
349
  },
225
350
  };
226
351
  }
352
+ /**
353
+ * Extends an entity schema object by merging additional schema options.
354
+ *
355
+ * @param {CoreEntity} entity
356
+ * The entity for which the schema should be extended.
357
+ *
358
+ * @param {...Object} options
359
+ * One or more objects defining schema extensions. Each object may contain:
360
+ * - `key` (string): The property key to add or extend.
361
+ * - `list` (boolean, optional): Indicates whether the property is a list.
362
+ * - `entity` (CoreEntity, optional): The entity type for the property.
363
+ * - `schema` (SSchemaEl, optional): A custom schema definition for the property.
364
+ * - `required` (boolean, optional): Whether the property is required.
365
+ *
366
+ * @return {SSchemaEl}
367
+ * The resulting schema element. If a single property is returned by
368
+ * `extendEntitySchema`, its schema is returned directly; otherwise an
369
+ * object schema with a type of `'object'` is returned.
370
+ */
371
+ static extendEntitySchemaObject(entity, ...options) {
372
+ const schema = this.extendEntitySchema(entity, ...options);
373
+ const ent = Object.entries(schema);
374
+ if (ent.length === 1) {
375
+ return ent[0][1];
376
+ }
377
+ return {
378
+ type: 'object',
379
+ };
380
+ }
381
+ /**
382
+ * Extends the schema of a given {@link CoreEntity} with additional properties.
383
+ *
384
+ * @param {CoreEntity} entity
385
+ * The entity whose schema will be extended.
386
+ *
387
+ * @param {...{
388
+ * key: string,
389
+ * list?: boolean,
390
+ * entity?: CoreEntity,
391
+ * schema?: SSchemaEl,
392
+ * required?: boolean
393
+ * }} options
394
+ * One or more option objects specifying the extensions to apply. Each option
395
+ * may provide either a direct schema (`schema`) or an entity reference
396
+ * (`entity`). The `list` flag indicates whether the property should be
397
+ * represented as an array of the provided schema. The `required` flag
398
+ * adds the property to the schema’s required list.
399
+ *
400
+ * @returns {SKey<SSchemaEl>}
401
+ * An object containing the updated schema for the entity, keyed by the
402
+ * entity’s name. If the entity metadata cannot be found, an empty
403
+ * object is returned.
404
+ */
405
+ static extendEntitySchema(entity, ...options) {
406
+ const meta = (0, core_1.getEntityMeta)(entity);
407
+ if (meta) {
408
+ const schema = SPathUtil.schemaEntryGen(entity)[meta.name];
409
+ if (schema && !(0, SwaggerTypes_js_1.isSwaggerRef)(schema) && schema.properties) {
410
+ for (const option of options) {
411
+ if (option.schema) {
412
+ if (option.list) {
413
+ schema.properties[option.key] = {
414
+ type: 'array',
415
+ items: option.schema,
416
+ };
417
+ }
418
+ else {
419
+ schema.properties[option.key] = option.schema;
420
+ }
421
+ }
422
+ else if (option.entity) {
423
+ const eMeta = (0, core_1.getEntityMeta)(option.entity);
424
+ if (eMeta) {
425
+ const scheme = SPathUtil.schemaEntryGen(option.entity)[eMeta.name];
426
+ if (option.list) {
427
+ schema.properties[option.key] = {
428
+ type: 'array',
429
+ items: scheme,
430
+ };
431
+ }
432
+ else {
433
+ schema.properties[option.key] = scheme;
434
+ }
435
+ }
436
+ }
437
+ if (option.required) {
438
+ schema.required = [...(schema.required || []), option.key];
439
+ }
440
+ }
441
+ }
442
+ return {
443
+ [meta.name]: schema,
444
+ };
445
+ }
446
+ return {};
447
+ }
448
+ /**
449
+ * Reduces the entity schema to a single schema element or a generic object.
450
+ *
451
+ * @param entity The entity whose schema should be reduced.
452
+ * @param keys Optional list of keys to include in the reduced schema. If omitted, all keys are considered.
453
+ * @return Returns the schema element of the sole key if only one key is present; otherwise, returns a generic object schema with type `'object'`. */
454
+ static reduceEntitySchemaObject(entity, ...keys) {
455
+ const schema = this.reduceEntitySchema(entity, ...keys);
456
+ const ent = Object.entries(schema);
457
+ if (ent.length === 1) {
458
+ return ent[0][1];
459
+ }
460
+ return {
461
+ type: 'object',
462
+ };
463
+ }
464
+ /**
465
+ * Creates a reduced version of an entity's schema by excluding specified properties.
466
+ *
467
+ * @param {CoreEntity} entity - The entity whose schema is to be processed.
468
+ * @param {...string} keys - Property names to remove from the schema's `properties` and `required` lists.
469
+ *
470
+ * @returns */
471
+ static reduceEntitySchema(entity, ...keys) {
472
+ const meta = (0, core_1.getEntityMeta)(entity);
473
+ if (meta) {
474
+ const schema = SPathUtil.schemaEntryGen(entity)[meta.name];
475
+ if (schema && !(0, SwaggerTypes_js_1.isSwaggerRef)(schema) && schema.properties) {
476
+ schema.properties = Object.fromEntries(Object.entries(schema.properties).filter(([e]) => !keys.includes(e)));
477
+ schema.required = (schema.required || []).filter((e) => !keys.includes(e));
478
+ }
479
+ return { [meta.name]: schema };
480
+ }
481
+ return {};
482
+ }
227
483
  }
228
484
  exports.default = SPathUtil;
@@ -1,11 +1,27 @@
1
- import { ObjectLike } from '@grandlinex/core';
1
+ import { CoreLogChannel, ObjectLike } from '@grandlinex/core';
2
2
  import { Server } from 'net';
3
3
  import { MergeInputType, SwaggerConfig, SwaggerRPath } from './Meta/SwaggerTypes.js';
4
4
  import { RouteData } from './annotation/index.js';
5
5
  export default class SwaggerUtil {
6
+ static logger: CoreLogChannel | null;
7
+ static getLogger(): CoreLogChannel;
6
8
  static writeMeta(conf: SwaggerConfig, kind: 'JSON' | 'YAML', path?: string): void;
7
9
  static readMeta(path: string): any;
8
- static serveMeta(conf: SwaggerConfig, port?: number, auth?: string): Promise<Server | null>;
10
+ /**
11
+ * Serves a meta page for Swagger UI or rapi-doc.
12
+ *
13
+ * @param {SwaggerConfig} conf The swagger configuration to expose via `/spec`.
14
+ * @param {Object} [option] Options for serving the meta page.
15
+ * @param {'swagger-ui'|'rapi-doc'} [option.type='swagger-ui'] The type of UI to serve.
16
+ * @param {number} [option.port] The port to listen on. Defaults to 9000.
17
+ * @param {string} [option.auth] Optional authentication key appended to the URL.
18
+ * @returns {Promise<Server|null>} A promise that resolves with the created server instance or null.
19
+ */
20
+ static serveMeta(conf: SwaggerConfig, option?: {
21
+ type?: 'swagger-ui' | 'rapi-doc';
22
+ port?: number;
23
+ auth?: string;
24
+ }): Promise<Server | null>;
9
25
  static metaExtractor(root: ObjectLike, npmPackageVersion: boolean, ...path: ObjectLike[]): SwaggerConfig | undefined;
10
26
  static routeToSwaggerPath(route: RouteData): SwaggerRPath;
11
27
  static merge(root: SwaggerConfig, data: MergeInputType[]): SwaggerConfig;
@@ -47,6 +47,13 @@ const PathHelp_js_1 = __importStar(require("../PathHelp.js"));
47
47
  const index_js_1 = require("./annotation/index.js");
48
48
  const index_js_2 = require("../index.js");
49
49
  class SwaggerUtil {
50
+ static getLogger() {
51
+ if (!this.logger) {
52
+ const logger = new core_1.DefaultLogger();
53
+ this.logger = new core_1.CoreLogChannel('SwaggerUtil', logger);
54
+ }
55
+ return this.logger;
56
+ }
50
57
  static writeMeta(conf, kind, path) {
51
58
  if (kind === 'JSON') {
52
59
  const p = Path.join(path || process.cwd(), 'openapi.json');
@@ -63,11 +70,25 @@ class SwaggerUtil {
63
70
  return JSON.parse(file);
64
71
  }
65
72
  catch (e) {
73
+ this.getLogger().error(e);
66
74
  return null;
67
75
  }
68
76
  }
69
- static async serveMeta(conf, port, auth) {
70
- const resFiles = (0, PathHelp_js_1.default)((0, PathHelp_js_1.getBaseFolder)(), '..', 'res', 'html');
77
+ /**
78
+ * Serves a meta page for Swagger UI or rapi-doc.
79
+ *
80
+ * @param {SwaggerConfig} conf The swagger configuration to expose via `/spec`.
81
+ * @param {Object} [option] Options for serving the meta page.
82
+ * @param {'swagger-ui'|'rapi-doc'} [option.type='swagger-ui'] The type of UI to serve.
83
+ * @param {number} [option.port] The port to listen on. Defaults to 9000.
84
+ * @param {string} [option.auth] Optional authentication key appended to the URL.
85
+ * @returns {Promise<Server|null>} A promise that resolves with the created server instance or null.
86
+ */
87
+ static async serveMeta(conf, option) {
88
+ const type = option?.type ?? 'swagger-ui';
89
+ const port = option?.port || 9000;
90
+ const auth = option?.auth;
91
+ const resFiles = (0, PathHelp_js_1.default)((0, PathHelp_js_1.getBaseFolder)(), '..', 'res', 'html', type);
71
92
  const key = auth ? `?auth=${auth}` : '';
72
93
  const app = (0, express_1.default)();
73
94
  app.use('/', express_1.default.static(resFiles));
@@ -75,8 +96,8 @@ class SwaggerUtil {
75
96
  res.status(200).send(conf);
76
97
  });
77
98
  return new Promise((resolve) => {
78
- const s = app.listen(port || 9000, () => {
79
- console.log(`listen on http://localhost:${port || 9000}${key}#`);
99
+ const s = app.listen(port, () => {
100
+ this.getLogger().log(`${type} listen on http://localhost:${port}${key}#`);
80
101
  resolve(s);
81
102
  });
82
103
  });
@@ -121,7 +142,13 @@ class SwaggerUtil {
121
142
  // Handle requestBody
122
143
  if (!conf.requestBody) {
123
144
  if (route.meta.requestSchema) {
124
- conf.requestBody = index_js_2.SPathUtil.jsonBody(route.meta.requestSchema);
145
+ if (typeof route.meta.requestSchema === 'string' ||
146
+ (0, core_1.instanceOfEntity)(route.meta.requestSchema)) {
147
+ conf.requestBody = index_js_2.SPathUtil.refRequest(route.meta.requestSchema, route.meta.responseType === 'LIST');
148
+ }
149
+ else {
150
+ conf.requestBody = index_js_2.SPathUtil.jsonBody(route.meta.requestSchema);
151
+ }
125
152
  }
126
153
  }
127
154
  // Handle responses
@@ -8,13 +8,18 @@ export declare enum ActionMode {
8
8
  'DMZ_WITH_USER' = 2
9
9
  }
10
10
  export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
11
- export type ResponseTypes = 'LIST';
11
+ /**
12
+ * LIST - Response is an array of items
13
+ * SINGLE - Response is a single item (default)
14
+ */
15
+ export type ResponseRequestTypes = 'LIST' | 'SINGLE';
12
16
  export type RouteMeta = {
13
17
  pathOverride?: string;
14
18
  mode?: ActionMode;
15
- requestSchema?: SSchemaEl;
19
+ requestSchema?: SSchemaEl | CoreEntity | string;
16
20
  responseSchema?: SSchemaEl | CoreEntity | string;
17
- responseType?: ResponseTypes;
21
+ requestType?: ResponseRequestTypes;
22
+ responseType?: ResponseRequestTypes;
18
23
  responseCodes?: HttpStatusTypes[];
19
24
  } & SwaggerRPathConf;
20
25
  export type RouteData = {