@steerprotocol/data-connector-loader 0.0.3 → 0.0.4
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/lib/dataConnector.js +1 -1
- package/lib/index.js +4 -1
- package/lib/pluginManager.d.ts +11 -0
- package/lib/pluginManager.js +84 -0
- package/package.json +2 -2
package/lib/dataConnector.js
CHANGED
|
@@ -68,7 +68,7 @@ class DataConnector {
|
|
|
68
68
|
// Calculate block number for determinism
|
|
69
69
|
// Best way to handle contextually non-null values?
|
|
70
70
|
const syncedBlock = yield (0, blockFinder_1.getBlockFromTimestamp)(this.executionContext.epochTimestamp, this.providerIdentity); //@note uncomment for deterministic blocks
|
|
71
|
-
// Call the desired method with the specified
|
|
71
|
+
// Call the desired method with the specified arguments at synced block
|
|
72
72
|
const result = yield contract[config.method](...paramsArray, { blockTag: syncedBlock }); //@note uncomment for deterministic blocks
|
|
73
73
|
// This gets stringified before calling main
|
|
74
74
|
return result;
|
package/lib/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.DataConnectorExecution = void 0;
|
|
16
16
|
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
const pluginManager_1 = require("./pluginManager");
|
|
17
18
|
class DataConnectorExecution {
|
|
18
19
|
constructor(dataConnector) {
|
|
19
20
|
this.dataConnector = dataConnector;
|
|
@@ -29,6 +30,8 @@ class DataConnectorExecution {
|
|
|
29
30
|
// Each data connector results will be pushed into single array
|
|
30
31
|
// they will passed to execution bundle
|
|
31
32
|
const transformationData = [];
|
|
33
|
+
const pluginManager = new pluginManager_1.PluginManager();
|
|
34
|
+
yield pluginManager.registerFromPublicConfig();
|
|
32
35
|
// Iterate over each data connector
|
|
33
36
|
for (const connectorConfig of dataConnectorConfigs) {
|
|
34
37
|
// If bundle hash is not present, skip the data connector
|
|
@@ -51,7 +54,7 @@ class DataConnectorExecution {
|
|
|
51
54
|
let request = yield this.dataConnector.execute('');
|
|
52
55
|
let response;
|
|
53
56
|
while (request != 'true') {
|
|
54
|
-
const parsedRequest = JSON.parse(request);
|
|
57
|
+
const parsedRequest = yield pluginManager.process(JSON.parse(request));
|
|
55
58
|
// On Chain
|
|
56
59
|
if (this.dataConnector.configObj.isChainRead == true) {
|
|
57
60
|
response = yield this.dataConnector.view(parsedRequest);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export declare class PluginManager {
|
|
3
|
+
private registeredPluginConfigs;
|
|
4
|
+
constructor();
|
|
5
|
+
registerFromPublicConfig(): Promise<void>;
|
|
6
|
+
registerPluginConfig(name: string, domain: string, headers: object): void;
|
|
7
|
+
private findMatchingPluginConfigs;
|
|
8
|
+
private interpolateEnvVars;
|
|
9
|
+
private interpolateHeaders;
|
|
10
|
+
process(config: AxiosRequestConfig): Promise<AxiosRequestConfig>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.PluginManager = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
class PluginManager {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.registeredPluginConfigs = [];
|
|
20
|
+
}
|
|
21
|
+
registerFromPublicConfig() {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const { data: dataConnectorPlugins } = yield axios_1.default.get('https://igk02ovpui.execute-api.us-east-1.amazonaws.com/data-connector-plugins');
|
|
24
|
+
for (const plugin of dataConnectorPlugins) {
|
|
25
|
+
this.registerPluginConfig(plugin.name, plugin.domain, plugin.headers);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
registerPluginConfig(name, domain, headers) {
|
|
30
|
+
this.registeredPluginConfigs.push({ name, domain, headers });
|
|
31
|
+
}
|
|
32
|
+
findMatchingPluginConfigs(url) {
|
|
33
|
+
const matchingHeaders = {};
|
|
34
|
+
for (const pluginConfig of this.registeredPluginConfigs) {
|
|
35
|
+
if (url.includes(pluginConfig.domain)) {
|
|
36
|
+
Object.assign(matchingHeaders, pluginConfig.headers);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return matchingHeaders;
|
|
40
|
+
}
|
|
41
|
+
interpolateEnvVars(str) {
|
|
42
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_match, envVar) => {
|
|
43
|
+
const envValue = process.env[envVar];
|
|
44
|
+
if (envValue === undefined) {
|
|
45
|
+
console.log(`Environment variable '${envVar}' not found.`);
|
|
46
|
+
return '';
|
|
47
|
+
}
|
|
48
|
+
return envValue;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
interpolateHeaders(headers) {
|
|
52
|
+
const interpolatedHeaders = {};
|
|
53
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
54
|
+
interpolatedHeaders[key] = typeof value === 'string' ? this.interpolateEnvVars(value) : value;
|
|
55
|
+
}
|
|
56
|
+
return interpolatedHeaders;
|
|
57
|
+
}
|
|
58
|
+
process(config) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
if (!config.url) {
|
|
61
|
+
throw new Error('Request URL is not provided');
|
|
62
|
+
}
|
|
63
|
+
const matchingHeaders = this.findMatchingPluginConfigs(config.url);
|
|
64
|
+
const interpolatedHeaders = this.interpolateHeaders(matchingHeaders);
|
|
65
|
+
const updatedHeaders = Object.assign(Object.assign({}, config.headers), interpolatedHeaders);
|
|
66
|
+
const updatedConfig = Object.assign(Object.assign({}, config), { headers: updatedHeaders });
|
|
67
|
+
return updatedConfig;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.PluginManager = PluginManager;
|
|
72
|
+
// // Usage example:
|
|
73
|
+
// (async () => {
|
|
74
|
+
// const headerMerger = new PluginManager();
|
|
75
|
+
// headerMerger.registerPluginConfig('Example API', 'example.com', { 'X-Custom-Header': '{{MY_ENV_VAR}}' });
|
|
76
|
+
// process.env['MY_ENV_VAR'] = 'ExampleValue';
|
|
77
|
+
// const requestConfig: AxiosRequestConfig = {
|
|
78
|
+
// url: 'https://example.com/api/some-resource',
|
|
79
|
+
// method: 'GET',
|
|
80
|
+
// };
|
|
81
|
+
// const updatedRequestConfig = await headerMerger.process(requestConfig);
|
|
82
|
+
// const response = await axios(updatedRequestConfig);
|
|
83
|
+
// console.log(response.data);
|
|
84
|
+
// })();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steerprotocol/data-connector-loader",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "App Loader for Steer Protocol",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@steerprotocol/app-loader": "0.1.6",
|
|
50
50
|
"@steerprotocol/strategy-utils": "^2.1.0",
|
|
51
|
-
"axios": "^1.3.
|
|
51
|
+
"axios": "^1.3.4",
|
|
52
52
|
"ethers": "^6.1.0",
|
|
53
53
|
"pondjs": "1.0.0-alpha.10"
|
|
54
54
|
}
|