skyeye-svc-common-utils 2.0.0-dev0.2 → 2.0.0-dev0.21

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.
Files changed (110) hide show
  1. package/dist/index.d.ts +10 -7
  2. package/dist/index.js +10 -7
  3. package/dist/index.js.map +1 -1
  4. package/dist/lib/azure/azureKeyVault.d.ts +10 -0
  5. package/dist/lib/azure/azureKeyVault.js +39 -1
  6. package/dist/lib/azure/azureKeyVault.js.map +1 -1
  7. package/dist/lib/azure/cognitiveSearch/QueryType.d.ts +8 -0
  8. package/dist/lib/azure/cognitiveSearch/QueryType.js +13 -0
  9. package/dist/lib/azure/cognitiveSearch/QueryType.js.map +1 -0
  10. package/dist/lib/azure/cognitiveSearch/SearchClient.d.ts +42 -0
  11. package/dist/lib/azure/cognitiveSearch/SearchClient.js +161 -0
  12. package/dist/lib/azure/cognitiveSearch/SearchClient.js.map +1 -0
  13. package/dist/lib/azure/cognitiveSearch/SearchFacets.d.ts +16 -0
  14. package/dist/lib/azure/cognitiveSearch/SearchFacets.js +3 -0
  15. package/dist/lib/azure/cognitiveSearch/SearchFacets.js.map +1 -0
  16. package/dist/lib/azure/cognitiveSearch/SearchFilter.d.ts +68 -0
  17. package/dist/lib/azure/cognitiveSearch/SearchFilter.js +125 -0
  18. package/dist/lib/azure/cognitiveSearch/SearchFilter.js.map +1 -0
  19. package/dist/lib/azure/cognitiveSearch/SearchMode.d.ts +9 -0
  20. package/dist/lib/azure/cognitiveSearch/SearchMode.js +14 -0
  21. package/dist/lib/azure/cognitiveSearch/SearchMode.js.map +1 -0
  22. package/dist/lib/azure/cognitiveSearch/SearchOption.d.ts +88 -0
  23. package/dist/lib/azure/cognitiveSearch/SearchOption.js +104 -0
  24. package/dist/lib/azure/cognitiveSearch/SearchOption.js.map +1 -0
  25. package/dist/lib/azure/cognitiveSearch/SearchOrder.d.ts +9 -0
  26. package/dist/lib/azure/cognitiveSearch/SearchOrder.js +3 -0
  27. package/dist/lib/azure/cognitiveSearch/SearchOrder.js.map +1 -0
  28. package/dist/lib/azure/cognitiveSearch/index.d.ts +7 -0
  29. package/dist/lib/azure/cognitiveSearch/index.js +24 -0
  30. package/dist/lib/azure/cognitiveSearch/index.js.map +1 -0
  31. package/dist/lib/azure/index.d.ts +7 -0
  32. package/dist/lib/azure/index.js +24 -0
  33. package/dist/lib/azure/index.js.map +1 -0
  34. package/dist/lib/fetch/index.d.ts +2 -0
  35. package/dist/lib/fetch/index.js +19 -0
  36. package/dist/lib/fetch/index.js.map +1 -0
  37. package/dist/lib/fetch/sendRequest.d.ts +8 -0
  38. package/dist/lib/fetch/sendRequest.js +25 -0
  39. package/dist/lib/fetch/sendRequest.js.map +1 -0
  40. package/dist/lib/fetch/sendRequestWithTimeout.d.ts +9 -0
  41. package/dist/lib/fetch/sendRequestWithTimeout.js +53 -0
  42. package/dist/lib/fetch/sendRequestWithTimeout.js.map +1 -0
  43. package/dist/lib/httpClient.d.ts +1 -0
  44. package/dist/lib/httpClient.js +18 -14
  45. package/dist/lib/httpClient.js.map +1 -1
  46. package/dist/lib/index.d.ts +3 -0
  47. package/dist/lib/index.js +20 -0
  48. package/dist/lib/index.js.map +1 -0
  49. package/dist/lib/teams/broadcastLogMessage.d.ts +8 -0
  50. package/dist/lib/teams/broadcastLogMessage.js +81 -0
  51. package/dist/lib/teams/broadcastLogMessage.js.map +1 -0
  52. package/dist/lib/teams/index.d.ts +2 -0
  53. package/dist/lib/teams/index.js +19 -0
  54. package/dist/lib/teams/index.js.map +1 -0
  55. package/dist/lib/teams/logMessage.d.ts +51 -0
  56. package/dist/lib/teams/logMessage.js +33 -0
  57. package/dist/lib/teams/logMessage.js.map +1 -0
  58. package/dist/utils/appConst.d.ts +57 -0
  59. package/dist/utils/appConst.js +56 -1
  60. package/dist/utils/appConst.js.map +1 -1
  61. package/dist/utils/baseClass/appDataSource.d.ts +7 -0
  62. package/dist/utils/baseClass/appDataSource.js +31 -0
  63. package/dist/utils/baseClass/appDataSource.js.map +1 -0
  64. package/dist/utils/baseClass/baseController.js +8 -8
  65. package/dist/utils/baseClass/baseController.js.map +1 -1
  66. package/dist/utils/index.d.ts +2 -0
  67. package/dist/utils/index.js +19 -0
  68. package/dist/utils/index.js.map +1 -0
  69. package/dist/utils/logger/morganLogger.d.ts +2 -4
  70. package/dist/utils/logger/morganLogger.js +3 -3
  71. package/dist/utils/logger/morganLogger.js.map +1 -1
  72. package/dist/utils/middleware/loadConfig.d.ts +128 -0
  73. package/dist/utils/middleware/loadConfig.js +143 -0
  74. package/dist/utils/middleware/loadConfig.js.map +1 -0
  75. package/package.json +26 -23
  76. package/src/index.ts +10 -7
  77. package/src/lib/azure/azureKeyVault.ts +44 -0
  78. package/src/lib/azure/cognitiveSearch/QueryType.ts +8 -0
  79. package/src/lib/azure/cognitiveSearch/SearchClient.ts +149 -0
  80. package/src/lib/azure/cognitiveSearch/SearchFacets.ts +16 -0
  81. package/src/lib/azure/cognitiveSearch/SearchFilter.ts +140 -0
  82. package/src/lib/azure/cognitiveSearch/SearchMode.ts +9 -0
  83. package/src/lib/azure/cognitiveSearch/SearchOption.ts +155 -0
  84. package/src/lib/azure/cognitiveSearch/SearchOrder.ts +9 -0
  85. package/src/lib/azure/cognitiveSearch/index.ts +7 -0
  86. package/src/lib/azure/index.ts +7 -0
  87. package/src/lib/fetch/index.ts +2 -0
  88. package/src/lib/fetch/sendRequest.ts +10 -0
  89. package/src/lib/fetch/sendRequestWithTimeout.ts +38 -0
  90. package/src/lib/httpClient.ts +27 -26
  91. package/src/lib/index.ts +3 -0
  92. package/src/lib/teams/broadcastLogMessage.ts +70 -0
  93. package/src/lib/teams/index.ts +2 -0
  94. package/src/lib/teams/logMessage.ts +55 -0
  95. package/src/utils/appConfig.ts +2 -2
  96. package/src/utils/appConst.ts +64 -0
  97. package/src/utils/baseClass/appDataSource.ts +23 -0
  98. package/src/utils/baseClass/baseController.ts +9 -9
  99. package/src/utils/index.ts +2 -0
  100. package/src/utils/logger/morganLogger.ts +2 -2
  101. package/src/utils/middleware/loadConfig.ts +150 -0
  102. package/.env +0 -3
  103. package/dist/utils/baseClass/dataSourceManager.d.ts +0 -11
  104. package/dist/utils/baseClass/dataSourceManager.js +0 -54
  105. package/dist/utils/baseClass/dataSourceManager.js.map +0 -1
  106. package/dist/utils/middleware/startdbConnection.d.ts +0 -1
  107. package/dist/utils/middleware/startdbConnection.js +0 -30
  108. package/dist/utils/middleware/startdbConnection.js.map +0 -1
  109. package/src/utils/baseClass/dataSourceManager.ts +0 -44
  110. package/src/utils/middleware/startdbConnection.ts +0 -20
