@chainlink/external-adapter-framework 0.0.10 → 0.0.14
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/adapter.d.ts +22 -3
- package/adapter.js +5 -2
- package/cache/factory.js +0 -2
- package/cache/index.d.ts +6 -2
- package/cache/index.js +13 -9
- package/cache/redis.js +5 -5
- package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
- package/config/index.d.ts +15 -1
- package/config/index.js +19 -4
- package/config/provider-limits.js +5 -1
- package/examples/bank-frick/accounts.d.ts +39 -0
- package/examples/bank-frick/accounts.js +191 -0
- package/examples/bank-frick/config/index.d.ts +4 -0
- package/examples/bank-frick/config/index.js +54 -0
- package/examples/bank-frick/index.d.ts +2 -0
- package/examples/bank-frick/index.js +14 -0
- package/examples/bank-frick/util.d.ts +4 -0
- package/examples/bank-frick/util.js +39 -0
- package/index.d.ts +1 -2
- package/index.js +42 -1
- package/metrics/index.js +0 -1
- package/metrics/util.d.ts +5 -1
- package/metrics/util.js +2 -2
- package/package/adapter.d.ts +88 -0
- package/package/adapter.js +112 -0
- package/package/background-executor.d.ts +11 -0
- package/package/background-executor.js +45 -0
- package/package/cache/factory.d.ts +6 -0
- package/package/cache/factory.js +57 -0
- package/package/cache/index.d.ts +90 -0
- package/package/cache/index.js +169 -0
- package/package/cache/local.d.ts +23 -0
- package/package/cache/local.js +83 -0
- package/package/cache/metrics.d.ts +27 -0
- package/package/cache/metrics.js +120 -0
- package/package/cache/redis.d.ts +16 -0
- package/package/cache/redis.js +100 -0
- package/package/config/index.d.ts +195 -0
- package/package/config/index.js +365 -0
- package/package/config/provider-limits.d.ts +31 -0
- package/package/config/provider-limits.js +76 -0
- package/package/examples/coingecko/batch-warming.d.ts +2 -0
- package/package/examples/coingecko/batch-warming.js +52 -0
- package/package/examples/coingecko/index.d.ts +2 -0
- package/package/examples/coingecko/index.js +10 -0
- package/package/examples/coingecko/rest.d.ts +2 -0
- package/package/examples/coingecko/rest.js +50 -0
- package/package/examples/ncfx/config/index.d.ts +12 -0
- package/package/examples/ncfx/config/index.js +15 -0
- package/package/examples/ncfx/index.d.ts +2 -0
- package/package/examples/ncfx/index.js +10 -0
- package/package/examples/ncfx/websocket.d.ts +36 -0
- package/package/examples/ncfx/websocket.js +72 -0
- package/package/index.d.ts +12 -0
- package/package/index.js +92 -0
- package/package/metrics/constants.d.ts +16 -0
- package/package/metrics/constants.js +25 -0
- package/package/metrics/index.d.ts +15 -0
- package/package/metrics/index.js +123 -0
- package/package/metrics/util.d.ts +3 -0
- package/package/metrics/util.js +9 -0
- package/package/package.json +72 -0
- package/package/rate-limiting/background/fixed-frequency.d.ts +10 -0
- package/package/rate-limiting/background/fixed-frequency.js +37 -0
- package/package/rate-limiting/index.d.ts +54 -0
- package/package/rate-limiting/index.js +63 -0
- package/package/rate-limiting/metrics.d.ts +3 -0
- package/package/rate-limiting/metrics.js +44 -0
- package/package/rate-limiting/request/simple-counting.d.ts +20 -0
- package/package/rate-limiting/request/simple-counting.js +62 -0
- package/package/test.d.ts +1 -0
- package/package/test.js +6 -0
- package/package/transports/batch-warming.d.ts +34 -0
- package/package/transports/batch-warming.js +101 -0
- package/package/transports/index.d.ts +87 -0
- package/package/transports/index.js +87 -0
- package/package/transports/metrics.d.ts +21 -0
- package/package/transports/metrics.js +105 -0
- package/package/transports/rest.d.ts +43 -0
- package/package/transports/rest.js +129 -0
- package/package/transports/util.d.ts +8 -0
- package/package/transports/util.js +85 -0
- package/package/transports/websocket.d.ts +80 -0
- package/package/transports/websocket.js +169 -0
- package/package/util/expiring-sorted-set.d.ts +21 -0
- package/package/util/expiring-sorted-set.js +47 -0
- package/package/util/index.d.ts +11 -0
- package/package/util/index.js +35 -0
- package/package/util/logger.d.ts +42 -0
- package/package/util/logger.js +62 -0
- package/package/util/request.d.ts +55 -0
- package/package/util/request.js +2 -0
- package/package/validation/error.d.ts +50 -0
- package/package/validation/error.js +79 -0
- package/package/validation/index.d.ts +5 -0
- package/package/validation/index.js +86 -0
- package/package/validation/input-params.d.ts +15 -0
- package/package/validation/input-params.js +30 -0
- package/package/validation/override-functions.d.ts +3 -0
- package/package/validation/override-functions.js +40 -0
- package/package/validation/preset-tokens.json +23 -0
- package/package/validation/validator.d.ts +47 -0
- package/package/validation/validator.js +303 -0
- package/package.json +5 -3
- package/rate-limiting/background/fixed-frequency.js +0 -2
- package/test.js +2 -2
- package/transports/batch-warming.d.ts +4 -3
- package/transports/batch-warming.js +4 -4
- package/transports/index.d.ts +4 -21
- package/transports/index.js +3 -3
- package/transports/metrics.d.ts +1 -1
- package/transports/metrics.js +2 -2
- package/transports/rest.d.ts +2 -1
- package/transports/rest.js +3 -1
- package/transports/websocket.d.ts +5 -4
- package/transports/websocket.js +5 -6
- package/util/index.d.ts +2 -1
- package/util/index.js +1 -1
- package/util/request.d.ts +3 -1
- package/util/subscription-set/expiring-sorted-set.d.ts +22 -0
- package/util/subscription-set/expiring-sorted-set.js +47 -0
- package/util/subscription-set/subscription-set.d.ts +18 -0
- package/util/subscription-set/subscription-set.js +19 -0
- package/util/test-payload-loader.d.ts +25 -0
- package/util/test-payload-loader.js +83 -0
- package/validation/error.d.ts +2 -2
- package/validation/error.js +1 -1
- package/validation/index.js +8 -3
package/adapter.d.ts
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Cache } from './cache';
|
|
2
|
+
import { AdapterConfig, BaseAdapterConfig, CustomSettingsType, SettingsMap } from './config';
|
|
3
|
+
import { AdapterRateLimitTier, BackgroundExecuteRateLimiter, RequestRateLimiter } from './rate-limiting';
|
|
4
|
+
import { Transport } from './transports';
|
|
5
|
+
import { SubscriptionSetFactory } from './util';
|
|
4
6
|
import { InputParameters } from './validation/input-params';
|
|
7
|
+
/**
|
|
8
|
+
* Dependencies that will be injected into the Adapter on startup
|
|
9
|
+
*/
|
|
10
|
+
export interface AdapterDependencies {
|
|
11
|
+
cache: Cache;
|
|
12
|
+
requestRateLimiter: RequestRateLimiter;
|
|
13
|
+
backgroundExecuteRateLimiter: BackgroundExecuteRateLimiter;
|
|
14
|
+
subscriptionSetFactory: SubscriptionSetFactory;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Context that will be used on background executions of a Transport.
|
|
18
|
+
* For example, the endpointName used to log statements or generate Cache keys.
|
|
19
|
+
*/
|
|
20
|
+
export interface AdapterContext<CustomSettings extends CustomSettingsType<CustomSettings> = SettingsMap> {
|
|
21
|
+
adapterEndpoint: AdapterEndpoint;
|
|
22
|
+
adapterConfig: AdapterConfig<CustomSettings>;
|
|
23
|
+
}
|
|
5
24
|
/**
|
|
6
25
|
* Structure to describe rate limits specs for the Adapter
|
|
7
26
|
*/
|
package/adapter.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initializeAdapter = exports.initializeDependencies = void 0;
|
|
4
|
-
const
|
|
4
|
+
const cache_1 = require("./cache");
|
|
5
5
|
const rate_limiting_1 = require("./rate-limiting");
|
|
6
6
|
const util_1 = require("./util");
|
|
7
7
|
const logger = (0, util_1.makeLogger)('Adapter');
|
|
@@ -47,7 +47,7 @@ const calculateRateLimitAllocations = (adapter) => {
|
|
|
47
47
|
const initializeDependencies = (adapter, config, inputDependencies) => {
|
|
48
48
|
const dependencies = inputDependencies || {};
|
|
49
49
|
if (!dependencies.cache) {
|
|
50
|
-
dependencies.cache =
|
|
50
|
+
dependencies.cache = cache_1.CacheFactory.buildCache(config);
|
|
51
51
|
}
|
|
52
52
|
// In the future we might want something more complex, but for now it's better to simplify
|
|
53
53
|
// and just use the same rate limiting for everything. Once we have a more complex use case we
|
|
@@ -59,6 +59,9 @@ const initializeDependencies = (adapter, config, inputDependencies) => {
|
|
|
59
59
|
if (!dependencies.backgroundExecuteRateLimiter) {
|
|
60
60
|
dependencies.backgroundExecuteRateLimiter = new rate_limiting_1.FixedFrequencyRateLimiter().initialize(adapter.endpoints, rateLimitingTier);
|
|
61
61
|
}
|
|
62
|
+
if (!dependencies.subscriptionSetFactory) {
|
|
63
|
+
dependencies.subscriptionSetFactory = new util_1.SubscriptionSetFactory(config);
|
|
64
|
+
}
|
|
62
65
|
return dependencies;
|
|
63
66
|
};
|
|
64
67
|
exports.initializeDependencies = initializeDependencies;
|
package/cache/factory.js
CHANGED
|
@@ -48,8 +48,6 @@ class CacheFactory {
|
|
|
48
48
|
redis.on('connect', () => {
|
|
49
49
|
cacheMetrics.redisConnectionsOpen.inc();
|
|
50
50
|
});
|
|
51
|
-
// TODO: Maybe track active redis connections instead of just total count of connections created
|
|
52
|
-
// Use a Gauge and track with a combo of connect/end events listeners
|
|
53
51
|
return new redis_1.RedisCache(redis);
|
|
54
52
|
}
|
|
55
53
|
}
|
package/cache/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { AdapterConfig } from '../config';
|
|
|
3
3
|
import { AdapterMiddlewareBuilder, AdapterResponse, sleep } from '../util';
|
|
4
4
|
export * from './local';
|
|
5
5
|
export * from './redis';
|
|
6
|
+
export * from './factory';
|
|
6
7
|
/**
|
|
7
8
|
* An object describing an entry in the cache.
|
|
8
9
|
* @typeParam T - the type of the entry's value
|
|
@@ -52,7 +53,10 @@ export declare const calculateCacheKey: ({ adapterEndpoint, adapterConfig, }: {
|
|
|
52
53
|
adapterEndpoint: AdapterEndpoint;
|
|
53
54
|
adapterConfig: AdapterConfig;
|
|
54
55
|
}, data: unknown) => string;
|
|
55
|
-
export declare const calculateFeedId: (adapterEndpoint
|
|
56
|
+
export declare const calculateFeedId: ({ adapterEndpoint, adapterConfig, }: {
|
|
57
|
+
adapterEndpoint: AdapterEndpoint;
|
|
58
|
+
adapterConfig: AdapterConfig;
|
|
59
|
+
}, data: unknown) => string;
|
|
56
60
|
/**
|
|
57
61
|
* Calculates a unique key from the provided data.
|
|
58
62
|
*
|
|
@@ -66,7 +70,7 @@ export declare const calculateFeedId: (adapterEndpoint: AdapterEndpoint, data: u
|
|
|
66
70
|
* // equals `|base:eth|quote:btc`
|
|
67
71
|
* ```
|
|
68
72
|
*/
|
|
69
|
-
export declare const calculateKey: (data: unknown, paramNames: string[]) => string;
|
|
73
|
+
export declare const calculateKey: (data: unknown, paramNames: string[], adapterConfig: AdapterConfig) => string;
|
|
70
74
|
/**
|
|
71
75
|
* Polls the provided Cache for an AdapterResponse set in the provided key. If the maximum
|
|
72
76
|
* amount of retries is exceeded, it returns undefined instead.
|
package/cache/index.js
CHANGED
|
@@ -31,6 +31,7 @@ const util_1 = require("../util");
|
|
|
31
31
|
const cacheMetrics = __importStar(require("./metrics"));
|
|
32
32
|
__exportStar(require("./local"), exports);
|
|
33
33
|
__exportStar(require("./redis"), exports);
|
|
34
|
+
__exportStar(require("./factory"), exports);
|
|
34
35
|
const logger = (0, util_1.makeLogger)('Cache');
|
|
35
36
|
// Uses calculateKey to generate a unique key from the endpoint name, data, and input parameters
|
|
36
37
|
const calculateCacheKey = ({ adapterEndpoint, adapterConfig, }, data) => {
|
|
@@ -39,16 +40,16 @@ const calculateCacheKey = ({ adapterEndpoint, adapterConfig, }, data) => {
|
|
|
39
40
|
logger.trace(`Using default cache key ${adapterConfig.DEFAULT_CACHE_KEY}`);
|
|
40
41
|
return adapterConfig.DEFAULT_CACHE_KEY;
|
|
41
42
|
}
|
|
42
|
-
return `${adapterEndpoint.name}-${(0, exports.calculateKey)(data, paramNames)}`;
|
|
43
|
+
return `${adapterEndpoint.name}-${(0, exports.calculateKey)(data, paramNames, adapterConfig)}`;
|
|
43
44
|
};
|
|
44
45
|
exports.calculateCacheKey = calculateCacheKey;
|
|
45
|
-
const calculateFeedId = (adapterEndpoint, data) => {
|
|
46
|
+
const calculateFeedId = ({ adapterEndpoint, adapterConfig, }, data) => {
|
|
46
47
|
const paramNames = Object.keys(adapterEndpoint.inputParameters);
|
|
47
48
|
if (paramNames.length === 0) {
|
|
48
49
|
logger.trace(`Cannot generate Feed ID without input parameters`);
|
|
49
50
|
return 'N/A';
|
|
50
51
|
}
|
|
51
|
-
return (0, exports.calculateKey)(data, paramNames);
|
|
52
|
+
return (0, exports.calculateKey)(data, paramNames, adapterConfig);
|
|
52
53
|
};
|
|
53
54
|
exports.calculateFeedId = calculateFeedId;
|
|
54
55
|
/**
|
|
@@ -64,15 +65,14 @@ exports.calculateFeedId = calculateFeedId;
|
|
|
64
65
|
* // equals `|base:eth|quote:btc`
|
|
65
66
|
* ```
|
|
66
67
|
*/
|
|
67
|
-
const calculateKey = (data, paramNames) => {
|
|
68
|
+
const calculateKey = (data, paramNames, adapterConfig) => {
|
|
68
69
|
if (data && typeof data !== 'object') {
|
|
69
70
|
throw new Error('Data to calculate cache key should be an object');
|
|
70
71
|
}
|
|
71
72
|
const params = data;
|
|
72
|
-
let cacheKey = '';
|
|
73
|
+
let cacheKey = '';
|
|
73
74
|
for (const paramName of paramNames) {
|
|
74
75
|
// Ignore overrides param when generating cache keys
|
|
75
|
-
// TODO: expand support for ignoring `includes` and `tokenOverrides` params when generating keys
|
|
76
76
|
if (paramName === 'overrides') {
|
|
77
77
|
continue;
|
|
78
78
|
}
|
|
@@ -90,11 +90,15 @@ const calculateKey = (data, paramNames) => {
|
|
|
90
90
|
cacheKey += param.toString();
|
|
91
91
|
break;
|
|
92
92
|
case 'object':
|
|
93
|
-
//
|
|
94
|
-
|
|
93
|
+
// Force cache keys to only use performant properties of the input params.
|
|
94
|
+
// If the object were to be used, we'd have to sort its properties.
|
|
95
|
+
logger.debug(`Property "${paramName}" in request parameters is of type object, and won't be used in the cacheKey`);
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
|
-
|
|
98
|
+
if (cacheKey.length > adapterConfig.MAX_COMMON_KEY_SIZE) {
|
|
99
|
+
logger.warn(`Generated cache key for adapter request is bigger than the MAX_COMMON_KEY_SIZE and will be truncated`);
|
|
100
|
+
cacheKey = cacheKey.slice(0, adapterConfig.MAX_COMMON_KEY_SIZE);
|
|
101
|
+
}
|
|
98
102
|
logger.trace(`Generated cache key for request: "${cacheKey}"`);
|
|
99
103
|
return cacheKey;
|
|
100
104
|
};
|
package/cache/redis.js
CHANGED
|
@@ -33,7 +33,6 @@ const logger = (0, util_1.makeLogger)('RedisCache');
|
|
|
33
33
|
*
|
|
34
34
|
* @typeParam T - the type for the entries' values
|
|
35
35
|
*/
|
|
36
|
-
// TODO: Add error handling for redis command failures
|
|
37
36
|
class RedisCache {
|
|
38
37
|
constructor(client) {
|
|
39
38
|
this.client = client;
|
|
@@ -49,7 +48,7 @@ class RedisCache {
|
|
|
49
48
|
logger.debug(`No entry in redis cache for key "${key}", returning undefined`);
|
|
50
49
|
return undefined;
|
|
51
50
|
}
|
|
52
|
-
return JSON.parse(value);
|
|
51
|
+
return JSON.parse(value);
|
|
53
52
|
}
|
|
54
53
|
async delete(key) {
|
|
55
54
|
logger.trace(`Deleting key ${key}`);
|
|
@@ -61,7 +60,6 @@ class RedisCache {
|
|
|
61
60
|
}
|
|
62
61
|
async set(key, value, ttl) {
|
|
63
62
|
logger.trace(`Setting key ${key}`);
|
|
64
|
-
// TODO: Check for invalid stringify
|
|
65
63
|
await this.client.set(key, JSON.stringify(value), 'PX', ttl);
|
|
66
64
|
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
67
65
|
const feedId = value.meta?.metrics?.feedId;
|
|
@@ -81,7 +79,10 @@ class RedisCache {
|
|
|
81
79
|
let chain = this.client.multi();
|
|
82
80
|
for (const entry of entries) {
|
|
83
81
|
chain = chain.set(entry.key, JSON.stringify(entry.value), 'PX', ttl);
|
|
84
|
-
|
|
82
|
+
}
|
|
83
|
+
await chain.exec();
|
|
84
|
+
// Loop again, but this time to record these in metrics
|
|
85
|
+
for (const entry of entries) {
|
|
85
86
|
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
86
87
|
const feedId = entry.value.meta?.metrics?.feedId;
|
|
87
88
|
if (feedId) {
|
|
@@ -90,7 +91,6 @@ class RedisCache {
|
|
|
90
91
|
cacheMetrics.cacheSet(label, ttl);
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
|
-
await chain.exec();
|
|
94
94
|
// Record setMany command sent to Redis
|
|
95
95
|
cacheMetrics.redisCommandsSentCount
|
|
96
96
|
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'exec' })
|
|
Binary file
|
package/config/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export declare const BaseSettings: {
|
|
2
|
+
readonly API_ENDPOINT: {
|
|
3
|
+
readonly description: "The URL that the certain transports use to retrieve data";
|
|
4
|
+
readonly type: "string";
|
|
5
|
+
};
|
|
2
6
|
readonly API_KEY: {
|
|
3
7
|
readonly description: "Default setting for an EA key";
|
|
4
8
|
readonly type: "string";
|
|
@@ -73,7 +77,7 @@ export declare const BaseSettings: {
|
|
|
73
77
|
};
|
|
74
78
|
readonly WARMUP_SUBSCRIPTION_TTL: {
|
|
75
79
|
readonly type: "number";
|
|
76
|
-
readonly description: "";
|
|
80
|
+
readonly description: "TTL for batch warmer subscriptions";
|
|
77
81
|
readonly default: 10000;
|
|
78
82
|
};
|
|
79
83
|
readonly CACHE_POLLING_MAX_RETRIES: {
|
|
@@ -112,6 +116,16 @@ export declare const BaseSettings: {
|
|
|
112
116
|
readonly type: "number";
|
|
113
117
|
readonly default: 400;
|
|
114
118
|
};
|
|
119
|
+
readonly MAX_COMMON_KEY_SIZE: {
|
|
120
|
+
readonly description: "Maximum amount of characters that the common part of the cache key or feed ID can have";
|
|
121
|
+
readonly type: "number";
|
|
122
|
+
readonly default: 300;
|
|
123
|
+
readonly validate: (value?: number) => "MAX_COMMON_KEY_SIZE must be a number between 150 and 500" | undefined;
|
|
124
|
+
};
|
|
125
|
+
readonly SMOKE_TEST_PAYLOAD_FILE_NAME: {
|
|
126
|
+
readonly description: "Name of the test payload file used for the smoke endpoint";
|
|
127
|
+
readonly type: "string";
|
|
128
|
+
};
|
|
115
129
|
};
|
|
116
130
|
export declare const buildAdapterConfig: <CustomSettings extends CustomSettingsType<CustomSettings> = EmptySettings>({ overrides, customSettings, }: {
|
|
117
131
|
overrides?: Partial<BaseAdapterConfig> | undefined;
|
package/config/index.js
CHANGED
|
@@ -5,9 +5,10 @@ exports.buildAdapterConfig = exports.BaseSettings = void 0;
|
|
|
5
5
|
exports.BaseSettings = {
|
|
6
6
|
// V2 compat
|
|
7
7
|
// TODO: Remove non used in v3 ones
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
API_ENDPOINT: {
|
|
9
|
+
description: 'The URL that the certain transports use to retrieve data',
|
|
10
|
+
type: 'string',
|
|
11
|
+
},
|
|
11
12
|
API_KEY: {
|
|
12
13
|
description: 'Default setting for an EA key',
|
|
13
14
|
type: 'string',
|
|
@@ -214,7 +215,7 @@ exports.BaseSettings = {
|
|
|
214
215
|
// },
|
|
215
216
|
WARMUP_SUBSCRIPTION_TTL: {
|
|
216
217
|
type: 'number',
|
|
217
|
-
description: '',
|
|
218
|
+
description: 'TTL for batch warmer subscriptions',
|
|
218
219
|
default: 10000,
|
|
219
220
|
},
|
|
220
221
|
// WARMUP_UNHEALTHY_THRESHOLD: {
|
|
@@ -306,10 +307,24 @@ exports.BaseSettings = {
|
|
|
306
307
|
type: 'number',
|
|
307
308
|
default: 400,
|
|
308
309
|
},
|
|
310
|
+
MAX_COMMON_KEY_SIZE: {
|
|
311
|
+
description: 'Maximum amount of characters that the common part of the cache key or feed ID can have',
|
|
312
|
+
type: 'number',
|
|
313
|
+
default: 300,
|
|
314
|
+
validate: (value) => {
|
|
315
|
+
if (!(value && value >= 150 && value <= 500)) {
|
|
316
|
+
return 'MAX_COMMON_KEY_SIZE must be a number between 150 and 500';
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
},
|
|
309
320
|
// CACHE_KEY_IGNORED_PROPS : {
|
|
310
321
|
// description: 'Properties to ignore when generating a feed ID for requests',
|
|
311
322
|
// type: 'string'
|
|
312
323
|
// }
|
|
324
|
+
SMOKE_TEST_PAYLOAD_FILE_NAME: {
|
|
325
|
+
description: 'Name of the test payload file used for the smoke endpoint',
|
|
326
|
+
type: 'string',
|
|
327
|
+
},
|
|
313
328
|
};
|
|
314
329
|
const buildAdapterConfig = ({ overrides = {}, customSettings = {}, }) => {
|
|
315
330
|
const validationErrors = [];
|
|
@@ -42,7 +42,11 @@ const getProviderLimits = (provider, limits, tier, protocol) => {
|
|
|
42
42
|
return limitsConfig;
|
|
43
43
|
};
|
|
44
44
|
const parseLimits = (limits) => {
|
|
45
|
-
const _mapObject = (fn) => (o) =>
|
|
45
|
+
const _mapObject = (fn) => (o) => {
|
|
46
|
+
const entries = Object.entries(o);
|
|
47
|
+
const mapped = entries.map(fn);
|
|
48
|
+
return Object.fromEntries(mapped);
|
|
49
|
+
};
|
|
46
50
|
const _formatProtocol = _mapObject((entry) => {
|
|
47
51
|
const [tierName, rest] = entry;
|
|
48
52
|
return [tierName.toLowerCase(), { ...rest }];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import { AdapterInputParameters, BankFrickAccountsRequestSchema, BankFrickAccountsResponseSchema, SigningAlgorithm } from './types';
|
|
3
|
+
import { customSettings } from './config';
|
|
4
|
+
import { Transport } from '../../transports';
|
|
5
|
+
import { AdapterRequest, AdapterResponse } from '../../util';
|
|
6
|
+
import { AdapterConfig } from '../../config';
|
|
7
|
+
import { Cache } from '../../cache';
|
|
8
|
+
import { AdapterDependencies, AdapterEndpoint } from '../../adapter';
|
|
9
|
+
/**
|
|
10
|
+
* RestTransport implementation for Bank Frick, which has unusually complex requirements for an EA
|
|
11
|
+
* The RestTransport is generally built to make a single request and return a single response.
|
|
12
|
+
* This transport instead is used to fetch and process pages of data, and also requires a JWT to run
|
|
13
|
+
*
|
|
14
|
+
* This transport does all the heavy lifting in setup(), which is where the paging happens, and it
|
|
15
|
+
* also has complex retry logic that will attempt to refresh the JWT when certain HTTP errors occur
|
|
16
|
+
*/
|
|
17
|
+
export declare class BankFrickAccountsTransport implements Transport<AdapterInputParameters, number, typeof customSettings> {
|
|
18
|
+
token: string;
|
|
19
|
+
cache: Cache;
|
|
20
|
+
initialize(dependencies: AdapterDependencies): Promise<void>;
|
|
21
|
+
hasBeenSetUp(): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an AxiosRequestConfig object for fetching a page of accounts from the Bank Frick API
|
|
24
|
+
*/
|
|
25
|
+
prepareRequest(firstPosition: number, inputParams: AdapterInputParameters, config: AdapterConfig<typeof customSettings>): AxiosRequestConfig<BankFrickAccountsRequestSchema>;
|
|
26
|
+
/**
|
|
27
|
+
* Request with retry logic for Bank Frick's API. In addition to standard retry logic, this function
|
|
28
|
+
* compares errors against expected errors from the Bank Frick API, and will throw without retries
|
|
29
|
+
* on a known fatal error, or try to refresh the JWT on a known auth error
|
|
30
|
+
**/
|
|
31
|
+
makeRequest(axiosRequest: AxiosRequestConfig<BankFrickAccountsRequestSchema>, config: AdapterConfig<typeof customSettings>, signingAlgorithm?: SigningAlgorithm): Promise<AxiosResponse<BankFrickAccountsResponseSchema>>;
|
|
32
|
+
validateInputParams(params: AdapterInputParameters): string[];
|
|
33
|
+
/**
|
|
34
|
+
* Fetches pages of data from the Bank Frick API, scans for accounts by IBAN, and returns the balance
|
|
35
|
+
* of all found accounts. Returns a 404 if any IBAN isn't found.
|
|
36
|
+
*/
|
|
37
|
+
setup(req: AdapterRequest<AdapterInputParameters>, config: AdapterConfig<typeof customSettings>): Promise<AdapterResponse<number>>;
|
|
38
|
+
}
|
|
39
|
+
export declare const accountsRestEndpoint: AdapterEndpoint;
|
|
@@ -0,0 +1,191 @@
|
|
|
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.accountsRestEndpoint = exports.BankFrickAccountsTransport = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const util_1 = require("./util");
|
|
9
|
+
const util_2 = require("../../util");
|
|
10
|
+
const error_1 = require("../../validation/error");
|
|
11
|
+
const logger = (0, util_2.makeLogger)('BankFrickTransport');
|
|
12
|
+
// Note: this is a shallow pattern that only checks for a country code since IBANs in the sandbox are invalid
|
|
13
|
+
const ibanPattern = /^[A-Z]{2}[A-Z\d]{14,30}$/;
|
|
14
|
+
const inputParameters = {
|
|
15
|
+
ibanIDs: {
|
|
16
|
+
description: 'The list of account ids included in the sum of balances',
|
|
17
|
+
required: true,
|
|
18
|
+
type: 'array',
|
|
19
|
+
},
|
|
20
|
+
signingAlgorithm: {
|
|
21
|
+
description: 'What signing algorithm is used to sign and verify authorization data, one of rsa-sha256, rsa-sha384, or rsa-sha512',
|
|
22
|
+
required: false,
|
|
23
|
+
type: 'string',
|
|
24
|
+
default: 'rsa-sha512',
|
|
25
|
+
options: ['rsa-sha256', 'rsa-sha384', 'rsa-sha512'],
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
// See here for all expected error returned by the API: https://developers.bankfrick.li/docs#errors
|
|
29
|
+
const AuthErrors = {
|
|
30
|
+
401: 'No JWT token provided or token is invalid',
|
|
31
|
+
403: 'API key is invalid or any other condition is hindering the login', // Unclear if this is fatal or not
|
|
32
|
+
};
|
|
33
|
+
const FatalErrors = {
|
|
34
|
+
400: "Invalid parameters passed to Bank Frick's API",
|
|
35
|
+
423: "Authorization is valid, but the user's account is locked",
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* RestTransport implementation for Bank Frick, which has unusually complex requirements for an EA
|
|
39
|
+
* The RestTransport is generally built to make a single request and return a single response.
|
|
40
|
+
* This transport instead is used to fetch and process pages of data, and also requires a JWT to run
|
|
41
|
+
*
|
|
42
|
+
* This transport does all the heavy lifting in setup(), which is where the paging happens, and it
|
|
43
|
+
* also has complex retry logic that will attempt to refresh the JWT when certain HTTP errors occur
|
|
44
|
+
*/
|
|
45
|
+
class BankFrickAccountsTransport {
|
|
46
|
+
async initialize(dependencies) {
|
|
47
|
+
this.cache = dependencies.cache;
|
|
48
|
+
}
|
|
49
|
+
async hasBeenSetUp() {
|
|
50
|
+
return false; // Return false since we aren't coalescing requests
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates an AxiosRequestConfig object for fetching a page of accounts from the Bank Frick API
|
|
54
|
+
*/
|
|
55
|
+
prepareRequest(firstPosition, inputParams, config) {
|
|
56
|
+
const { API_ENDPOINT, BASE_URL, PAGE_SIZE } = config;
|
|
57
|
+
return {
|
|
58
|
+
baseURL: API_ENDPOINT + BASE_URL,
|
|
59
|
+
url: 'accounts',
|
|
60
|
+
method: 'GET',
|
|
61
|
+
params: {
|
|
62
|
+
firstPosition,
|
|
63
|
+
maxResults: PAGE_SIZE,
|
|
64
|
+
},
|
|
65
|
+
headers: {
|
|
66
|
+
Authorization: `Bearer ${this.token}`,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Request with retry logic for Bank Frick's API. In addition to standard retry logic, this function
|
|
72
|
+
* compares errors against expected errors from the Bank Frick API, and will throw without retries
|
|
73
|
+
* on a known fatal error, or try to refresh the JWT on a known auth error
|
|
74
|
+
**/
|
|
75
|
+
async makeRequest(axiosRequest, config, signingAlgorithm) {
|
|
76
|
+
let retryNumber = 0;
|
|
77
|
+
let response = await axios_1.default.request(axiosRequest);
|
|
78
|
+
while (response.status !== 200) {
|
|
79
|
+
retryNumber++;
|
|
80
|
+
logger.warn('Encountered error when fetching accounts from Bank Frick:', response.status, response.statusText);
|
|
81
|
+
// Evaluate whether the error was fatal, auth, or transient and whether we've exceeded the max number of retries.
|
|
82
|
+
// Throw on fatal, refresh token on auth error, pass on transient until we've exhausted our retries
|
|
83
|
+
if (FatalErrors[response.status]) {
|
|
84
|
+
throw new error_1.AdapterError({
|
|
85
|
+
statusCode: response.status,
|
|
86
|
+
message: response.statusText,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else if (AuthErrors[response.status]) {
|
|
90
|
+
// We've encountered a known auth error, so try to refresh the token before making another request
|
|
91
|
+
logger.info('Auth error received from the Bank Frick API, attempting to refresh the token');
|
|
92
|
+
this.token = await (0, util_1.generateJWT)(config, signingAlgorithm);
|
|
93
|
+
}
|
|
94
|
+
else if (retryNumber === config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES) {
|
|
95
|
+
throw new error_1.AdapterError({
|
|
96
|
+
statusCode: 504,
|
|
97
|
+
message: `Bank Frick transport hit the max number of retries (${config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES} retries) and aborted`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
logger.debug(`Sleeping for ${config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES}ms before retrying`);
|
|
101
|
+
await (0, util_2.sleep)(config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES);
|
|
102
|
+
response = await axios_1.default.request(axiosRequest);
|
|
103
|
+
}
|
|
104
|
+
return response;
|
|
105
|
+
}
|
|
106
|
+
validateInputParams(params) {
|
|
107
|
+
const encounteredIds = {};
|
|
108
|
+
const errors = [];
|
|
109
|
+
const { ibanIDs } = params;
|
|
110
|
+
ibanIDs.forEach((v) => {
|
|
111
|
+
if (!v.match(ibanPattern)) {
|
|
112
|
+
errors.push(`Invalid IBAN: ${v}`);
|
|
113
|
+
}
|
|
114
|
+
encounteredIds[v] += 1;
|
|
115
|
+
if (encounteredIds[v] > 1) {
|
|
116
|
+
errors.push(`The following IBAN appears more than once in the input parameters: ${v}`);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return errors;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Fetches pages of data from the Bank Frick API, scans for accounts by IBAN, and returns the balance
|
|
123
|
+
* of all found accounts. Returns a 404 if any IBAN isn't found.
|
|
124
|
+
*/
|
|
125
|
+
async setup(req, config) {
|
|
126
|
+
const { ibanIDs, signingAlgorithm } = req.requestContext.data;
|
|
127
|
+
const { PAGE_SIZE = 500 } = config;
|
|
128
|
+
logger.debug(`Validating input: ${JSON.stringify(req.requestContext.data)}`);
|
|
129
|
+
// Scan ibanIDs for duplicates and invalid IBANs
|
|
130
|
+
const validationErrors = this.validateInputParams(req.requestContext.data);
|
|
131
|
+
if (validationErrors.length > 0) {
|
|
132
|
+
throw new error_1.AdapterError({
|
|
133
|
+
statusCode: 420,
|
|
134
|
+
message: `Received the following errors when validating inputParameters:\n ${validationErrors.join('\n')}`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
// Refresh the token if it isn't set
|
|
138
|
+
if (!this.token) {
|
|
139
|
+
this.token = await (0, util_1.generateJWT)(config, signingAlgorithm);
|
|
140
|
+
}
|
|
141
|
+
let sum = 0;
|
|
142
|
+
let position = 0;
|
|
143
|
+
const keys = ibanIDs;
|
|
144
|
+
logger.info("Fetching accounts from Bank Frick's API...");
|
|
145
|
+
while (keys.length > 0) {
|
|
146
|
+
// TODO Fetching and processing pages can be run concurrently
|
|
147
|
+
const axiosRequest = this.prepareRequest(position, req.requestContext.data, config);
|
|
148
|
+
const response = await this.makeRequest(axiosRequest, config, signingAlgorithm);
|
|
149
|
+
logger.debug(`Evaluating accounts from page ${position / PAGE_SIZE}`);
|
|
150
|
+
response.data.accounts.forEach((v) => {
|
|
151
|
+
logger.trace(`Evaluating ${v.account} (iban: ${v.iban}, type: ${v.type})`);
|
|
152
|
+
const index = ibanIDs.indexOf(v.iban);
|
|
153
|
+
if (index > -1) {
|
|
154
|
+
keys.splice(index, 1);
|
|
155
|
+
sum += v.balance;
|
|
156
|
+
logger.trace(`Found account: ${v.account} (iban: ${v.iban}) with balance of ${v.balance}`);
|
|
157
|
+
logger.trace(`Running sum: ${sum}, number of ibans left to find: ${keys.length}/${ibanIDs.length}`);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
if (!response.data.moreResults) {
|
|
161
|
+
logger.debug('No more results, breaking out of account query loop');
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
position += PAGE_SIZE || 0;
|
|
165
|
+
}
|
|
166
|
+
// 404 if one or more accounts were not found
|
|
167
|
+
if (keys.length > 0) {
|
|
168
|
+
logger.error(`Could not find all accounts, returning 404: ${keys.join(', ')}`);
|
|
169
|
+
const res = {
|
|
170
|
+
data: 0,
|
|
171
|
+
statusCode: 404,
|
|
172
|
+
result: 0,
|
|
173
|
+
};
|
|
174
|
+
await this.cache.set(req.requestContext.cacheKey, res, config.CACHE_MAX_AGE);
|
|
175
|
+
}
|
|
176
|
+
logger.debug('Was able to find all accounts, returning balance across all accounts: ', sum);
|
|
177
|
+
const res = {
|
|
178
|
+
data: sum,
|
|
179
|
+
statusCode: 200,
|
|
180
|
+
result: sum,
|
|
181
|
+
};
|
|
182
|
+
await this.cache.set(req.requestContext.cacheKey, res, config.CACHE_MAX_AGE);
|
|
183
|
+
return res;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
exports.BankFrickAccountsTransport = BankFrickAccountsTransport;
|
|
187
|
+
exports.accountsRestEndpoint = {
|
|
188
|
+
name: 'accounts',
|
|
189
|
+
transport: new BankFrickAccountsTransport(),
|
|
190
|
+
inputParameters,
|
|
191
|
+
};
|
|
@@ -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.customSettings = exports.signingAlgorithms = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const util_1 = require("../../../util");
|
|
9
|
+
const logger = (0, util_1.makeLogger)('BankFrickConfig');
|
|
10
|
+
// Used for enum options and validation
|
|
11
|
+
exports.signingAlgorithms = ['rsa-sha256', 'rsa-sha384', 'rsa-sha512'];
|
|
12
|
+
const MAX_PAGE_SIZE = 500;
|
|
13
|
+
exports.customSettings = {
|
|
14
|
+
PAGE_SIZE: {
|
|
15
|
+
description: 'The number of accounts to fetch per call to /accounts. Must be >= 1 and <= 500.',
|
|
16
|
+
type: 'number',
|
|
17
|
+
required: false,
|
|
18
|
+
default: MAX_PAGE_SIZE,
|
|
19
|
+
validate: (value) => {
|
|
20
|
+
if (!value) {
|
|
21
|
+
return '';
|
|
22
|
+
}
|
|
23
|
+
else if (value < 1) {
|
|
24
|
+
return `PAGE_SIZE must be at least >= 1, was ${value}`;
|
|
25
|
+
}
|
|
26
|
+
else if (value > MAX_PAGE_SIZE) {
|
|
27
|
+
return `PAGE_SIZE must be <= 500, was ${value}`;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
PRIVATE_KEY: {
|
|
32
|
+
description: '',
|
|
33
|
+
type: 'string',
|
|
34
|
+
required: true,
|
|
35
|
+
validate: (value) => {
|
|
36
|
+
const failedAlgos = [];
|
|
37
|
+
for (const algorithm of exports.signingAlgorithms) {
|
|
38
|
+
const body = { example: 123 };
|
|
39
|
+
try {
|
|
40
|
+
crypto_1.default.sign(algorithm, Buffer.from(JSON.stringify(body)), value);
|
|
41
|
+
logger.trace("Successfully tested PRIVATE_KEY by signing with algorithm '%s'", algorithm);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
logger.trace("PRIVATE_KEY failed to sign message with algorithm '%s'", algorithm);
|
|
45
|
+
failedAlgos.push(algorithm);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (failedAlgos.length > 0) {
|
|
49
|
+
return `Failed to sign a dummy body using $PRIVATE_KEY with the following algorithms ${failedAlgos.join(',')}`;
|
|
50
|
+
}
|
|
51
|
+
logger.debug('$PRIVATE_KEY successfully signed a dummy body with all supported algorithms');
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.adapter = void 0;
|
|
4
|
+
const accounts_1 = require("./accounts");
|
|
5
|
+
const config_1 = require("./config");
|
|
6
|
+
exports.adapter = {
|
|
7
|
+
name: 'bank-frick',
|
|
8
|
+
defaultEndpoint: 'accounts',
|
|
9
|
+
endpoints: [accounts_1.accountsRestEndpoint],
|
|
10
|
+
customSettings: config_1.customSettings,
|
|
11
|
+
envDefaultOverrides: {
|
|
12
|
+
API_ENDPOINT: 'https://olbsandbox.bankfrick.li/webapi/v2',
|
|
13
|
+
},
|
|
14
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { SigningAlgorithm } from './types';
|
|
2
|
+
import { customSettings } from './config';
|
|
3
|
+
import { AdapterConfig } from '../../config';
|
|
4
|
+
export declare const generateJWT: (config: AdapterConfig<typeof customSettings>, signingAlgorithm?: SigningAlgorithm) => Promise<string>;
|