@unito/integration-sdk 0.1.6 → 0.1.8
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/.eslintrc.cjs +2 -0
- package/dist/src/errors.d.ts +0 -6
- package/dist/src/errors.js +3 -6
- package/dist/src/httpErrors.d.ts +3 -3
- package/dist/src/httpErrors.js +5 -5
- package/dist/src/index.cjs +829 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/integration.d.ts +0 -1
- package/dist/src/integration.js +2 -7
- package/dist/src/middlewares/errors.d.ts +9 -1
- package/dist/src/middlewares/errors.js +28 -13
- package/dist/src/middlewares/finish.d.ts +3 -1
- package/dist/src/middlewares/finish.js +25 -2
- package/dist/src/resources/cache.d.ts +18 -4
- package/dist/src/resources/cache.js +52 -21
- package/dist/src/resources/logger.d.ts +49 -10
- package/dist/src/resources/logger.js +64 -18
- package/dist/src/resources/provider.d.ts +1 -1
- package/dist/src/resources/provider.js +2 -2
- package/dist/test/errors.test.js +1 -0
- package/dist/test/middlewares/errors.test.js +1 -1
- package/dist/test/resources/cache.test.js +7 -15
- package/dist/test/resources/logger.test.js +53 -6
- package/dist/test/resources/provider.test.js +6 -6
- package/package.json +11 -4
- package/src/errors.ts +2 -6
- package/src/httpErrors.ts +6 -6
- package/src/index.ts +1 -0
- package/src/integration.ts +2 -9
- package/src/middlewares/errors.ts +39 -14
- package/src/middlewares/finish.ts +28 -3
- package/src/resources/cache.ts +66 -23
- package/src/resources/logger.ts +84 -24
- package/src/resources/provider.ts +3 -3
- package/test/errors.test.ts +1 -0
- package/test/middlewares/errors.test.ts +1 -1
- package/test/resources/cache.test.ts +7 -17
- package/test/resources/logger.test.ts +60 -7
- package/test/resources/provider.test.ts +6 -6
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
2
|
import { describe, it } from 'node:test';
|
|
3
|
-
import Logger from '../../src/resources/logger.js';
|
|
3
|
+
import { default as Logger, Metadata } from '../../src/resources/logger.js';
|
|
4
4
|
|
|
5
5
|
describe('Logger', () => {
|
|
6
6
|
it('metadata', () => {
|
|
7
|
-
let metadata:
|
|
7
|
+
let metadata: Metadata = { correlation_id: 'test' };
|
|
8
8
|
const logger = new Logger(metadata);
|
|
9
9
|
|
|
10
10
|
// Changing initial object should not affect the logger
|
|
@@ -42,7 +42,7 @@ describe('Logger', () => {
|
|
|
42
42
|
logger.log('test');
|
|
43
43
|
assert.strictEqual(logSpy.mock.calls.length, 1);
|
|
44
44
|
assert.deepEqual(logSpy.mock.calls[0]?.arguments, [
|
|
45
|
-
JSON.stringify({
|
|
45
|
+
JSON.stringify({ correlation_id: '123456789', message: 'test' }),
|
|
46
46
|
]);
|
|
47
47
|
|
|
48
48
|
const errorSpy = testContext.mock.method(global.console, 'error', () => {});
|
|
@@ -50,7 +50,7 @@ describe('Logger', () => {
|
|
|
50
50
|
logger.error('test');
|
|
51
51
|
assert.strictEqual(errorSpy.mock.calls.length, 1);
|
|
52
52
|
assert.deepEqual(errorSpy.mock.calls[0]?.arguments, [
|
|
53
|
-
JSON.stringify({
|
|
53
|
+
JSON.stringify({ correlation_id: '123456789', message: 'test' }),
|
|
54
54
|
]);
|
|
55
55
|
|
|
56
56
|
const warnSpy = testContext.mock.method(global.console, 'warn', () => {});
|
|
@@ -58,7 +58,7 @@ describe('Logger', () => {
|
|
|
58
58
|
logger.warn('test');
|
|
59
59
|
assert.strictEqual(warnSpy.mock.calls.length, 1);
|
|
60
60
|
assert.deepEqual(warnSpy.mock.calls[0]?.arguments, [
|
|
61
|
-
JSON.stringify({
|
|
61
|
+
JSON.stringify({ correlation_id: '123456789', message: 'test' }),
|
|
62
62
|
]);
|
|
63
63
|
|
|
64
64
|
const infoSpy = testContext.mock.method(global.console, 'info', () => {});
|
|
@@ -66,7 +66,7 @@ describe('Logger', () => {
|
|
|
66
66
|
logger.info('test');
|
|
67
67
|
assert.strictEqual(infoSpy.mock.calls.length, 1);
|
|
68
68
|
assert.deepEqual(infoSpy.mock.calls[0]?.arguments, [
|
|
69
|
-
JSON.stringify({
|
|
69
|
+
JSON.stringify({ correlation_id: '123456789', message: 'test' }),
|
|
70
70
|
]);
|
|
71
71
|
|
|
72
72
|
const debugSpy = testContext.mock.method(global.console, 'debug', () => {});
|
|
@@ -74,7 +74,60 @@ describe('Logger', () => {
|
|
|
74
74
|
logger.debug('test');
|
|
75
75
|
assert.strictEqual(debugSpy.mock.calls.length, 1);
|
|
76
76
|
assert.deepEqual(debugSpy.mock.calls[0]?.arguments, [
|
|
77
|
-
JSON.stringify({
|
|
77
|
+
JSON.stringify({ correlation_id: '123456789', message: 'test' }),
|
|
78
|
+
]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('merges message payload with metadata', testContext => {
|
|
82
|
+
const metadata = { correlation_id: '123456789', http: { method: 'GET' } };
|
|
83
|
+
const logger = new Logger(metadata);
|
|
84
|
+
|
|
85
|
+
const logSpy = testContext.mock.method(global.console, 'log', () => {});
|
|
86
|
+
assert.strictEqual(logSpy.mock.calls.length, 0);
|
|
87
|
+
logger.log('test', { error: { code: '200', message: 'Page Not Found' } });
|
|
88
|
+
assert.strictEqual(logSpy.mock.calls.length, 1);
|
|
89
|
+
assert.deepEqual(logSpy.mock.calls[0]?.arguments, [
|
|
90
|
+
JSON.stringify({
|
|
91
|
+
correlation_id: '123456789',
|
|
92
|
+
http: { method: 'GET' },
|
|
93
|
+
error: { code: '200', message: 'Page Not Found' },
|
|
94
|
+
message: 'test',
|
|
95
|
+
}),
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('overwrites conflicting metadata keys (1st level) with message payload', testContext => {
|
|
100
|
+
const metadata = { correlation_id: '123456789', http: { method: 'GET' } };
|
|
101
|
+
const logger = new Logger(metadata);
|
|
102
|
+
|
|
103
|
+
const logSpy = testContext.mock.method(global.console, 'log', () => {});
|
|
104
|
+
assert.strictEqual(logSpy.mock.calls.length, 0);
|
|
105
|
+
logger.log('test', { http: { status_code: 200 } });
|
|
106
|
+
assert.strictEqual(logSpy.mock.calls.length, 1);
|
|
107
|
+
assert.deepEqual(logSpy.mock.calls[0]?.arguments, [
|
|
108
|
+
JSON.stringify({
|
|
109
|
+
correlation_id: '123456789',
|
|
110
|
+
http: { status_code: 200 },
|
|
111
|
+
message: 'test',
|
|
112
|
+
}),
|
|
113
|
+
]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('snakify keys of Message and Metadata', testContext => {
|
|
117
|
+
const metadata = { correlationId: '123456789', http: { method: 'GET', statusCode: 200 } };
|
|
118
|
+
const logger = new Logger(metadata);
|
|
119
|
+
|
|
120
|
+
const logSpy = testContext.mock.method(global.console, 'log', () => {});
|
|
121
|
+
assert.strictEqual(logSpy.mock.calls.length, 0);
|
|
122
|
+
logger.log('test', { errorContext: { errorCode: 200, errorMessage: 'Page Not Found' } });
|
|
123
|
+
assert.strictEqual(logSpy.mock.calls.length, 1);
|
|
124
|
+
assert.deepEqual(logSpy.mock.calls[0]?.arguments, [
|
|
125
|
+
JSON.stringify({
|
|
126
|
+
correlation_id: '123456789',
|
|
127
|
+
http: { method: 'GET', status_code: 200 },
|
|
128
|
+
error_context: { error_code: 200, error_message: 'Page Not Found' },
|
|
129
|
+
message: 'test',
|
|
130
|
+
}),
|
|
78
131
|
]);
|
|
79
132
|
});
|
|
80
133
|
});
|
|
@@ -48,7 +48,7 @@ describe('Provider', () => {
|
|
|
48
48
|
},
|
|
49
49
|
},
|
|
50
50
|
]);
|
|
51
|
-
assert.deepEqual(actualResponse, { status: 200, headers: response.headers,
|
|
51
|
+
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
it('post with url encoded body', async context => {
|
|
@@ -86,7 +86,7 @@ describe('Provider', () => {
|
|
|
86
86
|
},
|
|
87
87
|
},
|
|
88
88
|
]);
|
|
89
|
-
assert.deepEqual(actualResponse, { status: 201, headers: response.headers,
|
|
89
|
+
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
it('put with json body', async context => {
|
|
@@ -125,7 +125,7 @@ describe('Provider', () => {
|
|
|
125
125
|
},
|
|
126
126
|
},
|
|
127
127
|
]);
|
|
128
|
-
assert.deepEqual(actualResponse, { status: 201, headers: response.headers,
|
|
128
|
+
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
129
129
|
});
|
|
130
130
|
|
|
131
131
|
it('patch with query params', async context => {
|
|
@@ -164,7 +164,7 @@ describe('Provider', () => {
|
|
|
164
164
|
},
|
|
165
165
|
},
|
|
166
166
|
]);
|
|
167
|
-
assert.deepEqual(actualResponse, { status: 201, headers: response.headers,
|
|
167
|
+
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
168
168
|
});
|
|
169
169
|
|
|
170
170
|
it('delete', async context => {
|
|
@@ -196,7 +196,7 @@ describe('Provider', () => {
|
|
|
196
196
|
},
|
|
197
197
|
},
|
|
198
198
|
]);
|
|
199
|
-
assert.deepEqual(actualResponse, { status: 204, headers: response.headers,
|
|
199
|
+
assert.deepEqual(actualResponse, { status: 204, headers: response.headers, body: undefined });
|
|
200
200
|
});
|
|
201
201
|
|
|
202
202
|
it('uses rate limiter if provided', async context => {
|
|
@@ -247,7 +247,7 @@ describe('Provider', () => {
|
|
|
247
247
|
},
|
|
248
248
|
},
|
|
249
249
|
]);
|
|
250
|
-
assert.deepEqual(actualResponse, { status: 204, headers: response.headers,
|
|
250
|
+
assert.deepEqual(actualResponse, { status: 204, headers: response.headers, body: undefined });
|
|
251
251
|
});
|
|
252
252
|
|
|
253
253
|
it('throws on invalid json response', async context => {
|