@orion-js/echoes 3.8.0 → 3.9.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.
@@ -1,2 +1,2 @@
1
- import { RequestOptions } from '../types';
1
+ import type { RequestOptions } from '../types';
2
2
  export default function request<TData = any, TParams = any>(options: RequestOptions<TParams>): Promise<TData>;
@@ -9,6 +9,8 @@ const serialize_1 = __importDefault(require("../publish/serialize"));
9
9
  const deserialize_1 = __importDefault(require("../echo/deserialize"));
10
10
  const config_1 = __importDefault(require("../config"));
11
11
  const makeRequest_1 = require("./makeRequest");
12
+ const schema_1 = require("@orion-js/schema");
13
+ const helpers_1 = require("@orion-js/helpers");
12
14
  async function request(options) {
13
15
  const { method, service, params } = options;
14
16
  const serializedParams = (0, serialize_1.default)(params);
@@ -23,21 +25,32 @@ async function request(options) {
23
25
  timeout: options.timeout,
24
26
  data: {
25
27
  body,
26
- signature
27
- }
28
+ signature,
29
+ },
28
30
  };
29
31
  const result = await requestMaker(requestOptions);
30
32
  if (result.statusCode !== 200) {
31
- throw new Error(`Echoes request network error calling ${service}/${method}: Wrong status code ${result.statusCode}`);
33
+ throw new Error(`Wrong status code ${result.statusCode}`);
32
34
  }
33
35
  const data = result.data;
34
36
  if (data.error) {
35
- throw new Error(`Echoes request error calling ${service}/${method}: ${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}`);
36
47
  }
37
48
  const response = (0, deserialize_1.default)(data.result);
38
49
  return response;
39
50
  }
40
51
  catch (error) {
52
+ if (error.isOrionError)
53
+ throw error;
41
54
  throw new Error(`Echoes request network error calling ${service}/${method}: ${error.message}`);
42
55
  }
43
56
  }
@@ -20,17 +20,22 @@ exports.default = (options) => (0, http_1.route)({
20
20
  const result = await echo.onRequest(serializedParams);
21
21
  return {
22
22
  body: {
23
- result: (0, serialize_1.default)(result)
24
- }
23
+ result: (0, serialize_1.default)(result),
24
+ },
25
25
  };
26
26
  }
27
27
  catch (error) {
28
- console.error('Error at echo requests handler:', error);
28
+ if (!error.getInfo) {
29
+ console.error('Error at echo requests handler:', error);
30
+ }
29
31
  return {
30
32
  body: {
31
- error: error.message
32
- }
33
+ error: error.message,
34
+ errorInfo: error.getInfo ? error.getInfo() : null,
35
+ isValidationError: !!error.isValidationError,
36
+ isUserError: !!error.isUserError,
37
+ },
33
38
  };
34
39
  }
35
- }
40
+ },
36
41
  });
@@ -6,28 +6,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const __1 = require("../..");
7
7
  const supertest_1 = __importDefault(require("supertest"));
8
8
  const http_1 = require("@orion-js/http");
9
+ const schema_1 = require("@orion-js/schema");
9
10
  describe('Test echoes requests', () => {
10
11
  const makeRequest = async (options) => {
11
12
  const app = (0, http_1.getApp)();
12
13
  const response = await (0, supertest_1.default)(app).post('/echoes-services').send(options.data);
13
14
  return {
14
15
  statusCode: response.statusCode,
15
- data: response.body
16
+ data: response.body,
16
17
  };
17
18
  };
18
19
  it('Should start echoes requests service', () => {
19
20
  const echoes = {
20
21
  test: (0, __1.echo)({
21
22
  type: 'request',
22
- async resolve() { }
23
- })
23
+ async resolve() { },
24
+ }),
24
25
  };
25
26
  (0, __1.startService)({
26
27
  echoes,
27
28
  requests: {
28
29
  key: 'secret',
29
- services: {}
30
- }
30
+ services: {},
31
+ },
31
32
  });
32
33
  });
