@grandlinex/swagger-mate 1.3.1 → 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 +107 -5
- package/dist/cjs/Swagger/Path/SPathUtil.js +136 -52
- package/dist/cjs/Swagger/SwaggerUtil.d.ts +18 -2
- package/dist/cjs/Swagger/SwaggerUtil.js +32 -5
- package/dist/cjs/Swagger/annotation/index.d.ts +8 -3
- package/dist/cjs/Swagger/debug/BaseCon.d.ts +115 -11
- package/dist/cjs/Swagger/debug/BaseCon.js +142 -38
- package/dist/cjs/cli.js +5 -1
- 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 +107 -5
- package/dist/mjs/Swagger/Path/SPathUtil.js +137 -53
- package/dist/mjs/Swagger/SwaggerUtil.d.ts +18 -2
- package/dist/mjs/Swagger/SwaggerUtil.js +33 -6
- package/dist/mjs/Swagger/annotation/index.d.ts +8 -3
- package/dist/mjs/Swagger/debug/BaseCon.d.ts +115 -11
- package/dist/mjs/Swagger/debug/BaseCon.js +142 -38
- package/dist/mjs/cli.js +5 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/package.json +2 -2
- package/res/html/rapi-doc/index.html +28 -0
- package/res/html/rapi-doc/rapidoc-min.js +3915 -0
- package/res/html/{index.html → swagger-ui/index.html} +11 -3
- package/res/html/swagger-ui/swagger-ui-bundle.js +2 -0
- package/res/html/swagger-ui/swagger-ui-standalone-preset.js +2 -0
- package/res/html/swagger-ui/swagger-ui.css +3 -0
- package/res/templates/class/BaseCon.ts +160 -61
- package/res/html/swagger-ui-bundle.js +0 -2
- package/res/html/swagger-ui-standalone-preset.js +0 -2
- package/res/html/swagger-ui.css +0 -3
|
@@ -47,6 +47,13 @@ const PathHelp_js_1 = __importStar(require("../PathHelp.js"));
|
|
|
47
47
|
const index_js_1 = require("./annotation/index.js");
|
|
48
48
|
const index_js_2 = require("../index.js");
|
|
49
49
|
class SwaggerUtil {
|
|
50
|
+
static getLogger() {
|
|
51
|
+
if (!this.logger) {
|
|
52
|
+
const logger = new core_1.DefaultLogger();
|
|
53
|
+
this.logger = new core_1.CoreLogChannel('SwaggerUtil', logger);
|
|
54
|
+
}
|
|
55
|
+
return this.logger;
|
|
56
|
+
}
|
|
50
57
|
static writeMeta(conf, kind, path) {
|
|
51
58
|
if (kind === 'JSON') {
|
|
52
59
|
const p = Path.join(path || process.cwd(), 'openapi.json');
|
|
@@ -63,11 +70,25 @@ class SwaggerUtil {
|
|
|
63
70
|
return JSON.parse(file);
|
|
64
71
|
}
|
|
65
72
|
catch (e) {
|
|
73
|
+
this.getLogger().error(e);
|
|
66
74
|
return null;
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
|
-
|
|
70
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Serves a meta page for Swagger UI or rapi-doc.
|
|
79
|
+
*
|
|
80
|
+
* @param {SwaggerConfig} conf The swagger configuration to expose via `/spec`.
|
|
81
|
+
* @param {Object} [option] Options for serving the meta page.
|
|
82
|
+
* @param {'swagger-ui'|'rapi-doc'} [option.type='swagger-ui'] The type of UI to serve.
|
|
83
|
+
* @param {number} [option.port] The port to listen on. Defaults to 9000.
|
|
84
|
+
* @param {string} [option.auth] Optional authentication key appended to the URL.
|
|
85
|
+
* @returns {Promise<Server|null>} A promise that resolves with the created server instance or null.
|
|
86
|
+
*/
|
|
87
|
+
static async serveMeta(conf, option) {
|
|
88
|
+
const type = option?.type ?? 'swagger-ui';
|
|
89
|
+
const port = option?.port || 9000;
|
|
90
|
+
const auth = option?.auth;
|
|
91
|
+
const resFiles = (0, PathHelp_js_1.default)((0, PathHelp_js_1.getBaseFolder)(), '..', 'res', 'html', type);
|
|
71
92
|
const key = auth ? `?auth=${auth}` : '';
|
|
72
93
|
const app = (0, express_1.default)();
|
|
73
94
|
app.use('/', express_1.default.static(resFiles));
|
|
@@ -75,8 +96,8 @@ class SwaggerUtil {
|
|
|
75
96
|
res.status(200).send(conf);
|
|
76
97
|
});
|
|
77
98
|
return new Promise((resolve) => {
|
|
78
|
-
const s = app.listen(port
|
|
79
|
-
|
|
99
|
+
const s = app.listen(port, () => {
|
|
100
|
+
this.getLogger().log(`${type} listen on http://localhost:${port}${key}#`);
|
|
80
101
|
resolve(s);
|
|
81
102
|
});
|
|
82
103
|
});
|
|
@@ -121,7 +142,13 @@ class SwaggerUtil {
|
|
|
121
142
|
// Handle requestBody
|
|
122
143
|
if (!conf.requestBody) {
|
|
123
144
|
if (route.meta.requestSchema) {
|
|
124
|
-
|
|
145
|
+
if (typeof route.meta.requestSchema === 'string' ||
|
|
146
|
+
(0, core_1.instanceOfEntity)(route.meta.requestSchema)) {
|
|
147
|
+
conf.requestBody = index_js_2.SPathUtil.refRequest(route.meta.requestSchema, route.meta.responseType === 'LIST');
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
conf.requestBody = index_js_2.SPathUtil.jsonBody(route.meta.requestSchema);
|
|
151
|
+
}
|
|
125
152
|
}
|
|
126
153
|
}
|
|
127
154
|
// Handle responses
|
|
@@ -8,13 +8,18 @@ export declare enum ActionMode {
|
|
|
8
8
|
'DMZ_WITH_USER' = 2
|
|
9
9
|
}
|
|
10
10
|
export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* LIST - Response is an array of items
|
|
13
|
+
* SINGLE - Response is a single item (default)
|
|
14
|
+
*/
|
|
15
|
+
export type ResponseRequestTypes = 'LIST' | 'SINGLE';
|
|
12
16
|
export type RouteMeta = {
|
|
13
17
|
pathOverride?: string;
|
|
14
18
|
mode?: ActionMode;
|
|
15
|
-
requestSchema?: SSchemaEl;
|
|
19
|
+
requestSchema?: SSchemaEl | CoreEntity | string;
|
|
16
20
|
responseSchema?: SSchemaEl | CoreEntity | string;
|
|
17
|
-
|
|
21
|
+
requestType?: ResponseRequestTypes;
|
|
22
|
+
responseType?: ResponseRequestTypes;
|
|
18
23
|
responseCodes?: HttpStatusTypes[];
|
|
19
24
|
} & SwaggerRPathConf;
|
|
20
25
|
export type RouteData = {
|
|
@@ -38,13 +38,20 @@ export interface ConHandle {
|
|
|
38
38
|
patch<T, J>(url: string, body?: J, config?: ConHandleConfig): Promise<ConHandleResponse<T>>;
|
|
39
39
|
delete<T>(url: string, config?: ConHandleConfig): Promise<ConHandleResponse<T>>;
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* BaseCon provides a minimal client for interacting with an HTTP backend.
|
|
43
|
+
* It manages connection state, authentication tokens, and reconnection
|
|
44
|
+
* logic while delegating actual HTTP requests to a supplied {@link ConHandle}.
|
|
45
|
+
*
|
|
46
|
+
* @class
|
|
47
|
+
*/
|
|
41
48
|
export default class BaseCon {
|
|
42
|
-
api
|
|
43
|
-
permanentHeader
|
|
44
|
-
authorization
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
private api;
|
|
50
|
+
private permanentHeader;
|
|
51
|
+
private authorization;
|
|
52
|
+
private noAuth;
|
|
53
|
+
private disconnected;
|
|
54
|
+
private readonly logger;
|
|
48
55
|
con: ConHandle;
|
|
49
56
|
reconnect: () => Promise<boolean>;
|
|
50
57
|
onReconnect: (con: BaseCon) => Promise<boolean>;
|
|
@@ -53,16 +60,113 @@ export default class BaseCon {
|
|
|
53
60
|
endpoint: string;
|
|
54
61
|
logger?: (arg: any) => void;
|
|
55
62
|
});
|
|
63
|
+
/**
|
|
64
|
+
* Retrieves the API endpoint.
|
|
65
|
+
*
|
|
66
|
+
* @return {string} The API endpoint string.
|
|
67
|
+
*/
|
|
68
|
+
getApiEndpoint(): string;
|
|
69
|
+
/**
|
|
70
|
+
* Sets the API endpoint URL used by the client.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} endpoint - The full URL of the API endpoint.
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*/
|
|
75
|
+
setApiEndpoint(endpoint: string): void;
|
|
76
|
+
/**
|
|
77
|
+
* Indicates whether the instance is considered connected.
|
|
78
|
+
*
|
|
79
|
+
* The instance is regarded as connected when it either does not require authentication
|
|
80
|
+
* (`noAuth` is true) or it has an authorization token set (`authorization` is not null),
|
|
81
|
+
* and it is not currently marked as disconnected.
|
|
82
|
+
*
|
|
83
|
+
* @return {boolean} `true` if the instance is connected, `false` otherwise.
|
|
84
|
+
*/
|
|
56
85
|
isConnected(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Returns the current authorization token.
|
|
88
|
+
*
|
|
89
|
+
* @return {string} The authorization token or an empty string if none is set.
|
|
90
|
+
*/
|
|
57
91
|
token(): string;
|
|
58
|
-
p
|
|
92
|
+
private p;
|
|
93
|
+
/**
|
|
94
|
+
* Sends a ping request to the API to verify connectivity and version availability.
|
|
95
|
+
*
|
|
96
|
+
* @return {boolean} `true` if the API responded with a 200 status code and a valid version object; `false` otherwise.
|
|
97
|
+
*/
|
|
59
98
|
ping(): Promise<boolean>;
|
|
60
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Validates the current authentication token by performing a ping and a test request
|
|
101
|
+
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
102
|
+
* succeeds, it attempts to retrieve a token from the `/test/auth` endpoint using the
|
|
103
|
+
* current token in the `Authorization` header. The operation is considered successful
|
|
104
|
+
* if the response status code is 200 or 201.
|
|
105
|
+
*
|
|
106
|
+
* If any step fails, an error is logged and the method returns {@code false}. On
|
|
107
|
+
* success, it returns {@code true}.
|
|
108
|
+
*
|
|
109
|
+
* @return {Promise<boolean>} A promise that resolves to {@code true} if the token
|
|
110
|
+
* test succeeds, otherwise {@code false}.
|
|
111
|
+
*/
|
|
61
112
|
testToken(): Promise<boolean>;
|
|
62
|
-
connect(email: string, pw: string): Promise<boolean>;
|
|
63
113
|
/**
|
|
64
|
-
*
|
|
114
|
+
* Attempts to establish a connection to the backend without authentication.
|
|
115
|
+
*
|
|
116
|
+
* This method sends a ping request. If the ping succeeds, it clears any
|
|
117
|
+
* existing authorization data, marks the instance as connected,
|
|
118
|
+
* enables the no‑authentication mode, and returns `true`. If the ping
|
|
119
|
+
* fails, it logs a warning, clears authorization, marks the instance
|
|
120
|
+
* as disconnected, and returns `false`.
|
|
121
|
+
*
|
|
122
|
+
* @return {Promise<boolean>} `true` when a connection is successfully
|
|
123
|
+
* established without authentication, otherwise `false`.
|
|
124
|
+
*/
|
|
125
|
+
connectNoAuth(): Promise<boolean>;
|
|
126
|
+
/**
|
|
127
|
+
* Forces a connection using the provided bearer token.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} token The token to be used for authentication.
|
|
130
|
+
* @returns {void}
|
|
131
|
+
*/
|
|
132
|
+
forceConnectWithToken(token: string): void;
|
|
133
|
+
/**
|
|
134
|
+
* Establishes a connection to the backend using the supplied credentials.
|
|
135
|
+
* Performs a health‑check ping first; if successful, it requests an authentication
|
|
136
|
+
* token from the `/token` endpoint. When the token is obtained, the method
|
|
137
|
+
* updates internal state (authorization header, connection flags) and, unless
|
|
138
|
+
* a dry run is requested, sets up a reconnection routine. Any errors are
|
|
139
|
+
* logged and the method resolves to `false`.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} email - The user's email address for authentication.
|
|
142
|
+
* @param {string} pw - The password (or token) for the specified user.
|
|
143
|
+
* @param {boolean} [dry=false] - If `true`, the method performs a dry run
|
|
144
|
+
* without persisting credentials or configuring reconnection logic.
|
|
145
|
+
*
|
|
146
|
+
* @returns Promise<boolean> `true` if the connection was successfully
|
|
147
|
+
* established, otherwise `false`.
|
|
148
|
+
*/
|
|
149
|
+
connect(email: string, pw: string, dry?: boolean): Promise<boolean>;
|
|
150
|
+
/**
|
|
151
|
+
* Performs an HTTP request using the client’s internal connection.
|
|
152
|
+
*
|
|
153
|
+
* The method verifies that the client is connected before attempting a request.
|
|
154
|
+
* It automatically injects the authorization token, permanent headers, and any
|
|
155
|
+
* headers supplied in `config`. If the request body is a `FormData` instance
|
|
156
|
+
* (or provides a `getHeaders` method), the appropriate form headers are added.
|
|
157
|
+
*
|
|
158
|
+
* Response handling:
|
|
159
|
+
* - `200` or `201`: returns `success: true` with the received data.
|
|
160
|
+
* - `498`: attempts to reconnect and retries the request once.
|
|
161
|
+
* - `401`: logs an authentication error, marks the client as disconnected.
|
|
162
|
+
* - `403` and other status codes: return `success: false` with the status code.
|
|
163
|
+
*
|
|
164
|
+
* @param {'POST'|'GET'|'PATCH'|'DELETE'} type The HTTP method to use.
|
|
165
|
+
* @param {string} path The endpoint path relative to the base URL.
|
|
166
|
+
* @param {J} [body] Optional request payload. May be `FormData` or a plain object.
|
|
167
|
+
* @param {ConHandleConfig} [config] Optional Axios-like configuration for the request.
|
|
168
|
+
* @returns {Promise<HandleRes<T>>} A promise that resolves to a `HandleRes` object
|
|
169
|
+
* containing the response data, status code, any error information, and headers.
|
|
65
170
|
*/
|
|
66
|
-
fakeEnableClient(): void;
|
|
67
171
|
handle<T, J>(type: 'POST' | 'GET' | 'PATCH' | 'DELETE', path: string, body?: J, config?: ConHandleConfig): Promise<HandleRes<T>>;
|
|
68
172
|
}
|
|
@@ -12,13 +12,20 @@ const form_data_1 = __importDefault(require("form-data"));
|
|
|
12
12
|
function isErrorType(x) {
|
|
13
13
|
return x && typeof x === 'object' && x.type === 'error';
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* BaseCon provides a minimal client for interacting with an HTTP backend.
|
|
17
|
+
* It manages connection state, authentication tokens, and reconnection
|
|
18
|
+
* logic while delegating actual HTTP requests to a supplied {@link ConHandle}.
|
|
19
|
+
*
|
|
20
|
+
* @class
|
|
21
|
+
*/
|
|
15
22
|
class BaseCon {
|
|
16
23
|
constructor(conf) {
|
|
17
24
|
this.api = conf.endpoint;
|
|
18
|
-
this.logger = conf.logger
|
|
25
|
+
this.logger = conf.logger ?? console.log;
|
|
19
26
|
this.disconnected = true;
|
|
27
|
+
this.noAuth = false;
|
|
20
28
|
this.authorization = null;
|
|
21
|
-
this.failFlag = false;
|
|
22
29
|
this.con = conf.con;
|
|
23
30
|
this.reconnect = async () => {
|
|
24
31
|
this.disconnected = true;
|
|
@@ -27,10 +34,40 @@ class BaseCon {
|
|
|
27
34
|
this.onReconnect = () => Promise.resolve(true);
|
|
28
35
|
this.handle = this.handle.bind(this);
|
|
29
36
|
}
|
|
30
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Retrieves the API endpoint.
|
|
39
|
+
*
|
|
40
|
+
* @return {string} The API endpoint string.
|
|
41
|
+
*/
|
|
42
|
+
getApiEndpoint() {
|
|
43
|
+
return this.api;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sets the API endpoint URL used by the client.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} endpoint - The full URL of the API endpoint.
|
|
49
|
+
* @returns {void}
|
|
50
|
+
*/
|
|
51
|
+
setApiEndpoint(endpoint) {
|
|
52
|
+
this.api = endpoint;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Indicates whether the instance is considered connected.
|
|
56
|
+
*
|
|
57
|
+
* The instance is regarded as connected when it either does not require authentication
|
|
58
|
+
* (`noAuth` is true) or it has an authorization token set (`authorization` is not null),
|
|
59
|
+
* and it is not currently marked as disconnected.
|
|
60
|
+
*
|
|
61
|
+
* @return {boolean} `true` if the instance is connected, `false` otherwise.
|
|
62
|
+
*/
|
|
31
63
|
isConnected() {
|
|
32
|
-
return this.authorization !== null && !this.disconnected;
|
|
64
|
+
return (this.noAuth || this.authorization !== null) && !this.disconnected;
|
|
33
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Returns the current authorization token.
|
|
68
|
+
*
|
|
69
|
+
* @return {string} The authorization token or an empty string if none is set.
|
|
70
|
+
*/
|
|
34
71
|
token() {
|
|
35
72
|
return this.authorization || '';
|
|
36
73
|
}
|
|
@@ -56,35 +93,34 @@ class BaseCon {
|
|
|
56
93
|
}
|
|
57
94
|
return `${this.api}${pp}`;
|
|
58
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Sends a ping request to the API to verify connectivity and version availability.
|
|
98
|
+
*
|
|
99
|
+
* @return {boolean} `true` if the API responded with a 200 status code and a valid version object; `false` otherwise.
|
|
100
|
+
*/
|
|
59
101
|
async ping() {
|
|
60
102
|
try {
|
|
61
103
|
const version = await this.con.get(this.p('/version'));
|
|
62
|
-
return version.data?.api
|
|
104
|
+
return version.data?.api !== undefined && version.code === 200;
|
|
63
105
|
}
|
|
64
106
|
catch (e) {
|
|
65
107
|
this.logger('ping failed');
|
|
66
108
|
return false;
|
|
67
109
|
}
|
|
68
110
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this.logger('cant connect to backend');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
this.logger('test ping failed');
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
111
|
+
/**
|
|
112
|
+
* Validates the current authentication token by performing a ping and a test request
|
|
113
|
+
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
114
|
+
* succeeds, it attempts to retrieve a token from the `/test/auth` endpoint using the
|
|
115
|
+
* current token in the `Authorization` header. The operation is considered successful
|
|
116
|
+
* if the response status code is 200 or 201.
|
|
117
|
+
*
|
|
118
|
+
* If any step fails, an error is logged and the method returns {@code false}. On
|
|
119
|
+
* success, it returns {@code true}.
|
|
120
|
+
*
|
|
121
|
+
* @return {Promise<boolean>} A promise that resolves to {@code true} if the token
|
|
122
|
+
* test succeeds, otherwise {@code false}.
|
|
123
|
+
*/
|
|
88
124
|
async testToken() {
|
|
89
125
|
const ping = await this.ping();
|
|
90
126
|
if (ping) {
|
|
@@ -104,7 +140,59 @@ class BaseCon {
|
|
|
104
140
|
this.logger('test ping failed');
|
|
105
141
|
return false;
|
|
106
142
|
}
|
|
107
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Attempts to establish a connection to the backend without authentication.
|
|
145
|
+
*
|
|
146
|
+
* This method sends a ping request. If the ping succeeds, it clears any
|
|
147
|
+
* existing authorization data, marks the instance as connected,
|
|
148
|
+
* enables the no‑authentication mode, and returns `true`. If the ping
|
|
149
|
+
* fails, it logs a warning, clears authorization, marks the instance
|
|
150
|
+
* as disconnected, and returns `false`.
|
|
151
|
+
*
|
|
152
|
+
* @return {Promise<boolean>} `true` when a connection is successfully
|
|
153
|
+
* established without authentication, otherwise `false`.
|
|
154
|
+
*/
|
|
155
|
+
async connectNoAuth() {
|
|
156
|
+
const ping = await this.ping();
|
|
157
|
+
if (ping) {
|
|
158
|
+
this.authorization = null;
|
|
159
|
+
this.disconnected = false;
|
|
160
|
+
this.noAuth = true;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
this.logger('cant connect to backend');
|
|
164
|
+
this.authorization = null;
|
|
165
|
+
this.disconnected = true;
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Forces a connection using the provided bearer token.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} token The token to be used for authentication.
|
|
172
|
+
* @returns {void}
|
|
173
|
+
*/
|
|
174
|
+
forceConnectWithToken(token) {
|
|
175
|
+
this.authorization = `Bearer ${token}`;
|
|
176
|
+
this.disconnected = false;
|
|
177
|
+
this.noAuth = false;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Establishes a connection to the backend using the supplied credentials.
|
|
181
|
+
* Performs a health‑check ping first; if successful, it requests an authentication
|
|
182
|
+
* token from the `/token` endpoint. When the token is obtained, the method
|
|
183
|
+
* updates internal state (authorization header, connection flags) and, unless
|
|
184
|
+
* a dry run is requested, sets up a reconnection routine. Any errors are
|
|
185
|
+
* logged and the method resolves to `false`.
|
|
186
|
+
*
|
|
187
|
+
* @param {string} email - The user's email address for authentication.
|
|
188
|
+
* @param {string} pw - The password (or token) for the specified user.
|
|
189
|
+
* @param {boolean} [dry=false] - If `true`, the method performs a dry run
|
|
190
|
+
* without persisting credentials or configuring reconnection logic.
|
|
191
|
+
*
|
|
192
|
+
* @returns Promise<boolean> `true` if the connection was successfully
|
|
193
|
+
* established, otherwise `false`.
|
|
194
|
+
*/
|
|
195
|
+
async connect(email, pw, dry = false) {
|
|
108
196
|
const ping = await this.ping();
|
|
109
197
|
if (ping) {
|
|
110
198
|
try {
|
|
@@ -112,13 +200,15 @@ class BaseCon {
|
|
|
112
200
|
username: email,
|
|
113
201
|
token: pw,
|
|
114
202
|
});
|
|
115
|
-
// TODO check token
|
|
116
203
|
if (token.code === 200 || token.code === 201) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
204
|
+
if (!dry) {
|
|
205
|
+
this.authorization = `Bearer ${token.data?.token}`;
|
|
206
|
+
this.disconnected = false;
|
|
207
|
+
this.noAuth = false;
|
|
208
|
+
this.reconnect = async () => {
|
|
209
|
+
return ((await this.connect(email, pw)) && (await this.testToken()));
|
|
210
|
+
};
|
|
211
|
+
}
|
|
122
212
|
return true;
|
|
123
213
|
}
|
|
124
214
|
}
|
|
@@ -129,17 +219,32 @@ class BaseCon {
|
|
|
129
219
|
this.logger('cant connect to backend');
|
|
130
220
|
this.authorization = null;
|
|
131
221
|
this.disconnected = true;
|
|
222
|
+
this.noAuth = false;
|
|
132
223
|
return false;
|
|
133
224
|
}
|
|
134
225
|
/**
|
|
135
|
-
*
|
|
226
|
+
* Performs an HTTP request using the client’s internal connection.
|
|
227
|
+
*
|
|
228
|
+
* The method verifies that the client is connected before attempting a request.
|
|
229
|
+
* It automatically injects the authorization token, permanent headers, and any
|
|
230
|
+
* headers supplied in `config`. If the request body is a `FormData` instance
|
|
231
|
+
* (or provides a `getHeaders` method), the appropriate form headers are added.
|
|
232
|
+
*
|
|
233
|
+
* Response handling:
|
|
234
|
+
* - `200` or `201`: returns `success: true` with the received data.
|
|
235
|
+
* - `498`: attempts to reconnect and retries the request once.
|
|
236
|
+
* - `401`: logs an authentication error, marks the client as disconnected.
|
|
237
|
+
* - `403` and other status codes: return `success: false` with the status code.
|
|
238
|
+
*
|
|
239
|
+
* @param {'POST'|'GET'|'PATCH'|'DELETE'} type The HTTP method to use.
|
|
240
|
+
* @param {string} path The endpoint path relative to the base URL.
|
|
241
|
+
* @param {J} [body] Optional request payload. May be `FormData` or a plain object.
|
|
242
|
+
* @param {ConHandleConfig} [config] Optional Axios-like configuration for the request.
|
|
243
|
+
* @returns {Promise<HandleRes<T>>} A promise that resolves to a `HandleRes` object
|
|
244
|
+
* containing the response data, status code, any error information, and headers.
|
|
136
245
|
*/
|
|
137
|
-
fakeEnableClient() {
|
|
138
|
-
this.authorization = 'DEBUG';
|
|
139
|
-
this.disconnected = false;
|
|
140
|
-
}
|
|
141
246
|
async handle(type, path, body, config) {
|
|
142
|
-
if (!this.
|
|
247
|
+
if (!this.isConnected()) {
|
|
143
248
|
this.logger('Disconnected');
|
|
144
249
|
return {
|
|
145
250
|
success: false,
|
|
@@ -246,7 +351,6 @@ class BaseCon {
|
|
|
246
351
|
};
|
|
247
352
|
case 401:
|
|
248
353
|
this.logger('AUTH NOT VALID');
|
|
249
|
-
this.disconnected = true;
|
|
250
354
|
return {
|
|
251
355
|
success: false,
|
|
252
356
|
data: null,
|
package/dist/cjs/cli.js
CHANGED
|
@@ -76,7 +76,11 @@ async function run() {
|
|
|
76
76
|
console.log('Serving Swagger Meta');
|
|
77
77
|
const auth = process.env.SW_AUTH || undefined;
|
|
78
78
|
const port = process.env.SW_PORT || undefined;
|
|
79
|
-
SwaggerUtil_js_1.default.serveMeta(conf,
|
|
79
|
+
SwaggerUtil_js_1.default.serveMeta(conf, {
|
|
80
|
+
port: port ? parseInt(port, 10) : undefined,
|
|
81
|
+
auth,
|
|
82
|
+
type: 'rapi-doc',
|
|
83
|
+
});
|
|
80
84
|
}
|
|
81
85
|
if (arg[arg.length - 2] === '--build') {
|
|
82
86
|
console.log('Building Swagger Meta');
|
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
|
+
}
|