@candlerip/shared3 0.0.122 → 0.0.124

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. package/package.json +1 -1
  2. package/src/aws/app-config/get-app-config/util.js +33 -11
  3. package/src/backend/cache/cache/singleton.js +24 -8
  4. package/src/backend/database/mutation/error-log/create-error-log/util.js +3 -1
  5. package/src/backend/database/mutation/translation/get-translations/util.js +11 -7
  6. package/src/backend/database/mutation/translation/get-translations-as-dictionary/util.js +3 -1
  7. package/src/backend/environment/load-env-file/util.js +3 -1
  8. package/src/backend/message-broker/message-broker/singleton.js +3 -1
  9. package/src/dictionary/language/default.d.ts +2 -0
  10. package/src/dictionary/language/default.js +1 -0
  11. package/src/dictionary/language/index.d.ts +1 -0
  12. package/src/dictionary/language/index.js +1 -0
  13. package/src/error/custom-error/compose-custom-error/index.d.ts +2 -0
  14. package/src/error/custom-error/compose-custom-error/index.js +7 -0
  15. package/src/error/custom-error/compose-custom-error/type.d.ts +2 -0
  16. package/src/error/custom-error/compose-custom-error/type.js +1 -0
  17. package/src/error/custom-error/index.d.ts +3 -1
  18. package/src/error/custom-error/index.js +3 -1
  19. package/src/error/custom-error/serialize-info/index.d.ts +2 -0
  20. package/src/error/custom-error/serialize-info/index.js +9 -0
  21. package/src/error/custom-error/serialize-info/type.d.ts +2 -0
  22. package/src/error/custom-error/serialize-info/type.js +1 -0
  23. package/src/error/custom-error/worker/index.d.ts +8 -0
  24. package/src/error/custom-error/worker/index.js +24 -0
  25. package/src/error/custom-error/worker/type.d.ts +3 -0
  26. package/src/error/custom-error/worker/type.js +1 -0
  27. package/src/type/array/validate.js +9 -3
  28. package/src/type/string/comma-separated/validate.js +9 -3
  29. package/src/type/string/validate.js +9 -3
  30. package/src/type/type/validate.js +9 -3
  31. package/src/error/custom-error/worker.d.ts +0 -7
  32. package/src/error/custom-error/worker.js +0 -34
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@candlerip/shared3",
3
- "version": "0.0.122",
3
+ "version": "0.0.124",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.js",
@@ -11,45 +11,63 @@ export const getAppConfig = async (props) => {
11
11
  resp = await client.send(new appConfigSdk.ListApplicationsCommand());
12
12
  }
13
13
  catch (err) {
14
- return composeCustomError('Error during list AppConfig Applications', { err });
14
+ return {
15
+ customError: composeCustomError('Error during list AppConfig Applications', { err }),
16
+ };
15
17
  }
16
18
  const applications = resp.Items;
17
19
  if (!applications) {
18
- return composeCustomError('No AppConfig Applications found');
20
+ return {
21
+ customError: composeCustomError('No AppConfig Applications found'),
22
+ };
19
23
  }
20
24
  const application = applications.find((it) => it.Name === projectName);
21
25
  if (!application) {
22
- return composeCustomError('AppConfig Application not found');
26
+ return {
27
+ customError: composeCustomError('AppConfig Application not found'),
28
+ };
23
29
  }
24
30
  const applicationId = application.Id;
25
31
  try {
26
32
  resp = await client.send(new appConfigSdk.ListEnvironmentsCommand({ ApplicationId: applicationId }));
27
33
  }
28
34
  catch (err) {
29
- return composeCustomError('Error during list AppConfig Environments', { err });
35
+ return {
36
+ customError: composeCustomError('Error during list AppConfig Environments', { err }),
37
+ };
30
38
  }
31
39
  const environments = resp.Items;
32
40
  if (!environments) {
33
- return composeCustomError('No AppConfig Environments found');
41
+ return {
42
+ customError: composeCustomError('No AppConfig Environments found'),
43
+ };
34
44
  }
35
45
  const environment = environments.find((it) => it.Name === environmentMode);
36
46
  if (!environment) {
37
- return composeCustomError('AppConfig Environment not found');
47
+ return {
48
+ customError: composeCustomError('AppConfig Environment not found'),
49
+ };
38
50
  }
39
51
  const environmentId = environment.Id;
