@grandlinex/swagger-mate 1.1.1 → 1.2.1

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,95 @@ 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
+ if (typeof route.meta.responseSchema === 'string' ||
127
+ (0, core_1.instanceOfEntity)(route.meta.responseSchema)) {
128
+ conf.responses = index_js_2.SPathUtil.refResponse(route.meta.responseCodes?.[0] || '200', route.meta.responseSchema, route.meta.responseType === 'LIST', ...ax);
129
+ }
130
+ else {
131
+ conf.responses = index_js_2.SPathUtil.jsonResponse(route.meta.responseCodes?.[0] || '200', route.meta.responseSchema, route.meta.responseType === 'LIST', ...ax);
132
+ }
133
+ }
134
+ else if (route.meta.responseCodes) {
135
+ conf.responses = index_js_2.SPathUtil.defaultResponse(...route.meta.responseCodes);
136
+ }
137
+ else {
138
+ conf.responses = index_js_2.SPathUtil.defaultResponse('200');
139
+ }
140
+ }
141
+ const convertedPath = route.meta?.pathOverride ||
142
+ route.path
143
+ .split('/')
144
+ .map((e) => {
145
+ if (e.startsWith(':')) {
146
+ return `{${e.substring(1)}}`;
147
+ }
148
+ return e;
149
+ })
150
+ .join('/');
151
+ const path = {};
152
+ switch (route.type) {
153
+ case 'POST':
154
+ path[convertedPath] = {
155
+ post: conf,
156
+ };
157
+ break;
158
+ case 'GET':
159
+ path[convertedPath] = {
160
+ get: conf,
161
+ };
162
+ break;
163
+ case 'PATCH':
164
+ path[convertedPath] = {
165
+ patch: conf,
166
+ };
167
+ break;
168
+ case 'DELETE':
169
+ path[convertedPath] = {
170
+ delete: conf,
171
+ };
172
+ break;
173
+ case 'USE':
174
+ path[convertedPath] = {
175
+ get: conf,
176
+ patch: conf,
177
+ post: conf,
178
+ delete: conf,
179
+ };
180
+ break;
181
+ default:
182
+ }
183
+ return path;
97
184
  }
98
185
  static merge(root, data) {
99
186
  const out = root;
@@ -0,0 +1,26 @@
1
+ import 'reflect-metadata';
2
+ import { CoreEntity, 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 | CoreEntity | string;
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
  }
@@ -1,11 +1,13 @@
1
1
  import * as Path from 'path';
2
2
  import * as fs from 'fs';
3
3
  import jsyaml from 'js-yaml';
4
- import { CMap } from '@grandlinex/core';
4
+ import { CMap, instanceOfEntity } from '@grandlinex/core';
5
5
  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,95 @@ 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
+ if (typeof route.meta.responseSchema === 'string' ||
89
+ instanceOfEntity(route.meta.responseSchema)) {
90
+ conf.responses = SPathUtil.refResponse(route.meta.responseCodes?.[0] || '200', route.meta.responseSchema, route.meta.responseType === 'LIST', ...ax);
91
+ }
92
+ else {
93
+ conf.responses = SPathUtil.jsonResponse(route.meta.responseCodes?.[0] || '200', route.meta.responseSchema, route.meta.responseType === 'LIST', ...ax);
94
+ }
95
+ }
96
+ else if (route.meta.responseCodes) {
97
+ conf.responses = SPathUtil.defaultResponse(...route.meta.responseCodes);
98
+ }
99
+ else {
100
+ conf.responses = SPathUtil.defaultResponse('200');
101
+ }
102
+ }
103
+ const convertedPath = route.meta?.pathOverride ||
104
+ route.path
105
+ .split('/')
106
+ .map((e) => {
107
+ if (e.startsWith(':')) {
108
+ return `{${e.substring(1)}}`;
109
+ }
110
+ return e;
111
+ })
112
+ .join('/');
113
+ const path = {};
114
+ switch (route.type) {
115
+ case 'POST':
116
+ path[convertedPath] = {
117
+ post: conf,
118
+ };
119
+ break;
120
+ case 'GET':
121
+ path[convertedPath] = {
122
+ get: conf,
123
+ };
124
+ break;
125
+ case 'PATCH':
126
+ path[convertedPath] = {
127
+ patch: conf,
128
+ };
129
+ break;
130
+ case 'DELETE':
131
+ path[convertedPath] = {
132
+ delete: conf,
133
+ };
134
+ break;
135
+ case 'USE':
136
+ path[convertedPath] = {
137
+ get: conf,
138
+ patch: conf,
139
+ post: conf,
140
+ delete: conf,
141
+ };
142
+ break;
143
+ default:
144
+ }
145
+ return path;
59
146
  }
60
147
  static merge(root, data) {
61
148
  const out = root;
@@ -0,0 +1,26 @@
1
+ import 'reflect-metadata';
2
+ import { CoreEntity, 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 | CoreEntity | string;
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.1",
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",