@steerprotocol/data-connector-loader 0.0.3 → 0.0.5

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.
@@ -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 arguements at synced block
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
@@ -8,12 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.DataConnectorExecution = void 0;
16
- const axios_1 = __importDefault(require("axios"));
13
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
14
+ const axios = require('axios');
15
+ const pluginManager_1 = require("./pluginManager");
17
16
  class DataConnectorExecution {
18
17
  constructor(dataConnector) {
19
18
  this.dataConnector = dataConnector;
@@ -29,6 +28,8 @@ class DataConnectorExecution {
29
28
  // Each data connector results will be pushed into single array
30
29
  // they will passed to execution bundle
31
30
  const transformationData = [];
31
+ const pluginManager = new pluginManager_1.PluginManager();
32
+ yield pluginManager.registerFromPublicConfig();
32
33
  // Iterate over each data connector
33
34
  for (const connectorConfig of dataConnectorConfigs) {
34
35
  // If bundle hash is not present, skip the data connector
@@ -51,14 +52,14 @@ class DataConnectorExecution {
51
52
  let request = yield this.dataConnector.execute('');
52
53
  let response;
53
54
  while (request != 'true') {
54
- const parsedRequest = JSON.parse(request);
55
+ const parsedRequest = yield pluginManager.process(JSON.parse(request));
55
56
  // On Chain
56
57
  if (this.dataConnector.configObj.isChainRead == true) {
57
58
  response = yield this.dataConnector.view(parsedRequest);
58
59
  }
59
60
  else {
60
61
  // Axios logic (off-chain)
61
- const ax_instance = axios_1.default.create();
62
+ const ax_instance = axios.create();
62
63
  response = (yield ax_instance.request(parsedRequest)).data;
63
64
  }
64
65
  // break;
@@ -72,7 +73,7 @@ class DataConnectorExecution {
72
73
  }
73
74
  downloadWasmBundle(ipfsHash) {
74
75
  return __awaiter(this, void 0, void 0, function* () {
75
- const response = yield axios_1.default.get(`https://steerprotocol.mypinata.cloud/ipfs/${ipfsHash}`, {
76
+ const response = yield axios.get(`https://steerprotocol.mypinata.cloud/ipfs/${ipfsHash}`, {
76
77
  responseType: 'arraybuffer',
77
78
  });
78
79
  const wasmFile = new Uint8Array(response.data);
@@ -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",
3
+ "version": "0.0.5",
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.2",
51
+ "axios": "^1.3.4",
52
52
  "ethers": "^6.1.0",
53
53
  "pondjs": "1.0.0-alpha.10"
54
54
  }