@unito/integration-sdk 2.3.1 → 2.3.2
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/src/index.cjs +15 -0
- package/dist/src/middlewares/credentials.d.ts +17 -1
- package/dist/src/resources/provider.js +15 -0
- package/dist/test/resources/provider.test.js +19 -7
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +1 -1
- package/src/middlewares/credentials.ts +17 -1
- package/src/resources/provider.ts +21 -0
- package/test/resources/provider.test.ts +27 -7
package/dist/src/index.cjs
CHANGED
|
@@ -1415,6 +1415,7 @@ class Provider {
|
|
|
1415
1415
|
}
|
|
1416
1416
|
}
|
|
1417
1417
|
const callToProvider = async () => {
|
|
1418
|
+
const beforeRequestTimestamp = process.hrtime.bigint();
|
|
1418
1419
|
let response;
|
|
1419
1420
|
try {
|
|
1420
1421
|
response = await fetch(absoluteUrl, {
|
|
@@ -1436,6 +1437,20 @@ class Provider {
|
|
|
1436
1437
|
}
|
|
1437
1438
|
throw this.handleError(500, 'Unexpected error while calling the provider - this is not normal, investigate', options);
|
|
1438
1439
|
}
|
|
1440
|
+
const afterRequestTimestamp = process.hrtime.bigint();
|
|
1441
|
+
const requestDurationInNS = Number(afterRequestTimestamp - beforeRequestTimestamp);
|
|
1442
|
+
const requestDurationInMs = (requestDurationInNS / 1_000_000) | 0;
|
|
1443
|
+
options.logger.info(`Connector API Request ${options.method} ${absoluteUrl} ${response.status} - ${requestDurationInMs} ms`, {
|
|
1444
|
+
duration: requestDurationInNS,
|
|
1445
|
+
http: {
|
|
1446
|
+
method: options.method,
|
|
1447
|
+
status_code: response.status,
|
|
1448
|
+
content_type: headers['Content-Type'],
|
|
1449
|
+
url_details: {
|
|
1450
|
+
path: absoluteUrl,
|
|
1451
|
+
},
|
|
1452
|
+
},
|
|
1453
|
+
});
|
|
1439
1454
|
if (response.status >= 400) {
|
|
1440
1455
|
const textResult = await response.text();
|
|
1441
1456
|
throw this.handleError(response.status, textResult, options);
|
|
@@ -6,9 +6,25 @@ declare global {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* The credentials object passed to every handler function.
|
|
11
|
+
*
|
|
12
|
+
* It contains the parsed credentials payload associated with the request through the X-Unito-Credentials header.
|
|
13
|
+
*/
|
|
9
14
|
export type Credentials = {
|
|
15
|
+
/**
|
|
16
|
+
* The access token for the provider.
|
|
17
|
+
*/
|
|
10
18
|
accessToken?: string;
|
|
11
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The id of the unito credential record.
|
|
21
|
+
*
|
|
22
|
+
* This is not available on the initial call to /me before the creation of the credential.
|
|
23
|
+
*/
|
|
24
|
+
unitoCredentialId?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The id of the unito user record.
|
|
27
|
+
*/
|
|
12
28
|
unitoUserId?: string;
|
|
13
29
|
[keys: string]: unknown;
|
|
14
30
|
};
|
|
@@ -284,6 +284,7 @@ export class Provider {
|
|
|
284
284
|
}
|
|
285
285
|
}
|
|
286
286
|
const callToProvider = async () => {
|
|
287
|
+
const beforeRequestTimestamp = process.hrtime.bigint();
|
|
287
288
|
let response;
|
|
288
289
|
try {
|
|
289
290
|
response = await fetch(absoluteUrl, {
|
|
@@ -305,6 +306,20 @@ export class Provider {
|
|
|
305
306
|
}
|
|
306
307
|
throw this.handleError(500, 'Unexpected error while calling the provider - this is not normal, investigate', options);
|
|
307
308
|
}
|
|
309
|
+
const afterRequestTimestamp = process.hrtime.bigint();
|
|
310
|
+
const requestDurationInNS = Number(afterRequestTimestamp - beforeRequestTimestamp);
|
|
311
|
+
const requestDurationInMs = (requestDurationInNS / 1_000_000) | 0;
|
|
312
|
+
options.logger.info(`Connector API Request ${options.method} ${absoluteUrl} ${response.status} - ${requestDurationInMs} ms`, {
|
|
313
|
+
duration: requestDurationInNS,
|
|
314
|
+
http: {
|
|
315
|
+
method: options.method,
|
|
316
|
+
status_code: response.status,
|
|
317
|
+
content_type: headers['Content-Type'],
|
|
318
|
+
url_details: {
|
|
319
|
+
path: absoluteUrl,
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
});
|
|
308
323
|
if (response.status >= 400) {
|
|
309
324
|
const textResult = await response.text();
|
|
310
325
|
throw this.handleError(response.status, textResult, options);
|
|
@@ -50,7 +50,7 @@ describe('Provider', () => {
|
|
|
50
50
|
]);
|
|
51
51
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
52
52
|
});
|
|
53
|
-
it('
|
|
53
|
+
it('accepts text/html type response', async (context) => {
|
|
54
54
|
const response = new Response('', {
|
|
55
55
|
status: 200,
|
|
56
56
|
headers: { 'Content-Type': 'text/html; charset=UTF-8' },
|
|
@@ -79,7 +79,7 @@ describe('Provider', () => {
|
|
|
79
79
|
]);
|
|
80
80
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: '' });
|
|
81
81
|
});
|
|
82
|
-
it('
|
|
82
|
+
it('accepts application/schema+json type response', async (context) => {
|
|
83
83
|
const response = new Response('{"data": "value"}', {
|
|
84
84
|
status: 200,
|
|
85
85
|
headers: { 'Content-Type': 'application/schema+json; charset=UTF-8' },
|
|
@@ -108,7 +108,7 @@ describe('Provider', () => {
|
|
|
108
108
|
]);
|
|
109
109
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
110
110
|
});
|
|
111
|
-
it('
|
|
111
|
+
it('accepts application/swagger+json type response', async (context) => {
|
|
112
112
|
const response = new Response('{"data": "value"}', {
|
|
113
113
|
status: 200,
|
|
114
114
|
headers: { 'Content-Type': 'application/swagger+json; charset=UTF-8' },
|
|
@@ -137,7 +137,7 @@ describe('Provider', () => {
|
|
|
137
137
|
]);
|
|
138
138
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
139
139
|
});
|
|
140
|
-
it('
|
|
140
|
+
it('accepts application/vnd.oracle.resource+json type response', async (context) => {
|
|
141
141
|
const response = new Response('{"data": "value"}', {
|
|
142
142
|
status: 200,
|
|
143
143
|
headers: { 'Content-Type': 'application/vnd.oracle.resource+json; type=collection; charset=UTF-8' },
|
|
@@ -166,7 +166,7 @@ describe('Provider', () => {
|
|
|
166
166
|
]);
|
|
167
167
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
168
168
|
});
|
|
169
|
-
it('
|
|
169
|
+
it('returns the raw response body if specified', async (context) => {
|
|
170
170
|
const response = new Response(`IMAGINE A HUGE PAYLOAD`, {
|
|
171
171
|
status: 200,
|
|
172
172
|
headers: { 'Content-Type': 'image/png' },
|
|
@@ -244,7 +244,7 @@ describe('Provider', () => {
|
|
|
244
244
|
]);
|
|
245
245
|
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
246
246
|
});
|
|
247
|
-
it('
|
|
247
|
+
it('accepts an array as body for post request', async (context) => {
|
|
248
248
|
const response = new Response('{"data": "value"}', {
|
|
249
249
|
status: 201,
|
|
250
250
|
headers: { 'Content-Type': 'application/json' },
|
|
@@ -485,7 +485,7 @@ describe('Provider', () => {
|
|
|
485
485
|
assert.ok(error instanceof HttpErrors.HttpError);
|
|
486
486
|
assert.equal(error.message, 'Weird provider behavior');
|
|
487
487
|
});
|
|
488
|
-
it('
|
|
488
|
+
it('contains the credential in the custom error handler', async (context) => {
|
|
489
489
|
const provider = new Provider({
|
|
490
490
|
prepareRequest: requestOptions => {
|
|
491
491
|
return {
|
|
@@ -761,4 +761,16 @@ describe('Provider', () => {
|
|
|
761
761
|
assert.ok(error instanceof HttpErrors.RateLimitExceededError);
|
|
762
762
|
assert.equal(error.message, 'response body');
|
|
763
763
|
});
|
|
764
|
+
it('logs provider requests', async (context) => {
|
|
765
|
+
const response = new Response(undefined, { status: 201 });
|
|
766
|
+
context.mock.method(global, 'fetch', () => Promise.resolve(response));
|
|
767
|
+
const loggerStub = context.mock.method(logger, 'info');
|
|
768
|
+
await provider.get('/endpoint/123', {
|
|
769
|
+
credentials: { apiKey: 'apikey#1111', unitoCredentialId: '123' },
|
|
770
|
+
signal: new AbortController().signal,
|
|
771
|
+
logger: logger,
|
|
772
|
+
});
|
|
773
|
+
assert.equal(loggerStub.mock.callCount(), 1);
|
|
774
|
+
assert.match(String(loggerStub.mock.calls[0]?.arguments[0]), /Connector API Request GET www.myApi.com\/endpoint\/123 201 - \d+ ms/);
|
|
775
|
+
});
|
|
764
776
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/errors.ts","../src/handler.ts","../src/helpers.ts","../src/httpErrors.ts","../src/index.ts","../src/integration.ts","../src/middlewares/correlationId.ts","../src/middlewares/credentials.ts","../src/middlewares/errors.ts","../src/middlewares/filters.ts","../src/middlewares/finish.ts","../src/middlewares/health.ts","../src/middlewares/logger.ts","../src/middlewares/notFound.ts","../src/middlewares/relations.ts","../src/middlewares/search.ts","../src/middlewares/secrets.ts","../src/middlewares/selects.ts","../src/middlewares/signal.ts","../src/middlewares/start.ts","../src/resources/cache.ts","../src/resources/context.ts","../src/resources/logger.ts","../src/resources/provider.ts","../test/errors.test.ts","../test/handler.test.ts","../test/helpers.test.ts","../test/integration.test.ts","../test/middlewares/correlationId.test.ts","../test/middlewares/credentials.test.ts","../test/middlewares/errors.test.ts","../test/middlewares/filters.test.ts","../test/middlewares/finish.test.ts","../test/middlewares/health.test.ts","../test/middlewares/logger.test.ts","../test/middlewares/notFound.test.ts","../test/middlewares/relations.test.ts","../test/middlewares/search.test.ts","../test/middlewares/secrets.test.ts","../test/middlewares/selects.test.ts","../test/middlewares/signal.test.ts","../test/middlewares/start.test.ts","../test/resources/cache.test.ts","../test/resources/logger.test.ts","../test/resources/provider.test.ts"],"version":"5.8.3"}
|
package/package.json
CHANGED
|
@@ -10,9 +10,25 @@ declare global {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The credentials object passed to every handler function.
|
|
15
|
+
*
|
|
16
|
+
* It contains the parsed credentials payload associated with the request through the X-Unito-Credentials header.
|
|
17
|
+
*/
|
|
13
18
|
export type Credentials = {
|
|
19
|
+
/**
|
|
20
|
+
* The access token for the provider.
|
|
21
|
+
*/
|
|
14
22
|
accessToken?: string;
|
|
15
|
-
|
|
23
|
+
/**
|
|
24
|
+
* The id of the unito credential record.
|
|
25
|
+
*
|
|
26
|
+
* This is not available on the initial call to /me before the creation of the credential.
|
|
27
|
+
*/
|
|
28
|
+
unitoCredentialId?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The id of the unito user record.
|
|
31
|
+
*/
|
|
16
32
|
unitoUserId?: string;
|
|
17
33
|
[keys: string]: unknown;
|
|
18
34
|
};
|
|
@@ -388,6 +388,8 @@ export class Provider {
|
|
|
388
388
|
}
|
|
389
389
|
|
|
390
390
|
const callToProvider = async (): Promise<Response<T>> => {
|
|
391
|
+
const beforeRequestTimestamp = process.hrtime.bigint();
|
|
392
|
+
|
|
391
393
|
let response: globalThis.Response;
|
|
392
394
|
|
|
393
395
|
try {
|
|
@@ -420,6 +422,25 @@ export class Provider {
|
|
|
420
422
|
);
|
|
421
423
|
}
|
|
422
424
|
|
|
425
|
+
const afterRequestTimestamp = process.hrtime.bigint();
|
|
426
|
+
const requestDurationInNS = Number(afterRequestTimestamp - beforeRequestTimestamp);
|
|
427
|
+
const requestDurationInMs = (requestDurationInNS / 1_000_000) | 0;
|
|
428
|
+
|
|
429
|
+
options.logger.info(
|
|
430
|
+
`Connector API Request ${options.method} ${absoluteUrl} ${response.status} - ${requestDurationInMs} ms`,
|
|
431
|
+
{
|
|
432
|
+
duration: requestDurationInNS,
|
|
433
|
+
http: {
|
|
434
|
+
method: options.method,
|
|
435
|
+
status_code: response.status,
|
|
436
|
+
content_type: headers['Content-Type'],
|
|
437
|
+
url_details: {
|
|
438
|
+
path: absoluteUrl,
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
);
|
|
443
|
+
|
|
423
444
|
if (response.status >= 400) {
|
|
424
445
|
const textResult = await response.text();
|
|
425
446
|
throw this.handleError(response.status, textResult, options);
|
|
@@ -59,7 +59,7 @@ describe('Provider', () => {
|
|
|
59
59
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
it('
|
|
62
|
+
it('accepts text/html type response', async context => {
|
|
63
63
|
const response = new Response('', {
|
|
64
64
|
status: 200,
|
|
65
65
|
headers: { 'Content-Type': 'text/html; charset=UTF-8' },
|
|
@@ -94,7 +94,7 @@ describe('Provider', () => {
|
|
|
94
94
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: '' });
|
|
95
95
|
});
|
|
96
96
|
|
|
97
|
-
it('
|
|
97
|
+
it('accepts application/schema+json type response', async context => {
|
|
98
98
|
const response = new Response('{"data": "value"}', {
|
|
99
99
|
status: 200,
|
|
100
100
|
headers: { 'Content-Type': 'application/schema+json; charset=UTF-8' },
|
|
@@ -129,7 +129,7 @@ describe('Provider', () => {
|
|
|
129
129
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
130
130
|
});
|
|
131
131
|
|
|
132
|
-
it('
|
|
132
|
+
it('accepts application/swagger+json type response', async context => {
|
|
133
133
|
const response = new Response('{"data": "value"}', {
|
|
134
134
|
status: 200,
|
|
135
135
|
headers: { 'Content-Type': 'application/swagger+json; charset=UTF-8' },
|
|
@@ -164,7 +164,7 @@ describe('Provider', () => {
|
|
|
164
164
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
165
165
|
});
|
|
166
166
|
|
|
167
|
-
it('
|
|
167
|
+
it('accepts application/vnd.oracle.resource+json type response', async context => {
|
|
168
168
|
const response = new Response('{"data": "value"}', {
|
|
169
169
|
status: 200,
|
|
170
170
|
headers: { 'Content-Type': 'application/vnd.oracle.resource+json; type=collection; charset=UTF-8' },
|
|
@@ -199,7 +199,7 @@ describe('Provider', () => {
|
|
|
199
199
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
200
200
|
});
|
|
201
201
|
|
|
202
|
-
it('
|
|
202
|
+
it('returns the raw response body if specified', async context => {
|
|
203
203
|
const response = new Response(`IMAGINE A HUGE PAYLOAD`, {
|
|
204
204
|
status: 200,
|
|
205
205
|
headers: { 'Content-Type': 'image/png' },
|
|
@@ -293,7 +293,7 @@ describe('Provider', () => {
|
|
|
293
293
|
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
294
294
|
});
|
|
295
295
|
|
|
296
|
-
it('
|
|
296
|
+
it('accepts an array as body for post request', async context => {
|
|
297
297
|
const response = new Response('{"data": "value"}', {
|
|
298
298
|
status: 201,
|
|
299
299
|
headers: { 'Content-Type': 'application/json' },
|
|
@@ -580,7 +580,7 @@ describe('Provider', () => {
|
|
|
580
580
|
assert.equal(error.message, 'Weird provider behavior');
|
|
581
581
|
});
|
|
582
582
|
|
|
583
|
-
it('
|
|
583
|
+
it('contains the credential in the custom error handler', async context => {
|
|
584
584
|
const provider = new Provider({
|
|
585
585
|
prepareRequest: requestOptions => {
|
|
586
586
|
return {
|
|
@@ -911,4 +911,24 @@ describe('Provider', () => {
|
|
|
911
911
|
assert.ok(error instanceof HttpErrors.RateLimitExceededError);
|
|
912
912
|
assert.equal(error.message, 'response body');
|
|
913
913
|
});
|
|
914
|
+
|
|
915
|
+
it('logs provider requests', async context => {
|
|
916
|
+
const response = new Response(undefined, { status: 201 });
|
|
917
|
+
|
|
918
|
+
context.mock.method(global, 'fetch', () => Promise.resolve(response));
|
|
919
|
+
|
|
920
|
+
const loggerStub = context.mock.method(logger, 'info');
|
|
921
|
+
|
|
922
|
+
await provider.get('/endpoint/123', {
|
|
923
|
+
credentials: { apiKey: 'apikey#1111', unitoCredentialId: '123' },
|
|
924
|
+
signal: new AbortController().signal,
|
|
925
|
+
logger: logger,
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
assert.equal(loggerStub.mock.callCount(), 1);
|
|
929
|
+
assert.match(
|
|
930
|
+
String(loggerStub.mock.calls[0]?.arguments[0]),
|
|
931
|
+
/Connector API Request GET www.myApi.com\/endpoint\/123 201 - \d+ ms/,
|
|
932
|
+
);
|
|
933
|
+
});
|
|
914
934
|
});
|