expresso-macchiato 1.0.0-dev.7 → 1.0.0-dev.9
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/index.d.mts +68 -456
- package/dist/index.d.ts +68 -456
- package/dist/index.js +126 -306
- package/dist/index.mjs +111 -291
- package/package.json +2 -3
- package/types/db.sptypes.ts +6 -4
- package/types/router.sptypes.ts +10 -7
- package/types/swagger.sptypes.ts +0 -375
package/dist/index.js
CHANGED
|
@@ -114,267 +114,68 @@ _DbConnector.getDataSource = () => _DbConnector.DataSource;
|
|
|
114
114
|
var DbConnector = _DbConnector;
|
|
115
115
|
|
|
116
116
|
// src/Swagger.ts
|
|
117
|
-
var
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
// ----------------------- GENERIC ----------------------- //
|
|
130
|
-
// ------------------------------------------------------- //
|
|
131
|
-
// ------------------------------------------------------- //
|
|
132
|
-
_Swagger.addServer = (server) => _Swagger.apiDocument.servers.push(server);
|
|
133
|
-
// Call at init to add current server
|
|
134
|
-
_Swagger.generateOpenAPIDocument = () => JSON.parse(JSON.stringify(_Swagger.apiDocument, null, 2));
|
|
135
|
-
// Generates json to be served in api
|
|
136
|
-
_Swagger.apiDocument = // static document to be populated
|
|
137
|
-
{
|
|
138
|
-
openapi: "3.0.4",
|
|
139
|
-
servers: [],
|
|
140
|
-
info: { title: "Dynamically Generated API", version: "1.0.0" },
|
|
141
|
-
paths: {},
|
|
142
|
-
components: {
|
|
143
|
-
schemas: {},
|
|
144
|
-
securitySchemes: {
|
|
145
|
-
bearerAuth: {
|
|
146
|
-
type: "http",
|
|
147
|
-
scheme: "bearer",
|
|
148
|
-
bearerFormat: "JWT"
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
security: [
|
|
153
|
-
{ "bearerAuth": [] }
|
|
154
|
-
]
|
|
155
|
-
};
|
|
156
|
-
// ------------------------------------------------------- //
|
|
157
|
-
// ------------------------------------------------------- //
|
|
158
|
-
// ----------------------- METHODS ----------------------- //
|
|
159
|
-
// ------------------------------------------------------- //
|
|
160
|
-
// ------------------------------------------------------- //
|
|
161
|
-
/**
|
|
162
|
-
* @Description
|
|
163
|
-
* - ❌ ROUTER-WRAPPER.
|
|
164
|
-
* - 🛠️ ADD-API-PATH
|
|
165
|
-
* - This method is not directly to insert inside the structur of addApiPath but is useful to create Schemas while not using the routerWrapper
|
|
166
|
-
*/
|
|
167
|
-
_Swagger.addSchema = (schema, properties) => {
|
|
168
|
-
if (!_Swagger.apiDocument.components || !_Swagger.apiDocument.components.schemas) _Swagger.apiDocument.components = { schemas: {} };
|
|
169
|
-
if (!_Swagger.apiDocument.components.schemas[schema]) _Swagger.apiDocument.components.schemas[schema] = {
|
|
170
|
-
type: "object",
|
|
171
|
-
properties
|
|
172
|
-
};
|
|
173
|
-
};
|
|
174
|
-
/**
|
|
175
|
-
* @Description
|
|
176
|
-
* - ❌ ROUTER-WRAPPER.
|
|
177
|
-
* - 🛠️ ADD-API-PATH
|
|
178
|
-
* - This method is thought to reduce the code in the addApiPath method
|
|
179
|
-
* - You can create a schema while declaring it and associating to the path calling this method
|
|
180
|
-
*/
|
|
181
|
-
_Swagger.getBasicPost = (schema, required, parameters, properties, description) => ({
|
|
182
|
-
responses: {},
|
|
183
|
-
parameters,
|
|
184
|
-
requestBody: {
|
|
185
|
-
required,
|
|
186
|
-
description,
|
|
187
|
-
schemaName: schema,
|
|
188
|
-
schema: {
|
|
189
|
-
[schema]: {
|
|
190
|
-
type: "object",
|
|
191
|
-
properties
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
/**
|
|
197
|
-
* @Description
|
|
198
|
-
* - ❌ **ROUTER-WRAPPER.**
|
|
199
|
-
* - 🛠️ **ADD-API-PATH**
|
|
200
|
-
* - This method is thought to reduce the code in the addApiPath method
|
|
201
|
-
*/
|
|
202
|
-
_Swagger.getBasicGet = (parameters) => ({
|
|
203
|
-
responses: {},
|
|
204
|
-
parameters
|
|
205
|
-
});
|
|
206
|
-
/**
|
|
207
|
-
* @Description
|
|
208
|
-
* - ❌ ROUTER-WRAPPER.
|
|
209
|
-
* - This method is useful if you don't want to use the router wrapper but add your swagger schema anyway.
|
|
210
|
-
* - **It is thought to be used inside the library**, iterating through the paths of the router wrapper.
|
|
211
|
-
* - **Devs can't use inside the parameters of a RouterWrapper constructor**
|
|
212
|
-
* - You can create the structure of a specified path and method, with a schema name that should be created elsewhere
|
|
213
|
-
*/
|
|
214
|
-
_Swagger.addSingleApiPath = (tag, basePath, _path, method, parameters, bodyObj, responses = {}) => {
|
|
215
|
-
const path3 = `${_Swagger.fromExpressParamsToSwagger(basePath)}${_Swagger.fromExpressParamsToSwagger(_path)}`;
|
|
216
|
-
const transcribedMethod = _Swagger.restMethodToSwaggerKeyMapping[method];
|
|
217
|
-
if (!_Swagger.apiDocument.paths[path3]) _Swagger.apiDocument.paths[path3] = {};
|
|
218
|
-
if (_Swagger.apiDocument.paths[path3][transcribedMethod]) return;
|
|
219
|
-
_Swagger.apiDocument.paths[path3][transcribedMethod] = {
|
|
220
|
-
responses,
|
|
221
|
-
parameters,
|
|
222
|
-
tags: [tag]
|
|
223
|
-
};
|
|
224
|
-
if (bodyObj) _Swagger.apiDocument.paths[path3][transcribedMethod].requestBody = {
|
|
225
|
-
required: _nullishCoalesce(bodyObj.required, () => ( false)),
|
|
226
|
-
description: bodyObj.description,
|
|
227
|
-
content: {
|
|
228
|
-
[_nullishCoalesce(bodyObj.comType, () => ( "application/json"))]: {
|
|
229
|
-
schema: { "$ref": "#/components/schemas/" + bodyObj.schema }
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
};
|
|
234
|
-
/**
|
|
235
|
-
* @Description
|
|
236
|
-
* - ❌ ROUTER-WRAPPER.
|
|
237
|
-
* - 🛠️ ADD-API-PATH
|
|
238
|
-
* - **This method is useful if you don't want to use the router wrapper but add your swagger schema anyway**.
|
|
239
|
-
* - You can use other other methods of this class to ease the writing
|
|
240
|
-
* - It is thought to create a full section of apis (hypotetically under one single tag) **while you create the schema in the meanwhile**
|
|
241
|
-
*/
|
|
242
|
-
_Swagger._addApiPath = (tag, basePath, options) => {
|
|
243
|
-
const finalPaths = {};
|
|
244
|
-
const finalSchemas = {};
|
|
245
|
-
for (const _path in options) {
|
|
246
|
-
const path3 = `${basePath}${_path}`;
|
|
247
|
-
if (_optionalChain([options, 'access', _4 => _4[_path], 'access', _5 => _5.get, 'optionalAccess', _6 => _6.tags])) options[_path].get.tags = [tag];
|
|
248
|
-
finalPaths[path3] = {
|
|
249
|
-
get: options[_path].get,
|
|
250
|
-
$ref: options[_path].$ref,
|
|
251
|
-
head: options[_path].head,
|
|
252
|
-
parameters: options[_path].parameters
|
|
253
|
-
};
|
|
254
|
-
if (options[_path].get) options[_path].get.tags = [tag];
|
|
255
|
-
if (options[_path].post && options[_path].post.requestBody) {
|
|
256
|
-
finalPaths[path3].post = {
|
|
257
|
-
responses: {},
|
|
258
|
-
parameters: options[_path].post.parameters,
|
|
259
|
-
tags: [tag],
|
|
260
|
-
summary: options[_path].post.summary,
|
|
261
|
-
requestBody: {
|
|
262
|
-
required: options[_path].post.requestBody.required,
|
|
263
|
-
description: options[_path].post.requestBody.description,
|
|
264
|
-
content: {
|
|
265
|
-
"application/json": { schema: { "$ref": "#/components/schemas/" + options[_path].post.requestBody.schemaName } }
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
for (const key in options[_path].post.requestBody.schema) {
|
|
270
|
-
if (!finalSchemas[key]) finalSchemas[key] = options[_path].post.requestBody.schema[key];
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (options[_path].put && options[_path].put.requestBody) {
|
|
274
|
-
finalPaths[path3].put = {
|
|
275
|
-
responses: {},
|
|
276
|
-
tags: [tag],
|
|
277
|
-
parameters: options[_path].put.parameters,
|
|
278
|
-
summary: options[_path].put.summary,
|
|
279
|
-
requestBody: {
|
|
280
|
-
required: options[_path].put.requestBody.required,
|
|
281
|
-
description: options[_path].put.requestBody.description,
|
|
282
|
-
content: {
|
|
283
|
-
"application/json": { schema: { "$ref": "#/components/schemas/" + options[_path].put.requestBody.schemaName } }
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
for (const key in options[_path].put.requestBody.schema) {
|
|
288
|
-
if (!finalSchemas[key]) finalSchemas[key] = options[_path].put.requestBody.schema[key];
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
if (options[_path].delete) {
|
|
292
|
-
finalPaths[path3].delete = {
|
|
293
|
-
responses: {},
|
|
294
|
-
tags: [tag],
|
|
295
|
-
parameters: options[_path].delete.parameters,
|
|
296
|
-
summary: options[_path].delete.summary
|
|
297
|
-
};
|
|
298
|
-
if (options[_path].delete.requestBody) {
|
|
299
|
-
const finalComType = _nullishCoalesce(options[_path].delete.requestBody.comType, () => ( "application/json"));
|
|
300
|
-
finalPaths[path3].delete.requestBody = {
|
|
301
|
-
required: options[_path].delete.requestBody.required,
|
|
302
|
-
description: options[_path].delete.requestBody.description,
|
|
303
|
-
content: {
|
|
304
|
-
[finalComType]: {
|
|
305
|
-
schema: { "$ref": "#/components/schemas/" + options[_path].delete.requestBody.schemaName }
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
};
|
|
309
|
-
for (const key in options[_path].delete.requestBody.schema) {
|
|
310
|
-
if (!finalSchemas[key]) finalSchemas[key] = options[_path].delete.requestBody.schema[key];
|
|
311
|
-
}
|
|
117
|
+
var _expressoswagger = require('expresso-swagger');
|
|
118
|
+
var MySwagger = class extends _expressoswagger.ExpressoSwagger {
|
|
119
|
+
constructor() {
|
|
120
|
+
super({
|
|
121
|
+
info: {
|
|
122
|
+
title: "Expresso Macchiato API",
|
|
123
|
+
version: "1.0.0",
|
|
124
|
+
description: "API documentation for Expresso Macchiato"
|
|
125
|
+
},
|
|
126
|
+
settings: {
|
|
127
|
+
theme: "dark",
|
|
128
|
+
withCredentials: true
|
|
312
129
|
}
|
|
313
|
-
}
|
|
130
|
+
});
|
|
131
|
+
this.fromExpressParamsToSwagger = (str) => str.replace(/:(\w+)(?=\/|$)/g, "{$1}");
|
|
314
132
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const methodKey = method;
|
|
318
|
-
if (!_Swagger.apiDocument.paths[path3]) _Swagger.apiDocument.paths[path3] = {};
|
|
319
|
-
if (!_Swagger.apiDocument.paths[path3][methodKey]) _Swagger.apiDocument.paths[path3][methodKey] = finalPaths[path3][methodKey];
|
|
320
|
-
}
|
|
133
|
+
setBaseUrl(url) {
|
|
134
|
+
this.config.baseUrl = url;
|
|
321
135
|
}
|
|
322
|
-
|
|
323
|
-
if (!
|
|
324
|
-
|
|
136
|
+
addSchema(name, schema) {
|
|
137
|
+
if (!this.config.models) this.config.models = {};
|
|
138
|
+
this.config.models[name] = schema;
|
|
325
139
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
};
|
|
352
|
-
/**
|
|
353
|
-
* @Description
|
|
354
|
-
* - ✅ ROUTER-WRAPPER
|
|
355
|
-
* - Creates the swagger schema expected by your api
|
|
356
|
-
*/
|
|
357
|
-
_Swagger.createMultipartSchema = (name = "file", props) => ({
|
|
358
|
-
type: "object",
|
|
359
|
-
properties: {
|
|
360
|
-
[name]: { type: "string", format: "binary" },
|
|
361
|
-
..._nullishCoalesce(props, () => ( {}))
|
|
140
|
+
/**
|
|
141
|
+
* Adds a single API path by accumulating it into the internal list.
|
|
142
|
+
*/
|
|
143
|
+
addSingleApiPath(group, basePath, _path, method, options) {
|
|
144
|
+
const fullPath = this.fromExpressParamsToSwagger(`${basePath}${_path}`);
|
|
145
|
+
const params = {};
|
|
146
|
+
const query = {};
|
|
147
|
+
if (options.parameters) {
|
|
148
|
+
options.parameters.forEach((p) => {
|
|
149
|
+
const def = { type: p.type || "string", required: p.required, description: p.description, default: p.default };
|
|
150
|
+
if (p.in === "path") params[p.name] = def;
|
|
151
|
+
if (p.in === "query") query[p.name] = def;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
this.endpoints.push({
|
|
155
|
+
path: fullPath,
|
|
156
|
+
method,
|
|
157
|
+
group,
|
|
158
|
+
name: options.name,
|
|
159
|
+
description: options.description,
|
|
160
|
+
params: Object.keys(params).length > 0 ? params : void 0,
|
|
161
|
+
query: Object.keys(query).length > 0 ? query : void 0,
|
|
162
|
+
body: options.body,
|
|
163
|
+
responses: options.responses
|
|
164
|
+
});
|
|
362
165
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
*/
|
|
369
|
-
_Swagger.createSchema = (props, required) => {
|
|
370
|
-
const finalSchemaObject = { type: "object", properties: {}, required };
|
|
371
|
-
for (const prop in props) {
|
|
372
|
-
if (typeof props[prop] === "string") finalSchemaObject.properties[prop] = { type: props[prop] };
|
|
373
|
-
else finalSchemaObject.properties[prop] = props[prop];
|
|
166
|
+
/**
|
|
167
|
+
* Instantiates ExpressoSwagger and generates the final document.
|
|
168
|
+
*/
|
|
169
|
+
generateDocument() {
|
|
170
|
+
return this.getDocument();
|
|
374
171
|
}
|
|
375
|
-
return finalSchemaObject;
|
|
376
172
|
};
|
|
377
|
-
|
|
173
|
+
MySwagger.getIdParam = (name = "id") => ({ name, in: "path", required: true, type: "string", description: `Unique identifier.` });
|
|
174
|
+
MySwagger.getPaginationParams = () => [
|
|
175
|
+
{ name: "page", in: "query", type: "number", default: 1 },
|
|
176
|
+
{ name: "limit", in: "query", type: "number", default: 10 }
|
|
177
|
+
];
|
|
178
|
+
var Swagger = new MySwagger();
|
|
378
179
|
|
|
379
180
|
// src/DynamicDbRouter.ts
|
|
380
181
|
var _DynamicDbRouter = class _DynamicDbRouter {
|
|
@@ -422,12 +223,12 @@ _DynamicDbRouter.createDbRouter = (options) => {
|
|
|
422
223
|
if (_DynamicDbRouter.listOptionsKeys.includes(key)) listOptions[key] = req.query[key];
|
|
423
224
|
}
|
|
424
225
|
const searchQuery = {};
|
|
425
|
-
for (const param of _nullishCoalesce(options.getParameters, () => (
|
|
426
|
-
const val = _nullishCoalesce(req.query[
|
|
427
|
-
if (param.required && !val) throw new Error(`Params Error: ${
|
|
226
|
+
for (const [key, param] of Object.entries(_nullishCoalesce(options.getParameters, () => ( {})))) {
|
|
227
|
+
const val = _nullishCoalesce(req.query[key], () => ( void 0));
|
|
228
|
+
if (param.required && !val) throw new Error(`Params Error: ${key} required`);
|
|
428
229
|
if (val !== void 0) {
|
|
429
|
-
if (param.like) searchQuery[
|
|
430
|
-
else searchQuery[
|
|
230
|
+
if (param.like) searchQuery[key] = _typeorm.Like.call(void 0, `%${val}%`);
|
|
231
|
+
else searchQuery[key] = _typeorm.Equal.call(void 0, val);
|
|
431
232
|
}
|
|
432
233
|
}
|
|
433
234
|
const secureSearchQuery = _DynamicDbRouter.setSecureParams("LIST", payload, options.secure);
|
|
@@ -473,7 +274,7 @@ _DynamicDbRouter.createDbRouter = (options) => {
|
|
|
473
274
|
const finalBody = {};
|
|
474
275
|
const body = req.body;
|
|
475
276
|
for (const key in body) {
|
|
476
|
-
if (key !== options.primaryKey && (!options.bodyParameters || _optionalChain([options, 'access',
|
|
277
|
+
if (key !== options.primaryKey && (!options.bodyParameters || _optionalChain([options, 'access', _4 => _4.bodyParameters, 'access', _5 => _5.properties, 'optionalAccess', _6 => _6[key]]))) {
|
|
477
278
|
finalBody[key] = body[key];
|
|
478
279
|
}
|
|
479
280
|
}
|
|
@@ -504,7 +305,7 @@ _DynamicDbRouter.createDbRouter = (options) => {
|
|
|
504
305
|
if (singleEntity === null) throw new Error("Entity not found");
|
|
505
306
|
for (const key in body) {
|
|
506
307
|
if (key !== options.primaryKey && key in singleEntity) {
|
|
507
|
-
if (!options.bodyParameters || _optionalChain([options, 'access',
|
|
308
|
+
if (!options.bodyParameters || _optionalChain([options, 'access', _7 => _7.bodyParameters, 'access', _8 => _8.properties, 'optionalAccess', _9 => _9[key]])) {
|
|
508
309
|
singleEntity[key] = body[key];
|
|
509
310
|
}
|
|
510
311
|
}
|
|
@@ -577,19 +378,28 @@ _DynamicDbRouter.addDbRouterSwagger = (options) => {
|
|
|
577
378
|
}
|
|
578
379
|
if (!avoidList.includes("LIST")) {
|
|
579
380
|
const listOptions = { page: 0, pageSize: 10, order: "ASC", orderBy: options.primaryKey };
|
|
580
|
-
Swagger.addSingleApiPath(options.tag, options.basePath, "/", "GET",
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
381
|
+
Swagger.addSingleApiPath(options.tag, options.basePath, "/", "GET", {
|
|
382
|
+
name: "/",
|
|
383
|
+
responses: options.responses,
|
|
384
|
+
parameters: [
|
|
385
|
+
{ name: "page", in: "query", type: "number", default: 0, required: false },
|
|
386
|
+
...Object.entries(listOptions).map(([key, val]) => ({ name: key, in: "query", default: val, required: false, type: typeof val === "number" ? "number" : "string" })),
|
|
387
|
+
..._nullishCoalesce(options.getParameters, () => ( []))
|
|
388
|
+
]
|
|
389
|
+
});
|
|
584
390
|
}
|
|
585
391
|
if (!avoidList.includes("GET")) {
|
|
586
|
-
Swagger.addSingleApiPath(options.tag, options.basePath, `/{${options.primaryKey}}`, "GET",
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
392
|
+
Swagger.addSingleApiPath(options.tag, options.basePath, `/{${options.primaryKey}}`, "GET", {
|
|
393
|
+
name: `/{${options.primaryKey}}`,
|
|
394
|
+
responses: options.responses,
|
|
395
|
+
parameters: [
|
|
396
|
+
{ name: options.primaryKey, in: "path", required: true, type: "string" },
|
|
397
|
+
..._nullishCoalesce(options.getParameters, () => ( []))
|
|
398
|
+
]
|
|
399
|
+
});
|
|
590
400
|
}
|
|
591
401
|
if (!avoidList.includes("POST")) {
|
|
592
|
-
Swagger.addSingleApiPath(options.tag, options.basePath, "/", "POST",
|
|
402
|
+
Swagger.addSingleApiPath(options.tag, options.basePath, "/", "POST", { body: options.tag, responses: options.responses });
|
|
593
403
|
}
|
|
594
404
|
if (!avoidList.includes("PUT")) {
|
|
595
405
|
Swagger.addSingleApiPath(
|
|
@@ -597,14 +407,22 @@ _DynamicDbRouter.addDbRouterSwagger = (options) => {
|
|
|
597
407
|
options.basePath,
|
|
598
408
|
`/{${options.primaryKey}}`,
|
|
599
409
|
"PUT",
|
|
600
|
-
|
|
601
|
-
|
|
410
|
+
{
|
|
411
|
+
name: `/{${options.primaryKey}}`,
|
|
412
|
+
body: options.tag,
|
|
413
|
+
parameters: [{ name: options.primaryKey, in: "path", required: true, type: "string" }],
|
|
414
|
+
responses: options.responses
|
|
415
|
+
}
|
|
602
416
|
);
|
|
603
417
|
}
|
|
604
418
|
if (!avoidList.includes("DELETE")) {
|
|
605
|
-
Swagger.addSingleApiPath(options.tag, options.basePath, `/{${options.primaryKey}}`, "DELETE",
|
|
606
|
-
|
|
607
|
-
|
|
419
|
+
Swagger.addSingleApiPath(options.tag, options.basePath, `/{${options.primaryKey}}`, "DELETE", {
|
|
420
|
+
name: `/{${options.primaryKey}}`,
|
|
421
|
+
responses: options.responses,
|
|
422
|
+
parameters: [
|
|
423
|
+
{ name: options.primaryKey, in: "path", required: true, type: "string" }
|
|
424
|
+
]
|
|
425
|
+
});
|
|
608
426
|
}
|
|
609
427
|
};
|
|
610
428
|
_DynamicDbRouter.setSecureParams = (method, payload, secure) => {
|
|
@@ -629,15 +447,15 @@ var RouterWrapper = class {
|
|
|
629
447
|
const newRouter = _express.Router.call(void 0, );
|
|
630
448
|
if (this.data.dbRouting) {
|
|
631
449
|
const avoidOptionsForDefinedApis = [];
|
|
632
|
-
if (_optionalChain([this, 'access',
|
|
633
|
-
if (_optionalChain([this, 'access',
|
|
634
|
-
if (_optionalChain([this, 'access',
|
|
635
|
-
if (_optionalChain([this, 'access',
|
|
636
|
-
if (_optionalChain([this, 'access',
|
|
450
|
+
if (_optionalChain([this, 'access', _10 => _10.data, 'access', _11 => _11.apis, 'optionalAccess', _12 => _12["/"], 'optionalAccess', _13 => _13.GET])) avoidOptionsForDefinedApis.push("LIST");
|
|
451
|
+
if (_optionalChain([this, 'access', _14 => _14.data, 'access', _15 => _15.apis, 'optionalAccess', _16 => _16["/:id"], 'optionalAccess', _17 => _17.GET])) avoidOptionsForDefinedApis.push("GET");
|
|
452
|
+
if (_optionalChain([this, 'access', _18 => _18.data, 'access', _19 => _19.apis, 'optionalAccess', _20 => _20["/"], 'optionalAccess', _21 => _21.POST])) avoidOptionsForDefinedApis.push("POST");
|
|
453
|
+
if (_optionalChain([this, 'access', _22 => _22.data, 'access', _23 => _23.apis, 'optionalAccess', _24 => _24["/:id"], 'optionalAccess', _25 => _25.PUT])) avoidOptionsForDefinedApis.push("PUT");
|
|
454
|
+
if (_optionalChain([this, 'access', _26 => _26.data, 'access', _27 => _27.apis, 'optionalAccess', _28 => _28["/:id"], 'optionalAccess', _29 => _29.DELETE])) avoidOptionsForDefinedApis.push("DELETE");
|
|
637
455
|
for (const avoid of _nullishCoalesce(this.data.dbRouting.avoid, () => ( []))) {
|
|
638
456
|
if (!avoidOptionsForDefinedApis.includes(avoid)) avoidOptionsForDefinedApis.push(avoid);
|
|
639
457
|
}
|
|
640
|
-
const { entity, bodyParameters, getParameters, primaryKey, secure, returningProps } = this.data.dbRouting;
|
|
458
|
+
const { entity, bodyParameters, getParameters, primaryKey, secure, returningProps, responses } = this.data.dbRouting;
|
|
641
459
|
DynamicDbRouter.createDbRouter({
|
|
642
460
|
entity,
|
|
643
461
|
bodyParameters,
|
|
@@ -648,7 +466,8 @@ var RouterWrapper = class {
|
|
|
648
466
|
tag: this.data.tag,
|
|
649
467
|
avoid: avoidOptionsForDefinedApis,
|
|
650
468
|
secure,
|
|
651
|
-
returningProps
|
|
469
|
+
returningProps,
|
|
470
|
+
responses
|
|
652
471
|
});
|
|
653
472
|
}
|
|
654
473
|
if (this.data.swaggerNewSchemas) {
|
|
@@ -658,7 +477,7 @@ var RouterWrapper = class {
|
|
|
658
477
|
const path3 = _path.startsWith("/") ? _path : `/${_path}`;
|
|
659
478
|
if (!this.data.apis || !this.data.apis[path3]) continue;
|
|
660
479
|
for (const method in this.data.apis[path3]) {
|
|
661
|
-
if ((_nullishCoalesce(_optionalChain([this, 'access',
|
|
480
|
+
if ((_nullishCoalesce(_optionalChain([this, 'access', _30 => _30.data, 'access', _31 => _31.apis, 'optionalAccess', _32 => _32[path3], 'optionalAccess', _33 => _33[method]]), () => ( void 0))) === void 0) continue;
|
|
662
481
|
const currentMethod = this.data.apis[path3][method];
|
|
663
482
|
const middlewares = _nullishCoalesce(currentMethod.middlewares, () => ( []));
|
|
664
483
|
const callBackFunction = async (req, res) => {
|
|
@@ -671,7 +490,7 @@ var RouterWrapper = class {
|
|
|
671
490
|
else res.status(handlerRes.status).send(handlerRes.result);
|
|
672
491
|
} catch (err) {
|
|
673
492
|
fullLogNok("api", `[${method}] ${this.data.basePath}${path3} => ${_nullishCoalesce(err.message, () => ( err))}`);
|
|
674
|
-
res.status(500).send(_nullishCoalesce(_optionalChain([err, 'optionalAccess',
|
|
493
|
+
res.status(500).send(_nullishCoalesce(_optionalChain([err, 'optionalAccess', _34 => _34.message]), () => ( err)));
|
|
675
494
|
}
|
|
676
495
|
};
|
|
677
496
|
if (method === "GET") newRouter.get(path3, middlewares, callBackFunction);
|
|
@@ -683,9 +502,12 @@ var RouterWrapper = class {
|
|
|
683
502
|
this.data.basePath,
|
|
684
503
|
path3,
|
|
685
504
|
method,
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
505
|
+
{
|
|
506
|
+
name: path3,
|
|
507
|
+
parameters: currentMethod.swaggerParameters,
|
|
508
|
+
body: currentMethod.swaggerBody ? currentMethod.swaggerBody.schema : void 0,
|
|
509
|
+
responses: currentMethod.swaggerResponses
|
|
510
|
+
}
|
|
689
511
|
);
|
|
690
512
|
}
|
|
691
513
|
}
|
|
@@ -700,7 +522,7 @@ var RouterWrapper = class {
|
|
|
700
522
|
basePath: this.data.basePath,
|
|
701
523
|
tag: this.data.tag,
|
|
702
524
|
avoid: this.data.dbRouting.avoid,
|
|
703
|
-
secure: _optionalChain([this, 'access',
|
|
525
|
+
secure: _optionalChain([this, 'access', _35 => _35.data, 'access', _36 => _36.dbRouting, 'optionalAccess', _37 => _37.secure])
|
|
704
526
|
});
|
|
705
527
|
}
|
|
706
528
|
return newRouter;
|
|
@@ -734,10 +556,10 @@ var SocketWrapper = class {
|
|
|
734
556
|
try {
|
|
735
557
|
if (!this.namespace) throw new Error("Socket.IO not initialized");
|
|
736
558
|
const metadata = client.handshake.query.metadata;
|
|
737
|
-
const finalClientId = this.data.clientConnectionKey ? _nullishCoalesce(_optionalChain([metadata, 'optionalAccess',
|
|
559
|
+
const finalClientId = this.data.clientConnectionKey ? _nullishCoalesce(_optionalChain([metadata, 'optionalAccess', _38 => _38[this.data.clientConnectionKey]]), () => ( client.id)) : client.id;
|
|
738
560
|
this.connectedClients.set(finalClientId, { socket: client, connectedAt: /* @__PURE__ */ new Date(), metadata });
|
|
739
561
|
if (this.data.afterClientConnect) this.data.afterClientConnect(this, client, metadata);
|
|
740
|
-
for (const eventName in _nullishCoalesce(this.data.listeners, () => ( []))) client.on(eventName, (...params) => _optionalChain([this, 'access',
|
|
562
|
+
for (const eventName in _nullishCoalesce(this.data.listeners, () => ( []))) client.on(eventName, (...params) => _optionalChain([this, 'access', _39 => _39.data, 'access', _40 => _40.listeners, 'optionalAccess', _41 => _41[eventName], 'call', _42 => _42(this, client, metadata, ...params)]));
|
|
741
563
|
client.on("disconnect", () => this.handleDisconnection(finalClientId));
|
|
742
564
|
} catch (err) {
|
|
743
565
|
fullLogNok(`SOCKETWRAPPER`, `[${this.socketNamespace.toUpperCase()}]`, err);
|
|
@@ -803,17 +625,17 @@ var SocketWrapper = class {
|
|
|
803
625
|
this.socketNamespace = data.socketNamespace;
|
|
804
626
|
this.data = data;
|
|
805
627
|
Object.keys(_nullishCoalesce(data.listeners, () => ( {}))).forEach((key) => {
|
|
806
|
-
this[key] = _optionalChain([data, 'access',
|
|
628
|
+
this[key] = _optionalChain([data, 'access', _43 => _43.listeners, 'optionalAccess', _44 => _44[key], 'access', _45 => _45.bind, 'call', _46 => _46(this)]);
|
|
807
629
|
});
|
|
808
630
|
}
|
|
809
631
|
};
|
|
810
632
|
|
|
811
633
|
// src/Starter.ts
|
|
812
634
|
|
|
635
|
+
|
|
813
636
|
var _http = require('http'); var _http2 = _interopRequireDefault(_http);
|
|
814
637
|
|
|
815
638
|
var _socketio = require('socket.io');
|
|
816
|
-
var _swaggeruiexpress = require('swagger-ui-express'); var _swaggeruiexpress2 = _interopRequireDefault(_swaggeruiexpress);
|
|
817
639
|
var Starter = class {
|
|
818
640
|
constructor(options) {
|
|
819
641
|
this.init = async (options) => {
|
|
@@ -827,24 +649,19 @@ var Starter = class {
|
|
|
827
649
|
let swaggerLogs = null;
|
|
828
650
|
if (options.swagger === void 0 || options.swagger) {
|
|
829
651
|
const swaggerUrl = `http://127.0.0.1:${options.projectConfig.SERVER_PORT}`;
|
|
830
|
-
Swagger.
|
|
831
|
-
app.get("/
|
|
832
|
-
|
|
833
|
-
res.json(openAPIDocument);
|
|
652
|
+
Swagger.setBaseUrl(swaggerUrl);
|
|
653
|
+
app.get("/docs", (_, res) => {
|
|
654
|
+
res.json(Swagger.generateDocument());
|
|
834
655
|
});
|
|
835
|
-
app.use("/
|
|
836
|
-
|
|
837
|
-
url: "/swagger"
|
|
838
|
-
}
|
|
839
|
-
}));
|
|
840
|
-
swaggerLogs = [`Swagger docs available at ${swaggerUrl}/swagger`, `Swagger UI available at ${swaggerUrl}/swagger-ui`];
|
|
656
|
+
app.use("/docs-ui", _express2.default.static(_expressoswagger.getUIPath.call(void 0, )));
|
|
657
|
+
swaggerLogs = [`Swagger docs available at ${swaggerUrl}/docs`, `Swagger UI available at ${swaggerUrl}/docs-ui`];
|
|
841
658
|
}
|
|
842
659
|
if (options.tokenOptions) DynamicDbRouter.setTokenInstance(options.tokenOptions.tokenInstance);
|
|
843
660
|
if (options.clientPath) {
|
|
844
661
|
const clientPath = _path3.default.resolve(process.cwd(), "client");
|
|
845
662
|
app.use(_express2.default.static(clientPath));
|
|
846
663
|
app.get("/apiUrl", (_, res) => res.send(options.projectConfig.API_URL));
|
|
847
|
-
if (_optionalChain([options, 'access',
|
|
664
|
+
if (_optionalChain([options, 'access', _47 => _47.tokenOptions, 'optionalAccess', _48 => _48.api])) {
|
|
848
665
|
app.get(_nullishCoalesce(options.tokenOptions.api.path, () => ( "/api/auth")), options.tokenOptions.api.callback);
|
|
849
666
|
}
|
|
850
667
|
app.get("*", (_, res) => {
|
|
@@ -904,9 +721,12 @@ var Token = class {
|
|
|
904
721
|
}
|
|
905
722
|
return this.keyStore.all({ use: "enc" })[0];
|
|
906
723
|
}
|
|
907
|
-
// Metodo per autorizzare una richiesta
|
|
724
|
+
// Metodo per autorizzare una richiesta (string is for sockets implementations outside this package)
|
|
908
725
|
async authorize(req) {
|
|
909
|
-
if (
|
|
726
|
+
if (typeof req === "string") {
|
|
727
|
+
const payload = await this.verifyJWE(req);
|
|
728
|
+
return payload;
|
|
729
|
+
} else if (this.TOKEN_COOKIE_NAME) {
|
|
910
730
|
const rawCookies = _nullishCoalesce(req.headers.cookie, () => ( ""));
|
|
911
731
|
const cookies = _cookie.parse.call(void 0, rawCookies);
|
|
912
732
|
const cookieToken = _nullishCoalesce(cookies[this.TOKEN_COOKIE_NAME], () => ( null));
|
|
@@ -914,7 +734,7 @@ var Token = class {
|
|
|
914
734
|
const payload = await this.verifyJWE(cookieToken);
|
|
915
735
|
return payload;
|
|
916
736
|
} else {
|
|
917
|
-
const token = _nullishCoalesce(_optionalChain([req, 'access',
|
|
737
|
+
const token = _nullishCoalesce(_optionalChain([req, 'access', _49 => _49.headers, 'access', _50 => _50.authorization, 'optionalAccess', _51 => _51.split, 'call', _52 => _52("Bearer "), 'optionalAccess', _53 => _53[1]]), () => ( null));
|
|
918
738
|
if (token === null) throw new Error("UNAUTH");
|
|
919
739
|
const payload = await this.verifyJWE(token);
|
|
920
740
|
return payload;
|