@commercetools/connect-payments-sdk 0.6.0 → 0.7.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/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CommercetoolsAuthorizationService } from '../../commercetools';
|
|
2
|
+
import { Logger } from '../../logger';
|
|
2
3
|
import { HandlerResponse } from './types/handler.type';
|
|
3
4
|
type HealthCheckStatus = {
|
|
4
5
|
status: 'OK' | 'Partially Available' | 'Unavailable';
|
|
@@ -11,13 +12,21 @@ export type HealthCheckResult = {
|
|
|
11
12
|
name: string;
|
|
12
13
|
status: 'UP' | 'DOWN';
|
|
13
14
|
details?: object;
|
|
15
|
+
message?: string;
|
|
14
16
|
};
|
|
15
17
|
export type HealthCheck = () => Promise<HealthCheckResult> | HealthCheckResult;
|
|
16
|
-
export
|
|
18
|
+
export type StatusHandlerOpts = {
|
|
17
19
|
timeout: number;
|
|
18
20
|
checks: HealthCheck[];
|
|
19
21
|
metadataFn?: () => Promise<object> | object;
|
|
20
|
-
|
|
22
|
+
log: Logger;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Handler to check the status of the service
|
|
26
|
+
* @param opts
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
export declare const statusHandler: (opts: StatusHandlerOpts) => () => Promise<HandlerResponse<HealthCheckStatus>>;
|
|
21
30
|
/**
|
|
22
31
|
* Check if CoCo permissions are available
|
|
23
32
|
* @param opts
|
|
@@ -1,19 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.healthCheckCommercetoolsPermissions = exports.statusHandler = void 0;
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Handler to check the status of the service
|
|
6
|
+
* @param opts
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const statusHandler = (opts) => async () => {
|
|
5
10
|
const status = {
|
|
6
11
|
timestamp: new Date().toISOString(),
|
|
7
12
|
version: process.env.npm_package_version ?? '0.0.0',
|
|
8
13
|
};
|
|
9
|
-
if (
|
|
10
|
-
status.metadata = await
|
|
14
|
+
if (opts.metadataFn) {
|
|
15
|
+
status.metadata = await opts.metadataFn();
|
|
11
16
|
}
|
|
12
17
|
let timeoutId = null;
|
|
13
|
-
const timeoutPromise = new Promise((_, reject) => (timeoutId = setTimeout(() => reject(new Error('Timeout')),
|
|
18
|
+
const timeoutPromise = new Promise((_, reject) => (timeoutId = setTimeout(() => reject(new Error('Timeout')), opts.timeout)));
|
|
14
19
|
try {
|
|
15
|
-
const results = await Promise.race([Promise.all(
|
|
16
|
-
|
|
20
|
+
const results = await Promise.race([Promise.all(opts.checks.map((check) => check())), timeoutPromise]);
|
|
21
|
+
let allDown = true;
|
|
22
|
+
let atLeastOneDown = false;
|
|
23
|
+
results.forEach((result) => {
|
|
24
|
+
if (result.status === 'DOWN') {
|
|
25
|
+
atLeastOneDown = true;
|
|
26
|
+
opts.log.error({ details: result.details }, result.message ?? `Health check for ${result.name} failed, check details`);
|
|
27
|
+
}
|
|
28
|
+
if (result.status === 'UP') {
|
|
29
|
+
allDown = false;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
if (allDown) {
|
|
33
|
+
status.status = 'Unavailable';
|
|
34
|
+
}
|
|
35
|
+
else if (atLeastOneDown) {
|
|
17
36
|
status.status = 'Partially Available';
|
|
18
37
|
}
|
|
19
38
|
else {
|
|
@@ -21,11 +40,14 @@ const statusHandler = (options) => async () => {
|
|
|
21
40
|
}
|
|
22
41
|
status.checks = results;
|
|
23
42
|
return {
|
|
24
|
-
status: 200,
|
|
43
|
+
status: allDown ? 503 : 200,
|
|
25
44
|
body: status,
|
|
26
45
|
};
|
|
27
46
|
}
|
|
28
|
-
catch (
|
|
47
|
+
catch (e) {
|
|
48
|
+
if (e instanceof Error && e.message === 'Timeout') {
|
|
49
|
+
opts.log.error({ err: e }, 'Health check timed out');
|
|
50
|
+
}
|
|
29
51
|
status.status = 'Unavailable';
|
|
30
52
|
status.checks = [];
|
|
31
53
|
return {
|
|
@@ -60,6 +82,7 @@ const healthCheckCommercetoolsPermissions = (opts) => async () => {
|
|
|
60
82
|
return {
|
|
61
83
|
name: 'CoCo Permissions',
|
|
62
84
|
status: 'DOWN',
|
|
85
|
+
message: `CoCo permissions are not correct, expected scopes: ${opts.requiredPermissions.join(' ')}, actual scopes: ${token.scope}`,
|
|
63
86
|
details: {
|
|
64
87
|
expectedScopes: opts.requiredPermissions,
|
|
65
88
|
actualScopes: token.scope,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commercetools/connect-payments-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Payment SDK for commercetools payment connectors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
],
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@commercetools-backend/loggers": "22.
|
|
19
|
-
"@commercetools/platform-sdk": "7.
|
|
20
|
-
"@commercetools/sdk-client-v2": "2.
|
|
18
|
+
"@commercetools-backend/loggers": "22.27.0",
|
|
19
|
+
"@commercetools/platform-sdk": "7.8.0",
|
|
20
|
+
"@commercetools/sdk-client-v2": "2.5.0",
|
|
21
21
|
"jsonwebtoken": "9.0.2",
|
|
22
22
|
"jwks-rsa": "3.1.0",
|
|
23
23
|
"lodash": "4.17.21",
|