skyeye-svc-common-utils 2.0.0-dev0.9 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +10 -7
- package/dist/index.js +10 -7
- package/dist/index.js.map +1 -1
- package/dist/lib/azure/azureKeyVault.d.ts +10 -0
- package/dist/lib/azure/azureKeyVault.js +39 -1
- package/dist/lib/azure/azureKeyVault.js.map +1 -1
- package/dist/lib/azure/cognitiveSearch/QueryType.d.ts +8 -0
- package/dist/lib/azure/cognitiveSearch/QueryType.js +13 -0
- package/dist/lib/azure/cognitiveSearch/QueryType.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchClient.d.ts +42 -0
- package/dist/lib/azure/cognitiveSearch/SearchClient.js +161 -0
- package/dist/lib/azure/cognitiveSearch/SearchClient.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchFacets.d.ts +16 -0
- package/dist/lib/azure/cognitiveSearch/SearchFacets.js +3 -0
- package/dist/lib/azure/cognitiveSearch/SearchFacets.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchFilter.d.ts +68 -0
- package/dist/lib/azure/cognitiveSearch/SearchFilter.js +125 -0
- package/dist/lib/azure/cognitiveSearch/SearchFilter.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchMode.d.ts +9 -0
- package/dist/lib/azure/cognitiveSearch/SearchMode.js +14 -0
- package/dist/lib/azure/cognitiveSearch/SearchMode.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchOption.d.ts +88 -0
- package/dist/lib/azure/cognitiveSearch/SearchOption.js +104 -0
- package/dist/lib/azure/cognitiveSearch/SearchOption.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchOrder.d.ts +9 -0
- package/dist/lib/azure/cognitiveSearch/SearchOrder.js +3 -0
- package/dist/lib/azure/cognitiveSearch/SearchOrder.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/index.d.ts +7 -0
- package/dist/lib/azure/cognitiveSearch/index.js +24 -0
- package/dist/lib/azure/cognitiveSearch/index.js.map +1 -0
- package/dist/lib/azure/index.d.ts +7 -0
- package/dist/lib/azure/index.js +24 -0
- package/dist/lib/azure/index.js.map +1 -0
- package/dist/lib/fetch/index.d.ts +2 -0
- package/dist/lib/fetch/index.js +19 -0
- package/dist/lib/fetch/index.js.map +1 -0
- package/dist/lib/fetch/sendRequest.d.ts +8 -0
- package/dist/lib/fetch/sendRequest.js +25 -0
- package/dist/lib/fetch/sendRequest.js.map +1 -0
- package/dist/lib/fetch/sendRequestWithTimeout.d.ts +9 -0
- package/dist/lib/fetch/sendRequestWithTimeout.js +53 -0
- package/dist/lib/fetch/sendRequestWithTimeout.js.map +1 -0
- package/dist/lib/httpClient.d.ts +1 -0
- package/dist/lib/httpClient.js +18 -14
- package/dist/lib/httpClient.js.map +1 -1
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +20 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/teams/broadcastLogMessage.d.ts +8 -0
- package/dist/lib/teams/broadcastLogMessage.js +81 -0
- package/dist/lib/teams/broadcastLogMessage.js.map +1 -0
- package/dist/lib/teams/index.d.ts +2 -0
- package/dist/lib/teams/index.js +19 -0
- package/dist/lib/teams/index.js.map +1 -0
- package/dist/lib/teams/logMessage.d.ts +51 -0
- package/dist/lib/teams/logMessage.js +33 -0
- package/dist/lib/teams/logMessage.js.map +1 -0
- package/dist/utils/appConst.d.ts +57 -0
- package/dist/utils/appConst.js +56 -1
- package/dist/utils/appConst.js.map +1 -1
- package/dist/utils/baseClass/appDataSource.d.ts +7 -0
- package/dist/utils/baseClass/appDataSource.js +31 -0
- package/dist/utils/baseClass/appDataSource.js.map +1 -0
- package/dist/utils/baseClass/baseController.d.ts +0 -3
- package/dist/utils/baseClass/baseController.js +27 -85
- package/dist/utils/baseClass/baseController.js.map +1 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +19 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger/morganLogger.d.ts +4 -4
- package/dist/utils/logger/morganLogger.js +7 -7
- package/dist/utils/logger/morganLogger.js.map +1 -1
- package/dist/utils/middleware/loadConfig.d.ts +128 -0
- package/dist/utils/middleware/loadConfig.js +143 -0
- package/dist/utils/middleware/loadConfig.js.map +1 -0
- package/package.json +34 -45
- package/src/index.ts +10 -7
- package/src/lib/azure/azureKeyVault.ts +44 -0
- package/src/lib/azure/cognitiveSearch/QueryType.ts +8 -0
- package/src/lib/azure/cognitiveSearch/SearchClient.ts +149 -0
- package/src/lib/azure/cognitiveSearch/SearchFacets.ts +16 -0
- package/src/lib/azure/cognitiveSearch/SearchFilter.ts +140 -0
- package/src/lib/azure/cognitiveSearch/SearchMode.ts +9 -0
- package/src/lib/azure/cognitiveSearch/SearchOption.ts +155 -0
- package/src/lib/azure/cognitiveSearch/SearchOrder.ts +9 -0
- package/src/lib/azure/cognitiveSearch/index.ts +7 -0
- package/src/lib/azure/index.ts +7 -0
- package/src/lib/fetch/index.ts +2 -0
- package/src/lib/fetch/sendRequest.ts +10 -0
- package/src/lib/fetch/sendRequestWithTimeout.ts +38 -0
- package/src/lib/httpClient.ts +27 -26
- package/src/lib/index.ts +3 -0
- package/src/lib/teams/broadcastLogMessage.ts +70 -0
- package/src/lib/teams/index.ts +2 -0
- package/src/lib/teams/logMessage.ts +55 -0
- package/src/utils/appConfig.ts +2 -2
- package/src/utils/appConst.ts +64 -0
- package/src/utils/baseClass/appDataSource.ts +23 -0
- package/src/utils/baseClass/baseController.ts +28 -88
- package/src/utils/index.ts +2 -0
- package/src/utils/logger/morganLogger.ts +28 -29
- package/src/utils/middleware/loadConfig.ts +150 -0
- package/.env +0 -3
- package/dist/utils/baseClass/dataSourceManager.d.ts +0 -11
- package/dist/utils/baseClass/dataSourceManager.js +0 -59
- package/dist/utils/baseClass/dataSourceManager.js.map +0 -1
- package/dist/utils/middleware/startdbConnection.d.ts +0 -1
- package/dist/utils/middleware/startdbConnection.js +0 -30
- package/dist/utils/middleware/startdbConnection.js.map +0 -1
- package/src/utils/baseClass/dataSourceManager.ts +0 -49
- package/src/utils/middleware/startdbConnection.ts +0 -20
package/dist/index.d.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/
|
|
10
|
+
export * from './utils/middleware/loadConfig';
|
|
11
11
|
export * from './utils/baseClass/baseController';
|
|
12
12
|
export * from './utils/baseClass/baseService';
|
|
13
|
-
export * from './
|
|
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 './
|
|
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';
|
package/dist/index.js
CHANGED
|
@@ -23,24 +23,27 @@ __exportStar(require("./utils/logger/morganLogger"), exports);
|
|
|
23
23
|
__exportStar(require("./utils/middleware/handleError"), exports);
|
|
24
24
|
__exportStar(require("./utils/middleware/isPublicUser"), exports);
|
|
25
25
|
__exportStar(require("./utils/middleware/overwriteConst"), exports);
|
|
26
|
-
__exportStar(require("./utils/middleware/
|
|
26
|
+
__exportStar(require("./utils/middleware/loadConfig"), exports);
|
|
27
27
|
__exportStar(require("./utils/baseClass/baseController"), exports);
|
|
28
28
|
__exportStar(require("./utils/baseClass/baseService"), exports);
|
|
29
|
-
__exportStar(require("./
|
|
29
|
+
__exportStar(require("./utils/baseClass/appDataSource"), exports);
|
|
30
|
+
__exportStar(require("./utils/monitoring/apm"), exports);
|
|
31
|
+
__exportStar(require("./utils/commonUtils/appConfigKeyRetriever"), exports);
|
|
32
|
+
__exportStar(require("./utils/documentation/swagger"), exports);
|
|
30
33
|
__exportStar(require("./lib/azure/azureKeyVault"), exports);
|
|
31
34
|
__exportStar(require("./lib/azure/azureServiceBusClient"), exports);
|
|
32
35
|
__exportStar(require("./lib/azure/azureStorageQueue"), exports);
|
|
33
36
|
__exportStar(require("./lib/azure/azureServiceBusQueue"), exports);
|
|
34
37
|
__exportStar(require("./lib/azure/azureTableStorage"), exports);
|
|
35
38
|
__exportStar(require("./lib/azure/azureBlobStorage"), exports);
|
|
36
|
-
__exportStar(require("./
|
|
39
|
+
__exportStar(require("./lib/azure/cognitiveSearch"), exports);
|
|
40
|
+
__exportStar(require("./lib/fetch"), exports);
|
|
37
41
|
__exportStar(require("./lib/httpClient"), exports);
|
|
38
|
-
__exportStar(require("./utils/monitoring/apm"), exports);
|
|
39
|
-
__exportStar(require("./models/baseControllerParameter"), exports);
|
|
40
|
-
__exportStar(require("./models"), exports);
|
|
41
42
|
__exportStar(require("./lib/redisClient"), exports);
|
|
42
43
|
__exportStar(require("./lib/sendGridClient"), exports);
|
|
44
|
+
__exportStar(require("./models"), exports);
|
|
45
|
+
__exportStar(require("./models/baseControllerParameter"), exports);
|
|
46
|
+
__exportStar(require("./interfaces"), exports);
|
|
43
47
|
__exportStar(require("./interfaces/grpcServiceResponse"), exports);
|
|
44
48
|
__exportStar(require("./interfaces/grpcListRequest"), exports);
|
|
45
|
-
__exportStar(require("./utils/commonUtils/appConfigKeyRetriever"), exports);
|
|
46
49
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,mDAAiC;AACjC,sDAAoC;AACpC,8DAA4C;AAC5C,wDAAsC;AACtC,8DAA4C;AAC5C,iEAA+C;AAC/C,kEAAgD;AAChD,oEAAkD;AAClD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,mDAAiC;AACjC,sDAAoC;AACpC,8DAA4C;AAC5C,wDAAsC;AACtC,8DAA4C;AAC5C,iEAA+C;AAC/C,kEAAgD;AAChD,oEAAkD;AAClD,gEAA8C;AAC9C,mEAAiD;AACjD,gEAA8C;AAC9C,kEAAgD;AAChD,yDAAuC;AACvC,4EAA0D;AAC1D,gEAA8C;AAC9C,4DAA0C;AAC1C,oEAAkD;AAClD,gEAA8C;AAC9C,mEAAiD;AACjD,gEAA8C;AAC9C,+DAA6C;AAC7C,8DAA4C;AAC5C,8CAA4B;AAC5B,mDAAiC;AACjC,oDAAkC;AAClC,uDAAqC;AACrC,2CAAyB;AACzB,mEAAiD;AACjD,+CAA6B;AAC7B,mEAAiD;AACjD,+DAA6C"}
|
|
@@ -1 +1,11 @@
|
|
|
1
|
+
import { ServiceResponse } from '../../interfaces';
|
|
1
2
|
export declare function getKey(secretName: string): Promise<string>;
|
|
3
|
+
/**
|
|
4
|
+
* @description To retrieve secret from a given Azure Key Vault
|
|
5
|
+
* @param {string} keyVaultName - name of Azure Key Vault
|
|
6
|
+
* @param {string} secretName - secret name of Azure Key Vault
|
|
7
|
+
* @param {number} abortWithinMsec - automatically abort within given millisecond
|
|
8
|
+
* @param {number} retry - automatically retry with given times
|
|
9
|
+
* @returns {ServiceResponse}
|
|
10
|
+
*/
|
|
11
|
+
export declare function getKeyVault(keyVaultName: string, secretName: string, abortWithinMsec?: number, retry?: number): Promise<ServiceResponse>;
|
|
@@ -9,10 +9,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.getKey = void 0;
|
|
12
|
+
exports.getKeyVault = exports.getKey = void 0;
|
|
13
13
|
const keyvault_secrets_1 = require("@azure/keyvault-secrets");
|
|
14
14
|
const identity_1 = require("@azure/identity");
|
|
15
|
+
const abort_controller_1 = require("@azure/abort-controller");
|
|
15
16
|
const appConfig_1 = require("../../utils/appConfig");
|
|
17
|
+
const appConst_1 = require("../../utils/appConst");
|
|
16
18
|
function getKey(secretName) {
|
|
17
19
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
20
|
const credential = new identity_1.DefaultAzureCredential();
|
|
@@ -24,4 +26,40 @@ function getKey(secretName) {
|
|
|
24
26
|
});
|
|
25
27
|
}
|
|
26
28
|
exports.getKey = getKey;
|
|
29
|
+
/**
|
|
30
|
+
* @description To retrieve secret from a given Azure Key Vault
|
|
31
|
+
* @param {string} keyVaultName - name of Azure Key Vault
|
|
32
|
+
* @param {string} secretName - secret name of Azure Key Vault
|
|
33
|
+
* @param {number} abortWithinMsec - automatically abort within given millisecond
|
|
34
|
+
* @param {number} retry - automatically retry with given times
|
|
35
|
+
* @returns {ServiceResponse}
|
|
36
|
+
*/
|
|
37
|
+
function getKeyVault(keyVaultName, secretName, abortWithinMsec = appConst_1.thirdPartyRegulation.requestLimitationMsec, retry = appConst_1.thirdPartyRegulation.requestDefaultRetry) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const resp = { isSuccess: false };
|
|
40
|
+
try {
|
|
41
|
+
const credential = new identity_1.DefaultAzureCredential();
|
|
42
|
+
const url = `https://${keyVaultName}.vault.azure.net`;
|
|
43
|
+
const client = new keyvault_secrets_1.SecretClient(url, credential);
|
|
44
|
+
let curTry = 0;
|
|
45
|
+
while (curTry <= retry) {
|
|
46
|
+
const signal = abort_controller_1.AbortController.timeout(abortWithinMsec);
|
|
47
|
+
const secret = yield client.getSecret(secretName, { abortSignal: signal });
|
|
48
|
+
if (secret && secret.value) {
|
|
49
|
+
resp.data = JSON.parse(secret.value);
|
|
50
|
+
resp.isSuccess = true;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
if (curTry === retry) {
|
|
54
|
+
throw new Error(`/keyVault/getKeyVault(): failed fetch secret with retries (${retry})`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
resp.message = err;
|
|
60
|
+
}
|
|
61
|
+
return resp;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
exports.getKeyVault = getKeyVault;
|
|
27
65
|
//# sourceMappingURL=azureKeyVault.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azureKeyVault.js","sourceRoot":"","sources":["../../../src/lib/azure/azureKeyVault.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,8DAAuD;AACvD,8CAAyD;AACzD,qDAAuD;
|
|
1
|
+
{"version":3,"file":"azureKeyVault.js","sourceRoot":"","sources":["../../../src/lib/azure/azureKeyVault.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,8DAAuD;AACvD,8CAAyD;AACzD,8DAA0D;AAC1D,qDAAuD;AACvD,mDAA4D;AAG5D,SAAsB,MAAM,CAAC,UAAmB;;QAC5C,MAAM,UAAU,GAAG,IAAI,iCA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IAAI,KAAK,CAAC,8DAA8D,KAAK,GAAG,CAAC,CAAC;gBAC5F,CAAC;YACL,CAAC;QACL,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,IAAI,CAAC,OAAO,GAAG,GAAa,CAAC;QACjC,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CAAA;AA/BD,kCA+BC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryType = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @description Specifies whether simple or full of query type must be matched
|
|
6
|
+
* @see https://docs.microsoft.com/en-us/rest/api/searchservice/search-documents
|
|
7
|
+
*/
|
|
8
|
+
var QueryType;
|
|
9
|
+
(function (QueryType) {
|
|
10
|
+
QueryType["simple"] = "simple";
|
|
11
|
+
QueryType["full"] = "full";
|
|
12
|
+
})(QueryType || (exports.QueryType = QueryType = {}));
|
|
13
|
+
//# sourceMappingURL=QueryType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryType.js","sourceRoot":"","sources":["../../../../src/lib/azure/cognitiveSearch/QueryType.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,IAAY,SAGX;AAHD,WAAY,SAAS;IACjB,8BAAiB,CAAA;IACjB,0BAAa,CAAA;AACjB,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ServiceResponse } from '../../../interfaces';
|
|
2
|
+
import { AzureSearchIndex } from '../../../utils';
|
|
3
|
+
/**
|
|
4
|
+
* @classdesc SearchClient as an adaptor to bind the third-party library
|
|
5
|
+
* (@azure/search-documents) with the internal application. That is to say, the
|
|
6
|
+
* class is aim to enhance dev's maintenance between lib and the exact usage.
|
|
7
|
+
* @property {SearchClient} instance - Singleton instance
|
|
8
|
+
* @property {string} endpoint - Azure cognitive search's request endpoint
|
|
9
|
+
* @property {string} credential - Azure cognitive search's API key
|
|
10
|
+
* @property {Semaphore} semaphore - to ensure not too many requests are sent to Azure cognitive search
|
|
11
|
+
* @property {Record<AzureSearchIndex, string>} indexMap - to store the index's alias
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const client = await SearchClient.getInstance();
|
|
15
|
+
* const searchOption = new SearchOption( { top: 1 });
|
|
16
|
+
* console.log(JSON.stringify(await client.query(AzureSearchIndex.company, '*', searchOption.value())));
|
|
17
|
+
*/
|
|
18
|
+
export declare class SearchClient {
|
|
19
|
+
private static instance;
|
|
20
|
+
private static endpoint;
|
|
21
|
+
private static credential;
|
|
22
|
+
private static indexMap;
|
|
23
|
+
/**
|
|
24
|
+
* @description To retrieve the instance in the Singleton pattern
|
|
25
|
+
* @returns {SearchClient} the singleton instance of SearchClient
|
|
26
|
+
*/
|
|
27
|
+
static getInstance(): Promise<SearchClient>;
|
|
28
|
+
/**
|
|
29
|
+
* @description To query the Azure cognitive search
|
|
30
|
+
* @param {AzureSearchIndex} index - to retrieve the document
|
|
31
|
+
* @param {string} keyword - as the prop 'search'
|
|
32
|
+
* @param {any} options - additional option for the search
|
|
33
|
+
* @param {number} retryTimes - specify how many times if Azure cognitive search returns a bad request
|
|
34
|
+
* @returns {ServiceResponse} a serviceResponse, noted that data is stated in `data.document`
|
|
35
|
+
*/
|
|
36
|
+
query(index: AzureSearchIndex, keyword?: string, options?: any, retryTimes?: number, tracking?: number): Promise<ServiceResponse>;
|
|
37
|
+
/**
|
|
38
|
+
* @description To postpone for specific millisecond
|
|
39
|
+
* @param {number} ms - millisecond to wait for
|
|
40
|
+
*/
|
|
41
|
+
private sleep;
|
|
42
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
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 __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.SearchClient = void 0;
|
|
20
|
+
const search_documents_1 = require("@azure/search-documents");
|
|
21
|
+
const logger_1 = require("../../../utils/logger/logger");
|
|
22
|
+
const azureKeyVault_1 = require("../../azure/azureKeyVault");
|
|
23
|
+
const utils_1 = require("../../../utils");
|
|
24
|
+
const loadConfig_1 = require("../../../utils/middleware/loadConfig");
|
|
25
|
+
/**
|
|
26
|
+
* @classdesc SearchClient as an adaptor to bind the third-party library
|
|
27
|
+
* (@azure/search-documents) with the internal application. That is to say, the
|
|
28
|
+
* class is aim to enhance dev's maintenance between lib and the exact usage.
|
|
29
|
+
* @property {SearchClient} instance - Singleton instance
|
|
30
|
+
* @property {string} endpoint - Azure cognitive search's request endpoint
|
|
31
|
+
* @property {string} credential - Azure cognitive search's API key
|
|
32
|
+
* @property {Semaphore} semaphore - to ensure not too many requests are sent to Azure cognitive search
|
|
33
|
+
* @property {Record<AzureSearchIndex, string>} indexMap - to store the index's alias
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const client = await SearchClient.getInstance();
|
|
37
|
+
* const searchOption = new SearchOption( { top: 1 });
|
|
38
|
+
* console.log(JSON.stringify(await client.query(AzureSearchIndex.company, '*', searchOption.value())));
|
|
39
|
+
*/
|
|
40
|
+
class SearchClient {
|
|
41
|
+
/**
|
|
42
|
+
* @description To retrieve the instance in the Singleton pattern
|
|
43
|
+
* @returns {SearchClient} the singleton instance of SearchClient
|
|
44
|
+
*/
|
|
45
|
+
static getInstance() {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
if (!!SearchClient.instance) {
|
|
48
|
+
return SearchClient.instance;
|
|
49
|
+
}
|
|
50
|
+
SearchClient.instance = new SearchClient();
|
|
51
|
+
/**
|
|
52
|
+
* When the keyVaultName is empty, loadConfig() should be called in order to fetch the value as possible
|
|
53
|
+
* since we are not using lazy-loading here. It would be better to transform into a lazy-loading mechanism.
|
|
54
|
+
*/
|
|
55
|
+
if (loadConfig_1.appConfig === undefined) {
|
|
56
|
+
try {
|
|
57
|
+
yield (0, loadConfig_1.loadConfig)();
|
|
58
|
+
if (loadConfig_1.appConfig.keyVaultName === undefined) {
|
|
59
|
+
throw new Error('SearchClient/getInstance: empty keyVaultName');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const credentialResp = yield (0, azureKeyVault_1.getKeyVault)(loadConfig_1.appConfig.keyVaultName, utils_1.AzureSearchConfig.credentialSecretName);
|
|
67
|
+
if (credentialResp.isSuccess === false) {
|
|
68
|
+
throw new Error('/SearchClient/getInstance(): retrieve search credential failed');
|
|
69
|
+
}
|
|
70
|
+
const secret = credentialResp.data;
|
|
71
|
+
const indexMapResp = yield (0, azureKeyVault_1.getKeyVault)(loadConfig_1.appConfig.keyVaultName, utils_1.AzureSearchConfig.indexSecretName);
|
|
72
|
+
if (indexMapResp.isSuccess === false) {
|
|
73
|
+
throw new Error('/SearchClient/getInstance(): retrieve search indexMap failed');
|
|
74
|
+
}
|
|
75
|
+
SearchClient.indexMap = indexMapResp.data;
|
|
76
|
+
SearchClient.endpoint = secret.endpoint;
|
|
77
|
+
SearchClient.credential = secret.credential;
|
|
78
|
+
return SearchClient.instance;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @description To query the Azure cognitive search
|
|
83
|
+
* @param {AzureSearchIndex} index - to retrieve the document
|
|
84
|
+
* @param {string} keyword - as the prop 'search'
|
|
85
|
+
* @param {any} options - additional option for the search
|
|
86
|
+
* @param {number} retryTimes - specify how many times if Azure cognitive search returns a bad request
|
|
87
|
+
* @returns {ServiceResponse} a serviceResponse, noted that data is stated in `data.document`
|
|
88
|
+
*/
|
|
89
|
+
query(index, keyword = '*', options, retryTimes = utils_1.AzureSearchConfig.queryRetryDefault, tracking = Math.floor(Math.random() * 100000000000)) {
|
|
90
|
+
var _a, e_1, _b, _c;
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
logger_1.logger.verbose(`start SearchClient/query: ${tracking}`);
|
|
93
|
+
let serviceResponse = { isSuccess: false };
|
|
94
|
+
let searchResults;
|
|
95
|
+
let curRetry = 0;
|
|
96
|
+
try {
|
|
97
|
+
const client = new search_documents_1.SearchClient(SearchClient.endpoint, SearchClient.indexMap[index], new search_documents_1.AzureKeyCredential(SearchClient.credential));
|
|
98
|
+
while (curRetry < retryTimes) {
|
|
99
|
+
try {
|
|
100
|
+
logger_1.logger.verbose(`SearchClient/query before sending : ${curRetry}|${tracking}`);
|
|
101
|
+
searchResults = yield client.search(keyword, options);
|
|
102
|
+
logger_1.logger.verbose(`SearchClient/query after sending : ${curRetry}|${tracking}`);
|
|
103
|
+
serviceResponse.data = { document: [] };
|
|
104
|
+
try {
|
|
105
|
+
for (var _d = true, _e = (e_1 = void 0, __asyncValues(searchResults.results)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
106
|
+
_c = _f.value;
|
|
107
|
+
_d = false;
|
|
108
|
+
const result = _c;
|
|
109
|
+
result.document.score = result.score;
|
|
110
|
+
result.document.highlights = result.highlights;
|
|
111
|
+
serviceResponse.data.document.push(result.document);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
115
|
+
finally {
|
|
116
|
+
try {
|
|
117
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
118
|
+
}
|
|
119
|
+
finally { if (e_1) throw e_1.error; }
|
|
120
|
+
}
|
|
121
|
+
logger_1.logger.verbose(`SearchClient/query after processing : ${curRetry}|${tracking}`);
|
|
122
|
+
if (!!searchResults.facets) {
|
|
123
|
+
serviceResponse.data.facets = searchResults.facets;
|
|
124
|
+
}
|
|
125
|
+
if (!!searchResults.count) {
|
|
126
|
+
serviceResponse.data.count = searchResults.count;
|
|
127
|
+
}
|
|
128
|
+
serviceResponse.isSuccess = true;
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
logger_1.logger.error(`SearchClient/query: fail - ${err}`);
|
|
132
|
+
}
|
|
133
|
+
if (!serviceResponse.isSuccess) {
|
|
134
|
+
logger_1.logger.info(`SearchClient/query: retry - ${curRetry}`);
|
|
135
|
+
++curRetry;
|
|
136
|
+
yield this.sleep(utils_1.AzureSearchConfig.retryIntervalSecond * 1000);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
catch (err) {
|
|
144
|
+
logger_1.logger.error(`SearchClient/query: ${err}`);
|
|
145
|
+
}
|
|
146
|
+
logger_1.logger.verbose(`end SearchClient/query : ${curRetry}|${serviceResponse.data.document.length}|${tracking}`);
|
|
147
|
+
return serviceResponse;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @description To postpone for specific millisecond
|
|
152
|
+
* @param {number} ms - millisecond to wait for
|
|
153
|
+
*/
|
|
154
|
+
sleep(ms) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
yield new Promise(_ => setTimeout(_, ms));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.SearchClient = SearchClient;
|
|
161
|
+
//# sourceMappingURL=SearchClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SearchClient.js","sourceRoot":"","sources":["../../../../src/lib/azure/cognitiveSearch/SearchClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,8DAAmG;AACnG,yDAAsD;AACtD,6DAAwD;AAExD,0CAAqE;AACrE,qEAA6E;AAG7E;;;;;;;;;;;;;;GAcG;AACH,MAAa,YAAY;IAMrB;;;OAGG;IACH,MAAM,CAAO,WAAW;;YACpB,IAAI,CAAC,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC;gBAAC,OAAO,YAAY,CAAC,QAAQ,CAAC;YAAC,CAAC;YAE9D,YAAY,CAAC,QAAQ,GAAG,IAAI,YAAY,EAAE,CAAC;YAE3C;;;eAGG;YACH,IAAI,sBAAS,KAAK,SAAS,EAAE,CAAC;gBAC1B,IAAI,CAAC;oBACD,MAAM,IAAA,uBAAU,GAAE,CAAC;oBACnB,IAAI,sBAAS,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;wBACvC,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;oBACpE,CAAC;gBACL,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACX,MAAM,GAAG,CAAC;gBACd,CAAC;YACL,CAAC;YAED,MAAM,cAAc,GAAG,MAAM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wBACX,eAAM,CAAC,KAAK,CAAC,8BAA8B,GAAG,EAAE,CAAC,CAAC;oBACtD,CAAC;oBAED,IAAI,CAAC,eAAe,CAAC,SAAS,EAAE,CAAC;wBAC7B,eAAM,CAAC,IAAI,CAAC,+BAA+B,QAAQ,EAAE,CAAC,CAAC;wBACvD,EAAE,QAAQ,CAAC;wBACX,MAAM,IAAI,CAAC,KAAK,CAAC,yBAAiB,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;oBACnE,CAAC;yBAAM,CAAC;wBACJ,MAAM;oBACV,CAAC;gBACL,CAAC;YACL,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACX,eAAM,CAAC,KAAK,CAAC,uBAAuB,GAAG,EAAE,CAAC,CAAC;YAC/C,CAAC;YACD,eAAM,CAAC,OAAO,CAAC,4BAA4B,QAAQ,IAAI,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC,CAAC;YAC3G,OAAO,eAAe,CAAC;;KAC1B;IAED;;;OAGG;IACW,KAAK,CAAC,EAAU;;YAC1B,MAAM,IAAI,OAAO,CAAE,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QAC/C,CAAC;KAAA;CACJ;AA7HD,oCA6HC"}
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SearchFacets.js","sourceRoot":"","sources":["../../../../src/lib/azure/cognitiveSearch/SearchFacets.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @classdesc SearchFilter is the object to construct the Filter when using Azure cognitive search
|
|
3
|
+
* @property {string} attr - the attribute, so called props, to be filtered by the condition
|
|
4
|
+
* @property {SearchFilterCondition} condition - the condition to filter, see enum @SearchFilter for more information
|
|
5
|
+
* @property {any} value - attr should under the condition with value
|
|
6
|
+
* @property {boolean} isCollection - true if the filed is defined as a collection in Azure Cognitive Search
|
|
7
|
+
* @property {'any' | 'all'} collectionOperation - collection search settings
|
|
8
|
+
* @see https://docs.microsoft.com/en-us/azure/search/search-query-understand-collection-filters
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const primaryFilter = new SearchFilter('isPrimary', EQUAL, true);
|
|
13
|
+
* const mentionedCompanyListFilter = new SearchFilter('companyMentioned', CONTAINS, [...]);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class SearchFilter {
|
|
17
|
+
attr: string;
|
|
18
|
+
condition: SearchFilterCondition;
|
|
19
|
+
value: any;
|
|
20
|
+
isCollection: boolean;
|
|
21
|
+
collectionOperation: 'any' | 'all';
|
|
22
|
+
constructor(attr: string, condition: SearchFilterCondition, value: any, isCollection?: boolean, collectionOperation?: 'any' | 'all');
|
|
23
|
+
/**
|
|
24
|
+
* @description flatten is to compose the instance into a string when using Search
|
|
25
|
+
* @returns {string} the composite of the instance
|
|
26
|
+
*/
|
|
27
|
+
flatten(): string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @description The condition when filtering an params.
|
|
31
|
+
*/
|
|
32
|
+
export declare enum SearchFilterCondition {
|
|
33
|
+
contains = "",
|
|
34
|
+
exclude = "not",
|
|
35
|
+
equal = "eq",
|
|
36
|
+
notEqual = "ne",
|
|
37
|
+
lessThanOrEqual = "le",
|
|
38
|
+
greaterThanOrEqual = "ge"
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @description The following const params is a easy-to-use type when implementing a SearchFilter,
|
|
42
|
+
* so that the developers no longer need to call the enum with the class reference
|
|
43
|
+
*/
|
|
44
|
+
export declare const CONTAINS = SearchFilterCondition.contains;
|
|
45
|
+
export declare const EXCLUDE = SearchFilterCondition.exclude;
|
|
46
|
+
export declare const EQUAL = SearchFilterCondition.equal;
|
|
47
|
+
export declare const NOT_EQUAL = SearchFilterCondition.notEqual;
|
|
48
|
+
export declare const LESS_THAN_OR_EQUAL = SearchFilterCondition.lessThanOrEqual;
|
|
49
|
+
export declare const GREATER_THAN_OR_EQUAL = SearchFilterCondition.greaterThanOrEqual;
|
|
50
|
+
/**
|
|
51
|
+
* @description describe types of combination
|
|
52
|
+
*/
|
|
53
|
+
export declare const enum CombinationType {
|
|
54
|
+
and = "and",
|
|
55
|
+
or = "or"
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @description AND helps developers to compose a nested filter for condition AND.
|
|
59
|
+
* @param {Array<Array<string | SearchFilter>>} args - the arguments which need to be construct into string
|
|
60
|
+
* @returns {string} aggregation of the given args
|
|
61
|
+
*/
|
|
62
|
+
export declare const AND: (...args: Array<string | SearchFilter>) => string;
|
|
63
|
+
/**
|
|
64
|
+
* @description OR helps developers to compose a nested filter for condition OR.
|
|
65
|
+
* @param {Array<Array<string | SearchFilter>>} args - the arguments which need to be construct into string
|
|
66
|
+
* @returns {string} aggregation of the given args
|
|
67
|
+
*/
|
|
68
|
+
export declare const OR: (...args: Array<string | SearchFilter>) => string;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OR = exports.AND = exports.GREATER_THAN_OR_EQUAL = exports.LESS_THAN_OR_EQUAL = exports.NOT_EQUAL = exports.EQUAL = exports.EXCLUDE = exports.CONTAINS = exports.SearchFilterCondition = exports.SearchFilter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @classdesc SearchFilter is the object to construct the Filter when using Azure cognitive search
|
|
6
|
+
* @property {string} attr - the attribute, so called props, to be filtered by the condition
|
|
7
|
+
* @property {SearchFilterCondition} condition - the condition to filter, see enum @SearchFilter for more information
|
|
8
|
+
* @property {any} value - attr should under the condition with value
|
|
9
|
+
* @property {boolean} isCollection - true if the filed is defined as a collection in Azure Cognitive Search
|
|
10
|
+
* @property {'any' | 'all'} collectionOperation - collection search settings
|
|
11
|
+
* @see https://docs.microsoft.com/en-us/azure/search/search-query-understand-collection-filters
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const primaryFilter = new SearchFilter('isPrimary', EQUAL, true);
|
|
16
|
+
* const mentionedCompanyListFilter = new SearchFilter('companyMentioned', CONTAINS, [...]);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
class SearchFilter {
|
|
20
|
+
constructor(attr, condition, value, isCollection = false, collectionOperation = 'any') {
|
|
21
|
+
this.isCollection = false;
|
|
22
|
+
this.collectionOperation = 'any';
|
|
23
|
+
this.attr = attr;
|
|
24
|
+
this.condition = condition;
|
|
25
|
+
this.value = value;
|
|
26
|
+
this.isCollection = isCollection;
|
|
27
|
+
this.collectionOperation = collectionOperation;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @description flatten is to compose the instance into a string when using Search
|
|
31
|
+
* @returns {string} the composite of the instance
|
|
32
|
+
*/
|
|
33
|
+
flatten() {
|
|
34
|
+
if (this.isCollection) {
|
|
35
|
+
switch (this.condition) {
|
|
36
|
+
case exports.CONTAINS:
|
|
37
|
+
return `${this.attr}/${this.collectionOperation}(i: search.in(i,'${this.value.toString()}', ','))`;
|
|
38
|
+
case exports.EXCLUDE:
|
|
39
|
+
return `${this.attr}/${this.collectionOperation}(i: not search.in(i,'${this.value.toString()}', ','))`;
|
|
40
|
+
default:
|
|
41
|
+
throw new Error(`SearchFilter/flatten: Unknown condition for collection`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
switch (this.condition) {
|
|
46
|
+
case exports.EQUAL:
|
|
47
|
+
case exports.NOT_EQUAL:
|
|
48
|
+
case exports.LESS_THAN_OR_EQUAL:
|
|
49
|
+
case exports.GREATER_THAN_OR_EQUAL:
|
|
50
|
+
return `${this.attr} ${this.condition} ${typeof (this.value) === 'string' ? `'${this.value}'` : this.value}`;
|
|
51
|
+
case exports.CONTAINS:
|
|
52
|
+
return `search.in(${this.attr}, '${this.value.toString()}', ',')`;
|
|
53
|
+
case exports.EXCLUDE:
|
|
54
|
+
return `${this.condition} search.in(${this.attr}, '${this.value.toString()}', ',')`;
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`SearchFilter/flatten: Unknown condition`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.SearchFilter = SearchFilter;
|
|
62
|
+
/**
|
|
63
|
+
* @description The condition when filtering an params.
|
|
64
|
+
*/
|
|
65
|
+
var SearchFilterCondition;
|
|
66
|
+
(function (SearchFilterCondition) {
|
|
67
|
+
SearchFilterCondition["contains"] = "";
|
|
68
|
+
SearchFilterCondition["exclude"] = "not";
|
|
69
|
+
SearchFilterCondition["equal"] = "eq";
|
|
70
|
+
SearchFilterCondition["notEqual"] = "ne";
|
|
71
|
+
SearchFilterCondition["lessThanOrEqual"] = "le";
|
|
72
|
+
SearchFilterCondition["greaterThanOrEqual"] = "ge";
|
|
73
|
+
})(SearchFilterCondition || (exports.SearchFilterCondition = SearchFilterCondition = {}));
|
|
74
|
+
/**
|
|
75
|
+
* @description The following const params is a easy-to-use type when implementing a SearchFilter,
|
|
76
|
+
* so that the developers no longer need to call the enum with the class reference
|
|
77
|
+
*/
|
|
78
|
+
exports.CONTAINS = SearchFilterCondition.contains;
|
|
79
|
+
exports.EXCLUDE = SearchFilterCondition.exclude;
|
|
80
|
+
exports.EQUAL = SearchFilterCondition.equal;
|
|
81
|
+
exports.NOT_EQUAL = SearchFilterCondition.notEqual;
|
|
82
|
+
exports.LESS_THAN_OR_EQUAL = SearchFilterCondition.lessThanOrEqual;
|
|
83
|
+
exports.GREATER_THAN_OR_EQUAL = SearchFilterCondition.greaterThanOrEqual;
|
|
84
|
+
;
|
|
85
|
+
/**
|
|
86
|
+
* combination is a function ONLY be used for constructing two function: AND and OR,
|
|
87
|
+
* then it would be express into a string.
|
|
88
|
+
* @param {CombinationType} type - to indicate the combination type
|
|
89
|
+
* @param {Array<Array<string | SearchFilter>>} args - the arguments which need to be construct into string
|
|
90
|
+
* @returns {string} aggregation of the given args
|
|
91
|
+
*/
|
|
92
|
+
const combination = (type, ...args) => {
|
|
93
|
+
if (args.length === 0) {
|
|
94
|
+
return '';
|
|
95
|
+
}
|
|
96
|
+
function flatten(obj) {
|
|
97
|
+
return typeof (obj) === 'string' ? obj : obj.flatten();
|
|
98
|
+
}
|
|
99
|
+
let res = !!args[0] ? flatten(args[0]) : '';
|
|
100
|
+
for (let i = 1; i < args.length; ++i) {
|
|
101
|
+
if (!!args) {
|
|
102
|
+
res += ` ${type} ${flatten(args[i])}`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return `(${res})`;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* @description AND helps developers to compose a nested filter for condition AND.
|
|
109
|
+
* @param {Array<Array<string | SearchFilter>>} args - the arguments which need to be construct into string
|
|
110
|
+
* @returns {string} aggregation of the given args
|
|
111
|
+
*/
|
|
112
|
+
const AND = (...args) => {
|
|
113
|
+
return combination("and" /* CombinationType.and */, ...args);
|
|
114
|
+
};
|
|
115
|
+
exports.AND = AND;
|
|
116
|
+
/**
|
|
117
|
+
* @description OR helps developers to compose a nested filter for condition OR.
|
|
118
|
+
* @param {Array<Array<string | SearchFilter>>} args - the arguments which need to be construct into string
|
|
119
|
+
* @returns {string} aggregation of the given args
|
|
120
|
+
*/
|
|
121
|
+
const OR = (...args) => {
|
|
122
|
+
return combination("or" /* CombinationType.or */, ...args);
|
|
123
|
+
};
|
|
124
|
+
exports.OR = OR;
|
|
125
|
+
//# sourceMappingURL=SearchFilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SearchFilter.js","sourceRoot":"","sources":["../../../../src/lib/azure/cognitiveSearch/SearchFilter.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACF,MAAa,YAAY;IAOtB,YACI,IAAY,EACZ,SAAgC,EAChC,KAAU,EACV,eAAwB,KAAK,EAC7B,sBAAqC,KAAK;QAR9C,iBAAY,GAAY,KAAK,CAAC;QAC9B,wBAAmB,GAAkB,KAAK,CAAC;QAQvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,mBAAmB,GAAG,mBAAmB,CAAC;IACnD,CAAC;IAED;;;OAGG;IACH,OAAO;QACH,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACpB,QAAQ,IAAI,CAAC,SAAS,EAAE,CAAC;gBACrB,KAAK,gBAAQ;oBACT,OAAO,GAAG,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,mBAAmB,oBAAoB,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC;gBACvG,KAAK,eAAO;oBACR,OAAO,GAAG,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,mBAAmB,wBAAwB,IAA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W,gCAAqB,GAAG,IAAI,CAAC,CAAC;AACpD,CAAC,CAAA;AAFY,QAAA,EAAE,MAEd"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Specifies whether any or all of the search terms must be matched
|
|
3
|
+
* in order to count the document as a match.
|
|
4
|
+
* @see https://docs.microsoft.com/en-us/rest/api/searchservice/search-documents
|
|
5
|
+
*/
|
|
6
|
+
export declare enum SearchMode {
|
|
7
|
+
all = "all",
|
|
8
|
+
any = "any"
|
|
9
|
+
}
|