@orion-js/echoes 3.12.0 → 3.13.1
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/LICENSE +21 -0
- package/lib/config.d.ts +3 -0
- package/lib/config.js +4 -0
- package/lib/echo/deserialize.d.ts +1 -0
- package/lib/echo/deserialize.js +11 -0
- package/lib/echo/index.d.ts +9 -0
- package/lib/echo/index.js +29 -0
- package/lib/echo/types.d.ts +5 -0
- package/lib/echo/types.js +6 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +43 -0
- package/lib/publish/index.d.ts +5 -0
- package/lib/publish/index.js +29 -0
- package/lib/publish/serialize.d.ts +1 -0
- package/lib/publish/serialize.js +13 -0
- package/lib/request/getPassword.d.ts +1 -0
- package/lib/request/getPassword.js +16 -0
- package/lib/request/getSignature.d.ts +1 -0
- package/lib/request/getSignature.js +15 -0
- package/lib/request/getURL.d.ts +1 -0
- package/lib/request/getURL.js +16 -0
- package/lib/request/index.d.ts +2 -0
- package/lib/request/index.js +57 -0
- package/lib/request/makeRequest.d.ts +2 -0
- package/lib/request/makeRequest.js +26 -0
- package/lib/requestsHandler/checkSignature.d.ts +1 -0
- package/lib/requestsHandler/checkSignature.js +13 -0
- package/lib/requestsHandler/getEcho.d.ts +1 -0
- package/lib/requestsHandler/getEcho.js +18 -0
- package/lib/requestsHandler/index.d.ts +3 -0
- package/lib/requestsHandler/index.js +44 -0
- package/lib/service/index.d.ts +8 -0
- package/lib/service/index.js +54 -0
- package/lib/service/index.test.d.ts +1 -0
- package/lib/service/index.test.js +51 -0
- package/lib/startService/KafkaManager.d.ts +24 -0
- package/lib/startService/KafkaManager.js +146 -0
- package/lib/startService/index.d.ts +3 -0
- package/lib/startService/index.js +33 -0
- package/lib/tests/requests/index.test.d.ts +1 -0
- package/lib/tests/requests/index.test.js +99 -0
- package/lib/types.d.ts +98 -0
- package/lib/types.js +2 -0
- package/package.json +19 -25
- package/dist/index.cjs +0 -74298
- package/dist/index.d.ts +0 -124
- package/dist/index.js +0 -74262
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Orionjs Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/config.d.ts
ADDED
package/lib/config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (serializedJavascript: string): any;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function default_1(serializedJavascript) {
|
|
4
|
+
try {
|
|
5
|
+
return eval('(' + serializedJavascript + ')');
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
throw new Error('Error deserializing echo message');
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const deserialize_1 = __importDefault(require("./deserialize"));
|
|
7
|
+
const types_1 = __importDefault(require("./types"));
|
|
8
|
+
const echo = function createNewEcho(options) {
|
|
9
|
+
return {
|
|
10
|
+
...options,
|
|
11
|
+
onMessage: async (messageData) => {
|
|
12
|
+
const { message } = messageData;
|
|
13
|
+
const data = (0, deserialize_1.default)(message.value.toString());
|
|
14
|
+
const context = {
|
|
15
|
+
...messageData,
|
|
16
|
+
data
|
|
17
|
+
};
|
|
18
|
+
await options.resolve(data.params || {}, context);
|
|
19
|
+
},
|
|
20
|
+
onRequest: async (serializedParams) => {
|
|
21
|
+
const context = {};
|
|
22
|
+
const params = (0, deserialize_1.default)(serializedParams);
|
|
23
|
+
const result = await options.resolve(params || {}, context);
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
echo.types = types_1.default;
|
|
29
|
+
exports.default = echo;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import startService, { stopService } from './startService';
|
|
2
|
+
import publish from './publish';
|
|
3
|
+
import echo from './echo';
|
|
4
|
+
import request from './request';
|
|
5
|
+
export * from './types';
|
|
6
|
+
export * from './service';
|
|
7
|
+
export { publish, startService, stopService, echo, request };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.request = exports.echo = exports.stopService = exports.startService = exports.publish = void 0;
|
|
33
|
+
const startService_1 = __importStar(require("./startService"));
|
|
34
|
+
exports.startService = startService_1.default;
|
|
35
|
+
Object.defineProperty(exports, "stopService", { enumerable: true, get: function () { return startService_1.stopService; } });
|
|
36
|
+
const publish_1 = __importDefault(require("./publish"));
|
|
37
|
+
exports.publish = publish_1.default;
|
|
38
|
+
const echo_1 = __importDefault(require("./echo"));
|
|
39
|
+
exports.echo = echo_1.default;
|
|
40
|
+
const request_1 = __importDefault(require("./request"));
|
|
41
|
+
exports.request = request_1.default;
|
|
42
|
+
__exportStar(require("./types"), exports);
|
|
43
|
+
__exportStar(require("./service"), exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const config_1 = __importDefault(require("../config"));
|
|
7
|
+
const serialize_1 = __importDefault(require("./serialize"));
|
|
8
|
+
/**
|
|
9
|
+
* Publish
|
|
10
|
+
*/
|
|
11
|
+
async function publish(options) {
|
|
12
|
+
if (!config_1.default.producer) {
|
|
13
|
+
throw new Error('You must initialize echoes configruation to use publish');
|
|
14
|
+
}
|
|
15
|
+
const payload = {
|
|
16
|
+
params: options.params,
|
|
17
|
+
};
|
|
18
|
+
return await config_1.default.producer.send({
|
|
19
|
+
acks: options.acks,
|
|
20
|
+
timeout: options.timeout,
|
|
21
|
+
topic: options.topic,
|
|
22
|
+
messages: [
|
|
23
|
+
{
|
|
24
|
+
value: (0, serialize_1.default)(payload),
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
exports.default = publish;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (data: any): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
|
|
7
|
+
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
8
|
+
function default_1(data) {
|
|
9
|
+
const cloned = (0, cloneDeep_1.default)(data);
|
|
10
|
+
const serialized = (0, serialize_javascript_1.default)(cloned, { ignoreFunction: true });
|
|
11
|
+
return serialized;
|
|
12
|
+
}
|
|
13
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getEchoesPassword(): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getEchoesPassword = void 0;
|
|
7
|
+
const config_1 = __importDefault(require("../config"));
|
|
8
|
+
const env_1 = require("@orion-js/env");
|
|
9
|
+
function getEchoesPassword() {
|
|
10
|
+
const secret = config_1.default?.requests?.key || (0, env_1.internalGetEnv)('echoes_password', 'ECHOES_PASSWORD');
|
|
11
|
+
if (!secret) {
|
|
12
|
+
console.warn('Warning: no secret key found for echoes requests. Init echoes or set the env var "echoes_password" or process.env.ECHOES_PASSWORD');
|
|
13
|
+
}
|
|
14
|
+
return secret;
|
|
15
|
+
}
|
|
16
|
+
exports.getEchoesPassword = getEchoesPassword;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (body: any): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const jssha_1 = __importDefault(require("jssha"));
|
|
7
|
+
const getPassword_1 = require("./getPassword");
|
|
8
|
+
function default_1(body) {
|
|
9
|
+
const password = (0, getPassword_1.getEchoesPassword)();
|
|
10
|
+
const shaObj = new jssha_1.default('SHA-1', 'TEXT');
|
|
11
|
+
shaObj.setHMACKey(password, 'TEXT');
|
|
12
|
+
shaObj.update(body);
|
|
13
|
+
return shaObj.getHMAC('HEX');
|
|
14
|
+
}
|
|
15
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (serviceName: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const config_1 = __importDefault(require("../config"));
|
|
7
|
+
function default_1(serviceName) {
|
|
8
|
+
if (serviceName.startsWith('http'))
|
|
9
|
+
return serviceName;
|
|
10
|
+
const url = config_1.default?.requests?.services[serviceName];
|
|
11
|
+
if (!url) {
|
|
12
|
+
throw new Error(`No URL found in echoes config for service ${serviceName}`);
|
|
13
|
+
}
|
|
14
|
+
return url;
|
|
15
|
+
}
|
|
16
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getURL_1 = __importDefault(require("./getURL"));
|
|
7
|
+
const getSignature_1 = __importDefault(require("./getSignature"));
|
|
8
|
+
const serialize_1 = __importDefault(require("../publish/serialize"));
|
|
9
|
+
const deserialize_1 = __importDefault(require("../echo/deserialize"));
|
|
10
|
+
const config_1 = __importDefault(require("../config"));
|
|
11
|
+
const makeRequest_1 = require("./makeRequest");
|
|
12
|
+
const schema_1 = require("@orion-js/schema");
|
|
13
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
14
|
+
async function request(options) {
|
|
15
|
+
const { method, service, params } = options;
|
|
16
|
+
const serializedParams = (0, serialize_1.default)(params);
|
|
17
|
+
const date = new Date();
|
|
18
|
+
const body = { method, service, serializedParams, date };
|
|
19
|
+
const signature = (0, getSignature_1.default)(body);
|
|
20
|
+
try {
|
|
21
|
+
const requestMaker = config_1.default?.requests?.makeRequest || makeRequest_1.makeRequest;
|
|
22
|
+
const requestOptions = {
|
|
23
|
+
url: (0, getURL_1.default)(service),
|
|
24
|
+
retries: options.retries,
|
|
25
|
+
timeout: options.timeout,
|
|
26
|
+
data: {
|
|
27
|
+
body,
|
|
28
|
+
signature,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const result = await requestMaker(requestOptions);
|
|
32
|
+
if (result.statusCode !== 200) {
|
|
33
|
+
throw new Error(`Wrong status code ${result.statusCode}`);
|
|
34
|
+
}
|
|
35
|
+
const data = result.data;
|
|
36
|
+
if (data.error) {
|
|
37
|
+
const info = data.errorInfo;
|
|
38
|
+
if (info) {
|
|
39
|
+
if (data.isValidationError) {
|
|
40
|
+
throw new schema_1.ValidationError(info.validationErrors);
|
|
41
|
+
}
|
|
42
|
+
if (data.isUserError) {
|
|
43
|
+
throw new helpers_1.UserError(info.error, info.message, info.extra);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`${data.error}`);
|
|
47
|
+
}
|
|
48
|
+
const response = (0, deserialize_1.default)(data.result);
|
|
49
|
+
return response;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error.isOrionError)
|
|
53
|
+
throw error;
|
|
54
|
+
throw new Error(`Echoes request network error calling ${service}/${method}: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.default = request;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.makeRequest = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
9
|
+
const makeRequest = async (options) => {
|
|
10
|
+
const result = await (0, helpers_1.executeWithRetries)(async () => {
|
|
11
|
+
return await (0, axios_1.default)({
|
|
12
|
+
method: 'post',
|
|
13
|
+
url: options.url,
|
|
14
|
+
timeout: options.timeout,
|
|
15
|
+
headers: {
|
|
16
|
+
'User-Agent': 'Orionjs-Echoes/1.1'
|
|
17
|
+
},
|
|
18
|
+
data: options.data
|
|
19
|
+
});
|
|
20
|
+
}, options.retries, 200);
|
|
21
|
+
return {
|
|
22
|
+
data: result.data,
|
|
23
|
+
statusCode: result.status
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
exports.makeRequest = makeRequest;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (body: any, signature: string): void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getSignature_1 = __importDefault(require("../request/getSignature"));
|
|
7
|
+
function default_1(body, signature) {
|
|
8
|
+
const generatedSignature = (0, getSignature_1.default)(body);
|
|
9
|
+
if (generatedSignature !== signature) {
|
|
10
|
+
throw new Error('Echoes invalid signature');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (method: string): import("..").EchoType;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const config_1 = __importDefault(require("../config"));
|
|
7
|
+
const types_1 = __importDefault(require("../echo/types"));
|
|
8
|
+
function default_1(method) {
|
|
9
|
+
const echo = config_1.default.echoes[method];
|
|
10
|
+
if (!echo) {
|
|
11
|
+
throw new Error(`Echo named ${method} not found in this service`);
|
|
12
|
+
}
|
|
13
|
+
if (echo.type !== types_1.default.request) {
|
|
14
|
+
throw new Error(`Echo named ${method} is not of type request`);
|
|
15
|
+
}
|
|
16
|
+
return echo;
|
|
17
|
+
}
|
|
18
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getEcho_1 = __importDefault(require("./getEcho"));
|
|
7
|
+
const serialize_1 = __importDefault(require("../publish/serialize"));
|
|
8
|
+
const checkSignature_1 = __importDefault(require("./checkSignature"));
|
|
9
|
+
const http_1 = require("@orion-js/http");
|
|
10
|
+
exports.default = (options) => (0, http_1.route)({
|
|
11
|
+
method: 'post',
|
|
12
|
+
path: options.requests.handlerPath || '/echoes-services',
|
|
13
|
+
bodyParser: 'json',
|
|
14
|
+
bodyParserOptions: {
|
|
15
|
+
limit: '10mb',
|
|
16
|
+
},
|
|
17
|
+
async resolve(req) {
|
|
18
|
+
try {
|
|
19
|
+
const { body, signature } = req.body;
|
|
20
|
+
(0, checkSignature_1.default)(body, signature);
|
|
21
|
+
const { method, serializedParams } = body;
|
|
22
|
+
const echo = (0, getEcho_1.default)(method);
|
|
23
|
+
const result = await echo.onRequest(serializedParams);
|
|
24
|
+
return {
|
|
25
|
+
body: {
|
|
26
|
+
result: (0, serialize_1.default)(result),
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (!error.getInfo) {
|
|
32
|
+
console.error('Error at echo requests handler:', error);
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
body: {
|
|
36
|
+
error: error.message,
|
|
37
|
+
errorInfo: error.getInfo ? error.getInfo() : null,
|
|
38
|
+
isValidationError: !!error.isValidationError,
|
|
39
|
+
isUserError: !!error.isUserError,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EchoConfig, EchoesMap } from '../types';
|
|
2
|
+
export declare function Echoes(): ClassDecorator;
|
|
3
|
+
export interface EchoesPropertyDescriptor extends Omit<PropertyDecorator, 'value'> {
|
|
4
|
+
value?: EchoConfig['resolve'];
|
|
5
|
+
}
|
|
6
|
+
export declare function EchoRequest(options?: Omit<EchoConfig, 'resolve' | 'type'>): (target: any, propertyKey: string, descriptor: EchoesPropertyDescriptor) => void;
|
|
7
|
+
export declare function EchoEvent(options?: Omit<EchoConfig, 'resolve' | 'type'>): (target: any, propertyKey: string, descriptor: EchoesPropertyDescriptor) => void;
|
|
8
|
+
export declare function getServiceEchoes(target: any): EchoesMap;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getServiceEchoes = exports.EchoEvent = exports.EchoRequest = exports.Echoes = void 0;
|
|
7
|
+
const services_1 = require("@orion-js/services");
|
|
8
|
+
const echo_1 = __importDefault(require("../echo"));
|
|
9
|
+
function Echoes() {
|
|
10
|
+
return function (target) {
|
|
11
|
+
(0, services_1.Service)()(target);
|
|
12
|
+
target.prototype.service = target;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
exports.Echoes = Echoes;
|
|
16
|
+
function EchoRequest(options = {}) {
|
|
17
|
+
return function (target, propertyKey, descriptor) {
|
|
18
|
+
if (!descriptor.value)
|
|
19
|
+
throw new Error(`You must pass resolver function to ${propertyKey}`);
|
|
20
|
+
target.echoes = target.echoes || {};
|
|
21
|
+
target.echoes[propertyKey] = (0, echo_1.default)({
|
|
22
|
+
...options,
|
|
23
|
+
type: 'request',
|
|
24
|
+
resolve: async (params, viewer) => {
|
|
25
|
+
const instance = (0, services_1.getInstance)(target.service);
|
|
26
|
+
return await instance[propertyKey](params, viewer);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.EchoRequest = EchoRequest;
|
|
32
|
+
function EchoEvent(options = {}) {
|
|
33
|
+
return function (target, propertyKey, descriptor) {
|
|
34
|
+
if (!descriptor.value)
|
|
35
|
+
throw new Error(`You must pass resolver function to ${propertyKey}`);
|
|
36
|
+
target.echoes = target.echoes || {};
|
|
37
|
+
target.echoes[propertyKey] = (0, echo_1.default)({
|
|
38
|
+
...options,
|
|
39
|
+
type: 'event',
|
|
40
|
+
resolve: async (params, viewer) => {
|
|
41
|
+
const instance = (0, services_1.getInstance)(target.service);
|
|
42
|
+
return await instance[propertyKey](params, viewer);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
exports.EchoEvent = EchoEvent;
|
|
48
|
+
function getServiceEchoes(target) {
|
|
49
|
+
if (!target.prototype) {
|
|
50
|
+
throw new Error('You must pass a class to getServiceRoutes');
|
|
51
|
+
}
|
|
52
|
+
return target.prototype.echoes || {};
|
|
53
|
+
}
|
|
54
|
+
exports.getServiceEchoes = getServiceEchoes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
require("reflect-metadata");
|
|
13
|
+
const _1 = require(".");
|
|
14
|
+
describe('Echoes with service injections', () => {
|
|
15
|
+
it('Should define a echoes map using services', async () => {
|
|
16
|
+
let ExampleEchoesService = class ExampleEchoesService {
|
|
17
|
+
async echo() {
|
|
18
|
+
return 1;
|
|
19
|
+
}
|
|
20
|
+
async echoEvent() {
|
|
21
|
+
return 2;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
__decorate([
|
|
25
|
+
(0, _1.EchoRequest)(),
|
|
26
|
+
__metadata("design:type", Function),
|
|
27
|
+
__metadata("design:paramtypes", []),
|
|
28
|
+
__metadata("design:returntype", Promise)
|
|
29
|
+
], ExampleEchoesService.prototype, "echo", null);
|
|
30
|
+
__decorate([
|
|
31
|
+
(0, _1.EchoEvent)(),
|
|
32
|
+
__metadata("design:type", Function),
|
|
33
|
+
__metadata("design:paramtypes", []),
|
|
34
|
+
__metadata("design:returntype", Promise)
|
|
35
|
+
], ExampleEchoesService.prototype, "echoEvent", null);
|
|
36
|
+
ExampleEchoesService = __decorate([
|
|
37
|
+
(0, _1.Echoes)()
|
|
38
|
+
], ExampleEchoesService);
|
|
39
|
+
const echoes = (0, _1.getServiceEchoes)(ExampleEchoesService);
|
|
40
|
+
expect(echoes).toMatchObject({
|
|
41
|
+
echo: {
|
|
42
|
+
type: 'request',
|
|
43
|
+
onRequest: expect.any(Function)
|
|
44
|
+
},
|
|
45
|
+
echoEvent: {
|
|
46
|
+
type: 'event',
|
|
47
|
+
resolve: expect.any(Function)
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Kafka, EachMessagePayload, Producer, Consumer } from 'kafkajs';
|
|
3
|
+
import { EchoesOptions, EchoType } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* Manages the Kafka connection and the consumers.
|
|
6
|
+
*/
|
|
7
|
+
declare class KafkaManager {
|
|
8
|
+
kafka: Kafka;
|
|
9
|
+
options: EchoesOptions;
|
|
10
|
+
producer: Producer;
|
|
11
|
+
consumer: Consumer;
|
|
12
|
+
topics: string[];
|
|
13
|
+
started: boolean;
|
|
14
|
+
interval: NodeJS.Timeout;
|
|
15
|
+
constructor(options: EchoesOptions);
|
|
16
|
+
checkJoinConsumerGroupConditions(): Promise<boolean>;
|
|
17
|
+
joinConsumerGroup(): Promise<void>;
|
|
18
|
+
conditionalStart(): Promise<boolean>;
|
|
19
|
+
start(): Promise<void>;
|
|
20
|
+
stop(): Promise<void>;
|
|
21
|
+
handleMessage(params: EachMessagePayload): Promise<void>;
|
|
22
|
+
handleRetries(echo: EchoType, params: EachMessagePayload, error: Error): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export default KafkaManager;
|