@futdevpro/nts-dynamo 1.2.90
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/BuildPackage.bat +1 -0
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/lib/_constants/dynamobe-global.settings.d.ts +2 -0
- package/lib/_constants/dynamobe-global.settings.d.ts.map +1 -0
- package/lib/_constants/dynamobe-global.settings.js +11 -0
- package/lib/_enum/data-model-types.enum.d.ts +5 -0
- package/lib/_enum/data-model-types.enum.d.ts.map +1 -0
- package/lib/_enum/data-model-types.enum.js +9 -0
- package/lib/_enum/dynamobe-data-service-function.enum.d.ts +7 -0
- package/lib/_enum/dynamobe-data-service-function.enum.d.ts.map +1 -0
- package/lib/_enum/dynamobe-data-service-function.enum.js +14 -0
- package/lib/_enum/dynamobe-route-security.enum.d.ts +5 -0
- package/lib/_enum/dynamobe-route-security.enum.d.ts.map +1 -0
- package/lib/_enum/dynamobe-route-security.enum.js +9 -0
- package/lib/_enum/http/http-call-type.enum.d.ts +7 -0
- package/lib/_enum/http/http-call-type.enum.d.ts.map +1 -0
- package/lib/_enum/http/http-call-type.enum.js +11 -0
- package/lib/_enum/http/http-response-type.enum.d.ts +6 -0
- package/lib/_enum/http/http-response-type.enum.d.ts.map +1 -0
- package/lib/_enum/http/http-response-type.enum.js +10 -0
- package/lib/_enum/log-style.enum.d.ts +25 -0
- package/lib/_enum/log-style.enum.d.ts.map +1 -0
- package/lib/_enum/log-style.enum.js +29 -0
- package/lib/_enum/predefined-data-types.enum.d.ts +7 -0
- package/lib/_enum/predefined-data-types.enum.d.ts.map +1 -0
- package/lib/_enum/predefined-data-types.enum.js +11 -0
- package/lib/_models/dynamobe-api-call-params.d.ts +34 -0
- package/lib/_models/dynamobe-api-call-params.d.ts.map +1 -0
- package/lib/_models/dynamobe-api-call-params.js +24 -0
- package/lib/_models/dynamobe-app-params.d.ts +14 -0
- package/lib/_models/dynamobe-app-params.d.ts.map +1 -0
- package/lib/_models/dynamobe-app-params.js +13 -0
- package/lib/_models/dynamobe-data-model-params.d.ts +12 -0
- package/lib/_models/dynamobe-data-model-params.d.ts.map +1 -0
- package/lib/_models/dynamobe-data-model-params.js +14 -0
- package/lib/_models/dynamobe-endpoint-control.d.ts +28 -0
- package/lib/_models/dynamobe-endpoint-control.d.ts.map +1 -0
- package/lib/_models/dynamobe-endpoint-control.js +85 -0
- package/lib/_models/dynamobe-global-settings.d.ts +8 -0
- package/lib/_models/dynamobe-global-settings.d.ts.map +1 -0
- package/lib/_models/dynamobe-global-settings.js +6 -0
- package/lib/_models/fullstack-models/control-models/daily-usage-data.d.ts +17 -0
- package/lib/_models/fullstack-models/control-models/daily-usage-data.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/daily-usage-data.js +21 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-data-property-params.d.ts +27 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-data-property-params.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-data-property-params.js +66 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-error.d.ts +17 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-error.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-error.js +195 -0
- package/lib/_models/fullstack-models/control-models/geo-ip-location.d.ts +25 -0
- package/lib/_models/fullstack-models/control-models/geo-ip-location.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/geo-ip-location.js +2 -0
- package/lib/_models/fullstack-models/control-models/usage-action.d.ts +8 -0
- package/lib/_models/fullstack-models/control-models/usage-action.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/usage-action.js +12 -0
- package/lib/_models/fullstack-models/control-models/usage-data.d.ts +13 -0
- package/lib/_models/fullstack-models/control-models/usage-data.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/usage-data.js +15 -0
- package/lib/_models/fullstack-models/data-models/custom-data.d.ts +15 -0
- package/lib/_models/fullstack-models/data-models/custom-data.d.ts.map +1 -0
- package/lib/_models/fullstack-models/data-models/custom-data.js +23 -0
- package/lib/_models/fullstack-models/data-models/metadata.d.ts +16 -0
- package/lib/_models/fullstack-models/data-models/metadata.d.ts.map +1 -0
- package/lib/_models/fullstack-models/data-models/metadata.js +16 -0
- package/lib/_models/fullstack-models/data-models/usage-session.d.ts +33 -0
- package/lib/_models/fullstack-models/data-models/usage-session.d.ts.map +1 -0
- package/lib/_models/fullstack-models/data-models/usage-session.js +48 -0
- package/lib/_modules/test-route/custom-data.service.d.ts +5 -0
- package/lib/_modules/test-route/custom-data.service.d.ts.map +1 -0
- package/lib/_modules/test-route/custom-data.service.js +12 -0
- package/lib/_modules/test-route/test-controller.d.ts +5 -0
- package/lib/_modules/test-route/test-controller.d.ts.map +1 -0
- package/lib/_modules/test-route/test-controller.js +125 -0
- package/lib/_modules/usage-route/usage-controller.d.ts +6 -0
- package/lib/_modules/usage-route/usage-controller.d.ts.map +1 -0
- package/lib/_modules/usage-route/usage-controller.js +88 -0
- package/lib/_modules/usage-route/usage.service.d.ts +18 -0
- package/lib/_modules/usage-route/usage.service.d.ts.map +1 -0
- package/lib/_modules/usage-route/usage.service.js +110 -0
- package/lib/_services/dynamobe-api.service.d.ts +9 -0
- package/lib/_services/dynamobe-api.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-api.service.js +130 -0
- package/lib/_services/dynamobe-app.d.ts +11 -0
- package/lib/_services/dynamobe-app.d.ts.map +1 -0
- package/lib/_services/dynamobe-app.js +38 -0
- package/lib/_services/dynamobe-auth.service.d.ts +25 -0
- package/lib/_services/dynamobe-auth.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-auth.service.js +24 -0
- package/lib/_services/dynamobe-controller.service.d.ts +10 -0
- package/lib/_services/dynamobe-controller.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-controller.service.js +11 -0
- package/lib/_services/dynamobe-data.service.d.ts +51 -0
- package/lib/_services/dynamobe-data.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-data.service.js +224 -0
- package/lib/_services/dynamobe-db-service-collection.service.d.ts +5 -0
- package/lib/_services/dynamobe-db-service-collection.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-db-service-collection.service.js +7 -0
- package/lib/_services/dynamobe-db.service.d.ts +150 -0
- package/lib/_services/dynamobe-db.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-db.service.js +577 -0
- package/lib/_services/dynamobe-email-service-collection.service.d.ts +5 -0
- package/lib/_services/dynamobe-email-service-collection.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-email-service-collection.service.js +7 -0
- package/lib/_services/dynamobe-email.service.d.ts +32 -0
- package/lib/_services/dynamobe-email.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-email.service.js +144 -0
- package/lib/_services/dynamobe-global.service.d.ts +22 -0
- package/lib/_services/dynamobe-global.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-global.service.js +84 -0
- package/lib/_services/dynamobe-routing-module.service.d.ts +25 -0
- package/lib/_services/dynamobe-routing-module.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-routing-module.service.js +112 -0
- package/lib/_services/dynamobe-server.d.ts +31 -0
- package/lib/_services/dynamobe-server.d.ts.map +1 -0
- package/lib/_services/dynamobe-server.js +179 -0
- package/lib/_services/dynamobe-shared.service.d.ts +100 -0
- package/lib/_services/dynamobe-shared.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-shared.service.js +294 -0
- package/lib/_services/dynamobe-singleton.service.d.ts +5 -0
- package/lib/_services/dynamobe-singleton.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-singleton.service.js +15 -0
- package/lib/index-newo.d.ts +0 -0
- package/lib/index-newo.d.ts.map +1 -0
- package/lib/index-newo.js +71 -0
- package/lib/index-old.d.ts +39 -0
- package/lib/index-old.js +49 -0
- package/lib/index.d.ts +39 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +49 -0
- package/lib/tsconfig.tsbuildinfo +2080 -0
- package/nodemon.json +6 -0
- package/package.json +52 -0
- package/src/_constants/dynamobe-global.settings.ts +11 -0
- package/src/_enum/data-model-types.enum.ts +6 -0
- package/src/_enum/dynamobe-data-service-function.enum.ts +11 -0
- package/src/_enum/dynamobe-route-security.enum.ts +6 -0
- package/src/_enum/http/http-call-type.enum.ts +8 -0
- package/src/_enum/http/http-response-type.enum.ts +7 -0
- package/src/_enum/log-style.enum.ts +30 -0
- package/src/_enum/predefined-data-types.enum.ts +8 -0
- package/src/_models/dynamobe-api-call-params.ts +62 -0
- package/src/_models/dynamobe-app-params.ts +24 -0
- package/src/_models/dynamobe-data-model-params.ts +23 -0
- package/src/_models/dynamobe-endpoint-control.ts +127 -0
- package/src/_models/dynamobe-global-settings.ts +9 -0
- package/src/_models/fullstack-models/control-models/daily-usage-data.ts +34 -0
- package/src/_models/fullstack-models/control-models/dynamobe-data-property-params.ts +98 -0
- package/src/_models/fullstack-models/control-models/dynamobe-error.ts +207 -0
- package/src/_models/fullstack-models/control-models/geo-ip-location.ts +26 -0
- package/src/_models/fullstack-models/control-models/usage-action.ts +18 -0
- package/src/_models/fullstack-models/control-models/usage-data.ts +26 -0
- package/src/_models/fullstack-models/data-models/custom-data.ts +37 -0
- package/src/_models/fullstack-models/data-models/metadata.ts +28 -0
- package/src/_models/fullstack-models/data-models/usage-session.ts +82 -0
- package/src/_modules/test-route/custom-data.service.ts +21 -0
- package/src/_modules/test-route/test-controller.ts +136 -0
- package/src/_modules/usage-route/usage-controller.ts +114 -0
- package/src/_modules/usage-route/usage.service.ts +125 -0
- package/src/_services/dynamobe-api.service.ts +135 -0
- package/src/_services/dynamobe-app.ts +49 -0
- package/src/_services/dynamobe-auth.service.ts +42 -0
- package/src/_services/dynamobe-controller.service.ts +20 -0
- package/src/_services/dynamobe-data.service.ts +215 -0
- package/src/_services/dynamobe-db-service-collection.service.ts +9 -0
- package/src/_services/dynamobe-db.service.ts +556 -0
- package/src/_services/dynamobe-email-service-collection.service.ts +9 -0
- package/src/_services/dynamobe-email.service.ts +169 -0
- package/src/_services/dynamobe-global.service.ts +99 -0
- package/src/_services/dynamobe-routing-module.service.ts +141 -0
- package/src/_services/dynamobe-server.ts +225 -0
- package/src/_services/dynamobe-shared.service.ts +320 -0
- package/src/_services/dynamobe-singleton.service.ts +16 -0
- package/src/index-newo.ts +71 -0
- package/src/index.ts +50 -0
- package/src/version.json +2 -0
- package/tsconfig.app.json +12 -0
- package/tsconfig.json +72 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
|
|
2
|
+
import * as FileSystem from 'fs';
|
|
3
|
+
import * as Path from 'path';
|
|
4
|
+
import * as NodeMailer from 'nodemailer';
|
|
5
|
+
import { Options as MailOptions, Attachment } from 'nodemailer/lib/mailer';
|
|
6
|
+
import { DynamoBEError } from '../_models/fullstack-models/control-models/dynamobe-error';
|
|
7
|
+
import { DBE_Shared } from './dynamobe-shared.service';
|
|
8
|
+
|
|
9
|
+
export class DynamoBEEmailService {
|
|
10
|
+
private nmTransporter: NodeMailer.Transporter;
|
|
11
|
+
private senderName: string;
|
|
12
|
+
private senderNEmail: string;
|
|
13
|
+
private templates: {
|
|
14
|
+
[templateName: string]: string
|
|
15
|
+
} = {};
|
|
16
|
+
private templatePropertyKeys: {
|
|
17
|
+
[templateName: string]: string[]
|
|
18
|
+
} = {};
|
|
19
|
+
private templateLoadingInProgressCounter = 0;
|
|
20
|
+
|
|
21
|
+
constructor (
|
|
22
|
+
set: {
|
|
23
|
+
host: string,
|
|
24
|
+
port: number,
|
|
25
|
+
email: string,
|
|
26
|
+
pass: string,
|
|
27
|
+
senderName: string,
|
|
28
|
+
/* constructionFinishedCallback: () => void, */
|
|
29
|
+
templatePaths: {
|
|
30
|
+
[templateName: string]: string
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
) {
|
|
34
|
+
try {
|
|
35
|
+
// console.log('\n\n\n\n\nNEW CONSTRUCT STARTED', set.email);
|
|
36
|
+
this.senderName = set.senderName;
|
|
37
|
+
this.senderNEmail = `${set.senderName} <${set.email}>`;
|
|
38
|
+
this.nmTransporter = NodeMailer.createTransport({
|
|
39
|
+
host: set.host,
|
|
40
|
+
port: set.port,
|
|
41
|
+
auth: {
|
|
42
|
+
user: set.email,
|
|
43
|
+
pass: set.pass
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
/* this.constructionFinishedCallback = set.constructionFinishedCallback; */
|
|
47
|
+
if (set.templatePaths) {
|
|
48
|
+
for(let key in set.templatePaths) {
|
|
49
|
+
this.loadTemplate(key, set.templatePaths[key]);
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
this.constructionFinished();
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.log(`\nDynamoBEEmailService ERROR, The emailService construction failed for ${set.email}.`, new Error());
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private constructionFinished(): void {
|
|
60
|
+
DBE_Shared.logSuccess(`\n${this.senderName} EmailService construction finished\n`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async sendEmail(set: {
|
|
64
|
+
to: string,
|
|
65
|
+
subject: string,
|
|
66
|
+
content?: string,
|
|
67
|
+
useTemplate?: string,
|
|
68
|
+
templateProperties?: {
|
|
69
|
+
[propertyKey: string]: string
|
|
70
|
+
},
|
|
71
|
+
attachments?: Attachment[]
|
|
72
|
+
}): Promise<void> {
|
|
73
|
+
try {
|
|
74
|
+
let content: string;
|
|
75
|
+
|
|
76
|
+
if (set.content) {
|
|
77
|
+
content = set.content
|
|
78
|
+
} else if (set.useTemplate) {
|
|
79
|
+
if (!this.templates[set.useTemplate]) {
|
|
80
|
+
throw new DynamoBEError({ message: `no template found with this parameter! (${set.useTemplate})` });
|
|
81
|
+
}
|
|
82
|
+
content = this.templates[set.useTemplate];
|
|
83
|
+
|
|
84
|
+
if (this.templatePropertyKeys[set.useTemplate].length > 0) {
|
|
85
|
+
if (!set.templateProperties) {
|
|
86
|
+
throw new DynamoBEError({ message: `templateProperties missing!` });
|
|
87
|
+
}
|
|
88
|
+
this.templatePropertyKeys[set.useTemplate].forEach((propertyKey: string) => {
|
|
89
|
+
if (!set.templateProperties[propertyKey]) {
|
|
90
|
+
console.log(`templateProperty missing! (${propertyKey})`);
|
|
91
|
+
}
|
|
92
|
+
const reg = new RegExp(`{{${propertyKey}}}`, 'g')
|
|
93
|
+
content = content.replace(reg, set.templateProperties[propertyKey]);
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
throw new DynamoBEError({ message: 'no content setting passsed to emailSending!' });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let mailOptions: MailOptions = {
|
|
101
|
+
from: this.senderNEmail,
|
|
102
|
+
to: set.to,
|
|
103
|
+
subject: set.subject,
|
|
104
|
+
html: content,
|
|
105
|
+
attachments: set.attachments,
|
|
106
|
+
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
await this.nmTransporter.sendMail(mailOptions, (error, info) => {
|
|
110
|
+
if (error) {
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.log('\nDynamoBEEmailService ERROR, email sending error', error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private async loadTemplate(templateName: string, templatePath: string): Promise<void> {
|
|
120
|
+
try {
|
|
121
|
+
this.templateLoadingInProgressCounter++;
|
|
122
|
+
return new Promise<void>((resolve, reject) => {
|
|
123
|
+
templatePath = Path.join(__dirname, templatePath);
|
|
124
|
+
FileSystem.readFile(templatePath, 'utf8' , (err, template) => {
|
|
125
|
+
if (err) {
|
|
126
|
+
console.log(`\nDynamoBEEmailService readFile ERROR (0), Couldn't load email template; ${templateName} from ${templatePath}\nerror:\n`, err, '\n');
|
|
127
|
+
reject(err);
|
|
128
|
+
return;
|
|
129
|
+
} else {
|
|
130
|
+
this.templates[templateName] = template;
|
|
131
|
+
this.templatePropertyKeys[templateName] = this.getTemplatePropertyKeys(template);
|
|
132
|
+
|
|
133
|
+
this.templateLoadingInProgressCounter--;
|
|
134
|
+
if (this.templateLoadingInProgressCounter == 0) {
|
|
135
|
+
this.constructionFinished();
|
|
136
|
+
}
|
|
137
|
+
resolve();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.log(`\nDynamoBEEmailService ERROR (1), Couldn't load email template; ${templateName} from ${templatePath}`, new Error());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private getTemplatePropertyKeys(template: string): string[] {
|
|
147
|
+
try {
|
|
148
|
+
const propertyKeys: string[] = [];
|
|
149
|
+
let propertyOpenTagIndex = template.indexOf('{{');
|
|
150
|
+
let propertyCloseTagIndex = template.indexOf('}}', propertyOpenTagIndex + 2);
|
|
151
|
+
while (propertyOpenTagIndex >= 0) {
|
|
152
|
+
if (propertyCloseTagIndex === -1) {
|
|
153
|
+
console.log(`\nDynamoBEEmailService ERROR, missing closing tag from email template! (${propertyOpenTagIndex} -)`, propertyKeys);
|
|
154
|
+
throw new DynamoBEError({ message: `ERROR, missing closing tag from email template! (${propertyOpenTagIndex} -)` });
|
|
155
|
+
}
|
|
156
|
+
const newKey: string = template.substring(propertyOpenTagIndex + 2, propertyCloseTagIndex);
|
|
157
|
+
if (!propertyKeys.includes(newKey)) {
|
|
158
|
+
propertyKeys.push(newKey);
|
|
159
|
+
}
|
|
160
|
+
propertyOpenTagIndex = template.indexOf('{{', propertyOpenTagIndex + 2);
|
|
161
|
+
propertyCloseTagIndex = template.indexOf('}}', propertyOpenTagIndex + 2);
|
|
162
|
+
}
|
|
163
|
+
// console.log('\n\n\nTEST propertyKeys: ', propertyKeys);
|
|
164
|
+
return propertyKeys;
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.log(`\nDynamoBEEmailService ERROR, getTemplatePropertyKeys ERROR`, new Error(), '\ntemplate:\n', template);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { DynamoBERouteSecurity } from '../_enum/dynamobe-route-security.enum';
|
|
4
|
+
import { DynamoBEGlobalSettings } from '../_models/dynamobe-global-settings';
|
|
5
|
+
import { DynamoBEAuthService } from './dynamobe-auth.service';
|
|
6
|
+
import { DynamoBEDBServiceCollection } from './dynamobe-db-service-collection.service';
|
|
7
|
+
import { DynamoBEEmailServiceCollection } from './dynamobe-email-service-collection.service';
|
|
8
|
+
import { DynamoBESingletonService } from './dynamobe-singleton.service';
|
|
9
|
+
|
|
10
|
+
export class DBE_Global_S extends DynamoBESingletonService {
|
|
11
|
+
|
|
12
|
+
static getInstance(): DBE_Global_S {
|
|
13
|
+
return DBE_Global_S.getSingletonInstance();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
globalSettings: DynamoBEGlobalSettings = {
|
|
17
|
+
defaultRouteSecurity: DynamoBERouteSecurity.open,
|
|
18
|
+
logSetup: false,
|
|
19
|
+
logRequests: true,
|
|
20
|
+
logRequestsContent: true,
|
|
21
|
+
logResponseContent: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
authService: DynamoBEAuthService;
|
|
25
|
+
dbServiceCollection: DynamoBEDBServiceCollection;
|
|
26
|
+
emailServiceCollection: DynamoBEEmailServiceCollection;
|
|
27
|
+
|
|
28
|
+
static setServices(
|
|
29
|
+
set: {
|
|
30
|
+
authService: DynamoBEAuthService,
|
|
31
|
+
dbServiceCollection: DynamoBEDBServiceCollection,
|
|
32
|
+
emailServiceCollection: DynamoBEEmailServiceCollection,
|
|
33
|
+
}
|
|
34
|
+
): void {
|
|
35
|
+
this.getInstance();
|
|
36
|
+
this.instance.authService = set.authService;
|
|
37
|
+
this.instance.dbServiceCollection = set.dbServiceCollection;
|
|
38
|
+
this.instance.emailServiceCollection = set.emailServiceCollection;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static getGlobalSettings(): DynamoBEGlobalSettings {
|
|
42
|
+
if (!this.instance) {
|
|
43
|
+
throw new Error(`DynamoBE Global Service missing!` +
|
|
44
|
+
`\nPlease setup DynamoBE Global Service by calling "DBE_Global_SC.setServices(...)"` +
|
|
45
|
+
`before calling getGlobalSettings"`);
|
|
46
|
+
}
|
|
47
|
+
return this.instance.globalSettings;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static setGlobalSettings(set: DynamoBEGlobalSettings): void {
|
|
51
|
+
if (!this.instance) {
|
|
52
|
+
throw new Error(`DynamoBE Global Service missing!` +
|
|
53
|
+
`\nPlease setup DynamoBE Global Service by calling "DBE_Global_SC.setServices(...)"` +
|
|
54
|
+
`before calling setGlobalSettings"`);
|
|
55
|
+
}
|
|
56
|
+
if (set.defaultRouteSecurity) {
|
|
57
|
+
this.instance.globalSettings.defaultRouteSecurity = set.defaultRouteSecurity;
|
|
58
|
+
}
|
|
59
|
+
if (set.logSetup !== undefined) {
|
|
60
|
+
this.instance.globalSettings.logSetup = set.logSetup;
|
|
61
|
+
}
|
|
62
|
+
if (set.logRequests !== undefined) {
|
|
63
|
+
this.instance.globalSettings.logRequests = set.logRequests;
|
|
64
|
+
}
|
|
65
|
+
if (set.logRequestsContent !== undefined) {
|
|
66
|
+
this.instance.globalSettings.logRequestsContent = set.logRequestsContent;
|
|
67
|
+
}
|
|
68
|
+
if (set.logResponseContent !== undefined) {
|
|
69
|
+
this.instance.globalSettings.logResponseContent = set.logResponseContent;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static getAuthService(): DynamoBEAuthService {
|
|
74
|
+
if (!this.instance?.authService?.authenticateToken) {
|
|
75
|
+
throw new Error(`Unique Authentication Service missing!` +
|
|
76
|
+
`\nPlease create a Unique Authentication Service extending DynamoBEAuthService, ` +
|
|
77
|
+
`and Setup with "DBE_Global_SC.setServices(...)"`);
|
|
78
|
+
}
|
|
79
|
+
return this.instance.authService;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static getDBServiceCollection(): DynamoBEDBServiceCollection {
|
|
83
|
+
if (!this.instance?.dbServiceCollection) {
|
|
84
|
+
throw new Error(`Unique DataBase Service Collection missing!` +
|
|
85
|
+
`\nPlease create a Unique DataBase Service Collectio extending DynamoBEDBServiceCollection, ` +
|
|
86
|
+
`and Setup with "DBE_Global_SC.setServices(...)"`);
|
|
87
|
+
}
|
|
88
|
+
return this.instance.authService;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static getEmailServiceCollection(): DynamoBEEmailServiceCollection {
|
|
92
|
+
if (!this.instance?.emailServiceCollection) {
|
|
93
|
+
throw new Error(`Unique Email Service Collection missing!` +
|
|
94
|
+
`\nPlease create a Unique Email Service Collection extending DynamoBEEmailServiceCollection, ` +
|
|
95
|
+
`and Setup with "DBE_Global_SC.setServices(...)"`);
|
|
96
|
+
}
|
|
97
|
+
return this.instance.emailServiceCollection;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
import { DynamoBERouteSecurity } from '../_enum/dynamobe-route-security.enum';
|
|
4
|
+
import { HttpCallType } from '../_enum/http/http-call-type.enum';
|
|
5
|
+
import { DynamoBEEndpointParams } from '../_models/dynamobe-endpoint-control';
|
|
6
|
+
import { DynamoBEError } from '../_models/fullstack-models/control-models/dynamobe-error';
|
|
7
|
+
import { DynamoBEController } from './dynamobe-controller.service';
|
|
8
|
+
import { DBE_Global_S } from './dynamobe-global.service';
|
|
9
|
+
|
|
10
|
+
export class DynamoBERountingModule {
|
|
11
|
+
|
|
12
|
+
security: DynamoBERouteSecurity;
|
|
13
|
+
route: string;
|
|
14
|
+
|
|
15
|
+
private controllers: DynamoBEController[];
|
|
16
|
+
endpoints: DynamoBEEndpointParams[] = [];
|
|
17
|
+
|
|
18
|
+
openRouter: Router = Router();
|
|
19
|
+
secureRouter: Router = Router();
|
|
20
|
+
|
|
21
|
+
protected log: boolean = DBE_Global_S.getGlobalSettings().logSetup;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
set: {
|
|
25
|
+
route: string,
|
|
26
|
+
controllers: DynamoBEController[],
|
|
27
|
+
securityOverride?: DynamoBERouteSecurity,
|
|
28
|
+
}
|
|
29
|
+
){
|
|
30
|
+
|
|
31
|
+
this.route = set.route;
|
|
32
|
+
this.controllers = set.controllers;
|
|
33
|
+
this.setupRoutes();
|
|
34
|
+
this.mountRoutes(set.securityOverride);
|
|
35
|
+
|
|
36
|
+
if (this.log) console.log(`routing module setup done: ${this.route} serurity: ${this.security}\n`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* You must setup endpoints and required services in this function
|
|
41
|
+
*/
|
|
42
|
+
setupRoutes(): void {
|
|
43
|
+
this.controllers.forEach((controller: DynamoBEController) => {
|
|
44
|
+
this.endpoints.push(...controller.endpoints);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private mountRoutes(securityOverride?: DynamoBERouteSecurity): void {
|
|
49
|
+
if (securityOverride) {
|
|
50
|
+
this.security = securityOverride;
|
|
51
|
+
}
|
|
52
|
+
this.endpoints.forEach((endpoint: DynamoBEEndpointParams) => {
|
|
53
|
+
if (securityOverride) {
|
|
54
|
+
endpoint.security = securityOverride;
|
|
55
|
+
} else {
|
|
56
|
+
if (!this.security) {
|
|
57
|
+
this.security = endpoint.security;
|
|
58
|
+
} else if (this.security !== endpoint.security) {
|
|
59
|
+
this.security = DynamoBERouteSecurity.both;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (this.log) console.log(`endpoint mount (${endpoint.security}): ${this.route}${endpoint.endpoint}`);
|
|
64
|
+
const existingEndPoints: DynamoBEEndpointParams[] = this.endpoints.filter(
|
|
65
|
+
(ep: DynamoBEEndpointParams) => ep.endpoint === endpoint.endpoint);
|
|
66
|
+
if (1 < existingEndPoints.length) {
|
|
67
|
+
let error = new Error(`ENDPOINT DUPLICATION: ${endpoint.endpoint}`);
|
|
68
|
+
let errorStack: string[] = error.stack.split('\n');
|
|
69
|
+
errorStack.splice(1, 4);
|
|
70
|
+
error.stack = errorStack.join('\n');
|
|
71
|
+
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
if (endpoint.security !== DynamoBERouteSecurity.secure) {
|
|
77
|
+
this.mountOpenRoute(endpoint);
|
|
78
|
+
}
|
|
79
|
+
if (endpoint.security !== DynamoBERouteSecurity.open) {
|
|
80
|
+
this.mountSecureRoute(endpoint);
|
|
81
|
+
}
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw new DynamoBEError({ message: `DYNAMOBEERROR: Failed to mount routes.`, error })
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private mountOpenRoute(endpointParams: DynamoBEEndpointParams): void {
|
|
89
|
+
switch(endpointParams.type) {
|
|
90
|
+
case HttpCallType.get:
|
|
91
|
+
this.openRouter.get(endpointParams.endpoint,
|
|
92
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
93
|
+
break;
|
|
94
|
+
case HttpCallType.post:
|
|
95
|
+
this.openRouter.post(endpointParams.endpoint,
|
|
96
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
97
|
+
break;
|
|
98
|
+
case HttpCallType.put:
|
|
99
|
+
this.openRouter.put(endpointParams.endpoint,
|
|
100
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
101
|
+
break;
|
|
102
|
+
case HttpCallType.patch:
|
|
103
|
+
this.openRouter.patch(endpointParams.endpoint,
|
|
104
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
105
|
+
break;
|
|
106
|
+
case HttpCallType.delete:
|
|
107
|
+
this.openRouter.delete(endpointParams.endpoint,
|
|
108
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
109
|
+
break;
|
|
110
|
+
default:
|
|
111
|
+
throw new DynamoBEError({ message: `DYNAMOBEERROR: INVALID route type: ${endpointParams.type} - ${endpointParams.name}` })
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private mountSecureRoute(endpointParams: DynamoBEEndpointParams): void {
|
|
116
|
+
switch(endpointParams.type) {
|
|
117
|
+
case HttpCallType.get:
|
|
118
|
+
this.secureRouter.get(endpointParams.endpoint,
|
|
119
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
120
|
+
break;
|
|
121
|
+
case HttpCallType.post:
|
|
122
|
+
this.secureRouter.post(endpointParams.endpoint,
|
|
123
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
124
|
+
break;
|
|
125
|
+
case HttpCallType.put:
|
|
126
|
+
this.secureRouter.put(endpointParams.endpoint,
|
|
127
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
128
|
+
break;
|
|
129
|
+
case HttpCallType.patch:
|
|
130
|
+
this.secureRouter.patch(endpointParams.endpoint,
|
|
131
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
132
|
+
break;
|
|
133
|
+
case HttpCallType.delete:
|
|
134
|
+
this.secureRouter.delete(endpointParams.endpoint,
|
|
135
|
+
endpointParams.preProcessess, endpointParams.getTasksExecution());
|
|
136
|
+
break;
|
|
137
|
+
default:
|
|
138
|
+
throw new DynamoBEError({ message: `DYNAMOBEERROR: INVALID route type: ${endpointParams.type} - ${endpointParams.name}` })
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
/* import * as Express from 'express'; */
|
|
4
|
+
/* import { e as Express } from 'express'; */
|
|
5
|
+
import Express = require("express")
|
|
6
|
+
|
|
7
|
+
/* import express from 'express'; */
|
|
8
|
+
import * as Https from 'https';
|
|
9
|
+
import * as FileSystem from 'fs';
|
|
10
|
+
import * as BodyParser from 'body-parser';
|
|
11
|
+
import { DynamoBERountingModule } from './dynamobe-routing-module.service';
|
|
12
|
+
import { DynamoBESingletonService } from './dynamobe-singleton.service';
|
|
13
|
+
import { DynamoBERouteSecurity } from '../_enum/dynamobe-route-security.enum';
|
|
14
|
+
import { DBE_Shared } from './dynamobe-shared.service';
|
|
15
|
+
import { DynamoBEEndpointParams } from '../_models/dynamobe-endpoint-control';
|
|
16
|
+
import { DBE_Global_S } from './dynamobe-global.service';
|
|
17
|
+
|
|
18
|
+
export abstract class DynamoBEServer extends DynamoBESingletonService {
|
|
19
|
+
|
|
20
|
+
protected security: DynamoBERouteSecurity;
|
|
21
|
+
|
|
22
|
+
protected httpPort?: number;
|
|
23
|
+
protected httpsPort?: number;
|
|
24
|
+
|
|
25
|
+
protected cert?: {
|
|
26
|
+
keyPath: FileSystem.PathLike,
|
|
27
|
+
certPath: FileSystem.PathLike,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
private secureExpress: Express.Application;
|
|
31
|
+
private httpsServer: Https.Server;
|
|
32
|
+
private openExpress: Express.Application;
|
|
33
|
+
|
|
34
|
+
protected routingModules: DynamoBERountingModule[];
|
|
35
|
+
|
|
36
|
+
protected log: boolean = DBE_Global_S.getGlobalSettings().logSetup;
|
|
37
|
+
|
|
38
|
+
protected constructor(){
|
|
39
|
+
super();
|
|
40
|
+
|
|
41
|
+
this.setupRoutingModules();
|
|
42
|
+
this.setSecurity();
|
|
43
|
+
|
|
44
|
+
this.initExpresses();
|
|
45
|
+
this.startExpresses();
|
|
46
|
+
|
|
47
|
+
if (this.security !== DynamoBERouteSecurity.secure) {
|
|
48
|
+
this.mountOpenRoutes();
|
|
49
|
+
}
|
|
50
|
+
if (this.security !== DynamoBERouteSecurity.open && this.cert) {
|
|
51
|
+
this.mountSecureRoutes();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (this.log) console.log(`\nRoutes mounted.... server using security: ${this.security}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private initExpresses() {
|
|
58
|
+
if (this.security !== DynamoBERouteSecurity.secure) {
|
|
59
|
+
if (!this.httpPort) {
|
|
60
|
+
let errorMsg: string =
|
|
61
|
+
`You have open routes, but httpPort is not set!` +
|
|
62
|
+
`\nsecurity: ${this.security}` +
|
|
63
|
+
`\nset httpPort in DynamoBEServer - setupRoutingModules() to enable secure routes.`;
|
|
64
|
+
|
|
65
|
+
errorMsg += '\n\nThe routes setted to use open server:';
|
|
66
|
+
this.routingModules.forEach((module: DynamoBERountingModule) => {
|
|
67
|
+
if (module.security != DynamoBERouteSecurity.secure) {
|
|
68
|
+
errorMsg += `\n ${module.route} (security: ${module.security}) \n subroutes using open sever:`;
|
|
69
|
+
module.endpoints.forEach((endpoint: DynamoBEEndpointParams) => {
|
|
70
|
+
if (endpoint.security != DynamoBERouteSecurity.secure) {
|
|
71
|
+
errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
DBE_Shared.logError(errorMsg);
|
|
77
|
+
|
|
78
|
+
let error = new Error('Open routes cannot be established!');
|
|
79
|
+
let errorStack: string[] = error.stack.split('\n');
|
|
80
|
+
errorStack.splice(1, 2);
|
|
81
|
+
error.stack = errorStack.join('\n');
|
|
82
|
+
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.initOpenExpress();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (this.security !== DynamoBERouteSecurity.open) {
|
|
90
|
+
if (!this.cert || !this.httpsPort) {
|
|
91
|
+
let errorMsg: string =
|
|
92
|
+
`You have secure routes, but the certification paths or httpsPort are not set!` +
|
|
93
|
+
`\nsecurity: ${this.security}` +
|
|
94
|
+
`\nset...` +
|
|
95
|
+
`\n httpsPort and` +
|
|
96
|
+
`\n cert: {` +
|
|
97
|
+
`\n keyPath: FileSystem.PathLike,` +
|
|
98
|
+
`\n certPath: FileSystem.PathLike,` +
|
|
99
|
+
`\n }` +
|
|
100
|
+
`\nin DynamoBEServer - setupRoutingModules() to enable secure routes.`;
|
|
101
|
+
|
|
102
|
+
errorMsg += '\n\nThe routes setted to use secure server:';
|
|
103
|
+
this.routingModules.forEach((module: DynamoBERountingModule) => {
|
|
104
|
+
if (module.security != DynamoBERouteSecurity.open) {
|
|
105
|
+
errorMsg += `\n ${module.route} (security: ${module.security}) \n subroutes using secure sever:`;
|
|
106
|
+
module.endpoints.forEach((endpoint: DynamoBEEndpointParams) => {
|
|
107
|
+
if (endpoint.security != DynamoBERouteSecurity.open) {
|
|
108
|
+
errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
DBE_Shared.logError(errorMsg);
|
|
114
|
+
|
|
115
|
+
let error = new Error('Secure routes cannot be established!');
|
|
116
|
+
let errorStack: string[] = error.stack.split('\n');
|
|
117
|
+
errorStack.splice(1, 2);
|
|
118
|
+
error.stack = errorStack.join('\n');
|
|
119
|
+
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.initSecureExpress();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private initOpenExpress(): void {
|
|
128
|
+
this.openExpress = Express();
|
|
129
|
+
this.openExpress.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
|
|
130
|
+
this.openExpress.use(BodyParser.json({ limit: '50mb' }));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private initSecureExpress(): void {
|
|
134
|
+
this.secureExpress = Express();
|
|
135
|
+
this.secureExpress.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
|
|
136
|
+
this.secureExpress.use(BodyParser.json({ limit: '50mb' }));
|
|
137
|
+
|
|
138
|
+
const options = {
|
|
139
|
+
key: FileSystem.readFileSync(this.cert.keyPath),
|
|
140
|
+
cert: FileSystem.readFileSync(this.cert.certPath),
|
|
141
|
+
};
|
|
142
|
+
this.httpsServer = Https.createServer(options, this.secureExpress);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private async startExpresses(): Promise<void> {
|
|
146
|
+
if (this.security !== DynamoBERouteSecurity.open) {
|
|
147
|
+
this.httpsServer
|
|
148
|
+
.listen(this.httpsPort, 'localhost', 0, () => {
|
|
149
|
+
DBE_Shared.logSuccess(`\nHTTPS (secure) server is listening on port ${this.httpsPort}\n`);
|
|
150
|
+
})
|
|
151
|
+
.on('error', (error) => {
|
|
152
|
+
DBE_Shared.logError(`https (secure) server ERROR`, error);
|
|
153
|
+
})
|
|
154
|
+
.on('uncaughtException', (ex) => {
|
|
155
|
+
DBE_Shared.logWarning(`https (secure) server uncaughtException`, ex);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (this.security !== DynamoBERouteSecurity.secure) {
|
|
160
|
+
this.openExpress
|
|
161
|
+
.listen(this.httpPort, () => {
|
|
162
|
+
DBE_Shared.logSuccess(`\nHTTP (-open-) server is listening on port ${this.httpPort}\n`);
|
|
163
|
+
})
|
|
164
|
+
.on('error', (error) => {
|
|
165
|
+
DBE_Shared.logError(`http (-open-) server ERROR`, error);
|
|
166
|
+
})
|
|
167
|
+
.on('uncaughtException', (ex) => {
|
|
168
|
+
DBE_Shared.logWarning(`http (-open-) server uncaughtException`, ex);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private mountSecureRoutes (): void {
|
|
174
|
+
this.routingModules.forEach((module: DynamoBERountingModule) => {
|
|
175
|
+
if (module.security !== DynamoBERouteSecurity.open) {
|
|
176
|
+
if (this.log) console.log(`route mount (secure): ${module.route}`);
|
|
177
|
+
const existingRoutes: DynamoBERountingModule[] = this.routingModules.filter(
|
|
178
|
+
(mod: DynamoBERountingModule) => mod.route === module.route);
|
|
179
|
+
if (1 < existingRoutes.length) {
|
|
180
|
+
let error = new Error(`ROUTE DUPLICATION: ${module.route}`);
|
|
181
|
+
let errorStack: string[] = error.stack.split('\n');
|
|
182
|
+
errorStack.splice(1, 4);
|
|
183
|
+
error.stack = errorStack.join('\n');
|
|
184
|
+
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
this.secureExpress.use(module.route, module.secureRouter);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private mountOpenRoutes (): void {
|
|
193
|
+
this.routingModules.forEach((module: DynamoBERountingModule) => {
|
|
194
|
+
if (module.security !== DynamoBERouteSecurity.secure) {
|
|
195
|
+
if (this.log) console.log(`route mount (open): ${module.route}`);
|
|
196
|
+
const existingRoutes: DynamoBERountingModule[] = this.routingModules.filter(
|
|
197
|
+
(mod: DynamoBERountingModule) => mod.route === module.route);
|
|
198
|
+
if (1 < existingRoutes.length) {
|
|
199
|
+
let error = new Error(`ROUTE DUPLICATION: ${module.route}`);
|
|
200
|
+
let errorStack: string[] = error.stack.split('\n');
|
|
201
|
+
errorStack.splice(1, 4);
|
|
202
|
+
error.stack = errorStack.join('\n');
|
|
203
|
+
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
this.openExpress.use(module.route, module.openRouter);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private setSecurity(): void {
|
|
212
|
+
this.routingModules.forEach((module: DynamoBERountingModule) => {
|
|
213
|
+
if (!this.security) {
|
|
214
|
+
this.security = module.security;
|
|
215
|
+
} else if (this.security !== module.security) {
|
|
216
|
+
this.security = DynamoBERouteSecurity.both;
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* You must setup endpoints and required services in this function
|
|
223
|
+
*/
|
|
224
|
+
abstract setupRoutingModules(): void;
|
|
225
|
+
}
|