@grandlinex/swagger-mate 1.3.2 → 1.3.3
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.
- package/dist/cjs/Swagger/Meta/SwaggerTypes.d.ts +3 -2
- package/dist/cjs/Swagger/Path/ESchemaEditor.d.ts +51 -0
- package/dist/cjs/Swagger/Path/ESchemaEditor.js +151 -0
- package/dist/cjs/Swagger/Path/SPathUtil.d.ts +7 -76
- package/dist/cjs/Swagger/Path/SPathUtil.js +10 -182
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/mjs/Swagger/Meta/SwaggerTypes.d.ts +3 -2
- package/dist/mjs/Swagger/Path/ESchemaEditor.d.ts +51 -0
- package/dist/mjs/Swagger/Path/ESchemaEditor.js +147 -0
- package/dist/mjs/Swagger/Path/SPathUtil.d.ts +7 -76
- package/dist/mjs/Swagger/Path/SPathUtil.js +11 -183
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/package.json +1 -1
|
@@ -56,7 +56,7 @@ export interface SwaggerRInfo {
|
|
|
56
56
|
version: string;
|
|
57
57
|
license?: SwaggerRInfoLicence;
|
|
58
58
|
}
|
|
59
|
-
export type
|
|
59
|
+
export type SSchemaElObj = {
|
|
60
60
|
type: SDataType;
|
|
61
61
|
example?: string;
|
|
62
62
|
format?: SDataFormat;
|
|
@@ -68,7 +68,8 @@ export type SSchemaEl = {
|
|
|
68
68
|
required?: string[];
|
|
69
69
|
enum?: string[];
|
|
70
70
|
nullable?: boolean;
|
|
71
|
-
}
|
|
71
|
+
};
|
|
72
|
+
export type SSchemaEl = SSchemaElObj | SwaggerRRef;
|
|
72
73
|
export type SwaggerContent = {
|
|
73
74
|
[K in SMediaType]?: {
|
|
74
75
|
schema: SSchemaEl;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { CoreEntity } from '@grandlinex/core';
|
|
2
|
+
import { SKey, SSchemaEl, SSchemaElObj } from '../Meta/SwaggerTypes.js';
|
|
3
|
+
export type ESchemaAddOption = {
|
|
4
|
+
key: string;
|
|
5
|
+
list?: boolean;
|
|
6
|
+
entity?: CoreEntity;
|
|
7
|
+
schema?: SSchemaEl;
|
|
8
|
+
required?: boolean;
|
|
9
|
+
nullable?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare class ESchemaEditor<T extends CoreEntity> {
|
|
12
|
+
private data;
|
|
13
|
+
private name;
|
|
14
|
+
private constructor();
|
|
15
|
+
/**
|
|
16
|
+
* Removes the specified keys from the schema's properties and updates the required list.
|
|
17
|
+
*
|
|
18
|
+
* @param {...(keyof T)} keys - The keys of the properties to remove.
|
|
19
|
+
* @returns {ESchemaEditor<T>} The editor instance for method chaining.
|
|
20
|
+
*/
|
|
21
|
+
remove(...keys: (keyof T)[]): ESchemaEditor<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Adds properties to the schema based on the provided options.
|
|
24
|
+
*
|
|
25
|
+
* @param {...ESchemaAddOption} options The options describing the properties to add. Each option may specify a
|
|
26
|
+
* `key`, a `schema` or an `entity`, and optionally whether the property is a `list`, `nullable`, or `required`.
|
|
27
|
+
* @returns {ESchemaEditor<T>} The editor instance for chaining.
|
|
28
|
+
*/
|
|
29
|
+
add(...options: ESchemaAddOption[]): ESchemaEditor<T>;
|
|
30
|
+
getSchema(): SSchemaElObj;
|
|
31
|
+
getSKeySchema(): SKey<SSchemaElObj>;
|
|
32
|
+
static fromEntity<J extends CoreEntity>(e: J): ESchemaEditor<J>;
|
|
33
|
+
/**
|
|
34
|
+
* Generates a JSON schema representation from a CoreEntity instance.
|
|
35
|
+
*
|
|
36
|
+
* This method inspects the entity's metadata to construct a schema object
|
|
37
|
+
* describing the entity's shape. The resulting schema contains:
|
|
38
|
+
* - `type`: always `"object"`.
|
|
39
|
+
* - `description`: a string indicating the entity name.
|
|
40
|
+
* - `required`: an array of property names that are defined on the entity.
|
|
41
|
+
* - `properties`: an object mapping each property name to an object that
|
|
42
|
+
* includes the resolved database type and its nullability.
|
|
43
|
+
*
|
|
44
|
+
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
45
|
+
*
|
|
46
|
+
* @param {T} entity - The entity instance for which to create a schema.
|
|
47
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
48
|
+
* if the entity's metadata could not be retrieved.
|
|
49
|
+
*/
|
|
50
|
+
static schemaFromEntity<T extends CoreEntity>(entity: T): SSchemaElObj | undefined;
|
|
51
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ESchemaEditor = void 0;
|
|
4
|
+
const core_1 = require("@grandlinex/core");
|
|
5
|
+
function resolveDBType(dType) {
|
|
6
|
+
switch (dType) {
|
|
7
|
+
case 'int':
|
|
8
|
+
case 'long':
|
|
9
|
+
return 'integer';
|
|
10
|
+
case 'double':
|
|
11
|
+
case 'float':
|
|
12
|
+
return 'number';
|
|
13
|
+
case 'blob':
|
|
14
|
+
return 'string';
|
|
15
|
+
case 'string':
|
|
16
|
+
case 'text':
|
|
17
|
+
case 'uuid':
|
|
18
|
+
return 'string';
|
|
19
|
+
case 'boolean':
|
|
20
|
+
return 'boolean';
|
|
21
|
+
case 'date':
|
|
22
|
+
return 'string';
|
|
23
|
+
case 'json':
|
|
24
|
+
return 'object';
|
|
25
|
+
default:
|
|
26
|
+
throw Error('TypeNotSupported');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
class ESchemaEditor {
|
|
30
|
+
constructor(entity, name) {
|
|
31
|
+
this.data = ESchemaEditor.schemaFromEntity(entity);
|
|
32
|
+
this.name = name;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Removes the specified keys from the schema's properties and updates the required list.
|
|
36
|
+
*
|
|
37
|
+
* @param {...(keyof T)} keys - The keys of the properties to remove.
|
|
38
|
+
* @returns {ESchemaEditor<T>} The editor instance for method chaining.
|
|
39
|
+
*/
|
|
40
|
+
remove(...keys) {
|
|
41
|
+
if (this.data.properties) {
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
this.data.properties = Object.fromEntries(Object.entries(this.data.properties).filter(([e]) => key !== e));
|
|
44
|
+
this.data.required = (this.data.required || []).filter((e) => key !== e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adds properties to the schema based on the provided options.
|
|
51
|
+
*
|
|
52
|
+
* @param {...ESchemaAddOption} options The options describing the properties to add. Each option may specify a
|
|
53
|
+
* `key`, a `schema` or an `entity`, and optionally whether the property is a `list`, `nullable`, or `required`.
|
|
54
|
+
* @returns {ESchemaEditor<T>} The editor instance for chaining.
|
|
55
|
+
*/
|
|
56
|
+
add(...options) {
|
|
57
|
+
if (!this.data.properties) {
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
for (const option of options) {
|
|
61
|
+
if (option.schema) {
|
|
62
|
+
if (option.list) {
|
|
63
|
+
this.data.properties[option.key] = {
|
|
64
|
+
type: 'array',
|
|
65
|
+
items: option.schema,
|
|
66
|
+
nullable: option.nullable,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.data.properties[option.key] = option.schema;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else if (option.entity) {
|
|
74
|
+
const scheme = ESchemaEditor.fromEntity(option.entity).getSchema();
|
|
75
|
+
if (option.nullable) {
|
|
76
|
+
scheme.nullable = true;
|
|
77
|
+
}
|
|
78
|
+
if (option.list) {
|
|
79
|
+
this.data.properties[option.key] = {
|
|
80
|
+
type: 'array',
|
|
81
|
+
items: scheme,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.data.properties[option.key] = scheme;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (option.required) {
|
|
89
|
+
this.data.required = [...(this.data.required || []), option.key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
getSchema() {
|
|
95
|
+
return this.data;
|
|
96
|
+
}
|
|
97
|
+
getSKeySchema() {
|
|
98
|
+
return {
|
|
99
|
+
[this.name]: this.data,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
static fromEntity(e) {
|
|
103
|
+
const meta = (0, core_1.getEntityMeta)(e);
|
|
104
|
+
if (meta) {
|
|
105
|
+
return new ESchemaEditor(e, meta.name);
|
|
106
|
+
}
|
|
107
|
+
throw new Error('Entity metadata not found');
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Generates a JSON schema representation from a CoreEntity instance.
|
|
111
|
+
*
|
|
112
|
+
* This method inspects the entity's metadata to construct a schema object
|
|
113
|
+
* describing the entity's shape. The resulting schema contains:
|
|
114
|
+
* - `type`: always `"object"`.
|
|
115
|
+
* - `description`: a string indicating the entity name.
|
|
116
|
+
* - `required`: an array of property names that are defined on the entity.
|
|
117
|
+
* - `properties`: an object mapping each property name to an object that
|
|
118
|
+
* includes the resolved database type and its nullability.
|
|
119
|
+
*
|
|
120
|
+
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
121
|
+
*
|
|
122
|
+
* @param {T} entity - The entity instance for which to create a schema.
|
|
123
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
124
|
+
* if the entity's metadata could not be retrieved.
|
|
125
|
+
*/
|
|
126
|
+
static schemaFromEntity(entity) {
|
|
127
|
+
const schema = {
|
|
128
|
+
type: 'object',
|
|
129
|
+
properties: {},
|
|
130
|
+
};
|
|
131
|
+
const meta = (0, core_1.getEntityMeta)(entity);
|
|
132
|
+
if (!meta) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
schema.description = `Entity: ${meta.name}`;
|
|
136
|
+
schema.required = [];
|
|
137
|
+
const keys = Object.keys(entity);
|
|
138
|
+
keys.forEach((k) => {
|
|
139
|
+
const cMeta = (0, core_1.getColumnMeta)(entity, k);
|
|
140
|
+
if (cMeta && schema.properties) {
|
|
141
|
+
schema.required.push(k);
|
|
142
|
+
schema.properties[k] = {
|
|
143
|
+
type: resolveDBType(cMeta.dataType),
|
|
144
|
+
nullable: cMeta.canBeNull,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return schema;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.ESchemaEditor = ESchemaEditor;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CoreEntity } from '@grandlinex/core';
|
|
2
2
|
import { HttpStatusTypes } from '../Meta/SwaggerTypesStatic.js';
|
|
3
|
-
import { SKey, SSchemaEl, SwaggerContent, SwaggerRPathConfResponse, SwaggerRPathReqBody } from '../Meta/SwaggerTypes.js';
|
|
3
|
+
import { SKey, SSchemaEl, SSchemaElObj, SwaggerContent, SwaggerRPathConfResponse, SwaggerRPathReqBody } from '../Meta/SwaggerTypes.js';
|
|
4
|
+
import { ESchemaEditor } from './ESchemaEditor.js';
|
|
4
5
|
export default class SPathUtil {
|
|
5
6
|
/**
|
|
6
7
|
* Generates a default response mapping for the specified HTTP status types.
|
|
@@ -90,10 +91,11 @@ export default class SPathUtil {
|
|
|
90
91
|
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
91
92
|
*
|
|
92
93
|
* @param {T} entity - The entity instance for which to create a schema.
|
|
93
|
-
* @returns {
|
|
94
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
94
95
|
* if the entity's metadata could not be retrieved.
|
|
96
|
+
* @deprecated Use {@link ESchemaEditor.schemaFromEntity} instead.
|
|
95
97
|
*/
|
|
96
|
-
static schemaFromEntity<T extends CoreEntity>(entity: T):
|
|
98
|
+
static schemaFromEntity<T extends CoreEntity>(entity: T): SSchemaElObj | undefined;
|
|
97
99
|
/**
|
|
98
100
|
* 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
101
|
*
|
|
@@ -114,7 +116,7 @@ export default class SPathUtil {
|
|
|
114
116
|
* @param {CoreEntity[]} e The entities for which schema entries should be generated.
|
|
115
117
|
* @return {SKey<SSchemaEl>} An object whose keys are entity names and values are the schemas derived from those entities.
|
|
116
118
|
*/
|
|
117
|
-
static schemaEntryGen(...e: CoreEntity[]): SKey<
|
|
119
|
+
static schemaEntryGen(...e: CoreEntity[]): SKey<SSchemaElObj>;
|
|
118
120
|
/**
|
|
119
121
|
* Builds a JSON schema representation for an entity view that includes both the
|
|
120
122
|
* entity data and its related entity map.
|
|
@@ -124,76 +126,5 @@ export default class SPathUtil {
|
|
|
124
126
|
* @returns A {@link SSchemaEl} object schema with properties `i`, `dat`, and `join_map`.
|
|
125
127
|
*/
|
|
126
128
|
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>;
|
|
129
|
+
static editor<J extends CoreEntity>(e: J): ESchemaEditor<J>;
|
|
199
130
|
}
|
|
@@ -5,31 +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
|
|
9
|
-
function resolveDBType(dType) {
|
|
10
|
-
switch (dType) {
|
|
11
|
-
case 'int':
|
|
12
|
-
case 'long':
|
|
13
|
-
return 'integer';
|
|
14
|
-
case 'double':
|
|
15
|
-
case 'float':
|
|
16
|
-
return 'number';
|
|
17
|
-
case 'blob':
|
|
18
|
-
return 'string';
|
|
19
|
-
case 'string':
|
|
20
|
-
case 'text':
|
|
21
|
-
case 'uuid':
|
|
22
|
-
return 'string';
|
|
23
|
-
case 'boolean':
|
|
24
|
-
return 'boolean';
|
|
25
|
-
case 'date':
|
|
26
|
-
return 'string';
|
|
27
|
-
case 'json':
|
|
28
|
-
return 'object';
|
|
29
|
-
default:
|
|
30
|
-
throw Error('TypeNotSupported');
|
|
31
|
-
}
|
|
32
|
-
}
|
|
8
|
+
const ESchemaEditor_js_1 = require("./ESchemaEditor.js");
|
|
33
9
|
class SPathUtil {
|
|
34
10
|
/**
|
|
35
11
|
* Generates a default response mapping for the specified HTTP status types.
|
|
@@ -266,32 +242,12 @@ class SPathUtil {
|
|
|
266
242
|
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
267
243
|
*
|
|
268
244
|
* @param {T} entity - The entity instance for which to create a schema.
|
|
269
|
-
* @returns {
|
|
245
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
270
246
|
* if the entity's metadata could not be retrieved.
|
|
247
|
+
* @deprecated Use {@link ESchemaEditor.schemaFromEntity} instead.
|
|
271
248
|
*/
|
|
272
249
|
static schemaFromEntity(entity) {
|
|
273
|
-
|
|
274
|
-
type: 'object',
|
|
275
|
-
properties: {},
|
|
276
|
-
};
|
|
277
|
-
const meta = (0, core_1.getEntityMeta)(entity);
|
|
278
|
-
if (!meta) {
|
|
279
|
-
return undefined;
|
|
280
|
-
}
|
|
281
|
-
schema.description = `Entity: ${meta.name}`;
|
|
282
|
-
schema.required = [];
|
|
283
|
-
const keys = Object.keys(entity);
|
|
284
|
-
keys.forEach((k) => {
|
|
285
|
-
const cMeta = (0, core_1.getColumnMeta)(entity, k);
|
|
286
|
-
if (cMeta && schema.properties) {
|
|
287
|
-
schema.required.push(k);
|
|
288
|
-
schema.properties[k] = {
|
|
289
|
-
type: resolveDBType(cMeta.dataType),
|
|
290
|
-
nullable: cMeta.canBeNull,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
return schema;
|
|
250
|
+
return ESchemaEditor_js_1.ESchemaEditor.schemaFromEntity(entity);
|
|
295
251
|
}
|
|
296
252
|
/**
|
|
297
253
|
* 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.
|
|
@@ -308,7 +264,7 @@ class SPathUtil {
|
|
|
308
264
|
description: `Entity: ${meta.name}`,
|
|
309
265
|
content: {
|
|
310
266
|
'application/json': {
|
|
311
|
-
schema:
|
|
267
|
+
schema: ESchemaEditor_js_1.ESchemaEditor.schemaFromEntity(entity),
|
|
312
268
|
},
|
|
313
269
|
},
|
|
314
270
|
};
|
|
@@ -324,7 +280,7 @@ class SPathUtil {
|
|
|
324
280
|
e.forEach((el) => {
|
|
325
281
|
const meta = (0, core_1.getEntityMeta)(el);
|
|
326
282
|
if (meta) {
|
|
327
|
-
out[meta.name] =
|
|
283
|
+
out[meta.name] = ESchemaEditor_js_1.ESchemaEditor.schemaFromEntity(el);
|
|
328
284
|
}
|
|
329
285
|
});
|
|
330
286
|
return out;
|
|
@@ -344,141 +300,13 @@ class SPathUtil {
|
|
|
344
300
|
i: {
|
|
345
301
|
type: 'integer',
|
|
346
302
|
},
|
|
347
|
-
dat:
|
|
348
|
-
join_map:
|
|
303
|
+
dat: ESchemaEditor_js_1.ESchemaEditor.schemaFromEntity(entity),
|
|
304
|
+
join_map: ESchemaEditor_js_1.ESchemaEditor.schemaFromEntity(entityMap),
|
|
349
305
|
},
|
|
350
306
|
};
|
|
351
307
|
}
|
|
352
|
-
|
|
353
|
-
|
|
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 {};
|
|
308
|
+
static editor(e) {
|
|
309
|
+
return ESchemaEditor_js_1.ESchemaEditor.fromEntity(e);
|
|
482
310
|
}
|
|
483
311
|
}
|
|
484
312
|
exports.default = SPathUtil;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import SwaggerUtil from './Swagger/SwaggerUtil.js';
|
|
2
2
|
import SPathUtil from './Swagger/Path/SPathUtil.js';
|
|
3
3
|
import SwaggerClient from './Swagger/Client/SwaggerClient.js';
|
|
4
|
+
export * from './Swagger/Path/ESchemaEditor.js';
|
|
4
5
|
export * from './Swagger/debug/index.js';
|
|
5
6
|
export * from './Swagger/annotation/index.js';
|
|
6
7
|
export * from './Swagger/Meta/Swagger.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -24,6 +24,7 @@ const SPathUtil_js_1 = __importDefault(require("./Swagger/Path/SPathUtil.js"));
|
|
|
24
24
|
exports.SPathUtil = SPathUtil_js_1.default;
|
|
25
25
|
const SwaggerClient_js_1 = __importDefault(require("./Swagger/Client/SwaggerClient.js"));
|
|
26
26
|
exports.SwaggerClient = SwaggerClient_js_1.default;
|
|
27
|
+
__exportStar(require("./Swagger/Path/ESchemaEditor.js"), exports);
|
|
27
28
|
__exportStar(require("./Swagger/debug/index.js"), exports);
|
|
28
29
|
__exportStar(require("./Swagger/annotation/index.js"), exports);
|
|
29
30
|
__exportStar(require("./Swagger/Meta/Swagger.js"), exports);
|
|
@@ -56,7 +56,7 @@ export interface SwaggerRInfo {
|
|
|
56
56
|
version: string;
|
|
57
57
|
license?: SwaggerRInfoLicence;
|
|
58
58
|
}
|
|
59
|
-
export type
|
|
59
|
+
export type SSchemaElObj = {
|
|
60
60
|
type: SDataType;
|
|
61
61
|
example?: string;
|
|
62
62
|
format?: SDataFormat;
|
|
@@ -68,7 +68,8 @@ export type SSchemaEl = {
|
|
|
68
68
|
required?: string[];
|
|
69
69
|
enum?: string[];
|
|
70
70
|
nullable?: boolean;
|
|
71
|
-
}
|
|
71
|
+
};
|
|
72
|
+
export type SSchemaEl = SSchemaElObj | SwaggerRRef;
|
|
72
73
|
export type SwaggerContent = {
|
|
73
74
|
[K in SMediaType]?: {
|
|
74
75
|
schema: SSchemaEl;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { CoreEntity } from '@grandlinex/core';
|
|
2
|
+
import { SKey, SSchemaEl, SSchemaElObj } from '../Meta/SwaggerTypes.js';
|
|
3
|
+
export type ESchemaAddOption = {
|
|
4
|
+
key: string;
|
|
5
|
+
list?: boolean;
|
|
6
|
+
entity?: CoreEntity;
|
|
7
|
+
schema?: SSchemaEl;
|
|
8
|
+
required?: boolean;
|
|
9
|
+
nullable?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare class ESchemaEditor<T extends CoreEntity> {
|
|
12
|
+
private data;
|
|
13
|
+
private name;
|
|
14
|
+
private constructor();
|
|
15
|
+
/**
|
|
16
|
+
* Removes the specified keys from the schema's properties and updates the required list.
|
|
17
|
+
*
|
|
18
|
+
* @param {...(keyof T)} keys - The keys of the properties to remove.
|
|
19
|
+
* @returns {ESchemaEditor<T>} The editor instance for method chaining.
|
|
20
|
+
*/
|
|
21
|
+
remove(...keys: (keyof T)[]): ESchemaEditor<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Adds properties to the schema based on the provided options.
|
|
24
|
+
*
|
|
25
|
+
* @param {...ESchemaAddOption} options The options describing the properties to add. Each option may specify a
|
|
26
|
+
* `key`, a `schema` or an `entity`, and optionally whether the property is a `list`, `nullable`, or `required`.
|
|
27
|
+
* @returns {ESchemaEditor<T>} The editor instance for chaining.
|
|
28
|
+
*/
|
|
29
|
+
add(...options: ESchemaAddOption[]): ESchemaEditor<T>;
|
|
30
|
+
getSchema(): SSchemaElObj;
|
|
31
|
+
getSKeySchema(): SKey<SSchemaElObj>;
|
|
32
|
+
static fromEntity<J extends CoreEntity>(e: J): ESchemaEditor<J>;
|
|
33
|
+
/**
|
|
34
|
+
* Generates a JSON schema representation from a CoreEntity instance.
|
|
35
|
+
*
|
|
36
|
+
* This method inspects the entity's metadata to construct a schema object
|
|
37
|
+
* describing the entity's shape. The resulting schema contains:
|
|
38
|
+
* - `type`: always `"object"`.
|
|
39
|
+
* - `description`: a string indicating the entity name.
|
|
40
|
+
* - `required`: an array of property names that are defined on the entity.
|
|
41
|
+
* - `properties`: an object mapping each property name to an object that
|
|
42
|
+
* includes the resolved database type and its nullability.
|
|
43
|
+
*
|
|
44
|
+
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
45
|
+
*
|
|
46
|
+
* @param {T} entity - The entity instance for which to create a schema.
|
|
47
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
48
|
+
* if the entity's metadata could not be retrieved.
|
|
49
|
+
*/
|
|
50
|
+
static schemaFromEntity<T extends CoreEntity>(entity: T): SSchemaElObj | undefined;
|
|
51
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { getColumnMeta, getEntityMeta, } from '@grandlinex/core';
|
|
2
|
+
function resolveDBType(dType) {
|
|
3
|
+
switch (dType) {
|
|
4
|
+
case 'int':
|
|
5
|
+
case 'long':
|
|
6
|
+
return 'integer';
|
|
7
|
+
case 'double':
|
|
8
|
+
case 'float':
|
|
9
|
+
return 'number';
|
|
10
|
+
case 'blob':
|
|
11
|
+
return 'string';
|
|
12
|
+
case 'string':
|
|
13
|
+
case 'text':
|
|
14
|
+
case 'uuid':
|
|
15
|
+
return 'string';
|
|
16
|
+
case 'boolean':
|
|
17
|
+
return 'boolean';
|
|
18
|
+
case 'date':
|
|
19
|
+
return 'string';
|
|
20
|
+
case 'json':
|
|
21
|
+
return 'object';
|
|
22
|
+
default:
|
|
23
|
+
throw Error('TypeNotSupported');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class ESchemaEditor {
|
|
27
|
+
constructor(entity, name) {
|
|
28
|
+
this.data = ESchemaEditor.schemaFromEntity(entity);
|
|
29
|
+
this.name = name;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Removes the specified keys from the schema's properties and updates the required list.
|
|
33
|
+
*
|
|
34
|
+
* @param {...(keyof T)} keys - The keys of the properties to remove.
|
|
35
|
+
* @returns {ESchemaEditor<T>} The editor instance for method chaining.
|
|
36
|
+
*/
|
|
37
|
+
remove(...keys) {
|
|
38
|
+
if (this.data.properties) {
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
this.data.properties = Object.fromEntries(Object.entries(this.data.properties).filter(([e]) => key !== e));
|
|
41
|
+
this.data.required = (this.data.required || []).filter((e) => key !== e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Adds properties to the schema based on the provided options.
|
|
48
|
+
*
|
|
49
|
+
* @param {...ESchemaAddOption} options The options describing the properties to add. Each option may specify a
|
|
50
|
+
* `key`, a `schema` or an `entity`, and optionally whether the property is a `list`, `nullable`, or `required`.
|
|
51
|
+
* @returns {ESchemaEditor<T>} The editor instance for chaining.
|
|
52
|
+
*/
|
|
53
|
+
add(...options) {
|
|
54
|
+
if (!this.data.properties) {
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
for (const option of options) {
|
|
58
|
+
if (option.schema) {
|
|
59
|
+
if (option.list) {
|
|
60
|
+
this.data.properties[option.key] = {
|
|
61
|
+
type: 'array',
|
|
62
|
+
items: option.schema,
|
|
63
|
+
nullable: option.nullable,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this.data.properties[option.key] = option.schema;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (option.entity) {
|
|
71
|
+
const scheme = ESchemaEditor.fromEntity(option.entity).getSchema();
|
|
72
|
+
if (option.nullable) {
|
|
73
|
+
scheme.nullable = true;
|
|
74
|
+
}
|
|
75
|
+
if (option.list) {
|
|
76
|
+
this.data.properties[option.key] = {
|
|
77
|
+
type: 'array',
|
|
78
|
+
items: scheme,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.data.properties[option.key] = scheme;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (option.required) {
|
|
86
|
+
this.data.required = [...(this.data.required || []), option.key];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
getSchema() {
|
|
92
|
+
return this.data;
|
|
93
|
+
}
|
|
94
|
+
getSKeySchema() {
|
|
95
|
+
return {
|
|
96
|
+
[this.name]: this.data,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
static fromEntity(e) {
|
|
100
|
+
const meta = getEntityMeta(e);
|
|
101
|
+
if (meta) {
|
|
102
|
+
return new ESchemaEditor(e, meta.name);
|
|
103
|
+
}
|
|
104
|
+
throw new Error('Entity metadata not found');
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Generates a JSON schema representation from a CoreEntity instance.
|
|
108
|
+
*
|
|
109
|
+
* This method inspects the entity's metadata to construct a schema object
|
|
110
|
+
* describing the entity's shape. The resulting schema contains:
|
|
111
|
+
* - `type`: always `"object"`.
|
|
112
|
+
* - `description`: a string indicating the entity name.
|
|
113
|
+
* - `required`: an array of property names that are defined on the entity.
|
|
114
|
+
* - `properties`: an object mapping each property name to an object that
|
|
115
|
+
* includes the resolved database type and its nullability.
|
|
116
|
+
*
|
|
117
|
+
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
118
|
+
*
|
|
119
|
+
* @param {T} entity - The entity instance for which to create a schema.
|
|
120
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
121
|
+
* if the entity's metadata could not be retrieved.
|
|
122
|
+
*/
|
|
123
|
+
static schemaFromEntity(entity) {
|
|
124
|
+
const schema = {
|
|
125
|
+
type: 'object',
|
|
126
|
+
properties: {},
|
|
127
|
+
};
|
|
128
|
+
const meta = getEntityMeta(entity);
|
|
129
|
+
if (!meta) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
schema.description = `Entity: ${meta.name}`;
|
|
133
|
+
schema.required = [];
|
|
134
|
+
const keys = Object.keys(entity);
|
|
135
|
+
keys.forEach((k) => {
|
|
136
|
+
const cMeta = getColumnMeta(entity, k);
|
|
137
|
+
if (cMeta && schema.properties) {
|
|
138
|
+
schema.required.push(k);
|
|
139
|
+
schema.properties[k] = {
|
|
140
|
+
type: resolveDBType(cMeta.dataType),
|
|
141
|
+
nullable: cMeta.canBeNull,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return schema;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CoreEntity } from '@grandlinex/core';
|
|
2
2
|
import { HttpStatusTypes } from '../Meta/SwaggerTypesStatic.js';
|
|
3
|
-
import { SKey, SSchemaEl, SwaggerContent, SwaggerRPathConfResponse, SwaggerRPathReqBody } from '../Meta/SwaggerTypes.js';
|
|
3
|
+
import { SKey, SSchemaEl, SSchemaElObj, SwaggerContent, SwaggerRPathConfResponse, SwaggerRPathReqBody } from '../Meta/SwaggerTypes.js';
|
|
4
|
+
import { ESchemaEditor } from './ESchemaEditor.js';
|
|
4
5
|
export default class SPathUtil {
|
|
5
6
|
/**
|
|
6
7
|
* Generates a default response mapping for the specified HTTP status types.
|
|
@@ -90,10 +91,11 @@ export default class SPathUtil {
|
|
|
90
91
|
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
91
92
|
*
|
|
92
93
|
* @param {T} entity - The entity instance for which to create a schema.
|
|
93
|
-
* @returns {
|
|
94
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
94
95
|
* if the entity's metadata could not be retrieved.
|
|
96
|
+
* @deprecated Use {@link ESchemaEditor.schemaFromEntity} instead.
|
|
95
97
|
*/
|
|
96
|
-
static schemaFromEntity<T extends CoreEntity>(entity: T):
|
|
98
|
+
static schemaFromEntity<T extends CoreEntity>(entity: T): SSchemaElObj | undefined;
|
|
97
99
|
/**
|
|
98
100
|
* 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
101
|
*
|
|
@@ -114,7 +116,7 @@ export default class SPathUtil {
|
|
|
114
116
|
* @param {CoreEntity[]} e The entities for which schema entries should be generated.
|
|
115
117
|
* @return {SKey<SSchemaEl>} An object whose keys are entity names and values are the schemas derived from those entities.
|
|
116
118
|
*/
|
|
117
|
-
static schemaEntryGen(...e: CoreEntity[]): SKey<
|
|
119
|
+
static schemaEntryGen(...e: CoreEntity[]): SKey<SSchemaElObj>;
|
|
118
120
|
/**
|
|
119
121
|
* Builds a JSON schema representation for an entity view that includes both the
|
|
120
122
|
* entity data and its related entity map.
|
|
@@ -124,76 +126,5 @@ export default class SPathUtil {
|
|
|
124
126
|
* @returns A {@link SSchemaEl} object schema with properties `i`, `dat`, and `join_map`.
|
|
125
127
|
*/
|
|
126
128
|
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>;
|
|
129
|
+
static editor<J extends CoreEntity>(e: J): ESchemaEditor<J>;
|
|
199
130
|
}
|
|
@@ -1,30 +1,6 @@
|
|
|
1
|
-
import { getEntityMeta, XUtil
|
|
1
|
+
import { getEntityMeta, XUtil } from '@grandlinex/core';
|
|
2
2
|
import map from './SUtilMap.js';
|
|
3
|
-
import {
|
|
4
|
-
function resolveDBType(dType) {
|
|
5
|
-
switch (dType) {
|
|
6
|
-
case 'int':
|
|
7
|
-
case 'long':
|
|
8
|
-
return 'integer';
|
|
9
|
-
case 'double':
|
|
10
|
-
case 'float':
|
|
11
|
-
return 'number';
|
|
12
|
-
case 'blob':
|
|
13
|
-
return 'string';
|
|
14
|
-
case 'string':
|
|
15
|
-
case 'text':
|
|
16
|
-
case 'uuid':
|
|
17
|
-
return 'string';
|
|
18
|
-
case 'boolean':
|
|
19
|
-
return 'boolean';
|
|
20
|
-
case 'date':
|
|
21
|
-
return 'string';
|
|
22
|
-
case 'json':
|
|
23
|
-
return 'object';
|
|
24
|
-
default:
|
|
25
|
-
throw Error('TypeNotSupported');
|
|
26
|
-
}
|
|
27
|
-
}
|
|
3
|
+
import { ESchemaEditor } from './ESchemaEditor.js';
|
|
28
4
|
export default class SPathUtil {
|
|
29
5
|
/**
|
|
30
6
|
* Generates a default response mapping for the specified HTTP status types.
|
|
@@ -261,32 +237,12 @@ export default class SPathUtil {
|
|
|
261
237
|
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
262
238
|
*
|
|
263
239
|
* @param {T} entity - The entity instance for which to create a schema.
|
|
264
|
-
* @returns {
|
|
240
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
265
241
|
* if the entity's metadata could not be retrieved.
|
|
242
|
+
* @deprecated Use {@link ESchemaEditor.schemaFromEntity} instead.
|
|
266
243
|
*/
|
|
267
244
|
static schemaFromEntity(entity) {
|
|
268
|
-
|
|
269
|
-
type: 'object',
|
|
270
|
-
properties: {},
|
|
271
|
-
};
|
|
272
|
-
const meta = getEntityMeta(entity);
|
|
273
|
-
if (!meta) {
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
schema.description = `Entity: ${meta.name}`;
|
|
277
|
-
schema.required = [];
|
|
278
|
-
const keys = Object.keys(entity);
|
|
279
|
-
keys.forEach((k) => {
|
|
280
|
-
const cMeta = getColumnMeta(entity, k);
|
|
281
|
-
if (cMeta && schema.properties) {
|
|
282
|
-
schema.required.push(k);
|
|
283
|
-
schema.properties[k] = {
|
|
284
|
-
type: resolveDBType(cMeta.dataType),
|
|
285
|
-
nullable: cMeta.canBeNull,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
return schema;
|
|
245
|
+
return ESchemaEditor.schemaFromEntity(entity);
|
|
290
246
|
}
|
|
291
247
|
/**
|
|
292
248
|
* 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.
|
|
@@ -303,7 +259,7 @@ export default class SPathUtil {
|
|
|
303
259
|
description: `Entity: ${meta.name}`,
|
|
304
260
|
content: {
|
|
305
261
|
'application/json': {
|
|
306
|
-
schema:
|
|
262
|
+
schema: ESchemaEditor.schemaFromEntity(entity),
|
|
307
263
|
},
|
|
308
264
|
},
|
|
309
265
|
};
|
|
@@ -319,7 +275,7 @@ export default class SPathUtil {
|
|
|
319
275
|
e.forEach((el) => {
|
|
320
276
|
const meta = getEntityMeta(el);
|
|
321
277
|
if (meta) {
|
|
322
|
-
out[meta.name] =
|
|
278
|
+
out[meta.name] = ESchemaEditor.schemaFromEntity(el);
|
|
323
279
|
}
|
|
324
280
|
});
|
|
325
281
|
return out;
|
|
@@ -339,140 +295,12 @@ export default class SPathUtil {
|
|
|
339
295
|
i: {
|
|
340
296
|
type: 'integer',
|
|
341
297
|
},
|
|
342
|
-
dat:
|
|
343
|
-
join_map:
|
|
298
|
+
dat: ESchemaEditor.schemaFromEntity(entity),
|
|
299
|
+
join_map: ESchemaEditor.schemaFromEntity(entityMap),
|
|
344
300
|
},
|
|
345
301
|
};
|
|
346
302
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
*
|
|
350
|
-
* @param {CoreEntity} entity
|
|
351
|
-
* The entity for which the schema should be extended.
|
|
352
|
-
*
|
|
353
|
-
* @param {...Object} options
|
|
354
|
-
* One or more objects defining schema extensions. Each object may contain:
|
|
355
|
-
* - `key` (string): The property key to add or extend.
|
|
356
|
-
* - `list` (boolean, optional): Indicates whether the property is a list.
|
|
357
|
-
* - `entity` (CoreEntity, optional): The entity type for the property.
|
|
358
|
-
* - `schema` (SSchemaEl, optional): A custom schema definition for the property.
|
|
359
|
-
* - `required` (boolean, optional): Whether the property is required.
|
|
360
|
-
*
|
|
361
|
-
* @return {SSchemaEl}
|
|
362
|
-
* The resulting schema element. If a single property is returned by
|
|
363
|
-
* `extendEntitySchema`, its schema is returned directly; otherwise an
|
|
364
|
-
* object schema with a type of `'object'` is returned.
|
|
365
|
-
*/
|
|
366
|
-
static extendEntitySchemaObject(entity, ...options) {
|
|
367
|
-
const schema = this.extendEntitySchema(entity, ...options);
|
|
368
|
-
const ent = Object.entries(schema);
|
|
369
|
-
if (ent.length === 1) {
|
|
370
|
-
return ent[0][1];
|
|
371
|
-
}
|
|
372
|
-
return {
|
|
373
|
-
type: 'object',
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Extends the schema of a given {@link CoreEntity} with additional properties.
|
|
378
|
-
*
|
|
379
|
-
* @param {CoreEntity} entity
|
|
380
|
-
* The entity whose schema will be extended.
|
|
381
|
-
*
|
|
382
|
-
* @param {...{
|
|
383
|
-
* key: string,
|
|
384
|
-
* list?: boolean,
|
|
385
|
-
* entity?: CoreEntity,
|
|
386
|
-
* schema?: SSchemaEl,
|
|
387
|
-
* required?: boolean
|
|
388
|
-
* }} options
|
|
389
|
-
* One or more option objects specifying the extensions to apply. Each option
|
|
390
|
-
* may provide either a direct schema (`schema`) or an entity reference
|
|
391
|
-
* (`entity`). The `list` flag indicates whether the property should be
|
|
392
|
-
* represented as an array of the provided schema. The `required` flag
|
|
393
|
-
* adds the property to the schema’s required list.
|
|
394
|
-
*
|
|
395
|
-
* @returns {SKey<SSchemaEl>}
|
|
396
|
-
* An object containing the updated schema for the entity, keyed by the
|
|
397
|
-
* entity’s name. If the entity metadata cannot be found, an empty
|
|
398
|
-
* object is returned.
|
|
399
|
-
*/
|
|
400
|
-
static extendEntitySchema(entity, ...options) {
|
|
401
|
-
const meta = getEntityMeta(entity);
|
|
402
|
-
if (meta) {
|
|
403
|
-
const schema = SPathUtil.schemaEntryGen(entity)[meta.name];
|
|
404
|
-
if (schema && !isSwaggerRef(schema) && schema.properties) {
|
|
405
|
-
for (const option of options) {
|
|
406
|
-
if (option.schema) {
|
|
407
|
-
if (option.list) {
|
|
408
|
-
schema.properties[option.key] = {
|
|
409
|
-
type: 'array',
|
|
410
|
-
items: option.schema,
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
schema.properties[option.key] = option.schema;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
else if (option.entity) {
|
|
418
|
-
const eMeta = getEntityMeta(option.entity);
|
|
419
|
-
if (eMeta) {
|
|
420
|
-
const scheme = SPathUtil.schemaEntryGen(option.entity)[eMeta.name];
|
|
421
|
-
if (option.list) {
|
|
422
|
-
schema.properties[option.key] = {
|
|
423
|
-
type: 'array',
|
|
424
|
-
items: scheme,
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
schema.properties[option.key] = scheme;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (option.required) {
|
|
433
|
-
schema.required = [...(schema.required || []), option.key];
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return {
|
|
438
|
-
[meta.name]: schema,
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
return {};
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Reduces the entity schema to a single schema element or a generic object.
|
|
445
|
-
*
|
|
446
|
-
* @param entity The entity whose schema should be reduced.
|
|
447
|
-
* @param keys Optional list of keys to include in the reduced schema. If omitted, all keys are considered.
|
|
448
|
-
* @return Returns the schema element of the sole key if only one key is present; otherwise, returns a generic object schema with type `'object'`. */
|
|
449
|
-
static reduceEntitySchemaObject(entity, ...keys) {
|
|
450
|
-
const schema = this.reduceEntitySchema(entity, ...keys);
|
|
451
|
-
const ent = Object.entries(schema);
|
|
452
|
-
if (ent.length === 1) {
|
|
453
|
-
return ent[0][1];
|
|
454
|
-
}
|
|
455
|
-
return {
|
|
456
|
-
type: 'object',
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Creates a reduced version of an entity's schema by excluding specified properties.
|
|
461
|
-
*
|
|
462
|
-
* @param {CoreEntity} entity - The entity whose schema is to be processed.
|
|
463
|
-
* @param {...string} keys - Property names to remove from the schema's `properties` and `required` lists.
|
|
464
|
-
*
|
|
465
|
-
* @returns */
|
|
466
|
-
static reduceEntitySchema(entity, ...keys) {
|
|
467
|
-
const meta = getEntityMeta(entity);
|
|
468
|
-
if (meta) {
|
|
469
|
-
const schema = SPathUtil.schemaEntryGen(entity)[meta.name];
|
|
470
|
-
if (schema && !isSwaggerRef(schema) && schema.properties) {
|
|
471
|
-
schema.properties = Object.fromEntries(Object.entries(schema.properties).filter(([e]) => !keys.includes(e)));
|
|
472
|
-
schema.required = (schema.required || []).filter((e) => !keys.includes(e));
|
|
473
|
-
}
|
|
474
|
-
return { [meta.name]: schema };
|
|
475
|
-
}
|
|
476
|
-
return {};
|
|
303
|
+
static editor(e) {
|
|
304
|
+
return ESchemaEditor.fromEntity(e);
|
|
477
305
|
}
|
|
478
306
|
}
|
package/dist/mjs/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import SwaggerUtil from './Swagger/SwaggerUtil.js';
|
|
2
2
|
import SPathUtil from './Swagger/Path/SPathUtil.js';
|
|
3
3
|
import SwaggerClient from './Swagger/Client/SwaggerClient.js';
|
|
4
|
+
export * from './Swagger/Path/ESchemaEditor.js';
|
|
4
5
|
export * from './Swagger/debug/index.js';
|
|
5
6
|
export * from './Swagger/annotation/index.js';
|
|
6
7
|
export * from './Swagger/Meta/Swagger.js';
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import SwaggerUtil from './Swagger/SwaggerUtil.js';
|
|
2
2
|
import SPathUtil from './Swagger/Path/SPathUtil.js';
|
|
3
3
|
import SwaggerClient from './Swagger/Client/SwaggerClient.js';
|
|
4
|
+
export * from './Swagger/Path/ESchemaEditor.js';
|
|
4
5
|
export * from './Swagger/debug/index.js';
|
|
5
6
|
export * from './Swagger/annotation/index.js';
|
|
6
7
|
export * from './Swagger/Meta/Swagger.js';
|