40
52
  try {
41
53
  resp = await client.send(new appConfigSdk.ListConfigurationProfilesCommand({ ApplicationId: applicationId }));
42
54
  }
43
55
  catch (err) {
44
- return composeCustomError('Error during list AppConfig Configuration Profiles', { err });
56
+ return {
57
+ customError: composeCustomError('Error during list AppConfig Configuration Profiles', { err }),
58
+ };
45
59
  }
46
60
  const configurationProfiles = resp.Items;
47
61
  if (!configurationProfiles) {
48
- return composeCustomError('No AppConfig Configuration Profiles found');
62
+ return {
63
+ customError: composeCustomError('No AppConfig Configuration Profiles found'),
64
+ };
49
65
  }
50
66
  const configurationProfile = configurationProfiles.find((it) => it.Name === `${environmentMode}-profile`);
51
67
  if (!configurationProfile) {
52
- return composeCustomError('AppConfig Configuration Profile not found');
68
+ return {
69
+ customError: composeCustomError('AppConfig Configuration Profile not found'),
70
+ };
53
71
  }
54
72
  const configurationProfileId = configurationProfile.Id;
55
73
  client = new appConfigDataSdk.AppConfigDataClient();
@@ -61,7 +79,9 @@ export const getAppConfig = async (props) => {
61
79
  }));
62
80
  }
63
81
  catch (err) {
64
- return composeCustomError('Error during get AppConfig initial configuration token', { err });
82
+ return {
83
+ customError: composeCustomError('Error during get AppConfig initial configuration token', { err }),
84
+ };
65
85
  }
66
86
  const ConfigurationToken = resp.InitialConfigurationToken;
67
87
  let appConfig;
@@ -71,7 +91,9 @@ export const getAppConfig = async (props) => {
71
91
  appConfig = JSON.parse(appConfigStr);
72
92
  }
73
93
  catch (err) {
74
- return composeCustomError('Error during get AppConfig', { err });
94
+ return {
95
+ customError: composeCustomError('Error during get AppConfig', { err }),
96
+ };
75
97
  }
