namirasoft-node 1.1.13 → 1.1.15
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/BaseApplication.d.ts +1 -1
- package/dist/BaseController.d.ts +7 -8
- package/dist/BaseController.js +23 -36
- package/dist/BaseController.js.map +1 -1
- package/dist/BaseSequelizeTable.d.ts +5 -1
- package/dist/BaseSequelizeTable.js +53 -0
- package/dist/BaseSequelizeTable.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
- package/src/BaseApplication.ts +1 -1
- package/src/BaseController.ts +29 -44
- package/src/BaseSequelizeTable.ts +58 -6
- package/src/index.ts +0 -1
- package/dist/HTTPMethod.d.ts +0 -6
- package/dist/HTTPMethod.js +0 -11
- package/dist/HTTPMethod.js.map +0 -1
- package/dist/Schema/ApplicationSchema.d.ts +0 -7
- package/dist/Schema/ApplicationSchema.js +0 -11
- package/dist/Schema/ApplicationSchema.js.map +0 -1
- package/dist/Schema/ControllerSchema.d.ts +0 -13
- package/dist/Schema/ControllerSchema.js +0 -17
- package/dist/Schema/ControllerSchema.js.map +0 -1
- package/dist/Schema/VariableSchema.d.ts +0 -13
- package/dist/Schema/VariableSchema.js +0 -33
- package/dist/Schema/VariableSchema.js.map +0 -1
- package/src/HTTPMethod.ts +0 -7
- package/src/Schema/ApplicationSchema.ts +0 -12
- package/src/Schema/ControllerSchema.ts +0 -21
- package/src/Schema/VariableSchema.ts +0 -25
|
@@ -2,7 +2,7 @@ import express, { Router } from 'express';
|
|
|
2
2
|
import { BaseDatabase } from './BaseDatabase';
|
|
3
3
|
import { ILogger } from "namirasoft-log";
|
|
4
4
|
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
5
|
-
import { ApplicationSchema } from '
|
|
5
|
+
import { ApplicationSchema } from 'namirasoft-schema';
|
|
6
6
|
export declare abstract class BaseApplication<D extends BaseDatabase> {
|
|
7
7
|
private title;
|
|
8
8
|
protected description: string;
|
package/dist/BaseController.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import * as express from 'express';
|
|
2
2
|
import { BaseDatabase } from './BaseDatabase';
|
|
3
|
-
import { SchemaLike } from 'joi';
|
|
4
3
|
import { AnomalyDetector } from './AnomalyDetector';
|
|
5
4
|
import { ILogger } from 'namirasoft-log';
|
|
6
5
|
import { Meta } from './Meta';
|
|
7
|
-
import { HTTPMethod } from '
|
|
6
|
+
import { HTTPMethod } from 'namirasoft-core';
|
|
8
7
|
import { BaseApplication } from './BaseApplication';
|
|
9
|
-
import { ControllerSchema } from '
|
|
10
|
-
import { VariableSchema } from './Schema/VariableSchema';
|
|
8
|
+
import { BaseVariableSchema, ControllerSchema } from 'namirasoft-schema';
|
|
11
9
|
export declare abstract class BaseController<D extends BaseDatabase, State, Props, Output> {
|
|
12
10
|
protected showLogAtTheBeginning: boolean;
|
|
13
11
|
protected showLogAtTheEnd: boolean;
|
|
@@ -31,10 +29,10 @@ export declare abstract class BaseController<D extends BaseDatabase, State, Prop
|
|
|
31
29
|
tag: string;
|
|
32
30
|
summary: string;
|
|
33
31
|
};
|
|
34
|
-
abstract getParameterSchema():
|
|
35
|
-
abstract getBodySchema():
|
|
36
|
-
abstract getQuerySchema():
|
|
37
|
-
abstract getOutputSchema():
|
|
32
|
+
abstract getParameterSchema(): BaseVariableSchema[];
|
|
33
|
+
abstract getBodySchema(): BaseVariableSchema | null;
|
|
34
|
+
abstract getQuerySchema(): BaseVariableSchema[];
|
|
35
|
+
abstract getOutputSchema(): BaseVariableSchema | null;
|
|
38
36
|
abstract getState(): Promise<State>;
|
|
39
37
|
abstract getProps(): Promise<Props>;
|
|
40
38
|
abstract preHandle(): Promise<void>;
|
|
@@ -42,4 +40,5 @@ export declare abstract class BaseController<D extends BaseDatabase, State, Prop
|
|
|
42
40
|
abstract postHandle(): Promise<void>;
|
|
43
41
|
run(): Promise<express.Response<any, Record<string, any>>>;
|
|
44
42
|
getSchema(): Promise<ControllerSchema>;
|
|
43
|
+
private checkParameterSchema;
|
|
45
44
|
}
|
package/dist/BaseController.js
CHANGED
|
@@ -8,16 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.BaseController = void 0;
|
|
16
13
|
const Meta_1 = require("./Meta");
|
|
17
|
-
const joi_1 = __importDefault(require("joi"));
|
|
18
14
|
const namirasoft_core_1 = require("namirasoft-core");
|
|
19
|
-
const
|
|
20
|
-
const VariableSchema_1 = require("./Schema/VariableSchema");
|
|
15
|
+
const namirasoft_schema_1 = require("namirasoft-schema");
|
|
21
16
|
class BaseController {
|
|
22
17
|
constructor(app, req, res) {
|
|
23
18
|
this.showLogAtTheBeginning = false;
|
|
@@ -50,28 +45,9 @@ class BaseController {
|
|
|
50
45
|
if (anomaly != null)
|
|
51
46
|
if (anomaly.isAnomaly(this.meta.ip, this.meta.url))
|
|
52
47
|
namirasoft_core_1.ErrorOperation.throwHTTP(403, 'Suspicious activity detected.');
|
|
53
|
-
// check
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const validation = yield joi_1.default.compile(bodySchema)
|
|
57
|
-
.prefs({ errors: { label: 'key' } })
|
|
58
|
-
.validate(this.req.body);
|
|
59
|
-
if (validation.error) {
|
|
60
|
-
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
61
|
-
namirasoft_core_1.ErrorOperation.throwHTTP(400, message);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
// check query validation
|
|
65
|
-
let querySchema = this.getQuerySchema();
|
|
66
|
-
if (querySchema != null) {
|
|
67
|
-
const validation = yield joi_1.default.compile(querySchema)
|
|
68
|
-
.prefs({ errors: { label: 'key' } })
|
|
69
|
-
.validate(this.req.query);
|
|
70
|
-
if (validation.error) {
|
|
71
|
-
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
72
|
-
namirasoft_core_1.ErrorOperation.throwHTTP(400, message);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
48
|
+
// check
|
|
49
|
+
namirasoft_schema_1.JoiValidator.check(this.getBodySchema(), this.req.body);
|
|
50
|
+
namirasoft_schema_1.JoiValidator.checkArray(this.getQuerySchema(), this.req.query);
|
|
75
51
|
// call controller
|
|
76
52
|
this.output = yield this.handle();
|
|
77
53
|
// postHandle
|
|
@@ -116,18 +92,29 @@ class BaseController {
|
|
|
116
92
|
}
|
|
117
93
|
getSchema() {
|
|
118
94
|
return __awaiter(this, void 0, void 0, function* () {
|
|
119
|
-
let
|
|
120
|
-
let
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
schema.parameters = yield VariableSchema_1.VariableSchema.parse(parameter);
|
|
125
|
-
schema.body = yield VariableSchema_1.VariableSchema.parse(body);
|
|
126
|
-
schema.queries = yield VariableSchema_1.VariableSchema.parse(query);
|
|
95
|
+
let info = this.getInfo();
|
|
96
|
+
let schema = new namirasoft_schema_1.ControllerSchema(info.method, info.path, info.tag, info.summary);
|
|
97
|
+
schema.parameters = this.getParameterSchema();
|
|
98
|
+
schema.body = this.getBodySchema();
|
|
99
|
+
schema.queries = this.getQuerySchema();
|
|
127
100
|
schema.output = this.getOutputSchema();
|
|
101
|
+
this.checkParameterSchema(schema.parameters, info.path);
|
|
128
102
|
return schema;
|
|
129
103
|
});
|
|
130
104
|
}
|
|
105
|
+
checkParameterSchema(parameters, path) {
|
|
106
|
+
let regex = /\/:(\w+)\//gm;
|
|
107
|
+
let ps = [];
|
|
108
|
+
let match;
|
|
109
|
+
while ((match = regex.exec("/" + path + "/")) !== null)
|
|
110
|
+
ps.push(match[1]);
|
|
111
|
+
for (let i = 0; i < ps.length; i++)
|
|
112
|
+
if (parameters.filter(p => p.name == ps[i]).length == 0)
|
|
113
|
+
throw new Error(`There is a parameter ${ps[i]} is path, but not in getParameterSchema function.`);
|
|
114
|
+
for (let i = 0; i < parameters.length; i++)
|
|
115
|
+
if (!ps.includes(parameters[i].name))
|
|
116
|
+
throw new Error(`There is a parameter ${parameters[i].name} is getParameterSchema function, but not in path.`);
|
|
117
|
+
}
|
|
131
118
|
}
|
|
132
119
|
exports.BaseController = BaseController;
|
|
133
120
|
//# sourceMappingURL=BaseController.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseController.js","sourceRoot":"","sources":["../src/BaseController.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BaseController.js","sourceRoot":"","sources":["../src/BaseController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,iCAA8B;AAC9B,qDAAwE;AAExE,yDAAuF;AAEvF,MAAsB,cAAc;IAchC,YAAY,GAAuB,EAAE,GAAoB,EAAE,GAAqB;QAZtE,0BAAqB,GAAY,KAAK,CAAC;QACvC,oBAAe,GAAY,IAAI,CAAC;QAatC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;IACnC,CAAC;IAeK,GAAG;;YAEL,IAAI,CAAC,IAAI,GAAG,IAAI,WAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC/B,SAAS;YACT,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;YACnB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;YACnB,IACA;gBACI,OAAO;gBACP,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACpB,IAAI,IAAI,CAAC,qBAAqB;oBAC1B,IAAI,CAAC,MAAM,CA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CAAC,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC;gBACnD,MAAM,IAAI,KAAK,CAAC,wBAAwB,EAAE,CAAC,CAAC,CAAC,mDAAmD,CAAC,CAAC;QAC1G,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE;YACtC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;gBAChC,MAAM,IAAI,KAAK,CAAC,wBAAwB,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,mDAAmD,CAAC,CAAC;IAC3H,CAAC;CACJ;AA1ID,wCA0IC"}
|
|
@@ -3,9 +3,13 @@ import { BaseSequelizeDatabase } from "./BaseSequelizeDatabase";
|
|
|
3
3
|
import { BaseTable } from "./BaseTable";
|
|
4
4
|
import { ModelCtor, FindOptions, Transaction, Attributes } from 'sequelize';
|
|
5
5
|
import { BaseSequelizeModel } from "./BaseSequelizeModel";
|
|
6
|
+
import { BaseVariableSchema } from "namirasoft-schema";
|
|
6
7
|
export declare class BaseSequelizeTable<D extends BaseSequelizeDatabase, M extends BaseSequelizeModel> extends BaseTable<D> {
|
|
7
8
|
model: ModelCtor<M>;
|
|
9
|
+
attributes: ModelAttributes<M, Attributes<M>>;
|
|
8
10
|
constructor(database: D);
|
|
9
11
|
find(options: FindOptions<Attributes<M>>, trx: Transaction | null): Promise<M>;
|
|
10
|
-
define
|
|
12
|
+
define(modelName: string, attributes: ModelAttributes<M, Attributes<M>>, options?: ModelOptions<M>): void;
|
|
13
|
+
getSchema(require: boolean, array: boolean): BaseVariableSchema;
|
|
14
|
+
private getVariable;
|
|
11
15
|
}
|
|
@@ -10,8 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.BaseSequelizeTable = void 0;
|
|
13
|
+
const sequelize_1 = require("sequelize");
|
|
13
14
|
const BaseTable_1 = require("./BaseTable");
|
|
14
15
|
const namirasoft_core_1 = require("namirasoft-core");
|
|
16
|
+
const namirasoft_schema_1 = require("namirasoft-schema");
|
|
15
17
|
class BaseSequelizeTable extends BaseTable_1.BaseTable {
|
|
16
18
|
constructor(database) {
|
|
17
19
|
super(database);
|
|
@@ -26,6 +28,7 @@ class BaseSequelizeTable extends BaseTable_1.BaseTable {
|
|
|
26
28
|
});
|
|
27
29
|
}
|
|
28
30
|
define(modelName, attributes, options) {
|
|
31
|
+
this.attributes = attributes;
|
|
29
32
|
if (!options)
|
|
30
33
|
options = {};
|
|
31
34
|
if (options.name == undefined)
|
|
@@ -51,6 +54,56 @@ class BaseSequelizeTable extends BaseTable_1.BaseTable {
|
|
|
51
54
|
options.updatedAt = true;
|
|
52
55
|
this.model = this.database.sequelize.define(modelName, attributes, options);
|
|
53
56
|
}
|
|
57
|
+
getSchema(require, array) {
|
|
58
|
+
let ans = new namirasoft_schema_1.ObjectSchema("", require, null);
|
|
59
|
+
let keys = Object.keys(this.attributes);
|
|
60
|
+
let secures = this.getSecureColumns();
|
|
61
|
+
for (let i = 0; i < keys.length; i++)
|
|
62
|
+
if (!secures.includes(keys[i])) {
|
|
63
|
+
const element = this.attributes[keys[i]];
|
|
64
|
+
ans.addField(this.getVariable(keys[i], element));
|
|
65
|
+
}
|
|
66
|
+
if (array)
|
|
67
|
+
return new namirasoft_schema_1.ArraySchema("", require, [ans]);
|
|
68
|
+
return ans;
|
|
69
|
+
}
|
|
70
|
+
getVariable(name, element) {
|
|
71
|
+
var _a;
|
|
72
|
+
let required = !((_a = element.allowNull) !== null && _a !== void 0 ? _a : false);
|
|
73
|
+
if (element.type == sequelize_1.DataTypes.BOOLEAN)
|
|
74
|
+
return new namirasoft_schema_1.BoolSchema(name, required);
|
|
75
|
+
else if (element.type == sequelize_1.DataTypes.TINYINT)
|
|
76
|
+
return new namirasoft_schema_1.TinyIntSchema(name, required);
|
|
77
|
+
else if (element.type == sequelize_1.DataTypes.SMALLINT)
|
|
78
|
+
return new namirasoft_schema_1.SmallIntSchema(name, required);
|
|
79
|
+
else if (element.type == sequelize_1.DataTypes.MEDIUMINT)
|
|
80
|
+
return new namirasoft_schema_1.MediumIntSchema(name, required);
|
|
81
|
+
else if (element.type == sequelize_1.DataTypes.INTEGER)
|
|
82
|
+
return new namirasoft_schema_1.IntegerSchema(name, required);
|
|
83
|
+
else if (element.type == sequelize_1.DataTypes.BIGINT)
|
|
84
|
+
return new namirasoft_schema_1.BigIntSchema(name, required);
|
|
85
|
+
else if (element.type == sequelize_1.DataTypes.FLOAT)
|
|
86
|
+
return new namirasoft_schema_1.FloatSchema(name, required, null);
|
|
87
|
+
else if (element.type == sequelize_1.DataTypes.DOUBLE)
|
|
88
|
+
return new namirasoft_schema_1.DoubleSchema(name, required, null);
|
|
89
|
+
else if (element.type == sequelize_1.DataTypes.DECIMAL)
|
|
90
|
+
return new namirasoft_schema_1.DecimalSchema(name, required, null);
|
|
91
|
+
else if (element.type == sequelize_1.DataTypes.REAL)
|
|
92
|
+
return new namirasoft_schema_1.RealSchema(name, required, null);
|
|
93
|
+
else if (element.type == sequelize_1.DataTypes.CHAR)
|
|
94
|
+
return new namirasoft_schema_1.StringSchema(name, required, element.type.length, element.type.length);
|
|
95
|
+
else if (element.type == sequelize_1.DataTypes.STRING || element.type == sequelize_1.DataTypes.TEXT)
|
|
96
|
+
return new namirasoft_schema_1.StringSchema(name, required, null, element.type.length);
|
|
97
|
+
else if (element.type == sequelize_1.DataTypes.DATE)
|
|
98
|
+
return new namirasoft_schema_1.DateTimeSchema(name, required);
|
|
99
|
+
else if (element.type == sequelize_1.DataTypes.DATEONLY)
|
|
100
|
+
return new namirasoft_schema_1.DateSchema(name, required);
|
|
101
|
+
else if (element.type == sequelize_1.DataTypes.TIME)
|
|
102
|
+
return new namirasoft_schema_1.TimeSchema(name, required);
|
|
103
|
+
else if (element.type == sequelize_1.DataTypes.JSON)
|
|
104
|
+
return new namirasoft_schema_1.AnySchema(name, required);
|
|
105
|
+
throw new Error("Unsupported datatype for schema: " + element.type);
|
|
106
|
+
}
|
|
54
107
|
}
|
|
55
108
|
exports.BaseSequelizeTable = BaseSequelizeTable;
|
|
56
109
|
//# sourceMappingURL=BaseSequelizeTable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseSequelizeTable.js","sourceRoot":"","sources":["../src/BaseSequelizeTable.ts"],"names":[],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"BaseSequelizeTable.js","sourceRoot":"","sources":["../src/BaseSequelizeTable.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAkG;AAElG,2CAAwC;AAGxC,qDAAiD;AACjD,yDAAkS;AAElS,MAAa,kBAAkF,SAAQ,qBAAY;IAI/G,YAAY,QAAW;QAEnB,KAAK,CAAC,QAAQ,CAAC,CAAC;IACpB,CAAC;IACK,IAAI,CAAC,OAAmC,EAAE,GAAuB;;YAEnE,OAAO,CAAC,WAAW,GAAG,GAAG,CAAC;YAC1B,IAAI,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAI,OAAO,CAAC,CAAC;YACjD,IAAI,KAAK,IAAI,IAAI;gBACb,OAAO,KAAK,CAAC;YACjB,MAAM,gCAAc,CAAC,OAAO,CAAC,GAAG,EAAE,kBAAkB,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5E,CAAC;KAAA;IACD,MAAM,CAAC,SAAiB,EAAE,UAA6C,EAAE,OAAyB;QAG9F,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO;YACR,OAAO,GAAG,EAAE,CAAC;QACjB,IAAI,OAAO,CAAC,IAAI,IAAI,SAAS;YACzB,OAAO,CAAC,IAAI,GAAG;gBACX,MAAM,EAAE,SAAS;gBACjB,QAAQ,EAAE,SAAS;aACtB,CAAC;QACN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,CAAC,CAAC;aACrC,IAAI,OAAO,CAAC,IAAI,IAAI,qBAAS,CAAC,IAAI;YACnC,OAAO,IAAI,6BAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxE,CAAC;CACJ;AAlGD,gDAkGC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,6 @@ export * from "./BaseSequelizeTable";
|
|
|
10
10
|
export * from "./BaseTable";
|
|
11
11
|
export * from "./EmailService";
|
|
12
12
|
export * from "./EnvService";
|
|
13
|
-
export * from "./HTTPMethod";
|
|
14
13
|
export * from "./IPOperation";
|
|
15
14
|
export * from "./Meta";
|
|
16
15
|
export * from "./OTPOperation";
|
package/dist/index.js
CHANGED
|
@@ -26,7 +26,6 @@ __exportStar(require("./BaseSequelizeTable"), exports);
|
|
|
26
26
|
__exportStar(require("./BaseTable"), exports);
|
|
27
27
|
__exportStar(require("./EmailService"), exports);
|
|
28
28
|
__exportStar(require("./EnvService"), exports);
|
|
29
|
-
__exportStar(require("./HTTPMethod"), exports);
|
|
30
29
|
__exportStar(require("./IPOperation"), exports);
|
|
31
30
|
__exportStar(require("./Meta"), exports);
|
|
32
31
|
__exportStar(require("./OTPOperation"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,oDAAkC;AAClC,wDAAsC;AACtC,mDAAiC;AACjC,iDAA+B;AAC/B,sDAAoC;AACpC,0DAAwC;AACxC,uDAAqC;AACrC,uDAAqC;AACrC,8CAA4B;AAC5B,iDAA+B;AAC/B,+CAA6B;AAC7B
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,oDAAkC;AAClC,wDAAsC;AACtC,mDAAiC;AACjC,iDAA+B;AAC/B,sDAAoC;AACpC,0DAAwC;AACxC,uDAAqC;AACrC,uDAAqC;AACrC,8CAA4B;AAC5B,iDAA+B;AAC/B,+CAA6B;AAC7B,gDAA8B;AAC9B,yCAAuB;AACvB,iDAA+B;AAC/B,yDAAuC;AACvC,4DAA0C"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "namirasoft-node",
|
|
3
3
|
"description": "Namira Software Corporation Node NPM Package",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.15",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"scripts": {},
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"@supercharge/request-ip": "^1.2.0",
|
|
12
12
|
"@types/cors": "^2.8.17",
|
|
13
13
|
"@types/express": "^4.17.21",
|
|
14
|
+
"@types/node": "^20.9.4",
|
|
14
15
|
"@types/nodemailer": "^6.4.14",
|
|
15
16
|
"@types/nodemailer-smtp-transport": "^2.7.8",
|
|
16
17
|
"@types/swagger-jsdoc": "^6.0.4",
|
|
@@ -18,14 +19,14 @@
|
|
|
18
19
|
"cors": "^2.8.5",
|
|
19
20
|
"express": "^4.18.2",
|
|
20
21
|
"joi": "^17.11.0",
|
|
21
|
-
"namirasoft-core": "^1.1.
|
|
22
|
+
"namirasoft-core": "^1.1.8",
|
|
22
23
|
"namirasoft-log": "^1.1.1",
|
|
24
|
+
"namirasoft-schema": "^1.1.26",
|
|
23
25
|
"nodemailer": "^6.9.7",
|
|
24
26
|
"nodemailer-smtp-transport": "^2.7.4",
|
|
25
27
|
"request-ip": "^3.3.0",
|
|
26
28
|
"sequelize": "^6.35.1",
|
|
27
29
|
"swagger-jsdoc": "^6.2.8",
|
|
28
|
-
"swagger-ui-express": "^5.0.0"
|
|
29
|
-
"@types/node": "^20.9.3"
|
|
30
|
+
"swagger-ui-express": "^5.0.0"
|
|
30
31
|
}
|
|
31
32
|
}
|
package/src/BaseApplication.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { ILogger } from "namirasoft-log";
|
|
|
8
8
|
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
9
9
|
import path from 'path';
|
|
10
10
|
import { PackageService } from 'namirasoft-core';
|
|
11
|
-
import { ApplicationSchema } from '
|
|
11
|
+
import { ApplicationSchema } from 'namirasoft-schema';
|
|
12
12
|
|
|
13
13
|
export abstract class BaseApplication<D extends BaseDatabase>
|
|
14
14
|
{
|
package/src/BaseController.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import * as express from 'express';
|
|
2
2
|
import { BaseDatabase } from './BaseDatabase';
|
|
3
|
-
import { SchemaLike } from 'joi';
|
|
4
3
|
import { AnomalyDetector } from './AnomalyDetector';
|
|
5
4
|
import { ILogger } from 'namirasoft-log';
|
|
6
5
|
import { Meta } from './Meta';
|
|
7
|
-
import
|
|
8
|
-
import { ErrorOperation, HTTPError } from 'namirasoft-core';
|
|
9
|
-
import { HTTPMethod } from './HTTPMethod';
|
|
6
|
+
import { ErrorOperation, HTTPError, HTTPMethod } from 'namirasoft-core';
|
|
10
7
|
import { BaseApplication } from './BaseApplication';
|
|
11
|
-
import { ControllerSchema } from '
|
|
12
|
-
import { VariableSchema } from './Schema/VariableSchema';
|
|
8
|
+
import { BaseVariableSchema, ControllerSchema, JoiValidator } from 'namirasoft-schema';
|
|
13
9
|
|
|
14
10
|
export abstract class BaseController<D extends BaseDatabase, State, Props, Output>
|
|
15
11
|
{
|
|
@@ -36,10 +32,10 @@ export abstract class BaseController<D extends BaseDatabase, State, Props, Outpu
|
|
|
36
32
|
abstract getDatabase(): D;
|
|
37
33
|
abstract getLogger(): ILogger;
|
|
38
34
|
abstract getInfo(): { method: HTTPMethod, path: string, tag: string, summary: string };
|
|
39
|
-
abstract getParameterSchema():
|
|
40
|
-
abstract getBodySchema():
|
|
41
|
-
abstract getQuerySchema():
|
|
42
|
-
abstract getOutputSchema():
|
|
35
|
+
abstract getParameterSchema(): BaseVariableSchema[];
|
|
36
|
+
abstract getBodySchema(): BaseVariableSchema | null;
|
|
37
|
+
abstract getQuerySchema(): BaseVariableSchema[];
|
|
38
|
+
abstract getOutputSchema(): BaseVariableSchema | null;
|
|
43
39
|
abstract getState(): Promise<State>;
|
|
44
40
|
abstract getProps(): Promise<Props>;
|
|
45
41
|
abstract preHandle(): Promise<void>;
|
|
@@ -73,32 +69,9 @@ export abstract class BaseController<D extends BaseDatabase, State, Props, Outpu
|
|
|
73
69
|
if (anomaly.isAnomaly(this.meta.ip, this.meta.url))
|
|
74
70
|
ErrorOperation.throwHTTP(403, 'Suspicious activity detected.');
|
|
75
71
|
|
|
76
|
-
// check
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
{
|
|
80
|
-
const validation = await Joi.compile(bodySchema)
|
|
81
|
-
.prefs({ errors: { label: 'key' } })
|
|
82
|
-
.validate(this.req.body);
|
|
83
|
-
if (validation.error)
|
|
84
|
-
{
|
|
85
|
-
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
86
|
-
ErrorOperation.throwHTTP(400, message);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
// check query validation
|
|
90
|
-
let querySchema = this.getQuerySchema();
|
|
91
|
-
if (querySchema != null)
|
|
92
|
-
{
|
|
93
|
-
const validation = await Joi.compile(querySchema)
|
|
94
|
-
.prefs({ errors: { label: 'key' } })
|
|
95
|
-
.validate(this.req.query);
|
|
96
|
-
if (validation.error)
|
|
97
|
-
{
|
|
98
|
-
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
99
|
-
ErrorOperation.throwHTTP(400, message);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
72
|
+
// check
|
|
73
|
+
JoiValidator.check(this.getBodySchema(), this.req.body);
|
|
74
|
+
JoiValidator.checkArray(this.getQuerySchema(), this.req.query);
|
|
102
75
|
|
|
103
76
|
// call controller
|
|
104
77
|
this.output = await this.handle();
|
|
@@ -149,15 +122,27 @@ export abstract class BaseController<D extends BaseDatabase, State, Props, Outpu
|
|
|
149
122
|
}
|
|
150
123
|
async getSchema(): Promise<ControllerSchema>
|
|
151
124
|
{
|
|
152
|
-
let
|
|
153
|
-
let
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
schema.parameters = await VariableSchema.parse(parameter);
|
|
158
|
-
schema.body = await VariableSchema.parse(body);
|
|
159
|
-
schema.queries = await VariableSchema.parse(query);
|
|
125
|
+
let info = this.getInfo();
|
|
126
|
+
let schema = new ControllerSchema(info.method, info.path, info.tag, info.summary);
|
|
127
|
+
schema.parameters = this.getParameterSchema();
|
|
128
|
+
schema.body = this.getBodySchema();
|
|
129
|
+
schema.queries = this.getQuerySchema();
|
|
160
130
|
schema.output = this.getOutputSchema();
|
|
131
|
+
this.checkParameterSchema(schema.parameters, info.path);
|
|
161
132
|
return schema;
|
|
162
133
|
}
|
|
134
|
+
private checkParameterSchema(parameters: BaseVariableSchema[], path: string)
|
|
135
|
+
{
|
|
136
|
+
let regex = /\/:(\w+)\//gm;
|
|
137
|
+
let ps: string[] = []
|
|
138
|
+
let match;
|
|
139
|
+
while ((match = regex.exec("/" + path + "/")) !== null)
|
|
140
|
+
ps.push(match[1]);
|
|
141
|
+
for (let i = 0; i < ps.length; i++)
|
|
142
|
+
if (parameters.filter(p => p.name == ps[i]).length == 0)
|
|
143
|
+
throw new Error(`There is a parameter ${ps[i]} is path, but not in getParameterSchema function.`);
|
|
144
|
+
for (let i = 0; i < parameters.length; i++)
|
|
145
|
+
if (!ps.includes(parameters[i].name))
|
|
146
|
+
throw new Error(`There is a parameter ${parameters[i].name} is getParameterSchema function, but not in path.`);
|
|
147
|
+
}
|
|
163
148
|
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import { ModelOptions, ModelAttributes } from "sequelize";
|
|
1
|
+
import { ModelOptions, ModelAttributes, ModelAttributeColumnOptions, DataTypes } from "sequelize";
|
|
2
2
|
import { BaseSequelizeDatabase } from "./BaseSequelizeDatabase";
|
|
3
3
|
import { BaseTable } from "./BaseTable";
|
|
4
4
|
import { ModelCtor, FindOptions, Transaction, Attributes } from 'sequelize';
|
|
5
5
|
import { BaseSequelizeModel } from "./BaseSequelizeModel";
|
|
6
6
|
import { ErrorOperation } from "namirasoft-core";
|
|
7
|
+
import { AnySchema, ArraySchema, BaseVariableSchema, BigIntSchema, BoolSchema, DateSchema, DateTimeSchema, DecimalSchema, DoubleSchema, FloatSchema, IntegerSchema, MediumIntSchema, ObjectSchema, RealSchema, SmallIntSchema, StringSchema, TimeSchema, TinyIntSchema } from "namirasoft-schema";
|
|
7
8
|
|
|
8
9
|
export class BaseSequelizeTable<D extends BaseSequelizeDatabase, M extends BaseSequelizeModel> extends BaseTable<D>
|
|
9
10
|
{
|
|
10
11
|
model!: ModelCtor<M>;
|
|
12
|
+
attributes!: ModelAttributes<M, Attributes<M>>;
|
|
11
13
|
constructor(database: D)
|
|
12
14
|
{
|
|
13
15
|
super(database);
|
|
14
16
|
}
|
|
15
|
-
async find(options: FindOptions<Attributes<M>>, trx: Transaction | null): Promise<M>
|
|
17
|
+
async find(options: FindOptions<Attributes<M>>, trx: Transaction | null): Promise<M>
|
|
16
18
|
{
|
|
17
19
|
options.transaction = trx;
|
|
18
20
|
let value = await this.model.findOne<M>(options);
|
|
@@ -20,12 +22,10 @@ export class BaseSequelizeTable<D extends BaseSequelizeDatabase, M extends BaseS
|
|
|
20
22
|
return value;
|
|
21
23
|
throw ErrorOperation.getHTTP(404, "Could not found " + this.model.name);
|
|
22
24
|
}
|
|
23
|
-
define<
|
|
24
|
-
modelName: string,
|
|
25
|
-
attributes: ModelAttributes<M, TAttributes>,
|
|
26
|
-
options?: ModelOptions<M>
|
|
25
|
+
define(modelName: string, attributes: ModelAttributes<M, Attributes<M>>, options?: ModelOptions<M>
|
|
27
26
|
): void
|
|
28
27
|
{
|
|
28
|
+
this.attributes = attributes;
|
|
29
29
|
if (!options)
|
|
30
30
|
options = {};
|
|
31
31
|
if (options.name == undefined)
|
|
@@ -52,4 +52,56 @@ export class BaseSequelizeTable<D extends BaseSequelizeDatabase, M extends BaseS
|
|
|
52
52
|
|
|
53
53
|
this.model = this.database.sequelize.define(modelName, attributes, options);
|
|
54
54
|
}
|
|
55
|
+
getSchema(require: boolean, array: boolean): BaseVariableSchema
|
|
56
|
+
{
|
|
57
|
+
let ans = new ObjectSchema("", require, null);
|
|
58
|
+
let keys = Object.keys(this.attributes);
|
|
59
|
+
let secures = this.getSecureColumns();
|
|
60
|
+
for (let i = 0; i < keys.length; i++)
|
|
61
|
+
if (!secures.includes(keys[i]))
|
|
62
|
+
{
|
|
63
|
+
const element = this.attributes[keys[i]] as ModelAttributeColumnOptions;
|
|
64
|
+
ans.addField(this.getVariable(keys[i], element));
|
|
65
|
+
}
|
|
66
|
+
if (array)
|
|
67
|
+
return new ArraySchema("", require, [ans]);
|
|
68
|
+
return ans;
|
|
69
|
+
}
|
|
70
|
+
private getVariable(name: string, element: ModelAttributeColumnOptions): BaseVariableSchema
|
|
71
|
+
{
|
|
72
|
+
let required = !(element.allowNull ?? false);
|
|
73
|
+
if (element.type == DataTypes.BOOLEAN)
|
|
74
|
+
return new BoolSchema(name, required);
|
|
75
|
+
else if (element.type == DataTypes.TINYINT)
|
|
76
|
+
return new TinyIntSchema(name, required);
|
|
77
|
+
else if (element.type == DataTypes.SMALLINT)
|
|
78
|
+
return new SmallIntSchema(name, required);
|
|
79
|
+
else if (element.type == DataTypes.MEDIUMINT)
|
|
80
|
+
return new MediumIntSchema(name, required);
|
|
81
|
+
else if (element.type == DataTypes.INTEGER)
|
|
82
|
+
return new IntegerSchema(name, required);
|
|
83
|
+
else if (element.type == DataTypes.BIGINT)
|
|
84
|
+
return new BigIntSchema(name, required);
|
|
85
|
+
else if (element.type == DataTypes.FLOAT)
|
|
86
|
+
return new FloatSchema(name, required, null);
|
|
87
|
+
else if (element.type == DataTypes.DOUBLE)
|
|
88
|
+
return new DoubleSchema(name, required, null);
|
|
89
|
+
else if (element.type == DataTypes.DECIMAL)
|
|
90
|
+
return new DecimalSchema(name, required, null);
|
|
91
|
+
else if (element.type == DataTypes.REAL)
|
|
92
|
+
return new RealSchema(name, required, null);
|
|
93
|
+
else if (element.type == DataTypes.CHAR)
|
|
94
|
+
return new StringSchema(name, required, element.type.length, element.type.length);
|
|
95
|
+
else if (element.type == DataTypes.STRING || element.type == DataTypes.TEXT)
|
|
96
|
+
return new StringSchema(name, required, null, element.type.length);
|
|
97
|
+
else if (element.type == DataTypes.DATE)
|
|
98
|
+
return new DateTimeSchema(name, required);
|
|
99
|
+
else if (element.type == DataTypes.DATEONLY)
|
|
100
|
+
return new DateSchema(name, required);
|
|
101
|
+
else if (element.type == DataTypes.TIME)
|
|
102
|
+
return new TimeSchema(name, required);
|
|
103
|
+
else if (element.type == DataTypes.JSON)
|
|
104
|
+
return new AnySchema(name, required);
|
|
105
|
+
throw new Error("Unsupported datatype for schema: " + element.type);
|
|
106
|
+
}
|
|
55
107
|
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,6 @@ export * from "./BaseSequelizeTable";
|
|
|
10
10
|
export * from "./BaseTable";
|
|
11
11
|
export * from "./EmailService";
|
|
12
12
|
export * from "./EnvService";
|
|
13
|
-
export * from "./HTTPMethod";
|
|
14
13
|
export * from "./IPOperation";
|
|
15
14
|
export * from "./Meta";
|
|
16
15
|
export * from "./OTPOperation";
|
package/dist/HTTPMethod.d.ts
DELETED
package/dist/HTTPMethod.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HTTPMethod = void 0;
|
|
4
|
-
var HTTPMethod;
|
|
5
|
-
(function (HTTPMethod) {
|
|
6
|
-
HTTPMethod[HTTPMethod["GET"] = 0] = "GET";
|
|
7
|
-
HTTPMethod[HTTPMethod["POST"] = 1] = "POST";
|
|
8
|
-
HTTPMethod[HTTPMethod["PUT"] = 2] = "PUT";
|
|
9
|
-
HTTPMethod[HTTPMethod["DELETE"] = 3] = "DELETE";
|
|
10
|
-
})(HTTPMethod || (exports.HTTPMethod = HTTPMethod = {}));
|
|
11
|
-
//# sourceMappingURL=HTTPMethod.js.map
|
package/dist/HTTPMethod.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"HTTPMethod.js","sourceRoot":"","sources":["../src/HTTPMethod.ts"],"names":[],"mappings":";;;AAAA,IAAY,UAMX;AAND,WAAY,UAAU;IAElB,yCAAG,CAAA;IACH,2CAAI,CAAA;IACJ,yCAAG,CAAA;IACH,+CAAM,CAAA;AACV,CAAC,EANW,UAAU,0BAAV,UAAU,QAMrB"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ApplicationSchema = void 0;
|
|
4
|
-
class ApplicationSchema {
|
|
5
|
-
constructor(pkg) {
|
|
6
|
-
this.controllers = [];
|
|
7
|
-
this.pkg = pkg;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.ApplicationSchema = ApplicationSchema;
|
|
11
|
-
//# sourceMappingURL=ApplicationSchema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ApplicationSchema.js","sourceRoot":"","sources":["../../src/Schema/ApplicationSchema.ts"],"names":[],"mappings":";;;AAGA,MAAa,iBAAiB;IAI1B,YAAY,GAAmB;QAD/B,gBAAW,GAAuB,EAAE,CAAC;QAGjC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACnB,CAAC;CACJ;AARD,8CAQC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { HTTPMethod } from "../HTTPMethod";
|
|
2
|
-
import { VariableSchema } from "./VariableSchema";
|
|
3
|
-
export declare class ControllerSchema {
|
|
4
|
-
method: HTTPMethod;
|
|
5
|
-
path: string;
|
|
6
|
-
tag: string;
|
|
7
|
-
summary: string;
|
|
8
|
-
parameters: VariableSchema[];
|
|
9
|
-
body: VariableSchema[];
|
|
10
|
-
queries: VariableSchema[];
|
|
11
|
-
output: VariableSchema | null;
|
|
12
|
-
constructor(method: HTTPMethod, path: string, tag: string, summary: string);
|
|
13
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ControllerSchema = void 0;
|
|
4
|
-
class ControllerSchema {
|
|
5
|
-
constructor(method, path, tag, summary) {
|
|
6
|
-
this.parameters = [];
|
|
7
|
-
this.body = [];
|
|
8
|
-
this.queries = [];
|
|
9
|
-
this.output = null;
|
|
10
|
-
this.method = method;
|
|
11
|
-
this.path = path;
|
|
12
|
-
this.tag = tag;
|
|
13
|
-
this.summary = summary;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.ControllerSchema = ControllerSchema;
|
|
17
|
-
//# sourceMappingURL=ControllerSchema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ControllerSchema.js","sourceRoot":"","sources":["../../src/Schema/ControllerSchema.ts"],"names":[],"mappings":";;;AAGA,MAAa,gBAAgB;IAUzB,YAAY,MAAkB,EAAE,IAAY,EAAE,GAAW,EAAE,OAAe;QAJ1E,eAAU,GAAqB,EAAE,CAAC;QAClC,SAAI,GAAqB,EAAE,CAAC;QAC5B,YAAO,GAAqB,EAAE,CAAC;QAC/B,WAAM,GAA0B,IAAI,CAAC;QAGjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CACJ;AAjBD,4CAiBC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { SchemaLike } from "joi";
|
|
2
|
-
export declare class VariableSchema {
|
|
3
|
-
static parse(schema: SchemaLike | null): Promise<VariableSchema[]>;
|
|
4
|
-
name: string;
|
|
5
|
-
type: string;
|
|
6
|
-
nullable: boolean;
|
|
7
|
-
array: number;
|
|
8
|
-
object: VariableSchema[];
|
|
9
|
-
valids: string[];
|
|
10
|
-
min: number | null;
|
|
11
|
-
max: number | null;
|
|
12
|
-
constructor(name: string, type: string, nullable: boolean);
|
|
13
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.VariableSchema = void 0;
|
|
13
|
-
class VariableSchema {
|
|
14
|
-
static parse(schema) {
|
|
15
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
if (!schema)
|
|
17
|
-
return [];
|
|
18
|
-
return [];
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
constructor(name, type, nullable) {
|
|
22
|
-
this.array = 0;
|
|
23
|
-
this.object = [];
|
|
24
|
-
this.valids = [];
|
|
25
|
-
this.min = null;
|
|
26
|
-
this.max = null;
|
|
27
|
-
this.name = name;
|
|
28
|
-
this.type = type;
|
|
29
|
-
this.nullable = nullable;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.VariableSchema = VariableSchema;
|
|
33
|
-
//# sourceMappingURL=VariableSchema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VariableSchema.js","sourceRoot":"","sources":["../../src/Schema/VariableSchema.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,MAAa,cAAc;IAEvB,MAAM,CAAO,KAAK,CAAC,MAAyB;;YAExC,IAAI,CAAC,MAAM;gBACP,OAAO,EAAE,CAAC;YACd,OAAO,EAAE,CAAC;QACd,CAAC;KAAA;IASD,YAAY,IAAY,EAAE,IAAY,EAAE,QAAiB;QALzD,UAAK,GAAW,CAAC,CAAC;QAClB,WAAM,GAAqB,EAAE,CAAC;QAC9B,WAAM,GAAa,EAAE,CAAC;QACtB,QAAG,GAAkB,IAAI,CAAC;QAC1B,QAAG,GAAkB,IAAI,CAAC;QAGtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;CACJ;AAtBD,wCAsBC"}
|
package/src/HTTPMethod.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { PackageService } from "namirasoft-core";
|
|
2
|
-
import { ControllerSchema } from "./ControllerSchema";
|
|
3
|
-
|
|
4
|
-
export class ApplicationSchema
|
|
5
|
-
{
|
|
6
|
-
pkg: PackageService;
|
|
7
|
-
controllers: ControllerSchema[] = [];
|
|
8
|
-
constructor(pkg: PackageService)
|
|
9
|
-
{
|
|
10
|
-
this.pkg = pkg;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { HTTPMethod } from "../HTTPMethod";
|
|
2
|
-
import { VariableSchema } from "./VariableSchema";
|
|
3
|
-
|
|
4
|
-
export class ControllerSchema
|
|
5
|
-
{
|
|
6
|
-
method: HTTPMethod;
|
|
7
|
-
path: string;
|
|
8
|
-
tag: string;
|
|
9
|
-
summary: string;
|
|
10
|
-
parameters: VariableSchema[] = [];
|
|
11
|
-
body: VariableSchema[] = [];
|
|
12
|
-
queries: VariableSchema[] = [];
|
|
13
|
-
output: VariableSchema | null = null;
|
|
14
|
-
constructor(method: HTTPMethod, path: string, tag: string, summary: string)
|
|
15
|
-
{
|
|
16
|
-
this.method = method;
|
|
17
|
-
this.path = path;
|
|
18
|
-
this.tag = tag;
|
|
19
|
-
this.summary = summary;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { SchemaLike } from "joi";
|
|
2
|
-
|
|
3
|
-
export class VariableSchema
|
|
4
|
-
{
|
|
5
|
-
static async parse(schema: SchemaLike | null): Promise<VariableSchema[]>
|
|
6
|
-
{
|
|
7
|
-
if (!schema)
|
|
8
|
-
return [];
|
|
9
|
-
return [];
|
|
10
|
-
}
|
|
11
|
-
name: string;
|
|
12
|
-
type: string;
|
|
13
|
-
nullable: boolean;
|
|
14
|
-
array: number = 0;
|
|
15
|
-
object: VariableSchema[] = [];
|
|
16
|
-
valids: string[] = [];
|
|
17
|
-
min: number | null = null;
|
|
18
|
-
max: number | null = null;
|
|
19
|
-
constructor(name: string, type: string, nullable: boolean)
|
|
20
|
-
{
|
|
21
|
-
this.name = name;
|
|
22
|
-
this.type = type;
|
|
23
|
-
this.nullable = nullable;
|
|
24
|
-
}
|
|
25
|
-
}
|