swaggular 1.0.3 → 1.0.6
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/README.md
CHANGED
|
@@ -59,7 +59,49 @@ You can configure Swaggular using CLI arguments or a configuration file (e.g., `
|
|
|
59
59
|
|
|
60
60
|
### Configuration File (`SwaggularConfig`)
|
|
61
61
|
|
|
62
|
-
You can create a `swaggular.config.json` file in your project root to define advanced configurations
|
|
62
|
+
You can create a `swaggular.config.json` file in your project root to define advanced configurations. Below is the detailed structure of the configuration object.
|
|
63
|
+
|
|
64
|
+
#### **Properties**
|
|
65
|
+
|
|
66
|
+
- **`input`** _(optional)_: `string` - Path to the input Swagger/OpenAPI file (e.g., `swagger.json`). If not specified, defaults to the basic input.
|
|
67
|
+
- **`output`** _(optional)_: `string` - Path to the output directory where generated files will be saved. Default: `results`.
|
|
68
|
+
- **`groupingMode`** _(optional)_: `'tags' | 'path'` - Strategy for grouping services.
|
|
69
|
+
- `'tags'`: Groups operations based on their Swagger tags.
|
|
70
|
+
- `'path'`: Groups operations based on their URL path segments.
|
|
71
|
+
- **`segmentsToIgnore`** _(optional)_: `string[]` - List of URL segments to ignore when generating service names (e.g., `['api', 'v1']`).
|
|
72
|
+
- **`types`** _(optional)_: `object` - Configuration for type generation and inheritance.
|
|
73
|
+
- **`extendsFrom`** _(optional)_: `InterfaceData[]` - Defines base classes that generated DTOs should extend if they match the properties.
|
|
74
|
+
- **`generic`** _(optional)_: `InterfaceData[]` - Defines generic types to replace duplicate generated classes (e.g., `PagedResultDto<T>`).
|
|
75
|
+
- **`templates`** _(optional)_: `object` - Configuration for templates used in generation.
|
|
76
|
+
- **`service`** _(optional)_: `object` - Service template configuration.
|
|
77
|
+
- **`path`**: `string` - Path to the internal or custom service template file (e.g., `'templates/angular-service.template'`).
|
|
78
|
+
- **`httpParamsHandler`** _(optional)_: `string` - Custom logic to handle HTTP parameters (e.g., `'const params = HttpHelper.toHttpParams(${params} || {});'`). The `${params}` is a placeholder for the query parameters, it MUST exist in the string.
|
|
79
|
+
- **`httpParamsHandlerImport`** _(optional)_: `string` - Import statement for the custom parameters handler (e.g., `'import { HttpHelper } from "@shared/utils";'`).
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
#### **Object Structures**
|
|
84
|
+
|
|
85
|
+
**`InterfaceData`**
|
|
86
|
+
|
|
87
|
+
Used in `types.extendsFrom` and `types.generic`.
|
|
88
|
+
|
|
89
|
+
- **`name`**: `string` - The name of the interface or class.
|
|
90
|
+
- **`imports`**: `string[]` - List of imports required for this interface. It has to be the name of other generated interfaces, e.g. `['SomeProperty']`.
|
|
91
|
+
- **`type`**: `'interface' | 'enum' | 'type'` - The kind of TypeScript structure.
|
|
92
|
+
- **`properties`**: `InterfaceDataProperty[]` - Array of properties belonging to this interface.
|
|
93
|
+
- **`extendsFrom`** _(optional)_: `string[]` - Names of other interfaces this one extends. It has to be the name of other generated interfaces, e.g. `['SomeProperty']`.
|
|
94
|
+
|
|
95
|
+
**`InterfaceDataProperty`**
|
|
96
|
+
|
|
97
|
+
Used in `InterfaceData.properties`.
|
|
98
|
+
|
|
99
|
+
- **`name`**: `string` - The name of the property.
|
|
100
|
+
- **`type`**: `string` - The TypeScript type of the property (e.g., `'string'`, `'number'`, `'T[]'`).
|
|
101
|
+
- **`optional`**: `boolean` - Whether the property is optional (`?`).
|
|
102
|
+
- **`comments`**: `string` - JSDoc comments or other annotations for the property. It must be surrounded by the `/** ... */` .
|
|
103
|
+
|
|
104
|
+
---
|
|
63
105
|
|
|
64
106
|
#### Example `swaggular.config.json`
|
|
65
107
|
|
|
@@ -95,6 +137,7 @@ You can create a `swaggular.config.json` file in your project root to define adv
|
|
|
95
137
|
},
|
|
96
138
|
"templates": {
|
|
97
139
|
"service": {
|
|
140
|
+
"path": "templates/angular-service.template",
|
|
98
141
|
"httpParamsHandlerImport": "import { HttpHelper } from '@shared/utils/http-helper';",
|
|
99
142
|
"httpParamsHandler": "const params = HttpHelper.toHttpParams(${params} || {});"
|
|
100
143
|
}
|
|
@@ -106,7 +149,7 @@ This configuration allows you to:
|
|
|
106
149
|
|
|
107
150
|
1. **extendsFrom**: Automatically make generated DTOs extend a base class (e.g., `PagedRequestDto`) if they share the same properties.
|
|
108
151
|
2. **generic**: Automatically detect and use generic types (e.g., `PagedResultDto<T>`) instead of generating duplicate classes like `PagedResultDtoOfUser`.
|
|
109
|
-
3. **templates.service
|
|
152
|
+
3. **templates.service**: Customize how HTTP parameters are handled in generated services, allowing you to use your own helper functions.
|
|
110
153
|
|
|
111
154
|
## Development
|
|
112
155
|
|
|
@@ -210,23 +210,6 @@ function buildMethodName(method, path, groupedPath, usedNames, serviceName) {
|
|
|
210
210
|
nameParts.push('ById');
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
|
-
const normalizedPath = path.toLowerCase();
|
|
214
|
-
const suffixes = [
|
|
215
|
-
{ pattern: '/all', label: 'All' },
|
|
216
|
-
{ pattern: '/search', label: 'Search' },
|
|
217
|
-
{ pattern: '/stats', label: 'Stats' },
|
|
218
|
-
{ pattern: '/history', label: 'History' },
|
|
219
|
-
{ pattern: '/summary', label: 'Summary' },
|
|
220
|
-
{ pattern: '/details', label: 'Details' },
|
|
221
|
-
];
|
|
222
|
-
for (const suffix of suffixes) {
|
|
223
|
-
if (normalizedPath.endsWith(suffix.pattern)) {
|
|
224
|
-
if (!nameParts.join('').endsWith(suffix.label)) {
|
|
225
|
-
nameParts.push(suffix.label);
|
|
226
|
-
}
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
213
|
let candidateName = (0, string_utils_1.lowerFirst)(nameParts.join(''));
|
|
231
214
|
if (candidateName === 'getById' && !usedNames.includes('get')) {
|
|
232
215
|
candidateName = 'get';
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { OpenAPIV3 } from 'openapi-types';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a JSON schema object to a TypeScript type string.
|
|
4
|
+
* @param schema The JSON schema object or reference object.
|
|
5
|
+
* @returns The corresponding TypeScript type.
|
|
6
|
+
*/
|
|
7
|
+
export declare function switchTypeJson(schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined): string;
|
|
@@ -1,44 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildTypes = buildTypes;
|
|
4
3
|
exports.switchTypeJson = switchTypeJson;
|
|
5
4
|
const interface_state_1 = require("../core/state/interface-state");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (formData) {
|
|
13
|
-
return 'FormData';
|
|
14
|
-
}
|
|
15
|
-
return defaultType;
|
|
16
|
-
}
|
|
17
|
-
const schema = jsonSchema?.['schema'];
|
|
18
|
-
if (!schema || Object.keys(schema).length === 0)
|
|
19
|
-
return defaultType;
|
|
20
|
-
const ref = schema?.['$ref'];
|
|
21
|
-
if (ref) {
|
|
22
|
-
const name = ref.split('/').pop();
|
|
23
|
-
return interface_state_1.interfaceState.getTypeMapping(name) || name;
|
|
24
|
-
}
|
|
25
|
-
return switchTypeJson(schema);
|
|
26
|
-
}
|
|
5
|
+
const type_guard_1 = require("./type-guard");
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON schema object to a TypeScript type string.
|
|
8
|
+
* @param schema The JSON schema object or reference object.
|
|
9
|
+
* @returns The corresponding TypeScript type.
|
|
10
|
+
*/
|
|
27
11
|
function switchTypeJson(schema) {
|
|
28
12
|
if (!schema || Object.keys(schema).length === 0)
|
|
29
13
|
return 'any';
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const format = schema?.['format'];
|
|
33
|
-
if (ref) {
|
|
34
|
-
const name = ref.split('/').pop();
|
|
14
|
+
if ((0, type_guard_1.isReference)(schema)) {
|
|
15
|
+
const name = schema.$ref.split('/').pop();
|
|
35
16
|
return interface_state_1.interfaceState.getTypeMapping(name) || name;
|
|
36
17
|
}
|
|
18
|
+
const type = schema.type;
|
|
19
|
+
const format = schema.format;
|
|
37
20
|
if (type === 'object') {
|
|
38
21
|
return 'any';
|
|
39
22
|
}
|
|
40
23
|
if (type === 'array') {
|
|
41
|
-
return switchTypeJson(schema
|
|
24
|
+
return switchTypeJson(schema.items) + '[]';
|
|
42
25
|
}
|
|
43
26
|
if (type === 'number') {
|
|
44
27
|
return 'number';
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.kebabToCamelCase = kebabToCamelCase;
|
|
4
3
|
exports.kebabToPascalCase = kebabToPascalCase;
|
|
5
4
|
exports.removeAllWhitespace = removeAllWhitespace;
|
|
6
5
|
exports.lowerFirst = lowerFirst;
|
|
@@ -8,9 +7,6 @@ exports.upFirst = upFirst;
|
|
|
8
7
|
exports.toKebabCase = toKebabCase;
|
|
9
8
|
exports.normalizeSegment = normalizeSegment;
|
|
10
9
|
exports.toPascalCase = toPascalCase;
|
|
11
|
-
function kebabToCamelCase(str) {
|
|
12
|
-
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
13
|
-
}
|
|
14
10
|
function kebabToPascalCase(str) {
|
|
15
11
|
const camel = str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
16
12
|
return camel.charAt(0).toUpperCase() + camel.slice(1);
|