namirasoft-node 1.1.13 → 1.1.14

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.
@@ -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 './Schema/ApplicationSchema';
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;
@@ -1,13 +1,10 @@
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 './HTTPMethod';
8
6
  import { BaseApplication } from './BaseApplication';
9
- import { ControllerSchema } from './Schema/ControllerSchema';
10
- import { VariableSchema } from './Schema/VariableSchema';
7
+ import { HTTPMethod, BaseVariableSchema, ControllerSchema } from 'namirasoft-schema';
11
8
  export declare abstract class BaseController<D extends BaseDatabase, State, Props, Output> {
12
9
  protected showLogAtTheBeginning: boolean;
13
10
  protected showLogAtTheEnd: boolean;
@@ -31,10 +28,10 @@ export declare abstract class BaseController<D extends BaseDatabase, State, Prop
31
28
  tag: string;
32
29
  summary: string;
33
30
  };
34
- abstract getParameterSchema(): SchemaLike | null;
35
- abstract getBodySchema(): SchemaLike | null;
36
- abstract getQuerySchema(): SchemaLike | null;
37
- abstract getOutputSchema(): VariableSchema;
31
+ abstract getParameterSchema(): BaseVariableSchema[];
32
+ abstract getBodySchema(): BaseVariableSchema | null;
33
+ abstract getQuerySchema(): BaseVariableSchema[];
34
+ abstract getOutputSchema(): BaseVariableSchema | null;
38
35
  abstract getState(): Promise<State>;
39
36
  abstract getProps(): Promise<Props>;
40
37
  abstract preHandle(): Promise<void>;
@@ -42,4 +39,5 @@ export declare abstract class BaseController<D extends BaseDatabase, State, Prop
42
39
  abstract postHandle(): Promise<void>;
43
40
  run(): Promise<express.Response<any, Record<string, any>>>;
44
41
  getSchema(): Promise<ControllerSchema>;
42
+ private checkParameterSchema;
45
43
  }
@@ -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 ControllerSchema_1 = require("./Schema/ControllerSchema");
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 body validation
54
- let bodySchema = this.getBodySchema();
55
- if (bodySchema != null) {
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 x = this.getInfo();
120
- let parameter = this.getParameterSchema();
121
- let body = this.getBodySchema();
122
- let query = this.getQuerySchema();
123
- let schema = new ControllerSchema_1.ControllerSchema(x.method, x.path, x.tag, x.summary);
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
- {"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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"}
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,+CAA6B;AAC7B,gDAA8B;AAC9B,yCAAuB;AACvB,iDAA+B;AAC/B,yDAAuC;AACvC,4DAA0C"}
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.13",
4
+ "version": "1.1.14",
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",
@@ -20,12 +21,12 @@
20
21
  "joi": "^17.11.0",
21
22
  "namirasoft-core": "^1.1.7",
22
23
  "namirasoft-log": "^1.1.1",
24
+ "namirasoft-schema": "^1.1.24",
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
  }
@@ -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 './Schema/ApplicationSchema';
11
+ import { ApplicationSchema } from 'namirasoft-schema';
12
12
 
13
13
  export abstract class BaseApplication<D extends BaseDatabase>
14
14
  {
@@ -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 Joi from 'joi';
8
6
  import { ErrorOperation, HTTPError } from 'namirasoft-core';
9
- import { HTTPMethod } from './HTTPMethod';
10
7
  import { BaseApplication } from './BaseApplication';
11
- import { ControllerSchema } from './Schema/ControllerSchema';
12
- import { VariableSchema } from './Schema/VariableSchema';
8
+ import { HTTPMethod, 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(): SchemaLike | null;
40
- abstract getBodySchema(): SchemaLike | null;
41
- abstract getQuerySchema(): SchemaLike | null;
42
- abstract getOutputSchema(): VariableSchema;
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 body validation
77
- let bodySchema = this.getBodySchema();
78
- if (bodySchema != null)
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 x = this.getInfo();
153
- let parameter = this.getParameterSchema();
154
- let body = this.getBodySchema();
155
- let query = this.getQuerySchema();
156
- let schema = new ControllerSchema(x.method, x.path, x.tag, x.summary);
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
  }
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";
@@ -1,6 +0,0 @@
1
- export declare enum HTTPMethod {
2
- GET = 0,
3
- POST = 1,
4
- PUT = 2,
5
- DELETE = 3
6
- }
@@ -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
@@ -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,7 +0,0 @@
1
- import { PackageService } from "namirasoft-core";
2
- import { ControllerSchema } from "./ControllerSchema";
3
- export declare class ApplicationSchema {
4
- pkg: PackageService;
5
- controllers: ControllerSchema[];
6
- constructor(pkg: PackageService);
7
- }
@@ -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,7 +0,0 @@
1
- export enum HTTPMethod
2
- {
3
- GET,
4
- POST,
5
- PUT,
6
- DELETE
7
- }
@@ -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
- }