@@ -0,0 +1,128 @@
1
+ import { z } from 'zod';
2
+ import { Environment } from '../appConst';
3
+ /**
4
+ * @description
5
+ * @property {string} appName - service's app name from package.json
6
+ * @property {string} version - service's version from package.json
7
+ * @property {enum} environment - current environment
8
+ * @property {string} keyVaultName - Azure key vault name
9
+ * @property {string} keyVaultSecretName - secret name of Azure key vault
10
+ * @todo migration to single typedef
11
+ */
12
+ declare const zEnvFileAppConfig: z.ZodObject<{
13
+ appName: z.ZodString;
14
+ version: z.ZodString;
15
+ environment: z.ZodEnum<["test", "dev", "development", "preview", "staging", "beta", "production"]>;
16
+ keyVaultName: z.ZodString;
17
+ keyVaultSecretName: z.ZodString;
18
+ }, "strict", z.ZodTypeAny, {
19
+ appName?: string;
20
+ version?: string;
21
+ environment?: "dev" | "development" | "test" | "preview" | "staging" | "beta" | "production";
22
+ keyVaultName?: string;
23
+ keyVaultSecretName?: string;
24
+ }, {
25
+ appName?: string;
26
+ version?: string;
27
+ environment?: "dev" | "development" | "test" | "preview" | "staging" | "beta" | "production";
28
+ keyVaultName?: string;
29
+ keyVaultSecretName?: string;
30
+ }>;
31
+ /**
32
+ * @description typedef of environment File App Config
33
+ */
34
+ type EnvFileAppConfig = z.infer<typeof zEnvFileAppConfig>;
35
+ /**
36
+ * @todo migration to single typedef
37
+ */
38
+ declare const zSecretFileAppConfig: z.ZodObject<{
39
+ telemetry: z.ZodObject<{
40
+ teamsChannelWebHook: z.ZodObject<{
41
+ errorChannel: z.ZodString;
42
+ warnChannel: z.ZodString;
43
+ }, "strip", z.ZodTypeAny, {
44
+ errorChannel?: string;
45
+ warnChannel?: string;
46
+ }, {
47
+ errorChannel?: string;
48
+ warnChannel?: string;
49
+ }>;
50
+ elasticKeyVault: z.ZodObject<{
51
+ secretToken: z.ZodString;
52
+ serverUrl: z.ZodString;
53
+ }, "strip", z.ZodTypeAny, {
54
+ secretToken?: string;
55
+ serverUrl?: string;
56
+ }, {
57
+ secretToken?: string;
58
+ serverUrl?: string;
59
+ }>;
60
+ }, "strip", z.ZodTypeAny, {
61
+ teamsChannelWebHook?: {
62
+ errorChannel?: string;
63
+ warnChannel?: string;
64
+ };
65
+ elasticKeyVault?: {
66
+ secretToken?: string;
67
+ serverUrl?: string;
68
+ };
69
+ }, {
70
+ teamsChannelWebHook?: {
71
+ errorChannel?: string;
72
+ warnChannel?: string;
73
+ };
74
+ elasticKeyVault?: {
75
+ secretToken?: string;
76
+ serverUrl?: string;
77
+ };
78
+ }>;
79
+ serviceConfig: z.ZodNullable<z.ZodAny>;
80
+ }, "strip", z.ZodTypeAny, {
81
+ telemetry?: {
82
+ teamsChannelWebHook?: {
83
+ errorChannel?: string;
84
+ warnChannel?: string;
85
+ };
86
+ elasticKeyVault?: {
87
+ secretToken?: string;
88
+ serverUrl?: string;
89
+ };
90
+ };
91
+ serviceConfig?: any;
92
+ }, {
93
+ telemetry?: {
94
+ teamsChannelWebHook?: {
95
+ errorChannel?: string;
96
+ warnChannel?: string;
97
+ };
98
+ elasticKeyVault?: {
99
+ secretToken?: string;
100
+ serverUrl?: string;
101
+ };
102
+ };
103
+ serviceConfig?: any;
104
+ }>;
105
+ /**
106
+ * @description typedef of secret file
107
+ */
108
+ type SecretFileAppConfig = z.infer<typeof zSecretFileAppConfig>;
109
+ /**
110
+ * @todo migration to single typedef
111
+ */
112
+ type AppConfig = EnvFileAppConfig & SecretFileAppConfig;
113
+ /**
114
+ * @description configuration carrier (or, instance). We can classified all the configurations into 2 categories:
115
+ * - Environment File App Config: configuration from the environment file (.env) and some other settings
116
+ * - Secret File App Config: loaded from the Azure Key Vault
117
+ */
118
+ export declare let appConfig: AppConfig;
119
+ /**
120
+ * @todo move to loadConfig since all the environment fields should be loaded from appConfig
121
+ */
122
+ export declare const environment: Environment;
123
+ /**
124
+ * @description preload for app configuration, once it is been called, the appConfig will be loaded and be freezed
125
+ * @param packagePath - optional, path of `package.json` in order to load app name and version
126
+ */
127
+ export declare const loadConfig: (packagePath?: string) => Promise<void>;
128
+ export {};
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ var _a;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.loadConfig = exports.environment = exports.appConfig = void 0;
17
+ const dotenv_1 = __importDefault(require("dotenv"));
18
+ const lib_1 = require("../../lib");
19
+ const logger_1 = require("../logger/logger");
20
+ const zod_1 = require("zod");
21
+ const appConst_1 = require("../appConst");
22
+ /**
23
+ * @description
24
+ * @property {string} appName - service's app name from package.json
25
+ * @property {string} version - service's version from package.json
26
+ * @property {enum} environment - current environment
27
+ * @property {string} keyVaultName - Azure key vault name
28
+ * @property {string} keyVaultSecretName - secret name of Azure key vault
29
+ * @todo migration to single typedef
30
+ */
31
+ const zEnvFileAppConfig = zod_1.z.object({
32
+ appName: zod_1.z.string(),
33
+ version: zod_1.z.string(),
34
+ environment: appConst_1.zEnvironment,
35
+ keyVaultName: zod_1.z.string(),
36
+ keyVaultSecretName: zod_1.z.string()
37
+ }).strict();
38
+ /**
39
+ * @todo migration to single typedef
40
+ */
41
+ const zElasticKeyVault = zod_1.z.object({
42
+ secretToken: zod_1.z.string(),
43
+ serverUrl: zod_1.z.string()
44
+ });
45
+ /**
46
+ * @todo migration to single typedef
47
+ */
48
+ const zTeamsChannelWebHook = zod_1.z.object({
49
+ errorChannel: zod_1.z.string(),
50
+ warnChannel: zod_1.z.string()
51
+ });
52
+ /**
53
+ * @todo migration to single typedef
54
+ */
55
+ const zTelemetry = zod_1.z.object({
56
+ teamsChannelWebHook: zTeamsChannelWebHook,
57
+ elasticKeyVault: zElasticKeyVault
58
+ });
59
+ /**
60
+ * @todo migration to single typedef
61
+ */
62
+ const zSecretFileAppConfig = zod_1.z.object({
63
+ telemetry: zTelemetry,
64
+ serviceConfig: zod_1.z.any().nullable()
65
+ });
66
+ /**
67
+ * @todo move to loadConfig since all the environment fields should be loaded from appConfig
68
+ */
69
+ exports.environment = (_a = process.env.NODE_ENV) !== null && _a !== void 0 ? _a : 'development';
70
+ /**
71
+ * @description preload for app configuration, once it is been called, the appConfig will be loaded and be freezed
72
+ * @param packagePath - optional, path of `package.json` in order to load app name and version
73
+ */
74
+ const loadConfig = (packagePath) => __awaiter(void 0, void 0, void 0, function* () {
75
+ logger_1.logger.verbose('start /appConfig/preload()');
76
+ // load environment file
77
+ dotenv_1.default.config({ path: `${exports.environment}.env` });
78
+ /**
79
+ * To load the appName and the version, we read the package information in the following order:
80
+ * 1. check if the environment value is defined directly
81
+ * 2. check if environment given from npm is defined
82
+ * 3. check if value in package.json is defined
83
+ */
84
+ const pkg = require(packagePath !== null && packagePath !== void 0 ? packagePath : `${process.env.PWD}/package.json`);
85
+ const appName = (() => {
86
+ if (!!process.env.APP_NAME) {
87
+ return process.env.APP_NAME;
88
+ }
89
+ if (!!process.env.npm_package_name) {
90
+ return process.env.npm_package_name;
91
+ }
92
+ if (!!pkg.name) {
93
+ return pkg.name;
94
+ }
95
+ return 'default-app-name';
96
+ })();
97
+ const version = (() => {
98
+ if (!!process.env.VERSION) {
99
+ return process.env.VERSION;
100
+ }
101
+ if (!!process.env.npm_package_version) {
102
+ return process.env.npm_package_version;
103
+ }
104
+ if (!!pkg.version) {
105
+ return pkg.version;
106
+ }
107
+ return '0.0.0';
108
+ })();
109
+ /**
110
+ * check the env which suppose to be hide as possible as it can,
111
+ * which is not allowed to define in-memory.
112
+ */
113
+ if (process.env.AZURE_TENANT_ID === undefined || process.env.AZURE_CLIENT_ID === undefined || process.env.AZURE_CLIENT_SECRET === undefined) {
114
+ throw new Error('/appConfig/preload: missing Azure configuration');
115
+ }
116
+ const envFileAppConfig = {
117
+ appName,
118
+ version,
119
+ environment: exports.environment,
120
+ keyVaultName: process.env.KEYVAULT_NAME,
121
+ keyVaultSecretName: process.env.LIBRARY_KEY_VAULT_SECRET_NAME
122
+ };
123
+ // check envFileAppConfig
124
+ const envTypeCheck = zEnvFileAppConfig.safeParse(envFileAppConfig);
125
+ if (!envTypeCheck.success) {
126
+ throw new Error('/appConfig/preload: invalid schema of envFileAppConfig');
127
+ }
128
+ // check secretFileAppConfig
129
+ const rawSecretResp = yield (0, lib_1.getKeyVault)(envFileAppConfig.keyVaultName, envFileAppConfig.keyVaultSecretName);
130
+ if (!rawSecretResp.isSuccess) {
131
+ throw new Error(rawSecretResp.message);
132
+ }
133
+ const secretFileAppConfig = rawSecretResp.data;
134
+ const secretFileTypeCheck = zSecretFileAppConfig.safeParse(secretFileAppConfig);
135
+ if (!secretFileTypeCheck.success) {
136
+ throw new Error('/appConfig/preload: invalid schema of secretFileAppConfig');
137
+ }
138
+ exports.appConfig = Object.assign(Object.assign({}, envFileAppConfig), secretFileAppConfig);
139
+ Object.freeze(exports.appConfig);
140
+ logger_1.logger.verbose('end /appConfig/preload()');
141
+ });
142
+ exports.loadConfig = loadConfig;
143
+ //# sourceMappingURL=loadConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadConfig.js","sourceRoot":"","sources":["../../../src/utils/middleware/loadConfig.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAA4B;AAC5B,mCAAwC;AACxC,6CAA0C;AAC1C,6BAAwB;AACxB,0CAAwD;AAExD;;;;;;;;GAQG;AACH,MAAM,iBAAiB,GAAG,OAAC,CAAC,MAAM,CAAC;IAC/B,OAAO,EAAE,OAAC,CAAC,MAAM,EAAE;IACnB,OAAO,EAAE,OAAC,CAAC,MAAM,EAAE;IACnB,WAAW,EAAE,uBAAY;IACzB,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE;IACxB,kBAAkB,EAAE,OAAC,CAAC,MAAM,EAAE;CACjC,CAAC,CAAC,MAAM,EAAE,CAAC;AAOZ;;GAEG;AACH,MAAM,gBAAgB,GAAG,OAAC,CAAC,MAAM,CAAC;IAC9B,WAAW,EAAE,OAAC,CAAC,MAAM,EAAE;IACvB,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE;CACxB,CAAC,CAAA;AAEF;;GAEG;AACH,MAAM,oBAAoB,GAAG,OAAC,CAAC,MAAM,CAAC;IAClC,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE;IACxB,WAAW,EAAE,OAAC,CAAC,MAAM,EAAE;CAC1B,CAAC,CAAA;AAEF;;GAEG;AACH,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC;IACxB,mBAAmB,EAAE,oBAAoB;IACzC,eAAe,EAAE,gBAAgB;CACpC,CAAC,CAAA;AAEF;;GAEG;AACF,MAAM,oBAAoB,GAAG,OAAC,CAAC,MAAM,CAAC;IACnC,SAAS,EAAE,UAAU;IACrB,aAAa,EAAE,OAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;CACpC,CAAC,CAAC;AAmBH;;GAEG;AACU,QAAA,WAAW,GAAgB,MAAC,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,gBAAgB,GAAK,mBAAmB,CAAE,CAAC;IAC5D,MAAM,CAAC,MAAM,CAAC,iBAAS,CAAC,CAAC;IAEzB,eAAM,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC/C,CAAC,CAAA,CAAC;AA/DW,QAAA,UAAU,cA+DrB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skyeye-svc-common-utils",
3
- "version": "2.0.0-dev0.2",
3
+ "version": "2.0.0-dev0.21",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,20 +21,14 @@
21
21
  "homepage": "https://github.com/HandshakesByDC/SkyEye-SVC-Common-Utils#readme",
