@futdevpro/nts-dynamo 1.4.13 → 1.4.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,235 +2,11 @@
2
2
  import * as ReadLine from 'readline';
3
3
  import { Request } from 'express';
4
4
  import * as GeoIp from 'geoip-lite';
5
- import { DynamoBEError, GeoIpLocation, LocationCoordinates,
6
- year, hour, day, week, month } from '@futdevpro/fsm-dynamo';
5
+ import { GeoIpLocation, D_Shared } from '@futdevpro/fsm-dynamo/shared-service';
7
6
 
8
- import { LogStyle } from '../_enums/log-style.enum';
9
-
10
- export class DBE_Shared {
11
-
12
- /**
13
- * returns remapped object list by path list
14
- * @param objList object list to map
15
- * @param paths string list of path etc.:
16
- * [ [ 'first', 'sub', 'label' ], [ 'first', 'dif', 'name' ]] will be...
17
- * from
18
- * {
19
- * first: {
20
- * sub: {
21
- * label: 'vlmi'
22
- * },
23
- * dif: {
24
- * name: 'asd'
25
- * }
26
- * }
27
- * }[]
28
- * to
29
- * { label: 'vlmi', name: 'asd' }[]
30
- * @returns remapped object list (the final obj will use the deepest keys)
31
- */
32
- static mapObjList(objList: object[], paths: string[][]): object[] {
33
- let newObjList = [...objList];
34
- newObjList = newObjList.map(obj => {
35
- const newObj = {};
36
- paths.forEach((path: string[]) => {
37
- newObj[path[path.length - 1]] = this.getNestedData(obj, path);
38
- });
39
- return newObj;
40
- });
41
- return newObjList;
42
- }
43
-
44
- /**
45
- * returns nested value from object
46
- * @param parentObj data source object
47
- * @param nestedDataKeys path of value as string list
48
- * etc.: ['nestLvl1', 'nestLvl2', 'nestLvl3'] as parentObj.nestLvl1.nestLvl2.nestLvl3
49
- * @returns data from object by path
50
- */
51
- static getNestedData(parentObj: object, nestedDataKeys: string[]): any {
52
- let newData = {...parentObj};
53
- nestedDataKeys.forEach((dk: string) => {
54
- if (newData) {
55
- newData = newData[dk];
56
- }
57
- });
58
- return newData;
59
- }
60
-
61
- /**
62
- * recursive function that nests data
63
- * @param newData the object that that is needed to be updated
64
- * @param nestKeys the path where the data should be updated
65
- * @param dataToSet the actual value that will be setted
66
- * @returns modified data
67
- */
68
- static nestData(parentObj: object, nestKeys: string[], dataToSet: any): object {
69
- const newData = {...parentObj};
70
- if (nestKeys.length > 1) {
71
- const keys = [...nestKeys];
72
- const nextNestKey = keys.shift();
73
- newData[nextNestKey] = this.nestData(newData[nextNestKey], keys, dataToSet);
74
- } else {
75
- newData[nestKeys[0]] = dataToSet;
76
- }
77
- return newData;
78
- }
79
-
80
- /**
81
- * recursive function that adds to nested data
82
- * @param newData the object that that is needed to be updated
83
- * @param nestKeys the path where the data should be updated
84
- * @param dataToAdd the actual value that will be setted
85
- * @returns modified data
86
- */
87
- static addToNestedData(parentObj: object, nestKeys: string[], dataToAdd: any): object {
88
- const newData = {...parentObj};
89
- if (nestKeys.length > 1) {
90
- const keys = [...nestKeys];
91
- const nextNestKey = keys.shift();
92
- newData[nextNestKey] = this.addToNestedData(newData[nextNestKey], keys, dataToAdd);
93
- } else {
94
- newData[nestKeys[0]] += dataToAdd;
95
- }
96
- return newData;
97
- }
98
-
99
- /**
100
- * recursive function that pushes to nested data
101
- * @param newData the object that that is needed to be updated
102
- * @param nestKeys the path where the data should be updated
103
- * @param dataToPush the actual value that will be setted
104
- * @returns modified data
105
- */
106
- static pushToNestedData(parentObj: object, nestKeys: string[], dataToPush: any): object {
107
- const newData = {...parentObj};
108
- if (nestKeys.length > 1) {
109
- const keys = [...nestKeys];
110
- const nextNestKey = keys.shift();
111
- newData[nextNestKey] = this.pushToNestedData(newData[nextNestKey], keys, dataToPush);
112
- } else {
113
- if (Array.isArray(newData[nestKeys[0]])) {
114
- newData[nestKeys[0]].push(dataToPush);
115
- } else {
116
- newData[nestKeys[0]] = [dataToPush];
117
- }
118
- }
119
- return newData;
120
- }
121
-
122
- /**
123
- * recursive function that nests data on recursive data structure etc.: xData: { ..., 'nestedListKey': xData[] }
124
- * @param newData the object that that is needed to be updated
125
- * @param nestedListKey the location of nested dataList
126
- * @param nestIndexes path to the subject ect.: xData.listKey[1].listKey[4].listKey[0] will be [1, 4, 0]
127
- * @param dataToSet the actual value that will be setted on location
128
- * @returns modified data
129
- */
130
- static nestRecursiveListedData<T>(parentObj: T, nestedListKey: string, nestIndexes: number[], dataToSet: any): T {
131
- const newData = {...parentObj};
132
- if (nestIndexes.length > 1) {
133
- const indexes = [...nestIndexes];
134
- const nextLevelKey = indexes.shift();
135
- newData[nestedListKey][nextLevelKey] = this.nestRecursiveListedData(
136
- newData[nestedListKey][nextLevelKey], nestedListKey, indexes, dataToSet);
137
- } else {
138
- newData[nestedListKey][nestIndexes[0]] = dataToSet;
139
- }
140
- return newData;
141
- }
142
-
143
- static getAge(birthDate: Date | number | string) {
144
- return this.getYear(+new Date() - +new Date(birthDate));
145
- }
146
-
147
- static getYear(date: Date | number | string) {
148
- if (typeof date === 'string') {
149
- date = new Date(date);
150
- }
151
- return (+date / year);
152
- }
153
-
154
- static getHour(date: Date | number | string) {
155
- if (typeof date === 'string') {
156
- date = new Date(date);
157
- }
158
- return (+date / hour);
159
- }
160
-
161
- static getDateByAge(age: number) {
162
- return new Date(+new Date() - (year * age));
163
- }
164
-
165
- static getDistanceInKilometres(
166
- from: LocationCoordinates,
167
- to: LocationCoordinates
168
- ): number {
169
-
170
- const R = 6371; // kilometres
171
- const φ1 = this.toRadians(from.latitude);
172
- const φ2 = this.toRadians(to.latitude);
173
- const Δφ = this.toRadians(to.latitude - from.latitude);
174
- const Δλ = this.toRadians(to.longitude - from.longitude);
175
-
176
- const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
177
- Math.cos(φ1) * Math.cos(φ2) *
178
- Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
179
- const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
180
-
181
- return R * c;
182
- }
183
-
184
- static getLocationDegByKilometers(latiOrLong: 'latitude' | 'longitude', distanceInKm: number, latitude?: number): number {
185
- if (latiOrLong === 'latitude') {
186
- return distanceInKm / 110.574;
187
- } else {
188
- if (latitude) {
189
- return distanceInKm / (Math.cos(this.toRadians(latitude)) * 111.320);
190
- } else {
191
- throw new DynamoBEError({ status: 417, message: 'When using getLocationDegByKilometers for longitude, you need to give latitude!' });
192
- }
193
- }
194
- }
195
-
196
- static toRadians(deg: number): number {
197
- return deg * (Math.PI / 180);
198
- }
199
-
200
- static addMetadataToSchema(schema: any): any {
201
- schema.__created = { type: Date };
202
- schema.__createdBy = { type: String };
203
- schema.__lastModified = { type: Date };
204
- schema.__lastModifiedBy = { type: String };
205
- return schema;
206
- }
207
-
208
- static oneHourAgo(): Date {
209
- return new Date(+new Date() - hour);
210
- }
211
- static oneDayAgo(): Date {
212
- return new Date(+new Date() - day);
213
- }
214
- static oneWeekAgo(): Date {
215
- return new Date(+new Date() - week);
216
- }
217
- static oneMonthAgo(): Date {
218
- return new Date(+new Date() - month);
219
- }
220
- static oneYearAgo(): Date {
221
- return new Date(+new Date() - year);
222
- }
7
+ export class DBE_Shared extends D_Shared {
223
8
 
224
9
  static getIpFromRequest(request: Request): string {
225
- /* console.log('TESTTTT route:\n',
226
- `\n|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n
227
- HEADERS\n`, request.headers,
228
- `\n|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n
229
- ELSE\n`,
230
- `\nremote `, request.socket.remoteAddress,
231
- `\nlocal `, request.socket.localAddress,
232
- ); */
233
-
234
10
  let ip: string;
235
11
  if (request.headers['x-forwarded-for']) {
236
12
  const route: string[] = (request.headers['x-forwarded-for'] as string).split(', ');
@@ -239,8 +15,6 @@ export class DBE_Shared {
239
15
  } else {
240
16
  ip = request.socket.remoteAddress;
241
17
  }
242
-
243
- /* console.log(`ip from request: ${ip}`); */
244
18
  return ip;
245
19
  }
246
20
 
@@ -248,53 +22,6 @@ export class DBE_Shared {
248
22
  return GeoIp.lookup(this.getIpFromRequest(request));
249
23
  }
250
24
 
251
- static setLogStyle(styles: LogStyle[]): void {
252
- let styleSets = '';
253
- styles.forEach((style: LogStyle) => {
254
- styleSets += style;
255
- });
256
- console.log(styleSets);
257
- }
258
- static resetLogStyle(): void {
259
- console.log(LogStyle.reset);
260
- }
261
-
262
- static logStyle(input: string, styles: LogStyle[], dontReset?: boolean): string {
263
- let result = '';
264
- styles.forEach((style: LogStyle) => {
265
- result += style;
266
- });
267
- result += input;
268
- if (!dontReset) {
269
- result += LogStyle.reset;
270
- }
271
- return result;
272
- }
273
-
274
- static logSuccess(message: string, ...optionalParams: any[]): void {
275
- if (0 < optionalParams.length) {
276
- console.error(`${LogStyle.green}${LogStyle.bright}${message}`, ...optionalParams, LogStyle.reset);
277
- } else {
278
- console.error(`${LogStyle.green}${LogStyle.bright}${message}${LogStyle.reset}`);
279
- }
280
- }
281
-
282
- static logError(message: string, ...optionalParams: any[]): void {
283
- if (0 < optionalParams.length) {
284
- console.error(`${LogStyle.red}${LogStyle.bright}${message}`, ...optionalParams, LogStyle.reset);
285
- } else {
286
- console.error(`${LogStyle.red}${LogStyle.bright}${message}${LogStyle.reset}`);
287
- }
288
- }
289
-
290
- static logWarning(message: string, ...optionalParams: any[]): void {
291
- if (0 < optionalParams.length) {
292
- console.warn(`${LogStyle.yellow}${LogStyle.bright}${message}`, ...optionalParams, LogStyle.reset);
293
- } else {
294
- console.warn(`${LogStyle.yellow}${LogStyle.bright}${message}${LogStyle.reset}`);
295
- }
296
- }
297
-
298
25
  static async prompt(question: string): Promise<string> {
299
26
  const readLine = ReadLine.createInterface({
300
27
  input: process.stdin,
@@ -13,29 +13,6 @@ export * from './dynamobe-email-service-collection.service';
13
13
  export * from './dynamobe-email.service';
14
14
  export * from './dynamobe-global.service';
15
15
  export * from './dynamobe-routing-module.service';
16
- export * from './dynamobe-shared-be.service';
17
16
  export * from './dynamobe-shared.service';
18
17
  export * from './dynamobe-singleton.service';
19
18
 
20
- /*
21
- import { DynamoBEApiService } from './dynamobe-api.service';
22
- import { DynamoBEApp } from './dynamobe-app';
23
- import { DynamoBEAuthService } from './dynamobe-auth.service';
24
- import { DynamoBEController } from './dynamobe-controller.service';
25
- import { DynamoBEDataService } from './dynamobe-data.service';
26
- import { DynamoBEDBServiceCollection } from './dynamobe-db-service-collection.service';
27
- import { DynamoBEDBService } from './dynamobe-db.service';
28
- import { DynamoBEEmailServiceCollection } from './dynamobe-email-service-collection.service';
29
- import { DynamoBEEmailService } from './dynamobe-email.service';
30
- import { DBE_Global_S } from './dynamobe-global.service';
31
- import { DynamoBERountingModule } from './dynamobe-routing-module.service';
32
- import { DynamoBEServer } from './dynamobe-server';
33
- import { DBE_Shared } from './dynamobe-shared.service';
34
- import { DynamoBESingletonService } from './dynamobe-singleton.service';
35
-
36
- module.exports = {
37
- DynamoBEApiService, DynamoBEApp, DynamoBEAuthService, DynamoBEController,
38
- DynamoBEDataService, DynamoBEDBServiceCollection, DynamoBEDBService,
39
- DynamoBEEmailServiceCollection, DynamoBEEmailService, DBE_Global_S,
40
- DynamoBERountingModule, DynamoBEServer, DBE_Shared, DynamoBESingletonService
41
- } */
package/src/index.ts CHANGED
@@ -8,11 +8,11 @@ export * from './_enums';
8
8
  // MODELS
9
9
  export * from './_models';
10
10
 
11
- // SERVICES
12
- export * from './_services';
13
-
14
11
  // MODULES
15
12
  export * from './_modules/test/index';
16
13
  export * from './_modules/usage/index';
17
14
 
15
+ // SERVICES
16
+ export * from './_services';
17
+
18
18
 
@@ -1,30 +0,0 @@
1
-
2
-
3
- export enum LogStyle {
4
- reset = '\x1b[0m',
5
-
6
- bright = '\x1b[1m',
7
- dim = '\x1b[2m',
8
- underline = '\x1b[4m',
9
- blink = '\x1b[5m',
10
- reverse = '\x1b[7m',
11
- hidden = '\x1b[8m',
12
-
13
- black = '\x1b[30m',
14
- red = '\x1b[31m',
15
- green = '\x1b[32m',
16
- yellow = '\x1b[33m',
17
- blue = '\x1b[34m',
18
- magenta = '\x1b[35m',
19
- cyan = '\x1b[36m',
20
- white = '\x1b[37m',
21
-
22
- BgBlack = '\x1b[40m',
23
- BgRed = '\x1b[41m',
24
- BgGreen = '\x1b[42m',
25
- BgYellow = '\x1b[43m',
26
- BgBlue = '\x1b[44m',
27
- BgMagenta = '\x1b[45m',
28
- BgCyan = '\x1b[46m',
29
- BgWhite = '\x1b[47m',
30
- }
@@ -1,44 +0,0 @@
1
-
2
- import * as ReadLine from 'readline';
3
- import { Request } from 'express';
4
- import * as GeoIp from 'geoip-lite';
5
- import { GeoIpLocation } from '@futdevpro/fsm-dynamo';
6
-
7
- import { DBE_Shared } from './dynamobe-shared.service';
8
-
9
- export class DBE_Shared_BE extends DBE_Shared {
10
-
11
- static getIpFromRequest(request: Request): string {
12
- let ip: string;
13
- if (request.headers['x-forwarded-for']) {
14
- const route: string[] = (request.headers['x-forwarded-for'] as string).split(', ');
15
- console.log('TESTTTT route:', route);
16
- ip = route[route.length - 1];
17
- } else {
18
- ip = request.socket.remoteAddress;
19
- }
20
- return ip;
21
- }
22
-
23
- static getLocationDataByRequest(request: Request): GeoIpLocation {
24
- return GeoIp.lookup(this.getIpFromRequest(request));
25
- }
26
-
27
- static async prompt(question: string): Promise<string> {
28
- const readLine = ReadLine.createInterface({
29
- input: process.stdin,
30
- output: process.stdout
31
- });
32
-
33
- return new Promise((resolve, reject) => {
34
- try {
35
- readLine.question(question, (result: string) => {
36
- resolve(result);
37
- readLine.close();
38
- });
39
- } catch (error) {
40
- reject(error);
41
- }
42
- });
43
- }
44
- }