33
34
  it('Should be able to make a echoes request passing dates as params', async () => {
@@ -40,18 +41,18 @@ describe('Test echoes requests', () => {
40
41
  expect(params.date).toBeInstanceOf(Date);
41
42
  return {
42
43
  text: 'Hello world',
43
- date: params.date
44
+ date: params.date,
44
45
  };
45
- }
46
- })
46
+ },
47
+ }),
47
48
  };
48
49
  (0, __1.startService)({
49
50
  echoes,
50
51
  requests: {
51
52
  key: 'secret',
52
53
  services: { test: 'mockURL' },
53
- makeRequest
54
- }
54
+ makeRequest,
55
+ },
55
56
  });
56
57
  const date = new Date();
57
58
  const result = await (0, __1.request)({
@@ -59,11 +60,40 @@ describe('Test echoes requests', () => {
59
60
  service: 'test',
60
61
  params: {
61
62
  hello: 'world',
62
- date
63
- }
63
+ date,
64
+ },
64
65
  });
65
66
  expect(result.text).toBe('Hello world');
66
67
  expect(result.date).toBeInstanceOf(Date);
67
68
  expect(result.date.getTime()).toBe(date.getTime());
68
69
  });
70
+ it('should pass errors to Orion errors', async () => {
71
+ const echoes = {
72
+ test: (0, __1.echo)({
73
+ type: 'request',
74
+ async resolve() {
75
+ throw new schema_1.ValidationError({ hello: 'world' });
76
+ },
77
+ }),
78
+ };
79
+ (0, __1.startService)({
80
+ echoes,
81
+ requests: {
82
+ key: 'secret',
83
+ services: { test: 'mockURL' },
84
+ makeRequest,
85
+ },
86
+ });
87
+ expect.assertions(1);
88
+ try {
89
+ await (0, __1.request)({
90
+ method: 'test',
91
+ service: 'test',
92
+ params: {},
93
+ });
94
+ }
95
+ catch (error) {
96
+ expect(error.validationErrors).toEqual({ hello: 'world' });
97
+ }
98
+ });
69
99
  });
package/lib/types.d.ts CHANGED
@@ -23,6 +23,14 @@ export interface RequestOptions<TParams> {
23
23
  export interface RequestHandlerResponse {
24
24
  result?: any;
25
25
  error?: any;
26
+ isUserError?: boolean;
27
+ isValidationError?: boolean;
28
+ errorInfo?: {
29
+ error: string;
30
+ message: string;
31
+ extra?: any;
32
+ validationErrors?: any;
33
+ };
26
34
  }
27
35
  export interface MakeRequestParams {
28
36
  url: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/echoes",
3
- "version": "3.8.0",
3
+ "version": "3.9.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -17,10 +17,11 @@
17
17
  "upgrade-interactive": "yarn upgrade-interactive"
18
18
  },
19
19
  "dependencies": {
20
- "@orion-js/env": "^3.8.0",
21
- "@orion-js/helpers": "^3.7.4",
22
- "@orion-js/http": "^3.8.0",
23
- "@orion-js/services": "^3.7.4",
20
+ "@orion-js/env": "^3.9.0",
21
+ "@orion-js/helpers": "^3.9.0",
22
+ "@orion-js/http": "^3.9.0",
23
+ "@orion-js/schema": "^3.9.0",
24
+ "@orion-js/services": "^3.9.0",
24
25
  "axios": "^0.24.0",
25
26
  "jssha": "^3.2.0",
26
27
  "kafkajs": "^1.15.0",
@@ -40,5 +41,5 @@
40
41
  "publishConfig": {
41
42
  "access": "public"
42
43
  },
43
- "gitHead": "05bb0f7c0679b17993a2375332804d745dd49ba2"
44
+ "gitHead": "23e0eda5348153dcc07b307a97b641d52eb41f39"
44
45
  }