22
22
  "dependencies": {
23
23
  "@azure/data-tables": "^13.2.2",
24
- "@azure/identity": "latest",
25
- "@azure/keyvault-secrets": "latest",
26
- "@azure/service-bus": "7.9.1",
24
+ "@azure/identity": "^4.0.1",
25
+ "@azure/keyvault-secrets": "^4.8.0",
26
+ "@azure/service-bus": "^7.9.4",
27
27
  "@azure/storage-blob": "^12.1.2",
28
28
  "@azure/storage-queue": "^12.0.2",
29
+ "@azure/search-documents": "11.2.1",
30
+ "@azure/abort-controller": "1.0.4",
29
31
  "@sendgrid/mail": "^7.7.0",
30
- "@types/chai": "^4.2.11",
31
- "@types/express": "4.17.14",
32
- "@types/express-serve-static-core": "4.17.30",
33
- "@types/google-protobuf": "^3.7.2",
34
- "@types/jsonwebtoken": "^9.0.0",
35
- "@types/lodash": "4.14.182",
36
- "@types/swagger-jsdoc": "^3.0.2",
37
- "@types/swagger-ui-express": "^4.1.1",
38
32
  "async-mutex": "^0.3.2",
39
33
  "cron-parser": "4.8.1",
40
34
  "dateformat": "^3.0.3",
@@ -45,30 +39,39 @@
45
39
  "http-errors": "^1.7.3",
46
40
  "https": "^1.0.0",
47
41
  "ioredis": "^5.0.4",
48
- "jsonwebtoken": "^9.0.0",
42
+ "jsonwebtoken": "^9.0.2",
43
+ "lodash": "^4.17.21",
49
44
  "logform": "2.4.2",
50
45
  "moment": "^2.24.0",
51
46
  "morgan": "^1.9.1",
52
47
  "node-fetch": "^2.6.0",
53
48
  "nyc": "^15.1.0",
54
- "rotating-file-stream": "^1.4.6",
49
+ "rotating-file-stream": "^3.2.1",
55
50
  "skyeye-common-const": "1.0.42",
56
51
  "swagger-jsdoc": "^3.5.0",
57
52
  "swagger-ui-express": "^4.1.3",
58
53
  "ts-node": "^10.7.0",
59
54
  "typeorm": "^0.3.17",
60
55
  "typescript": "^5.3.3",
61
- "uuid": "^3.3.3",
62
56
  "winston": "^3.2.1",
63
- "winston-daily-rotate-file": "^4.2.1"
57
+ "winston-daily-rotate-file": "^4.2.1",
58
+ "abort-controller": "3.0.0",
59
+ "zod": "3.22.3"
64
60
  },
