@grandlinex/swagger-mate 1.1.1 → 1.2.0

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.
@@ -182,8 +182,8 @@ function createPackage(name, version, module) {
182
182
  module: module ? 'dist/index.js' : undefined,
183
183
  type: module ? 'module' : undefined,
184
184
  dependencies: {
185
- 'form-data': '4.0.3',
186
- axios: '1.10.0',
185
+ 'form-data': '4.0.4',
186
+ axios: '1.11.0',
187
187
  },
188
188
  devDependencies: {
189
189
  '@types/node': '22.15.32',
@@ -1,11 +1,13 @@
1
1
  import { ObjectLike } from '@grandlinex/core';
2
2
  import { Server } from 'net';
3
- import { MergeInputType, SwaggerConfig } from './Meta/SwaggerTypes.js';
3
+ import { MergeInputType, SwaggerConfig, SwaggerRPath } from './Meta/SwaggerTypes.js';
4
+ import { RouteData } from './annotation/index.js';
4
5
  export default class SwaggerUtil {
5
6
  static writeMeta(conf: SwaggerConfig, kind: 'JSON' | 'YAML', path?: string): void;
6
7
  static readMeta(path: string): any;
7
8
  static serveMeta(conf: SwaggerConfig, port?: number, auth?: string): Promise<Server | null>;
8
9
  static metaExtractor(root: ObjectLike, npmPackageVersion: boolean, ...path: ObjectLike[]): SwaggerConfig | undefined;
10
+ static routeToSwaggerPath(route: RouteData): SwaggerRPath;
9
11
  static merge(root: SwaggerConfig, data: MergeInputType[]): SwaggerConfig;
10
12
  static mergeConfig(root: SwaggerConfig, data: SwaggerConfig[]): SwaggerConfig;
11
13
  }
@@ -44,6 +44,8 @@ const express_1 = __importDefault(require("express"));
44
44
  const process = __importStar(require("process"));
45
45
  const Swagger_js_1 = require("./Meta/Swagger.js");
46
46
  const PathHelp_js_1 = __importStar(require("../PathHelp.js"));
47
+ const index_js_1 = require("./annotation/index.js");
48
+ const index_js_2 = require("../index.js");
47
49
  class SwaggerUtil {
48
50
  static writeMeta(conf, kind, path) {
49
51
  if (kind === 'JSON') {
@@ -90,10 +92,89 @@ class SwaggerUtil {
90
92
  rootMeta.info.version = version;
91
93
  }
92
94
  }
93
- return this.merge(rootMeta, path.map((el) => ({
94
- path: (0, Swagger_js_1.getSPath)(el),
95
- comp: (0, Swagger_js_1.getSComponent)(el),
96
- })));
95
+ return this.merge(rootMeta, path.map((el) => {
96
+ const sPath = (0, Swagger_js_1.getSPath)(el);
97
+ const meta = (0, index_js_1.getRouteMeta)(el);
98
+ let sMeta;
99
+ if (meta) {
100
+ sMeta = {
101
+ path: this.routeToSwaggerPath(meta),
102
+ };
103
+ }
104
+ return {
105
+ path: sPath || sMeta,
106
+ comp: (0, Swagger_js_1.getSComponent)(el),
107
+ };
108
+ }));
109
+ }
110
+ static routeToSwaggerPath(route) {
111
+ const conf = {
112
+ responses: route.meta?.responses,
113
+ description: route.meta?.description,
114
+ summary: route.meta?.summary,
115
+ tags: route.meta?.tags,
116
+ operationId: route.meta?.operationId,
117
+ requestBody: route.meta?.requestBody,
118
+ parameters: route.meta?.parameters,
119
+ };
120
+ if (route.meta) {
121
+ if (route.meta.requestSchema && !conf.requestBody) {
122
+ conf.requestBody = index_js_2.SPathUtil.jsonBody(route.meta.requestSchema);
123
+ }
124
+ if (route.meta.responseSchema && !conf.responses) {
125
+ const ax = route.meta.responseCodes?.slice(1) || [];
126
+ conf.responses = index_js_2.SPathUtil.jsonResponse(route.meta.responseCodes?.[0] || '200', route.meta.responseSchema, route.meta.responseType === 'LIST', ...ax);
127
+ }
128
+ else if (route.meta.responseCodes) {
129
+ conf.responses = index_js_2.SPathUtil.defaultResponse(...route.meta.responseCodes);
130
+ }
131
+ else {
132
+ conf.responses = index_js_2.SPathUtil.defaultResponse('200');
133
+ }
134
+ }
135
+ const convertedPath = route.meta?.pathOverride ||
136
+ route.path
137
+ .split('/')
138
+ .map((e) => {
139
+ if (e.startsWith(':')) {
140
+ return `{${e.substring(1)}}`;
141
+ }
142
+ return e;
143
+ })
144
+ .join('/');
145
+ const path = {};
146
+ switch (route.type) {
147
+ case 'POST':
148
+ path[convertedPath] = {
149
+ post: conf,
150
+ };
151
+ break;
152
+ case 'GET':
153
+ path[convertedPath] = {
154
+ get: conf,
155
+ };
156
+ break;
157
+ case 'PATCH':
158
+ path[convertedPath] = {
159
+ patch: conf,
160
+ };
161
+ break;
162
+ case 'DELETE':
163
+ path[convertedPath] = {
164
+ delete: conf,
165
+ };
166
+ break;
167
+ case 'USE':
168
+ path[convertedPath] = {
169
+ get: conf,
170
+ patch: conf,
171
+ post: conf,
172
+ delete: conf,
173
+ };
174
+ break;
175
+ default:
176
+ }
177
+ return path;
97
178
  }
98
179
  static merge(root, data) {
99
180
  const out = root;
@@ -0,0 +1,26 @@
1
+ import 'reflect-metadata';
2
+ import { ObjectLike } from '@grandlinex/core';
3
+ import { SSchemaEl, SwaggerRPathConf } from '../Meta/SwaggerTypes.js';
4
+ import { HttpStatusTypes } from '../Meta/SwaggerTypesStatic.js';
5
+ export declare enum ActionMode {
6
+ 'DEFAULT' = 0,
7
+ 'DMZ' = 1,
8
+ 'DMZ_WITH_USER' = 2
9
+ }
10
+ export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
11
+ export type ResponseTypes = 'LIST';
12
+ export type RouteMeta = {
13
+ pathOverride?: string;
14
+ mode?: ActionMode;
15
+ requestSchema?: SSchemaEl;
16
+ responseSchema?: SSchemaEl;
17
+ responseType?: ResponseTypes;
18
+ responseCodes?: HttpStatusTypes[];
19
+ } & SwaggerRPathConf;
20
+ export type RouteData = {
21
+ type: ActionTypes;
22
+ path: string;
23
+ meta?: RouteMeta;
24
+ };
25
+ export declare const Route: (type: RouteData["type"], path: RouteData["path"], meta: RouteMeta) => ClassDecorator;
26
+ export declare function getRouteMeta<T extends ObjectLike>(target: T): RouteData | undefined;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Route = exports.ActionMode = void 0;
4
+ exports.getRouteMeta = getRouteMeta;
5
+ require("reflect-metadata");
6
+ var ActionMode;
7
+ (function (ActionMode) {
8
+ ActionMode[ActionMode["DEFAULT"] = 0] = "DEFAULT";
9
+ ActionMode[ActionMode["DMZ"] = 1] = "DMZ";
10
+ ActionMode[ActionMode["DMZ_WITH_USER"] = 2] = "DMZ_WITH_USER";
11
+ })(ActionMode || (exports.ActionMode = ActionMode = {}));
12
+ const routeKey = Symbol('route');
13
+ const Route = (type, path, meta) => {
14
+ return (target) => {
15
+ const metadata = {
16
+ type,
17
+ path,
18
+ meta,
19
+ };
20
+ Reflect.defineMetadata(routeKey, metadata, target.prototype);
21
+ };
22
+ };
23
+ exports.Route = Route;
24
+ function getRouteMeta(target) {
25
+ return Reflect.getMetadata(routeKey, target.constructor.prototype);
26
+ }
@@ -2,6 +2,7 @@ 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
4
  export * from './Swagger/debug/index.js';
5
+ export * from './Swagger/annotation/index.js';
5
6
  export * from './Swagger/Meta/Swagger.js';
6
7
  export * from './Swagger/Meta/SwaggerTypes.js';
7
8
  export * from './Swagger/Meta/SwaggerTypesStatic.js';
package/dist/cjs/index.js CHANGED
@@ -25,6 +25,7 @@ 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
27
  __exportStar(require("./Swagger/debug/index.js"), exports);
28
+ __exportStar(require("./Swagger/annotation/index.js"), exports);
28
29
  __exportStar(require("./Swagger/Meta/Swagger.js"), exports);
29
30
  __exportStar(require("./Swagger/Meta/SwaggerTypes.js"), exports);
30
31
  __exportStar(require("./Swagger/Meta/SwaggerTypesStatic.js"), exports);
@@ -144,8 +144,8 @@ function createPackage(name, version, module) {
144
144
  module: module ? 'dist/index.js' : undefined,
145
145
  type: module ? 'module' : undefined,
146
146
  dependencies: {
147
- 'form-data': '4.0.3',
148
- axios: '1.10.0',
147
+ 'form-data': '4.0.4',
148
+ axios: '1.11.0',
149
149
  },
150
150
  devDependencies: {
151
151
  '@types/node': '22.15.32',
@@ -1,11 +1,13 @@
1
1
  import { ObjectLike } from '@grandlinex/core';
2
2
  import { Server } from 'net';
3
- import { MergeInputType, SwaggerConfig } from './Meta/SwaggerTypes.js';
3
+ import { MergeInputType, SwaggerConfig, SwaggerRPath } from './Meta/SwaggerTypes.js';
4
+ import { RouteData } from './annotation/index.js';
4
5
  export default class SwaggerUtil {
5
6
  static writeMeta(conf: SwaggerConfig, kind: 'JSON' | 'YAML', path?: string): void;
6
7
  static readMeta(path: string): any;
7
8
  static serveMeta(conf: SwaggerConfig, port?: number, auth?: string): Promise<Server | null>;
8
9
  static metaExtractor(root: ObjectLike, npmPackageVersion: boolean, ...path: ObjectLike[]): SwaggerConfig | undefined;
10
+ static routeToSwaggerPath(route: RouteData): SwaggerRPath;
9
11
  static merge(root: SwaggerConfig, data: MergeInputType[]): SwaggerConfig;
10
12
  static mergeConfig(root: SwaggerConfig, data: SwaggerConfig[]): SwaggerConfig;
11
13
  }
@@ -6,6 +6,8 @@ import express from 'express';
6
6
  import * as process from 'process';
7
7
  import { getSComponent, getSPath, getSwaggerMeta } from './Meta/Swagger.js';
8
8
  import PathHelp, { getBaseFolder } from '../PathHelp.js';
9
+ import { getRouteMeta } from './annotation/index.js';
10
+ import { SPathUtil } from '../index.js';
9
11
  export default class SwaggerUtil {
10
12
  static writeMeta(conf, kind, path) {
11
13
  if (kind === 'JSON') {
@@ -52,10 +54,89 @@ export default class SwaggerUtil {
52
54
  rootMeta.info.version = version;
53
55
  }
54
56
  }
55
- return this.merge(rootMeta, path.map((el) => ({
56
- path: getSPath(el),
57
- comp: getSComponent(el),
58
- })));
57
+ return this.merge(rootMeta, path.map((el) => {
58
+ const sPath = getSPath(el);
59
+ const meta = getRouteMeta(el);
60
+ let sMeta;
61
+ if (meta) {
62
+ sMeta = {
63
+ path: this.routeToSwaggerPath(meta),
64
+ };
65
+ }
66
+ return {
67
+ path: sPath || sMeta,
68
+ comp: getSComponent(el),
69
+ };
70
+ }));
71
+ }
72
+ static routeToSwaggerPath(route) {
73
+ const conf = {
74
+ responses: route.meta?.responses,
75
+ description: route.meta?.description,
76
+ summary: route.meta?.summary,
77
+ tags: route.meta?.tags,
78
+ operationId: route.meta?.operationId,
79
+ requestBody: route.meta?.requestBody,
80
+ parameters: route.meta?.parameters,
81
+ };
82
+ if (route.meta) {
83
+ if (route.meta.requestSchema && !conf.requestBody) {
84
+ conf.requestBody = SPathUtil.jsonBody(route.meta.requestSchema);
85
+ }
86
+ if (route.meta.responseSchema && !conf.responses) {
87
+ const ax = route.meta.responseCodes?.slice(1) || [];
88
+ conf.responses = SPathUtil.jsonResponse(route.meta.responseCodes?.[0] || '200', route.meta.responseSchema, route.meta.responseType === 'LIST', ...ax);
89
+ }
90
+ else if (route.meta.responseCodes) {
91
+ conf.responses = SPathUtil.defaultResponse(...route.meta.responseCodes);
92
+ }
93
+ else {
94
+ conf.responses = SPathUtil.defaultResponse('200');
95
+ }
96
+ }
97
+ const convertedPath = route.meta?.pathOverride ||
98
+ route.path
99
+ .split('/')
100
+ .map((e) => {
101
+ if (e.startsWith(':')) {
102
+ return `{${e.substring(1)}}`;
103
+ }
104
+ return e;
105
+ })
106
+ .join('/');
107
+ const path = {};
108
+ switch (route.type) {
109
+ case 'POST':
110
+ path[convertedPath] = {
111
+ post: conf,
112
+ };
113
+ break;
114
+ case 'GET':
115
+ path[convertedPath] = {
116
+ get: conf,
117
+ };
118
+ break;
119
+ case 'PATCH':
120
+ path[convertedPath] = {
121
+ patch: conf,
122
+ };
123
+ break;
124
+ case 'DELETE':
125
+ path[convertedPath] = {
126
+ delete: conf,
127
+ };
128
+ break;
129
+ case 'USE':
130
+ path[convertedPath] = {
131
+ get: conf,
132
+ patch: conf,
133
+ post: conf,
134
+ delete: conf,
135
+ };
136
+ break;
137
+ default:
138
+ }
139
+ return path;
59
140
  }
60
141
  static merge(root, data) {
61
142
  const out = root;
@@ -0,0 +1,26 @@
1
+ import 'reflect-metadata';
2
+ import { ObjectLike } from '@grandlinex/core';
3
+ import { SSchemaEl, SwaggerRPathConf } from '../Meta/SwaggerTypes.js';
4
+ import { HttpStatusTypes } from '../Meta/SwaggerTypesStatic.js';
5
+ export declare enum ActionMode {
6
+ 'DEFAULT' = 0,
7
+ 'DMZ' = 1,
8
+ 'DMZ_WITH_USER' = 2
9
+ }
10
+ export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
11
+ export type ResponseTypes = 'LIST';
12
+ export type RouteMeta = {
13
+ pathOverride?: string;
14
+ mode?: ActionMode;
15
+ requestSchema?: SSchemaEl;
16
+ responseSchema?: SSchemaEl;
17
+ responseType?: ResponseTypes;
18
+ responseCodes?: HttpStatusTypes[];
19
+ } & SwaggerRPathConf;
20
+ export type RouteData = {
21
+ type: ActionTypes;
22
+ path: string;
23
+ meta?: RouteMeta;
24
+ };
25
+ export declare const Route: (type: RouteData["type"], path: RouteData["path"], meta: RouteMeta) => ClassDecorator;
26
+ export declare function getRouteMeta<T extends ObjectLike>(target: T): RouteData | undefined;
@@ -0,0 +1,21 @@
1
+ import 'reflect-metadata';
2
+ export var ActionMode;
3
+ (function (ActionMode) {
4
+ ActionMode[ActionMode["DEFAULT"] = 0] = "DEFAULT";
5
+ ActionMode[ActionMode["DMZ"] = 1] = "DMZ";
6
+ ActionMode[ActionMode["DMZ_WITH_USER"] = 2] = "DMZ_WITH_USER";
7
+ })(ActionMode || (ActionMode = {}));
8
+ const routeKey = Symbol('route');
9
+ export const Route = (type, path, meta) => {
10
+ return (target) => {
11
+ const metadata = {
12
+ type,
13
+ path,
14
+ meta,
15
+ };
16
+ Reflect.defineMetadata(routeKey, metadata, target.prototype);
17
+ };
18
+ };
19
+ export function getRouteMeta(target) {
20
+ return Reflect.getMetadata(routeKey, target.constructor.prototype);
21
+ }
@@ -2,6 +2,7 @@ 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
4
  export * from './Swagger/debug/index.js';
5
+ export * from './Swagger/annotation/index.js';
5
6
  export * from './Swagger/Meta/Swagger.js';
6
7
  export * from './Swagger/Meta/SwaggerTypes.js';
7
8
  export * from './Swagger/Meta/SwaggerTypesStatic.js';
package/dist/mjs/index.js CHANGED
@@ -2,6 +2,7 @@ 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
4
  export * from './Swagger/debug/index.js';
5
+ export * from './Swagger/annotation/index.js';
5
6
  export * from './Swagger/Meta/Swagger.js';
6
7
  export * from './Swagger/Meta/SwaggerTypes.js';
7
8
  export * from './Swagger/Meta/SwaggerTypesStatic.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grandlinex/swagger-mate",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -38,14 +38,15 @@
38
38
  "swagger-mate-esm": "./dist/mjs/cli.js"
39
39
  },
40
40
  "dependencies": {
41
- "@grandlinex/core": "1.1.1",
41
+ "@grandlinex/core": "1.2.0",
42
42
  "express": "5.1.0",
43
- "form-data": "4.0.3",
43
+ "form-data": "4.0.4",
44
44
  "js-yaml": "4.1.0",
45
45
  "reflect-metadata": "0.2.2"
46
46
  },
47
47
  "devDependencies": {
48
48
  "axios": "1.11.0",
49
+ "node-fetch": "3.3.2",
49
50
  "cross-env": "10.0.0",
50
51
  "@types/express": "5.0.3",
51
52
  "@types/js-yaml": "4.0.9",