namirasoft-node 1.3.74 → 1.3.76

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/src/BaseTable.ts CHANGED
@@ -1,107 +1,105 @@
1
- import { ArraySchema, BaseTypeSchema, BaseVariableSchema, ObjectSchema } from "namirasoft-schema";
2
- import { BaseDatabase } from "./BaseDatabase";
3
- import { ErrorOperation } from "namirasoft-core";
4
-
5
- export abstract class BaseTable<D extends BaseDatabase, ModelColumnOption>
6
- {
7
- database: D;
8
- constructor(database: D)
9
- {
10
- this.database = database;
11
- }
12
- public abstract getName(): string;
13
- public addSecureColumns(columns: string[]): void
14
- {
15
- columns.toString();
16
- }
17
- public addReadOnlyColumns(columns: string[]): void
18
- {
19
- columns.toString();
20
- }
21
- public getSecureColumns(): string[]
22
- {
23
- let columns: string[] = [];
24
- this.addSecureColumns(columns);
25
- return columns;
26
- }
27
- public getReadOnlyColumns(): string[]
28
- {
29
- let columns: string[] = [];
30
- this.addReadOnlyColumns(columns);
31
- return columns;
32
- }
33
- public secure(obj: any)
34
- {
35
- if (obj.dataValues)
36
- obj = obj.dataValues;
37
- let secureColumns: string[] = this.getSecureColumns();
38
- secureColumns.forEach(col => delete obj[col]);
39
- return obj;
40
- }
41
- public getArraySchema(require: boolean, name?: string): ArraySchema
42
- {
43
- let ans = this.getSchema(require, name);
44
- return new ArraySchema(require, [ans]);
45
- }
46
- public getSchema(require: boolean, name?: string): ObjectSchema
47
- {
48
- let secureColumns: string[] = this.getSecureColumns();
49
- let readOnlyColumns: string[] = this.getReadOnlyColumns();
50
- let ans = new ObjectSchema(name ?? this.getName(), require, null);
51
- this.forEachColumn((name, element) =>
52
- {
53
- if (!secureColumns.includes(name))
54
- ans.addField(this.getVariableSchema(name, element, readOnlyColumns.includes(name)));
55
- });
56
- return ans;
57
- }
58
- public checkColumn(column: string)
59
- {
60
- let columns: string[] = [];
61
- this.forEachColumn(name => columns.push(name));
62
- if (!columns.includes(column))
63
- ErrorOperation.throwHTTP(404, `Column '${column}' not found`);
64
- }
65
- public abstract forEachColumn(handler: (name: string, column: ModelColumnOption) => void): void;
66
- private getVariableSchema(name: string, element: ModelColumnOption, read_only: boolean): BaseVariableSchema
67
- {
68
- let schema = this.getTypeSchema(element);
69
- schema.read_only = read_only;
70
- let es = this.getExamples();
71
- schema.example = es[name] ?? "";
72
- return new BaseVariableSchema(name, schema);
73
- }
74
- protected abstract getTypeSchema(element: ModelColumnOption): BaseTypeSchema;
75
- protected getExamples(): { [name: string]: string }
76
- {
77
- return {};
78
- }
79
- public getSchemaNames(): string[]
80
- {
81
- return [];
82
- }
83
- public getSchemaByName(require: boolean, name?: string): ObjectSchema
84
- {
85
- let ans = this.getSchema(require, name);
86
- if (name)
87
- {
88
- let names = this.getSchemaNames();
89
- if (!names.includes(name))
90
- throw new Error("Wrong schema name was provided: " + name);
91
- this.setSchemaByName(ans);
92
- }
93
- return ans;
94
- }
95
- public getArraySchemaByName(require: boolean, name?: string): ArraySchema
96
- {
97
- let ans = this.getSchemaByName(require, name);
98
- return new ArraySchema(require, [ans]);
99
- }
100
- protected setSchemaByName(_: ObjectSchema): void
101
- {
102
- }
103
- public getNotFoundError(conditions: any | null)
104
- {
105
- return ErrorOperation.getHTTP(404, "Could not found " + this.getName() + " for " + JSON.stringify(conditions));
106
- }
1
+ import { ArraySchema, BaseTypeSchema, BaseVariableSchema, ObjectSchema } from "namirasoft-schema";
2
+ import { BaseDatabase } from "./BaseDatabase";
3
+ import { ErrorOperation } from "namirasoft-core";
4
+
5
+ export abstract class BaseTable<D extends BaseDatabase, ModelColumnOption>
6
+ {
7
+ database: D;
8
+ constructor(database: D)
9
+ {
10
+ this.database = database;
11
+ }
12
+ public abstract getName(): string;
13
+ public addSecureColumns(columns: string[]): string[]
14
+ {
15
+ return columns;
16
+ }
17
+ public addReadOnlyColumns(columns: string[]): string[]
18
+ {
19
+ return columns;
20
+ }
21
+ public getSecureColumns(): string[]
22
+ {
23
+ let columns: string[] = [];
24
+ return this.addSecureColumns(columns);
25
+ }
26
+ public getReadOnlyColumns(): string[]
27
+ {
28
+ let columns: string[] = [];
29
+ return this.addReadOnlyColumns(columns);
30
+ }
31
+ public secure(obj: any)
32
+ {
33
+ if (obj.dataValues)
34
+ obj = obj.dataValues;
35
+ let secureColumns: string[] = this.getSecureColumns();
36
+ secureColumns.forEach(col => delete obj[col]);
37
+ return obj;
38
+ }
39
+ public getArraySchema(require: boolean, name?: string): ArraySchema
40
+ {
41
+ let ans = this.getSchema(require, name);
42
+ return new ArraySchema(require, [ans]);
43
+ }
44
+ public getSchema(require: boolean, name?: string): ObjectSchema
45
+ {
46
+ let secureColumns: string[] = this.getSecureColumns();
47
+ let readOnlyColumns: string[] = this.getReadOnlyColumns();
48
+ let ans = new ObjectSchema(name ?? this.getName(), require, null);
49
+ this.forEachColumn((name, element) =>
50
+ {
51
+ if (!secureColumns.includes(name))
52
+ ans.addField(this.getVariableSchema(name, element, readOnlyColumns.includes(name)));
53
+ });
54
+ return ans;
55
+ }
56
+ public checkColumn(column: string)
57
+ {
58
+ let columns: string[] = [];
59
+ this.forEachColumn(name => columns.push(name));
60
+ if (!columns.includes(column))
61
+ ErrorOperation.throwHTTP(404, `Column '${column}' not found`);
62
+ }
63
+ public abstract forEachColumn(handler: (name: string, column: ModelColumnOption) => void): void;
64
+ private getVariableSchema(name: string, element: ModelColumnOption, read_only: boolean): BaseVariableSchema
65
+ {
66
+ let schema = this.getTypeSchema(element);
67
+ schema.read_only = read_only;
68
+ let es = this.getExamples();
69
+ schema.example = es[name] ?? "";
70
+ return new BaseVariableSchema(name, schema);
71
+ }
72
+ protected abstract getTypeSchema(element: ModelColumnOption): BaseTypeSchema;
73
+ protected getExamples(): { [name: string]: string }
74
+ {
75
+ return {};
76
+ }
77
+ public getSchemaNames(): string[]
78
+ {
79
+ return [];
80
+ }
81
+ public getSchemaByName(require: boolean, name?: string): ObjectSchema
82
+ {
83
+ let ans = this.getSchema(require, name);
84
+ if (name)
85
+ {
86
+ let names = this.getSchemaNames();
87
+ if (!names.includes(name))
88
+ throw new Error("Wrong schema name was provided: " + name);
89
+ this.setSchemaByName(ans);
90
+ }
91
+ return ans;
92
+ }
93
+ public getArraySchemaByName(require: boolean, name?: string): ArraySchema
94
+ {
95
+ let ans = this.getSchemaByName(require, name);
96
+ return new ArraySchema(require, [ans]);
97
+ }
98
+ protected setSchemaByName(_: ObjectSchema): void
99
+ {
100
+ }
101
+ public getNotFoundError(conditions: any | null)
102
+ {
103
+ return ErrorOperation.getHTTP(404, "Could not found " + this.getName() + " for " + JSON.stringify(conditions));
104
+ }
107
105
  }
@@ -1,33 +1,33 @@
1
- import { ExecException, exec, execSync } from 'child_process';
2
- import { ConsoleOperation } from 'namirasoft-core';
3
- export class CommandOperation
4
- {
5
- static exec(command: string, cwd: string | undefined)
6
- {
7
- exec(command, { cwd }, (error: ExecException | null, stdout: string, stderr: string) =>
8
- {
9
- if (error)
10
- {
11
- ConsoleOperation.error(`error: ${error.message}`);
12
- return;
13
- }
14
- if (stderr)
15
- {
16
- ConsoleOperation.error(`stderr: ${stderr}`);
17
- return;
18
- }
19
- ConsoleOperation.log(`stdout:\n${stdout}`);
20
- });
21
- }
22
- static execSync(command: string, cwd: string | undefined)
23
- {
24
- try
25
- {
26
- return execSync(command, { cwd, encoding: 'utf-8' });
27
- } catch (error: any)
28
- {
29
- ConsoleOperation.error(error.stdout);
30
- throw error;
31
- }
32
- }
1
+ import { ExecException, exec, execSync } from 'child_process';
2
+ import { ConsoleOperation } from 'namirasoft-core';
3
+ export class CommandOperation
4
+ {
5
+ static exec(command: string, cwd: string | undefined)
6
+ {
7
+ exec(command, { cwd }, (error: ExecException | null, stdout: string, stderr: string) =>
8
+ {
9
+ if (error)
10
+ {
11
+ ConsoleOperation.error(`error: ${error.message}`);
12
+ return;
13
+ }
14
+ if (stderr)
15
+ {
16
+ ConsoleOperation.error(`stderr: ${stderr}`);
17
+ return;
18
+ }
19
+ ConsoleOperation.log(`stdout:\n${stdout}`);
20
+ });
21
+ }
22
+ static execSync(command: string, cwd: string | undefined)
23
+ {
24
+ try
25
+ {
26
+ return execSync(command, { cwd, encoding: 'utf-8' });
27
+ } catch (error: any)
28
+ {
29
+ ConsoleOperation.error(error.stdout);
30
+ throw error;
31
+ }
32
+ }
33
33
  }
@@ -1,8 +1,8 @@
1
- import { BaseDatabase } from "./BaseDatabase";
2
-
3
- export class EmptyDatabase extends BaseDatabase
4
- {
5
- override init() { }
6
- override connect() { }
7
- override async sync(_: boolean) { }
1
+ import { BaseDatabase } from "./BaseDatabase";
2
+
3
+ export class EmptyDatabase extends BaseDatabase
4
+ {
5
+ override init() { }
6
+ override connect() { }
7
+ override async sync(_: boolean) { }
8
8
  }
package/src/EnvService.ts CHANGED
@@ -1,23 +1,23 @@
1
- import { ConvertService } from "namirasoft-core";
2
-
3
- export class EnvService extends ConvertService
4
- {
5
- name: string;
6
- constructor(name: string, mandatory: boolean = false)
7
- {
8
- super(mandatory);
9
- this.name = name;
10
- }
11
- override getNullString()
12
- {
13
- let ans = process.env[this.name];
14
- if (ans)
15
- return ans;
16
- return null;
17
- }
18
- protected override onMandatoryError(): void
19
- {
20
- if (!process.env.NAMIRASOFT_MUTE)
21
- throw new Error(`Env value was not provided: ${this.name}`);
22
- }
1
+ import { ConvertService } from "namirasoft-core";
2
+
3
+ export class EnvService extends ConvertService
4
+ {
5
+ name: string;
6
+ constructor(name: string, mandatory: boolean = false)
7
+ {
8
+ super(mandatory);
9
+ this.name = name;
10
+ }
11
+ override getNullString()
12
+ {
13
+ let ans = process.env[this.name];
14
+ if (ans)
15
+ return ans;
16
+ return null;
17
+ }
18
+ protected override onMandatoryError(): void
19
+ {
20
+ if (!process.env.NAMIRASOFT_MUTE)
21
+ throw new Error(`Env value was not provided: ${this.name}`);
22
+ }
23
23
  }
@@ -1,23 +1,23 @@
1
- import smtpTransport from 'nodemailer-smtp-transport';
2
- import { BaseEmailService } from './BaseEmailService';
3
-
4
- export class GmailService extends BaseEmailService
5
- {
6
- password: string;
7
- constructor(username: string, password: string)
8
- {
9
- super(username);
10
- this.password = password;
11
- }
12
- protected override getTransform(): any
13
- {
14
- return smtpTransport({
15
- service: 'gmail',
16
- host: 'smtp.gmail.com',
17
- auth: {
18
- user: this.username,
19
- pass: this.password
20
- }
21
- });
22
- }
1
+ import smtpTransport from 'nodemailer-smtp-transport';
2
+ import { BaseEmailService } from './BaseEmailService';
3
+
4
+ export class GmailService extends BaseEmailService
5
+ {
6
+ password: string;
7
+ constructor(username: string, password: string)
8
+ {
9
+ super(username);
10
+ this.password = password;
11
+ }
12
+ protected override getTransform(): any
13
+ {
14
+ return smtpTransport({
15
+ service: 'gmail',
16
+ host: 'smtp.gmail.com',
17
+ auth: {
18
+ user: this.username,
19
+ pass: this.password
20
+ }
21
+ });
22
+ }
23
23
  }
@@ -1,39 +1,39 @@
1
- import * as express from 'express';
2
- import { getClientIp } from '@supercharge/request-ip';
3
- import { RequestHeaderService } from './RequestHeaderService';
4
- import { ErrorOperation } from 'namirasoft-core';
5
-
6
- export class IPOperation
7
- {
8
- static ERROR_MESSAGE_IP_IS_NOT_WHITELIST = `Ip does not match the whitelisted IP address: {0}`;
9
- static getIP(req: express.Request): string
10
- {
11
- let ip = new RequestHeaderService(req, 'cf-connecting-ip').getString();
12
- if (!ip)
13
- ip = new RequestHeaderService(req, 'x-forwarded-for').getString();
14
- if (!ip)
15
- ip = getClientIp(req) ?? "";
16
- ip = ip.split(',')[0];
17
- return ip;
18
- }
19
- static isWhitelist(req: express.Request, whitelist: string[])
20
- {
21
- let ip = this.getIP(req);
22
- if (!whitelist)
23
- return true;
24
- if (whitelist.length === 0)
25
- return true;
26
- if (whitelist.includes(ip))
27
- return true;
28
- for (let item of whitelist)
29
- if (ip.substring(0, item.length) === item)
30
- return true;
31
- return false;
32
- }
33
- static checkWhitelist(req: express.Request, whitelist: string[])
34
- {
35
- let valid = this.isWhitelist(req, whitelist);
36
- if (!valid)
37
- ErrorOperation.throwHTTP(403, this.ERROR_MESSAGE_IP_IS_NOT_WHITELIST, this.getIP(req));
38
- }
1
+ import * as express from 'express';
2
+ import { getClientIp } from '@supercharge/request-ip';
3
+ import { RequestHeaderService } from './RequestHeaderService';
4
+ import { ErrorOperation } from 'namirasoft-core';
5
+
6
+ export class IPOperation
7
+ {
8
+ static ERROR_MESSAGE_IP_IS_NOT_WHITELIST = `Ip does not match the whitelisted IP address: {0}`;
9
+ static getIP(req: express.Request): string
10
+ {
11
+ let ip = new RequestHeaderService(req, 'cf-connecting-ip').getString();
12
+ if (!ip)
13
+ ip = new RequestHeaderService(req, 'x-forwarded-for').getString();
14
+ if (!ip)
15
+ ip = getClientIp(req) ?? "";
16
+ ip = ip.split(',')[0];
17
+ return ip;
18
+ }
19
+ static isWhitelist(req: express.Request, whitelist: string[])
20
+ {
21
+ let ip = this.getIP(req);
22
+ if (!whitelist)
23
+ return true;
24
+ if (whitelist.length === 0)
25
+ return true;
26
+ if (whitelist.includes(ip))
27
+ return true;
28
+ for (let item of whitelist)
29
+ if (ip.substring(0, item.length) === item)
30
+ return true;
31
+ return false;
32
+ }
33
+ static checkWhitelist(req: express.Request, whitelist: string[])
34
+ {
35
+ let valid = this.isWhitelist(req, whitelist);
36
+ if (!valid)
37
+ ErrorOperation.throwHTTP(403, this.ERROR_MESSAGE_IP_IS_NOT_WHITELIST, this.getIP(req));
38
+ }
39
39
  }
package/src/Meta.ts CHANGED
@@ -1,41 +1,41 @@
1
- import * as express from 'express';
2
- import { IncomingHttpHeaders } from "http";
3
- import { IPOperation } from './IPOperation';
4
- import { HTTPMethod } from 'namirasoft-core';
5
-
6
- export class Meta
7
- {
8
- info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string };
9
- ip: string;
10
- method: string;
11
- url: string;
12
- headers?: IncomingHttpHeaders;
13
- body?: any;
14
- start_time: Date | null = null;
15
- end_time: Date | null = null;
16
- duration: number | null = null;
17
- code: number = 200;
18
- message: string = "Success";
19
- error: Error | null = null;
20
- constructor(info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string }, req: express.Request, sensitive: boolean)
21
- {
22
- this.info = info;
23
- this.ip = IPOperation.getIP(req);
24
- this.method = req.method;
25
- this.url = req.originalUrl;
26
- if (!sensitive)
27
- {
28
- this.headers = req.headers;
29
- this.body = req.body;
30
- }
31
- }
32
- onStart()
33
- {
34
- this.start_time = new Date();
35
- }
36
- onFinish()
37
- {
38
- this.end_time = new Date();
39
- this.duration = (this.end_time?.getTime() ?? 0) - (this.start_time?.getTime() ?? 0);
40
- }
1
+ import * as express from 'express';
2
+ import { IncomingHttpHeaders } from "http";
3
+ import { IPOperation } from './IPOperation';
4
+ import { HTTPMethod } from 'namirasoft-core';
5
+
6
+ export class Meta
7
+ {
8
+ info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string };
9
+ ip: string;
10
+ method: string;
11
+ url: string;
12
+ headers?: IncomingHttpHeaders;
13
+ body?: any;
14
+ start_time: Date | null = null;
15
+ end_time: Date | null = null;
16
+ duration: number | null = null;
17
+ code: number = 200;
18
+ message: string = "Success";
19
+ error: Error | null = null;
20
+ constructor(info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string }, req: express.Request, sensitive: boolean)
21
+ {
22
+ this.info = info;
23
+ this.ip = IPOperation.getIP(req);
24
+ this.method = req.method;
25
+ this.url = req.originalUrl;
26
+ if (!sensitive)
27
+ {
28
+ this.headers = req.headers;
29
+ this.body = req.body;
30
+ }
31
+ }
32
+ onStart()
33
+ {
34
+ this.start_time = new Date();
35
+ }
36
+ onFinish()
37
+ {
38
+ this.end_time = new Date();
39
+ this.duration = (this.end_time?.getTime() ?? 0) - (this.start_time?.getTime() ?? 0);
40
+ }
41
41
  }