65
61
  "devDependencies": {
66
- "@types/dateformat": "^3.0.1",
67
- "@types/debug": "~4.1.5",
68
- "@types/http-errors": "~1.6.1",
69
- "@types/morgan": "~1.7.37",
70
- "@types/node": "^14.0.27",
71
- "@types/node-fetch": "^2.5.3",
72
- "@types/uuid": "^3.4.6"
62
+ "@types/dateformat": "^5.0.2",
63
+ "@types/debug": "^4.1.12",
64
+ "@types/http-errors": "~2.0.4",
65
+ "@types/morgan": "~1.9.9",
66
+ "@types/node": "^20.11.19",
67
+ "@types/node-fetch": "^2.6.11",
68
+ "@types/chai": "^4.3.11",
69
+ "@types/express": "^4.17.21",
70
+ "@types/express-serve-static-core": "^4.17.43",
71
+ "@types/google-protobuf": "^3.15.12",
72
+ "@types/jsonwebtoken": "^9.0.6",
73
+ "@types/lodash": "^4.14.202",
74
+ "@types/swagger-jsdoc": "^6.0.4",
75
+ "@types/swagger-ui-express": "^4.1.6"
73
76
  }
74
77
  }
package/src/index.ts CHANGED
@@ -7,23 +7,26 @@ export * from './utils/logger/morganLogger';
7
7
  export * from './utils/middleware/handleError';
