namirasoft-node 1.4.21 → 1.4.23
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.js +7 -17
- package/dist/BaseApplication.js.map +1 -1
- package/dist/OTPOperation.d.ts +11 -2
- package/dist/OTPOperation.js +14 -4
- package/dist/OTPOperation.js.map +1 -1
- package/package.json +1 -1
- package/src/AnomalyDetector.ts +84 -84
- package/src/BaseApplication.ts +432 -432
- package/src/BaseApplicationLink.ts +6 -6
- package/src/BaseController.ts +193 -193
- package/src/BaseCron.ts +54 -54
- package/src/BaseDatabase.ts +199 -199
- package/src/BaseEmailService.ts +38 -38
- package/src/BaseTable.ts +107 -107
- package/src/CommandOperation.ts +32 -32
- package/src/EmptyDatabase.ts +11 -11
- package/src/GmailService.ts +22 -22
- package/src/IPOperation.ts +38 -38
- package/src/Meta.ts +40 -40
- package/src/OTPOperation.ts +90 -71
- package/src/RequestHeaderService.ts +27 -27
- package/src/SMTPService.ts +26 -26
- package/src/ServerToServerOperation.ts +23 -23
- package/src/index.ts +16 -16
package/src/BaseDatabase.ts
CHANGED
|
@@ -1,200 +1,200 @@
|
|
|
1
|
-
import { ErrorOperation, FilterItem, FilterItemOperator, SortItem } from "namirasoft-core";
|
|
2
|
-
|
|
3
|
-
export interface IFilterableDatabase<WhereOptions>
|
|
4
|
-
{
|
|
5
|
-
getIn: (filter: FilterItem, values: string[]) => { condition: WhereOptions, partial: boolean };
|
|
6
|
-
getNotIn: (filter: FilterItem, values: string[]) => { condition: WhereOptions, partial: boolean };
|
|
7
|
-
getLike: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
8
|
-
getNotLike: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
9
|
-
getRegex: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
10
|
-
getNotRegex: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
11
|
-
getEmpty: (filter: FilterItem) => { condition: WhereOptions, partial: boolean };
|
|
12
|
-
getNotEmpty: (filter: FilterItem) => { condition: WhereOptions, partial: boolean };
|
|
13
|
-
getExists: (filter: FilterItem, not: boolean) => { condition: WhereOptions, partial: boolean };
|
|
14
|
-
getStartsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
15
|
-
getNotStartsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
16
|
-
getEndsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
17
|
-
getNotEndsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
18
|
-
getLT: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
19
|
-
getLTE: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
20
|
-
getGT: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
21
|
-
getGTE: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export abstract class BaseDatabase
|
|
25
|
-
{
|
|
26
|
-
private tables: { [name: string]: any } = {};
|
|
27
|
-
abstract init(): Promise<void>;
|
|
28
|
-
abstract connect(): void;
|
|
29
|
-
abstract sync(force: boolean): Promise<void>;
|
|
30
|
-
async seedBefore(): Promise<void>
|
|
31
|
-
{
|
|
32
|
-
}
|
|
33
|
-
async seedAfter(): Promise<void>
|
|
34
|
-
{
|
|
35
|
-
}
|
|
36
|
-
addTable(name: string, table: any)
|
|
37
|
-
{
|
|
38
|
-
this.tables[name] = table;
|
|
39
|
-
}
|
|
40
|
-
getTable<T>(name: string): T
|
|
41
|
-
{
|
|
42
|
-
let ans = this.tables[name] as T;
|
|
43
|
-
if (!ans)
|
|
44
|
-
ErrorOperation.throwHTTP(404, `Table '${name}' not found`);
|
|
45
|
-
return ans;
|
|
46
|
-
}
|
|
47
|
-
paginate(page_number: number, page_size: number, page_size_default?: number): { offset: number, limit: number }
|
|
48
|
-
{
|
|
49
|
-
// page_number
|
|
50
|
-
if (isNaN(page_number))
|
|
51
|
-
page_number = 1;
|
|
52
|
-
// page_size
|
|
53
|
-
if (isNaN(page_size))
|
|
54
|
-
if (page_size_default)
|
|
55
|
-
page_size = page_size_default;
|
|
56
|
-
if (isNaN(page_size))
|
|
57
|
-
page_size = 25;
|
|
58
|
-
//
|
|
59
|
-
let offset = (page_number - 1) * page_size;
|
|
60
|
-
let limit = page_size;
|
|
61
|
-
return { offset, limit };
|
|
62
|
-
}
|
|
63
|
-
protected _getFiltersConditions<WhereOptions>(tables: { [table: string]: string[] }, idatabase: IFilterableDatabase<WhereOptions>, filters?: FilterItem[] | undefined): { [table: string]: WhereOptions[] }
|
|
64
|
-
{
|
|
65
|
-
let ans: { [table: string]: WhereOptions[] } = {};
|
|
66
|
-
Object.keys(tables).forEach(table => ans[table] = []);
|
|
67
|
-
if (filters)
|
|
68
|
-
{
|
|
69
|
-
let processed: { [index: number]: boolean } = {};
|
|
70
|
-
for (let i = 0; i < filters.length; i++)
|
|
71
|
-
if (!processed[i])
|
|
72
|
-
{
|
|
73
|
-
const filter = filters[i];
|
|
74
|
-
if (!tables[filter.table.name])
|
|
75
|
-
ErrorOperation.throwHTTP(404, `Invalid table name '${filter.table.name}'. Valid tables are '${Object.keys(tables).join(", ")}'.`);
|
|
76
|
-
let values = filter.values;
|
|
77
|
-
if (filter.operator.count > 0)
|
|
78
|
-
for (let j = i + 1; j < filters.length; j++)
|
|
79
|
-
{
|
|
80
|
-
const f = filters[j];
|
|
81
|
-
if (filter.table.name == f.table.name)
|
|
82
|
-
if (filter.column.name == f.column.name)
|
|
83
|
-
if (filter.not == f.not)
|
|
84
|
-
if (filter.operator.sign == f.operator.sign)
|
|
85
|
-
{
|
|
86
|
-
processed[j] = true;
|
|
87
|
-
values.push(...f.values);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
let getNumbers = () =>
|
|
92
|
-
{
|
|
93
|
-
let nums: number[] = values.map(x => parseFloat(x));
|
|
94
|
-
if (nums.filter(x => isNaN(x)).length > 0)
|
|
95
|
-
ErrorOperation.throwHTTP(400, `Invalid number values for: '${values.join(",")}'`);
|
|
96
|
-
return nums;
|
|
97
|
-
};
|
|
98
|
-
let addCondition = (res: { condition: WhereOptions, partial: boolean }) =>
|
|
99
|
-
{
|
|
100
|
-
if (res.partial)
|
|
101
|
-
{
|
|
102
|
-
let wh: any = {};
|
|
103
|
-
wh[filter.column.name] = res.condition;
|
|
104
|
-
ans[filter.table.name].push(wh);
|
|
105
|
-
}
|
|
106
|
-
else
|
|
107
|
-
ans[filter.table.name].push(res.condition);
|
|
108
|
-
};
|
|
109
|
-
if (filter.operator == FilterItemOperator.all.equals)
|
|
110
|
-
{
|
|
111
|
-
if (filter.not)
|
|
112
|
-
addCondition(idatabase.getNotIn(filter, values));
|
|
113
|
-
else
|
|
114
|
-
addCondition(idatabase.getIn(filter, values));
|
|
115
|
-
}
|
|
116
|
-
else if (filter.operator == FilterItemOperator.all.contains)
|
|
117
|
-
{
|
|
118
|
-
values.forEach(value =>
|
|
119
|
-
{
|
|
120
|
-
if (filter.not)
|
|
121
|
-
addCondition(idatabase.getNotLike(filter, value));
|
|
122
|
-
else
|
|
123
|
-
addCondition(idatabase.getLike(filter, value));
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
else if (filter.operator == FilterItemOperator.all.regex)
|
|
127
|
-
{
|
|
128
|
-
values.forEach(value =>
|
|
129
|
-
{
|
|
130
|
-
if (filter.not)
|
|
131
|
-
addCondition(idatabase.getNotRegex(filter, value));
|
|
132
|
-
else
|
|
133
|
-
addCondition(idatabase.getRegex(filter, value));
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
else if (filter.operator == FilterItemOperator.all.empty)
|
|
137
|
-
{
|
|
138
|
-
if (filter.not)
|
|
139
|
-
addCondition(idatabase.getNotEmpty(filter));
|
|
140
|
-
else
|
|
141
|
-
addCondition(idatabase.getEmpty(filter));
|
|
142
|
-
}
|
|
143
|
-
else if (filter.operator == FilterItemOperator.all.exists)
|
|
144
|
-
{
|
|
145
|
-
addCondition(idatabase.getExists(filter, filter.not));
|
|
146
|
-
}
|
|
147
|
-
else if (filter.operator == FilterItemOperator.all.startswith)
|
|
148
|
-
{
|
|
149
|
-
values.forEach(value =>
|
|
150
|
-
{
|
|
151
|
-
if (filter.not)
|
|
152
|
-
addCondition(idatabase.getNotStartsWith(filter, value));
|
|
153
|
-
else
|
|
154
|
-
addCondition(idatabase.getStartsWith(filter, value));
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
else if (filter.operator == FilterItemOperator.all.endswith)
|
|
158
|
-
{
|
|
159
|
-
values.forEach(value =>
|
|
160
|
-
{
|
|
161
|
-
if (filter.not)
|
|
162
|
-
addCondition(idatabase.getNotEndsWith(filter, value));
|
|
163
|
-
else
|
|
164
|
-
addCondition(idatabase.getEndsWith(filter, value));
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
else if (filter.operator == FilterItemOperator.all.lessthan)
|
|
168
|
-
{
|
|
169
|
-
if (filter.not)
|
|
170
|
-
addCondition(idatabase.getGTE(filter, Math.max(...getNumbers())));
|
|
171
|
-
else
|
|
172
|
-
addCondition(idatabase.getLT(filter, Math.max(...getNumbers())));
|
|
173
|
-
}
|
|
174
|
-
else if (filter.operator == FilterItemOperator.all.lessthanequal)
|
|
175
|
-
{
|
|
176
|
-
if (filter.not)
|
|
177
|
-
addCondition(idatabase.getGT(filter, Math.max(...getNumbers())));
|
|
178
|
-
else
|
|
179
|
-
addCondition(idatabase.getLTE(filter, Math.max(...getNumbers())));
|
|
180
|
-
}
|
|
181
|
-
else if (filter.operator == FilterItemOperator.all.morethan)
|
|
182
|
-
{
|
|
183
|
-
if (filter.not)
|
|
184
|
-
addCondition(idatabase.getLTE(filter, Math.max(...getNumbers())));
|
|
185
|
-
else
|
|
186
|
-
addCondition(idatabase.getGT(filter, Math.max(...getNumbers())));
|
|
187
|
-
}
|
|
188
|
-
else if (filter.operator == FilterItemOperator.all.morethanequal)
|
|
189
|
-
{
|
|
190
|
-
if (filter.not)
|
|
191
|
-
addCondition(idatabase.getLT(filter, Math.max(...getNumbers())));
|
|
192
|
-
else
|
|
193
|
-
addCondition(idatabase.getGTE(filter, Math.max(...getNumbers())));
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return ans;
|
|
198
|
-
}
|
|
199
|
-
public abstract getSortOptions(sorts?: SortItem[] | undefined): any;
|
|
1
|
+
import { ErrorOperation, FilterItem, FilterItemOperator, SortItem } from "namirasoft-core";
|
|
2
|
+
|
|
3
|
+
export interface IFilterableDatabase<WhereOptions>
|
|
4
|
+
{
|
|
5
|
+
getIn: (filter: FilterItem, values: string[]) => { condition: WhereOptions, partial: boolean };
|
|
6
|
+
getNotIn: (filter: FilterItem, values: string[]) => { condition: WhereOptions, partial: boolean };
|
|
7
|
+
getLike: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
8
|
+
getNotLike: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
9
|
+
getRegex: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
10
|
+
getNotRegex: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
11
|
+
getEmpty: (filter: FilterItem) => { condition: WhereOptions, partial: boolean };
|
|
12
|
+
getNotEmpty: (filter: FilterItem) => { condition: WhereOptions, partial: boolean };
|
|
13
|
+
getExists: (filter: FilterItem, not: boolean) => { condition: WhereOptions, partial: boolean };
|
|
14
|
+
getStartsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
15
|
+
getNotStartsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
16
|
+
getEndsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
17
|
+
getNotEndsWith: (filter: FilterItem, value: string) => { condition: WhereOptions, partial: boolean };
|
|
18
|
+
getLT: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
19
|
+
getLTE: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
20
|
+
getGT: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
21
|
+
getGTE: (filter: FilterItem, value: any) => { condition: WhereOptions, partial: boolean };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export abstract class BaseDatabase
|
|
25
|
+
{
|
|
26
|
+
private tables: { [name: string]: any } = {};
|
|
27
|
+
abstract init(): Promise<void>;
|
|
28
|
+
abstract connect(): void;
|
|
29
|
+
abstract sync(force: boolean): Promise<void>;
|
|
30
|
+
async seedBefore(): Promise<void>
|
|
31
|
+
{
|
|
32
|
+
}
|
|
33
|
+
async seedAfter(): Promise<void>
|
|
34
|
+
{
|
|
35
|
+
}
|
|
36
|
+
addTable(name: string, table: any)
|
|
37
|
+
{
|
|
38
|
+
this.tables[name] = table;
|
|
39
|
+
}
|
|
40
|
+
getTable<T>(name: string): T
|
|
41
|
+
{
|
|
42
|
+
let ans = this.tables[name] as T;
|
|
43
|
+
if (!ans)
|
|
44
|
+
ErrorOperation.throwHTTP(404, `Table '${name}' not found`);
|
|
45
|
+
return ans;
|
|
46
|
+
}
|
|
47
|
+
paginate(page_number: number, page_size: number, page_size_default?: number): { offset: number, limit: number }
|
|
48
|
+
{
|
|
49
|
+
// page_number
|
|
50
|
+
if (isNaN(page_number))
|
|
51
|
+
page_number = 1;
|
|
52
|
+
// page_size
|
|
53
|
+
if (isNaN(page_size))
|
|
54
|
+
if (page_size_default)
|
|
55
|
+
page_size = page_size_default;
|
|
56
|
+
if (isNaN(page_size))
|
|
57
|
+
page_size = 25;
|
|
58
|
+
//
|
|
59
|
+
let offset = (page_number - 1) * page_size;
|
|
60
|
+
let limit = page_size;
|
|
61
|
+
return { offset, limit };
|
|
62
|
+
}
|
|
63
|
+
protected _getFiltersConditions<WhereOptions>(tables: { [table: string]: string[] }, idatabase: IFilterableDatabase<WhereOptions>, filters?: FilterItem[] | undefined): { [table: string]: WhereOptions[] }
|
|
64
|
+
{
|
|
65
|
+
let ans: { [table: string]: WhereOptions[] } = {};
|
|
66
|
+
Object.keys(tables).forEach(table => ans[table] = []);
|
|
67
|
+
if (filters)
|
|
68
|
+
{
|
|
69
|
+
let processed: { [index: number]: boolean } = {};
|
|
70
|
+
for (let i = 0; i < filters.length; i++)
|
|
71
|
+
if (!processed[i])
|
|
72
|
+
{
|
|
73
|
+
const filter = filters[i];
|
|
74
|
+
if (!tables[filter.table.name])
|
|
75
|
+
ErrorOperation.throwHTTP(404, `Invalid table name '${filter.table.name}'. Valid tables are '${Object.keys(tables).join(", ")}'.`);
|
|
76
|
+
let values = filter.values;
|
|
77
|
+
if (filter.operator.count > 0)
|
|
78
|
+
for (let j = i + 1; j < filters.length; j++)
|
|
79
|
+
{
|
|
80
|
+
const f = filters[j];
|
|
81
|
+
if (filter.table.name == f.table.name)
|
|
82
|
+
if (filter.column.name == f.column.name)
|
|
83
|
+
if (filter.not == f.not)
|
|
84
|
+
if (filter.operator.sign == f.operator.sign)
|
|
85
|
+
{
|
|
86
|
+
processed[j] = true;
|
|
87
|
+
values.push(...f.values);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let getNumbers = () =>
|
|
92
|
+
{
|
|
93
|
+
let nums: number[] = values.map(x => parseFloat(x));
|
|
94
|
+
if (nums.filter(x => isNaN(x)).length > 0)
|
|
95
|
+
ErrorOperation.throwHTTP(400, `Invalid number values for: '${values.join(",")}'`);
|
|
96
|
+
return nums;
|
|
97
|
+
};
|
|
98
|
+
let addCondition = (res: { condition: WhereOptions, partial: boolean }) =>
|
|
99
|
+
{
|
|
100
|
+
if (res.partial)
|
|
101
|
+
{
|
|
102
|
+
let wh: any = {};
|
|
103
|
+
wh[filter.column.name] = res.condition;
|
|
104
|
+
ans[filter.table.name].push(wh);
|
|
105
|
+
}
|
|
106
|
+
else
|
|
107
|
+
ans[filter.table.name].push(res.condition);
|
|
108
|
+
};
|
|
109
|
+
if (filter.operator == FilterItemOperator.all.equals)
|
|
110
|
+
{
|
|
111
|
+
if (filter.not)
|
|
112
|
+
addCondition(idatabase.getNotIn(filter, values));
|
|
113
|
+
else
|
|
114
|
+
addCondition(idatabase.getIn(filter, values));
|
|
115
|
+
}
|
|
116
|
+
else if (filter.operator == FilterItemOperator.all.contains)
|
|
117
|
+
{
|
|
118
|
+
values.forEach(value =>
|
|
119
|
+
{
|
|
120
|
+
if (filter.not)
|
|
121
|
+
addCondition(idatabase.getNotLike(filter, value));
|
|
122
|
+
else
|
|
123
|
+
addCondition(idatabase.getLike(filter, value));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else if (filter.operator == FilterItemOperator.all.regex)
|
|
127
|
+
{
|
|
128
|
+
values.forEach(value =>
|
|
129
|
+
{
|
|
130
|
+
if (filter.not)
|
|
131
|
+
addCondition(idatabase.getNotRegex(filter, value));
|
|
132
|
+
else
|
|
133
|
+
addCondition(idatabase.getRegex(filter, value));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
else if (filter.operator == FilterItemOperator.all.empty)
|
|
137
|
+
{
|
|
138
|
+
if (filter.not)
|
|
139
|
+
addCondition(idatabase.getNotEmpty(filter));
|
|
140
|
+
else
|
|
141
|
+
addCondition(idatabase.getEmpty(filter));
|
|
142
|
+
}
|
|
143
|
+
else if (filter.operator == FilterItemOperator.all.exists)
|
|
144
|
+
{
|
|
145
|
+
addCondition(idatabase.getExists(filter, filter.not));
|
|
146
|
+
}
|
|
147
|
+
else if (filter.operator == FilterItemOperator.all.startswith)
|
|
148
|
+
{
|
|
149
|
+
values.forEach(value =>
|
|
150
|
+
{
|
|
151
|
+
if (filter.not)
|
|
152
|
+
addCondition(idatabase.getNotStartsWith(filter, value));
|
|
153
|
+
else
|
|
154
|
+
addCondition(idatabase.getStartsWith(filter, value));
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else if (filter.operator == FilterItemOperator.all.endswith)
|
|
158
|
+
{
|
|
159
|
+
values.forEach(value =>
|
|
160
|
+
{
|
|
161
|
+
if (filter.not)
|
|
162
|
+
addCondition(idatabase.getNotEndsWith(filter, value));
|
|
163
|
+
else
|
|
164
|
+
addCondition(idatabase.getEndsWith(filter, value));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
else if (filter.operator == FilterItemOperator.all.lessthan)
|
|
168
|
+
{
|
|
169
|
+
if (filter.not)
|
|
170
|
+
addCondition(idatabase.getGTE(filter, Math.max(...getNumbers())));
|
|
171
|
+
else
|
|
172
|
+
addCondition(idatabase.getLT(filter, Math.max(...getNumbers())));
|
|
173
|
+
}
|
|
174
|
+
else if (filter.operator == FilterItemOperator.all.lessthanequal)
|
|
175
|
+
{
|
|
176
|
+
if (filter.not)
|
|
177
|
+
addCondition(idatabase.getGT(filter, Math.max(...getNumbers())));
|
|
178
|
+
else
|
|
179
|
+
addCondition(idatabase.getLTE(filter, Math.max(...getNumbers())));
|
|
180
|
+
}
|
|
181
|
+
else if (filter.operator == FilterItemOperator.all.morethan)
|
|
182
|
+
{
|
|
183
|
+
if (filter.not)
|
|
184
|
+
addCondition(idatabase.getLTE(filter, Math.max(...getNumbers())));
|
|
185
|
+
else
|
|
186
|
+
addCondition(idatabase.getGT(filter, Math.max(...getNumbers())));
|
|
187
|
+
}
|
|
188
|
+
else if (filter.operator == FilterItemOperator.all.morethanequal)
|
|
189
|
+
{
|
|
190
|
+
if (filter.not)
|
|
191
|
+
addCondition(idatabase.getLT(filter, Math.max(...getNumbers())));
|
|
192
|
+
else
|
|
193
|
+
addCondition(idatabase.getGTE(filter, Math.max(...getNumbers())));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return ans;
|
|
198
|
+
}
|
|
199
|
+
public abstract getSortOptions(sorts?: SortItem[] | undefined): any;
|
|
200
200
|
}
|
package/src/BaseEmailService.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import nodemailer from 'nodemailer';
|
|
2
|
-
import Mail, { AttachmentLike } from "nodemailer/lib/mailer";
|
|
3
|
-
import { Readable } from "stream";
|
|
4
|
-
|
|
5
|
-
export abstract class BaseEmailService
|
|
6
|
-
{
|
|
7
|
-
username: string;
|
|
8
|
-
constructor(username: string)
|
|
9
|
-
{
|
|
10
|
-
this.username = username;
|
|
11
|
-
}
|
|
12
|
-
protected abstract getTransform(): any;
|
|
13
|
-
send(from: string | null, to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void)
|
|
14
|
-
{
|
|
15
|
-
let transform = this.getTransform();
|
|
16
|
-
let transporter = nodemailer.createTransport(transform);
|
|
17
|
-
|
|
18
|
-
let mailOptions: Mail.Options = {
|
|
19
|
-
from: from ?? this.username,
|
|
20
|
-
to,
|
|
21
|
-
subject,
|
|
22
|
-
text,
|
|
23
|
-
html
|
|
24
|
-
};
|
|
25
|
-
if (html)
|
|
26
|
-
mailOptions.html = html;
|
|
27
|
-
|
|
28
|
-
transporter.sendMail(mailOptions, function (error, info)
|
|
29
|
-
{
|
|
30
|
-
if (callback)
|
|
31
|
-
callback(error, info);
|
|
32
|
-
else
|
|
33
|
-
{
|
|
34
|
-
if (error)
|
|
35
|
-
console.log(error);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
1
|
+
import nodemailer from 'nodemailer';
|
|
2
|
+
import Mail, { AttachmentLike } from "nodemailer/lib/mailer";
|
|
3
|
+
import { Readable } from "stream";
|
|
4
|
+
|
|
5
|
+
export abstract class BaseEmailService
|
|
6
|
+
{
|
|
7
|
+
username: string;
|
|
8
|
+
constructor(username: string)
|
|
9
|
+
{
|
|
10
|
+
this.username = username;
|
|
11
|
+
}
|
|
12
|
+
protected abstract getTransform(): any;
|
|
13
|
+
send(from: string | null, to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void)
|
|
14
|
+
{
|
|
15
|
+
let transform = this.getTransform();
|
|
16
|
+
let transporter = nodemailer.createTransport(transform);
|
|
17
|
+
|
|
18
|
+
let mailOptions: Mail.Options = {
|
|
19
|
+
from: from ?? this.username,
|
|
20
|
+
to,
|
|
21
|
+
subject,
|
|
22
|
+
text,
|
|
23
|
+
html
|
|
24
|
+
};
|
|
25
|
+
if (html)
|
|
26
|
+
mailOptions.html = html;
|
|
27
|
+
|
|
28
|
+
transporter.sendMail(mailOptions, function (error, info)
|
|
29
|
+
{
|
|
30
|
+
if (callback)
|
|
31
|
+
callback(error, info);
|
|
32
|
+
else
|
|
33
|
+
{
|
|
34
|
+
if (error)
|
|
35
|
+
console.log(error);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
39
|
}
|