76
98
  return {
77
99
  data: appConfig,
@@ -8,10 +8,14 @@ export const CacheSingleton = (() => {
8
8
  let _customErrorWorker;
9
9
  const checkErrorStatus = () => {
10
10
  if (!_isEnabled) {
11
- return _customErrorWorker.composeCustomError('Cache disabled');
11
+ return {
12
+ customError: _customErrorWorker.composeCustomError('Cache disabled'),
13
+ };
12
14
  }
13
15
  if (_err) {
14
- return _customErrorWorker.composeCustomError('Error connect Cache', { err: _err });
16
+ return {
17
+ customError: _customErrorWorker.composeCustomError('Error connect Cache', { err: _err }),
18
+ };
15
19
  }
16
20
  return;
17
21
  };
@@ -44,10 +48,14 @@ export const CacheSingleton = (() => {
44
48
  }
45
49
  }
46
50
  catch (err) {
47
- return _customErrorWorker.composeCustomError('Error get data from Redis', { err });
51
+ return {
52
+ customError: _customErrorWorker.composeCustomError('Error get data from Redis', { err }),
53
+ };
48
54
  }
49
55
  if (!data) {
50
- return _customErrorWorker.composeCustomError('No data in Redis');
56
+ return {
57
+ customError: _customErrorWorker.composeCustomError('No data in Redis'),
58
+ };
51
59
  }
52
60
  return {
53
61
  data,
@@ -63,17 +71,23 @@ export const CacheSingleton = (() => {
63
71
  try {
64
72
  const cache = await _client.mGet(keys);
65
73
  if (!isArray(cache, isString)) {
66
- return _customErrorWorker.composeCustomError('Error get data from redis', { cache });
74
+ return {
75
+ customError: _customErrorWorker.composeCustomError('Error get data from redis', { cache }),
76
+ };
67
77
  }
68
78
  cache.forEach((it, i) => {
69
79
  data[keys[i]] = JSON.parse(it);
70
80
  });
71
81
  }
72
82
  catch (err) {
73
- return _customErrorWorker.composeCustomError('Error get data from redis', { err });
83
+ return {
84
+ customError: _customErrorWorker.composeCustomError('Error get data from redis', { err }),
85
+ };
74
86
  }
75
87
  if (Object.keys(data).length !== keys.length) {
76
- return _customErrorWorker.composeCustomError('Missing data from redis', { data });
88
+ return {
89
+ customError: _customErrorWorker.composeCustomError('Missing data from redis', { data }),
90
+ };
77
91
  }
78
92
  return {
79
93
  data,
@@ -89,7 +103,9 @@ export const CacheSingleton = (() => {
89
103
  await _client.set(key, JSON.stringify(data));
90
104
  }
91
105
  catch (err) {
92
- return _customErrorWorker.composeCustomError('Error set data to redis', { err });
106
+ return {
107
+ customError: _customErrorWorker.composeCustomError('Error set data to redis', { err }),
108
+ };
93
109
  }
94
110
  return {
95
111
  data: null,
@@ -7,7 +7,9 @@ export const createErrorLog = async (customError) => {
7
7
  data = await getErrorLogModel().create({ customError });
8
8
  }
9
9
  catch (err) {
10
- return composeCustomError('Create error log failed', { err });
10
+ return {
11
+ customError: composeCustomError('Create error log failed', { err }),
12
+ };
11
13
  }
12
14
  return {
13
15
  data,
@@ -13,15 +13,19 @@ export const getTranslations = async (ids) => {
13
13
  resp = await getTranslationLogModel().find(query);
14
14
  }
15
15
  catch (err) {
16
- return _customErrorWorker.composeCustomError('Get translations failed', { err, query });
16
+ return {
17
+ customError: _customErrorWorker.composeCustomError('Get translations failed', { err, query }),
18
+ };
17
19
  }
18
20
  if (ids && resp.length !== ids.length) {
19
- return _customErrorWorker.composeCustomError('Missing ids from translations', {
20
- info: {
21
- missing: ids.filter((it) => !resp.find((it2) => it2.id === it)).join(', '),
22
- value: ids,
23
- },
24
- });
21
+ return {
22
+ customError: _customErrorWorker.composeCustomError('Missing ids from translations', {
23
+ info: {
24
+ missing: ids.filter((it) => !resp.find((it2) => it2.id === it)).join(', '),
25
+ value: ids,
26
+ },
27
+ }),
28
+ };
25
29
  }
26
30
  const data = resp.map((it) => ({ ...it, _id: it._id.toString() }));
27
31
  return {
@@ -43,7 +43,9 @@ export const getTranslationsAsDictionary = async (language, ids) => {
43
43
  data = await getTranslationLogModel().aggregate(aggregates);
44
44
  }
45
45
  catch (err) {
46
- return _customErrorWorker.composeCustomError('Get translations failed', { err });
46
+ return {
47
+ customError: _customErrorWorker.composeCustomError('Get translations failed', { err }),
48
+ };
47
49
  }
48
50
  return {
49
51
  data: data[0],
@@ -6,7 +6,9 @@ export const loadEnvFile = (environmentMode, projectName) => {
6
6
  const envFile = dotenv.config({ path: `./${fileName}` });
7
7
  const { error: err, parsed } = envFile;
8
8
  if (err || !parsed) {
9
- return composeCustomError(`Error loading ${fileName}`, { err });
9
+ return {
10
+ customError: composeCustomError(`Error loading ${fileName}`, { err }),
11
+ };
10
12
  }
11
13
  const environmentVariables = ENVIRONMENT_VARIABLES_CONFIG[projectName];
12
14
  return {
@@ -8,7 +8,9 @@ export const MessageBrokerSingleton = (() => {
8
8
  let _customErrorWorker;
9
9
  const checkErrorStatus = () => {
10
10
  if (_errorMessage) {
11
- return _customErrorWorker.composeCustomError(_errorMessage);
11
+ return {
12
+ customError: _customErrorWorker.composeCustomError(_errorMessage),
13
+ };
12
14
  }
13
15
  return;
14
16
  };
@@ -0,0 +1,2 @@
1
+ import { Language } from './type.js';
2
+ export declare const DEFAULT_LANGUAGE: Language;
@@ -0,0 +1 @@
1
+ export const DEFAULT_LANGUAGE = 'en';
@@ -2,3 +2,4 @@ export * from './config.js';
2
2
  export * from './type.js';
3
3
  export * from './validate.js';
4
4
  export * from './type-guard.js';
5
+ export * from './default.js';
@@ -2,3 +2,4 @@ export * from './config.js';
2
2
  export * from './type.js';
3
3
  export * from './validate.js';
4
4
  export * from './type-guard.js';
5
+ export * from './default.js';
@@ -0,0 +1,2 @@
1
+ import { ComposeCustomError } from './type.js';
2
+ export declare const composeCustomError: ComposeCustomError;
@@ -0,0 +1,7 @@
1
+ import { serializeInfo } from '../serialize-info/index.js';
2
+ export const composeCustomError = (message, projectName, info) => ({
3
+ id: Math.random().toString(16).slice(2).toUpperCase(),
4
+ info: serializeInfo(info),
5
+ message,
6
+ projectName,
7
+ });
@@ -0,0 +1,2 @@
1
+ import { CustomError } from '../type.js';
2
+ export type ComposeCustomError = (message: CustomError['message'], projectName: CustomError['projectName'], info?: CustomError['info']) => CustomError;
@@ -1,3 +1,5 @@
1
1
  export * from './type.js';
2
+ export * from './compose-custom-error/index.js';
2
3
  export * from './console-custom-error/index.js';
3
- export * from './worker.js';
4
+ export * from './serialize-info/index.js';
5
+ export * from './worker/index.js';
@@ -1,3 +1,5 @@
1
1
  export * from './type.js';
2
+ export * from './compose-custom-error/index.js';
2
3
  export * from './console-custom-error/index.js';
3
- export * from './worker.js';
4
+ export * from './serialize-info/index.js';
5
+ export * from './worker/index.js';
@@ -0,0 +1,2 @@
1
+ import { SerializeInfo } from './type.js';
2
+ export declare const serializeInfo: SerializeInfo;
@@ -0,0 +1,9 @@
1
+ export const serializeInfo = (info) => {
2
+ if (!info) {
3
+ return;
4
+ }
5
+ if (info?.err) {
6
+ info.err = JSON.stringify(info?.err, Object.getOwnPropertyNames(info?.err));
7
+ }
8
+ return info;
9
+ };
@@ -0,0 +1,2 @@
1
+ import { CustomError } from '../type.js';
2
+ export type SerializeInfo = (info?: CustomError['info']) => CustomError['info'];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { ProjectName } from '../../../project/index.js';
2
+ import { CustomError } from '../type.js';
3
+ import { Info, Message } from './type.js';
4
+ export declare const customErrorWorker: (info?: Info) => {
5
+ addInfo: (info?: Info) => void;
6
+ composeCustomError: (message: Message, info?: Info) => CustomError;
7
+ setProjectName: (projectName: ProjectName) => void;
8
+ };
@@ -0,0 +1,24 @@
1
+ import { ProjectSingleton } from '../../../project/index.js';
2
+ import { composeCustomError as composeCustomErrorFunc } from '../compose-custom-error/index.js';
3
+ export const customErrorWorker = (info) => {
4
+ let _info = info;
5
+ let _projectName;
6
+ const addInfo = (info) => {
7
+ _info = {
8
+ ..._info,
9
+ ...info,
10
+ };
11
+ };
12
+ const composeCustomError = (message, info) => composeCustomErrorFunc(message, _projectName ?? ProjectSingleton.getProjectName(), {
13
+ ..._info,
14
+ ...info,
15
+ });
16
+ const setProjectName = (projectName) => {
17
+ _projectName = projectName;
18
+ };
19
+ return {
20
+ addInfo,
21
+ composeCustomError,
22
+ setProjectName,
23
+ };
24
+ };
@@ -0,0 +1,3 @@
1
+ import { CustomError } from '../type.js';
2
+ export type Message = CustomError['message'];
3
+ export type Info = CustomError['info'];
@@ -0,0 +1 @@
1
+ export {};
@@ -2,13 +2,19 @@ import { customErrorWorker } from '../../error/index.js';
2
2
  export const validateArray = (data, name, valid) => {
3
3
  const { composeCustomError } = customErrorWorker({ data, name, valid });
4
4
  if (!data) {
5
- return composeCustomError(`Missing ${name}`);
5
+ return {
6
+ customError: composeCustomError(`Missing ${name}`),
7
+ };
6
8
  }
7
9
  if (!Array.isArray(data)) {
8
- return composeCustomError(`Invalid ${name}`);
10
+ return {
11
+ customError: composeCustomError(`Invalid ${name}`),
12
+ };
9
13
  }
10
14
  if (!data.every((it) => valid.includes(it))) {
11
- return composeCustomError(`Invalid ${name}`);
15
+ return {
16
+ customError: composeCustomError(`Invalid ${name}`),
17
+ };
12
18
  }
13
19
  return {
14
20
  data,
@@ -4,14 +4,20 @@ import { isString } from '../type-guard.js';
4
4
  export const validateCommaSeparatedString = (data, name, valid) => {
5
5
  const { composeCustomError } = customErrorWorker({ data, name, valid });
6
6
  if (!data) {
7
- return composeCustomError(`Missing ${name}`);
7
+ return {
8
+ customError: composeCustomError(`Missing ${name}`),
9
+ };
8
10
  }
9
11
  if (!isString(data)) {
10
- return composeCustomError(`${name} not string`);
12
+ return {
13
+ customError: composeCustomError(`${name} not string`),
14
+ };
11
15
  }
12
16
  const parsedArr = data.split(',');
13
17
  if (parsedArr.length === 0) {
14
- return composeCustomError(`Invalid ${name}`);
18
+ return {
19
+ customError: composeCustomError(`Invalid ${name}`),
20
+ };
15
21
  }
16
22
  return validateArray(parsedArr, name, valid);
17
23
  };
@@ -3,14 +3,20 @@ import { isString } from './type-guard.js';
3
3
  export const validateString = (data, name, regexp) => {
4
4
  const { composeCustomError } = customErrorWorker({ data, name, regexp: JSON.stringify(regexp) });
5
5
  if (!data) {
6
- return composeCustomError(`Missing ${name}`);
6
+ return {
7
+ customError: composeCustomError(`Missing ${name}`),
8
+ };
7
9
  }
8
10
  if (!isString(data)) {
9
- return composeCustomError(`${name} not string`);
11
+ return {
12
+ customError: composeCustomError(`${name} not string`),
13
+ };
10
14
  }
11
15
  if (regexp) {
12
16
  if (!regexp.test(data)) {
13
- return composeCustomError(`Invalid ${name}`);
17
+ return {
18
+ customError: composeCustomError(`Invalid ${name}`),
19
+ };
14
20
  }
15
21
  }
16
22
  return {
@@ -4,13 +4,19 @@ export const validateType = (data, name, valids) => {
4
4
  const _customErrorWorker = customErrorWorker();
5
5
  _customErrorWorker.addInfo({ data, name, valids });
6
6
  if (!data) {
7
- return _customErrorWorker.composeCustomError(`Missing ${name}`);
7
+ return {
8
+ customError: _customErrorWorker.composeCustomError(`Missing ${name}`),
9
+ };
8
10
  }
9
11
  if (!isString(data)) {
10
- return _customErrorWorker.composeCustomError(`Invalid ${name}`);
12
+ return {
13
+ customError: _customErrorWorker.composeCustomError(`Invalid ${name}`),
14
+ };
11
15
  }
12
16
  if (!valids.includes(data)) {
13
- return _customErrorWorker.composeCustomError(`Invalid ${name}`);
17
+ return {
18
+ customError: _customErrorWorker.composeCustomError(`Invalid ${name}`),
19
+ };
14
20
  }
15
21
  return {
16
22
  data: data,
@@ -1,7 +0,0 @@
1
- import { CustomError } from './type.js';
2
- export declare const customErrorWorker: (info?: CustomError["info"]) => {
3
- addInfo: (info?: CustomError["info"]) => void;
4
- composeCustomError: (message: CustomError["message"], info?: CustomError["info"]) => {
5
- customError: CustomError;
6
- };
7
- };
@@ -1,34 +0,0 @@
1
- import { ProjectSingleton } from '../../project/index.js';
2
- export const customErrorWorker = (info) => {
3
- let _info = info;
4
- const _serializeInfo = (info) => {
5
- if (!info) {
6
- return;
7
- }
8
- if (info?.err) {
9
- info.err = JSON.stringify(info?.err, Object.getOwnPropertyNames(info?.err));
10
- }
11
- return info;
12
- };
13
- const addInfo = (info) => {
14
- _info = {
15
- ..._info,
16
- ..._serializeInfo(info),
17
- };
18
- };
19
- const composeCustomError = (message, info) => ({
20
- customError: {
21
- id: Math.random().toString(16).slice(2).toUpperCase(),
22
- info: {
23
- ..._info,
24
- ..._serializeInfo(info),
25
- },
26
- message,
27
- projectName: ProjectSingleton.getProjectName(),
28
- },
29
- });
30
- return {
31
- addInfo,
32
- composeCustomError,
33
- };
34
- };