8
8
  export * from './utils/middleware/isPublicUser';
9
9
  export * from './utils/middleware/overwriteConst';
10
- export * from './utils/middleware/startdbConnection';
10
+ export * from './utils/middleware/loadConfig';
11
11
  export * from './utils/baseClass/baseController';
12
12
  export * from './utils/baseClass/baseService';
13
- export * from './interfaces/index';
13
+ export * from './utils/baseClass/appDataSource';
14
+ export * from './utils/monitoring/apm';
15
+ export * from './utils/commonUtils/appConfigKeyRetriever';
16
+ export * from './utils/documentation/swagger';
14
17
  export * from './lib/azure/azureKeyVault';
15
18
  export * from './lib/azure/azureServiceBusClient';
16
19
  export * from './lib/azure/azureStorageQueue';
17
20
  export * from './lib/azure/azureServiceBusQueue';
18
21
  export * from './lib/azure/azureTableStorage';
19
22
  export * from './lib/azure/azureBlobStorage';
20
- export * from './utils/documentation/swagger';
23
+ export * from './lib/azure/cognitiveSearch';
24
+ export * from './lib/fetch';
21
25
  export * from './lib/httpClient';
22
- export * from './utils/monitoring/apm';
23
- export * from './models/baseControllerParameter';
24
- export * from './models';
25
26
  export * from './lib/redisClient';
26
27
  export * from './lib/sendGridClient';
28
+ export * from './models';
29
+ export * from './models/baseControllerParameter';
30
+ export * from './interfaces';
27
31
  export * from './interfaces/grpcServiceResponse';
28
32
  export * from './interfaces/grpcListRequest';
29
- export * from './utils/commonUtils/appConfigKeyRetriever';
@@ -1,6 +1,9 @@
1
1
  import { SecretClient } from "@azure/keyvault-secrets";
2
2
  import { DefaultAzureCredential } from "@azure/identity";
3
+ import { AbortController } from '@azure/abort-controller';
3
4
  import { commonAppConfig} from '../../utils/appConfig';
5
+ import { thirdPartyRegulation } from '../../utils/appConst';
6
+ import { ServiceResponse } from '../../interfaces';
4
7
 
5
8
  export async function getKey(secretName : string) : Promise<string>{
6
9
  const credential = new DefaultAzureCredential();
@@ -12,4 +15,45 @@ export async function getKey(secretName : string) : Promise<string>{
12
15
  const secret = await client.getSecret(secretName);
13
16
 
14
17
  return secret.value;
18
+ }
19
+
20
+ /**
21
+ * @description To retrieve secret from a given Azure Key Vault
22
+ * @param {string} keyVaultName - name of Azure Key Vault
23
+ * @param {string} secretName - secret name of Azure Key Vault
24
+ * @param {number} abortWithinMsec - automatically abort within given millisecond
25
+ * @param {number} retry - automatically retry with given times
26
+ * @returns {ServiceResponse}
27
+ */
28
+ export async function getKeyVault(
29
+ keyVaultName: string,
30
+ secretName : string,
31
+ abortWithinMsec: number = thirdPartyRegulation.requestLimitationMsec,
32
+ retry: number = thirdPartyRegulation.requestDefaultRetry
33
+ ): Promise<ServiceResponse> {
34
+ const resp: ServiceResponse = { isSuccess: false };
35
+
36
+ try {
37
+ const credential = new DefaultAzureCredential();
38
+ const url = `https://${keyVaultName}.vault.azure.net`;
39
+ const client = new SecretClient(url, credential);
40
+
41
+ let curTry = 0;
42
+ while (curTry <= retry) {
43
+ const signal = AbortController.timeout(abortWithinMsec);
44
+ const secret = await client.getSecret(secretName, { abortSignal: signal });
45
+ if (secret && secret.value) {
46
+ resp.data = JSON.parse(secret.value);
47
+ resp.isSuccess = true;
48
+ break;
49
+ }
50
+ if (curTry === retry) {
51
+ throw new Error(`/keyVault/getKeyVault(): failed fetch secret with retries (${retry})`);
52
+ }
53
+ }
54
+ } catch (err) {
55
+ resp.message = err as string;
56
+ }
57
+
58
+ return resp;
15
59
  }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @description Specifies whether simple or full of query type must be matched
3
+ * @see https://docs.microsoft.com/en-us/rest/api/searchservice/search-documents
4
+ */
5
+ export enum QueryType {
6
+ simple = 'simple',
7
+ full = 'full'
8
+ }
@@ -0,0 +1,149 @@
1
+ import { SearchClient as OfficialSearchClient, AzureKeyCredential } from '@azure/search-documents';
2
+ import { logger } from '../../../utils/logger/logger';
3
+ import { getKeyVault } from '../../azure/azureKeyVault';
4
+ import { ServiceResponse } from '../../../interfaces';
5
+ import { AzureSearchIndex, AzureSearchConfig } from '../../../utils';
6
+ import { appConfig, loadConfig } from '../../../utils/middleware/loadConfig';
7
+
8
+
9
+ /**
10
+ * @classdesc SearchClient as an adaptor to bind the third-party library
11
+ * (@azure/search-documents) with the internal application. That is to say, the
12
+ * class is aim to enhance dev's maintenance between lib and the exact usage.
13
+ * @property {SearchClient} instance - Singleton instance
14
+ * @property {string} endpoint - Azure cognitive search's request endpoint
15
+ * @property {string} credential - Azure cognitive search's API key
16
+ * @property {Semaphore} semaphore - to ensure not too many requests are sent to Azure cognitive search
17
+ * @property {Record<AzureSearchIndex, string>} indexMap - to store the index's alias
18
+ *
19
+ * @example
20
+ * const client = await SearchClient.getInstance();
21
+ * const searchOption = new SearchOption( { top: 1 });
22
+ * console.log(JSON.stringify(await client.query(AzureSearchIndex.company, '*', searchOption.value())));
23
+ */
24
+ export class SearchClient {
25
+ private static instance: SearchClient;
26
+ private static endpoint: string;
27
+ private static credential: string;
28
+ private static indexMap: Record<AzureSearchIndex, string>;
29
+
30
+ /**
31
+ * @description To retrieve the instance in the Singleton pattern
32
+ * @returns {SearchClient} the singleton instance of SearchClient
33
+ */
34
+ static async getInstance(): Promise<SearchClient> {
35
+ if (!!SearchClient.instance) { return SearchClient.instance; }
36
+
37
+ SearchClient.instance = new SearchClient();
38
+
39
+ /**
40
+ * When the keyVaultName is empty, loadConfig() should be called in order to fetch the value as possible
41
+ * since we are not using lazy-loading here. It would be better to transform into a lazy-loading mechanism.
42
+ */
43
+ if (appConfig === undefined) {
44
+ try {
45
+ await loadConfig();
46
+ if (appConfig.keyVaultName === undefined) {
47
+ throw new Error('SearchClient/getInstance: empty keyVaultName');
48
+ }
49
+ } catch (err) {
50
+ throw err;
51
+ }
52
+ }
53
+
54
+ const credentialResp = await getKeyVault(appConfig.keyVaultName, AzureSearchConfig.credentialSecretName);
55
+
56
+ if (credentialResp.isSuccess === false) {
57
+ throw new Error('/SearchClient/getInstance(): retrieve search credential failed');
58
+ }
59
+
60
+ const secret: { endpoint: string, credential: string } = credentialResp.data;
61
+
62
+ const indexMapResp = await getKeyVault(appConfig.keyVaultName, AzureSearchConfig.indexSecretName);
63
+
64
+ if (indexMapResp.isSuccess === false) {
65
+ throw new Error('/SearchClient/getInstance(): retrieve search indexMap failed');
66
+ }
67
+
68
+ SearchClient.indexMap = indexMapResp.data;
69
+ SearchClient.endpoint = secret.endpoint;
70
+ SearchClient.credential = secret.credential;
71
+ return SearchClient.instance;
72
+ }
73
+
74
+ /**
75
+ * @description To query the Azure cognitive search
76
+ * @param {AzureSearchIndex} index - to retrieve the document
77
+ * @param {string} keyword - as the prop 'search'
78
+ * @param {any} options - additional option for the search
79
+ * @param {number} retryTimes - specify how many times if Azure cognitive search returns a bad request
80
+ * @returns {ServiceResponse} a serviceResponse, noted that data is stated in `data.document`
81
+ */
82
+ async query(
83
+ index: AzureSearchIndex,
84
+ keyword: string = '*',
85
+ options?: any,
86
+ retryTimes: number = AzureSearchConfig.queryRetryDefault,
87
+ tracking: number = Math.floor(Math.random()*100000000000)
88
+ ): Promise<ServiceResponse> {
89
+ logger.verbose(`start SearchClient/query: ${tracking}`);
90
+ let serviceResponse: ServiceResponse = { isSuccess: false };
91
+ let searchResults: any;
92
+ let curRetry = 0;
93
+ try {
94
+
95
+ const client = new OfficialSearchClient(
96
+ SearchClient.endpoint,
97
+ SearchClient.indexMap[index],
98
+ new AzureKeyCredential(SearchClient.credential)
99
+ );
100
+
101
+ while (curRetry < retryTimes) {
102
+ try {
103
+ logger.verbose(`SearchClient/query before sending : ${curRetry}|${tracking}`);
104
+ searchResults = await client.search(keyword, options);
105
+ logger.verbose(`SearchClient/query after sending : ${curRetry}|${tracking}`);
106
+ serviceResponse.data = { document: [] };
107
+
108
+ for await (const result of searchResults.results) {
109
+ result.document.score = result.score;
110
+ result.document.highlights = result.highlights;
111
+ serviceResponse.data.document.push(result.document);
112
+ }
113
+ logger.verbose(`SearchClient/query after processing : ${curRetry}|${tracking}`);
114
+ if (!!searchResults.facets) {
115
+ serviceResponse.data.facets = searchResults.facets;
116
+ }
117
+
118
+ if (!!searchResults.count) {
119
+ serviceResponse.data.count = searchResults.count;
120
+ }
121
+
122
+ serviceResponse.isSuccess = true;
123
+ } catch (err) {
124
+ logger.error(`SearchClient/query: fail - ${err}`);
125
+ }
126
+
127
+ if (!serviceResponse.isSuccess) {
128
+ logger.info(`SearchClient/query: retry - ${curRetry}`);
129
+ ++curRetry;
130
+ await this.sleep(AzureSearchConfig.retryIntervalSecond * 1000);
131
+ } else {
132
+ break;
133
+ }
134
+ }
135
+ } catch (err) {
136
+ logger.error(`SearchClient/query: ${err}`);
137
+ }
138
+ logger.verbose(`end SearchClient/query : ${curRetry}|${serviceResponse.data.document.length}|${tracking}`);
139
+ return serviceResponse;
140
+ }
141
+
142
+ /**
143
+ * @description To postpone for specific millisecond
144
+ * @param {number} ms - millisecond to wait for
145
+ */
146
+ private async sleep(ms: number) {
147
+ await new Promise( _ => setTimeout(_, ms));
148
+ }
149
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @description SearchFacets as a type is to wrap facets for @azure/search-documents
3
+ * @see https://docs.microsoft.com/en-us/rest/api/searchservice/search-documents
4
+ * @property {string} attr - attribute/prop to reach
5
+ * @property {number} count - volume of the search
6
+ * @property {enum} sort - order
7
+ * @property {enum | number} interval - interval greater than 0 for numbers for date time values
8
+ * @property {string} timeoffset - specifying a dynamic set of facet entry values
9
+ */
10
+ export type SearchFacets = {
11
+ attr: string;
12
+ count?: number;
13
+ sort?: 'count' | '-count' | 'value' | '-value';
14
+ interval?: number | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
15
+ timeoffset?: string;
16
+ }