From 662f48d6ee46f7eee98426db2ac9bbbe7a2c0051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Sat, 25 Nov 2023 10:41:31 +0100 Subject: [PATCH] Implement processing v2 --- packages/e2e/src/pusher/pusher.json | 8 +-- packages/pusher/config/pusher.example.json | 8 +-- packages/pusher/package.json | 2 +- .../pusher/src/api-requests/data-provider.ts | 21 ++++---- packages/pusher/src/validation/schema.ts | 6 +-- pnpm-lock.yaml | 54 ++++++++++++++++++- 6 files changed, 75 insertions(+), 24 deletions(-) diff --git a/packages/e2e/src/pusher/pusher.json b/packages/e2e/src/pusher/pusher.json index bd4e6bdd..9097c3a1 100644 --- a/packages/e2e/src/pusher/pusher.json +++ b/packages/e2e/src/pusher/pusher.json @@ -87,17 +87,17 @@ { "name": "_type", "fixed": "int256" }, { "name": "_times", "fixed": "1000000000000000000" } ], - "preProcessingSpecifications": [ + "preProcessingSpecificationV2": [ { "environment": "Node", - "value": "const output = {};", + "value": "() => { return { endpointParameters: {} }; }", "timeoutMs": 5000 } ], - "postProcessingSpecifications": [ + "postProcessingSpecificationV2": [ { "environment": "Node", - "value": "const output = input[endpointParameters.name];", + "value": "({ endpointParameters, response }) => { return { response: response[endpointParameters.name].value }; }", "timeoutMs": 5000 } ] diff --git a/packages/pusher/config/pusher.example.json b/packages/pusher/config/pusher.example.json index 2b291f6d..33e0a88b 100644 --- a/packages/pusher/config/pusher.example.json +++ b/packages/pusher/config/pusher.example.json @@ -67,17 +67,17 @@ { "name": "_type", "fixed": "int256" }, { "name": "_times", "fixed": "1000000000000000000" } ], - "preProcessingSpecifications": [ + "preProcessingSpecificationV2": [ { "environment": "Node", - "value": "const output = {};", + "value": "() => { return { endpointParameters: {} }; }", "timeoutMs": 5000 } ], - "postProcessingSpecifications": [ + "postProcessingSpecificationV2": [ { "environment": "Node", - "value": "const output = input[endpointParameters.name].value;", + "value": "({ endpointParameters, response }) => { return { response: response[endpointParameters.name].value }; }", "timeoutMs": 5000 } ] diff --git a/packages/pusher/package.json b/packages/pusher/package.json index ed7c360d..ac3fd997 100644 --- a/packages/pusher/package.json +++ b/packages/pusher/package.json @@ -25,7 +25,7 @@ "@api3/airnode-adapter": "^0.12.0", "@api3/airnode-node": "^0.12.0", "@api3/airnode-validator": "^0.12.0", - "@api3/commons": "^0.3.0", + "@api3/commons": "file:../../../commons", "@api3/ois": "^2.2.1", "@api3/promise-utils": "^0.4.0", "axios": "^1.6.0", diff --git a/packages/pusher/src/api-requests/data-provider.ts b/packages/pusher/src/api-requests/data-provider.ts index c122102e..2db52844 100644 --- a/packages/pusher/src/api-requests/data-provider.ts +++ b/packages/pusher/src/api-requests/data-provider.ts @@ -1,7 +1,7 @@ import { type BuildRequestOptions, buildAndExecuteRequest, extractAndEncodeResponse } from '@api3/airnode-adapter'; import type * as node from '@api3/airnode-node'; import { getReservedParameters } from '@api3/airnode-node/dist/src/adapters/http/parameters'; -import { preProcessApiCallParameters, type ApiCallParameters, postProcessApiCallResponse } from '@api3/commons'; +import { preProcessEndpointParameters, type EndpointParameters, postProcessResponse } from '@api3/commons'; import type { OIS, Endpoint as OisEndpoint } from '@api3/ois'; import { go, goSync } from '@api3/promise-utils'; import { isNil } from 'lodash'; @@ -15,14 +15,17 @@ import type { SignedApiUpdate } from '../validation/schema'; export const callApi = async ( ois: OIS, endpoint: OisEndpoint, - apiCallParameters: ApiCallParameters, + endpointParameters: EndpointParameters, apiCredentials: node.ApiCredentials[] ) => { return go(async () => { const logContext = { endpointName: endpoint.name, oisTitle: ois.title }; logger.debug('Preprocessing API call payload', logContext); - const processedApiCallParameters = await preProcessApiCallParameters(endpoint, apiCallParameters); - logger.debug('Performing API call', { ...logContext, processedApiCallParameters }); + const { endpointParameters: processedEndpointParameters } = await preProcessEndpointParameters( + endpoint, + endpointParameters + ); + logger.debug('Performing API call', { ...logContext, processedEndpointParameters }); const response = await buildAndExecuteRequest( { @@ -53,14 +56,14 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr const operationEndpoint = endpoints[operationTemplate.endpointId]!; const ois = oises.find((o) => o.title === operationEndpoint.oisTitle)!; const operationOisEndpoint = ois.endpoints.find((e) => e.name === operationEndpoint.endpointName)!; - const operationApiCallParameters = operationTemplate.parameters.reduce((acc, parameter) => { + const endpointParameters = operationTemplate.parameters.reduce((acc, parameter) => { return { ...acc, [parameter.name]: parameter.value, }; }, {}); - const goCallApi = await callApi(ois, operationOisEndpoint, operationApiCallParameters, apiCredentials); + const goCallApi = await callApi(ois, operationOisEndpoint, endpointParameters, apiCredentials); if (!goCallApi.success) { logger.warn(`Failed to make API call`, { @@ -77,7 +80,7 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr const endpoint = endpoints[template.endpointId]!; const oisEndpoint = ois.endpoints.find((e) => e.name === endpoint.endpointName)!; - const apiCallParameters = template.parameters.reduce((acc, parameter) => { + const endpointParameters = template.parameters.reduce((acc, parameter) => { return { ...acc, [parameter.name]: parameter.value, @@ -85,9 +88,7 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr }, {}); logger.debug('Processing successful API call', { templateId, operationTemplateId }); - const goPostProcess = await go(async () => - postProcessApiCallResponse(apiCallResponse, oisEndpoint, apiCallParameters) - ); + const goPostProcess = await go(async () => postProcessResponse(apiCallResponse, oisEndpoint, endpointParameters)); if (!goPostProcess.success) { const message = `Failed to post process successful API call`; logger.warn(message, { templateId, operationTemplateId, errorMessage: goPostProcess.error.message }); diff --git a/packages/pusher/src/validation/schema.ts b/packages/pusher/src/validation/schema.ts index 8ddd0033..8fbac0ad 100644 --- a/packages/pusher/src/validation/schema.ts +++ b/packages/pusher/src/validation/schema.ts @@ -5,7 +5,7 @@ import { type LogLevel, logFormatOptions, logLevelOptions, - preProcessApiCallParameters, + preProcessEndpointParameters, } from '@api3/commons'; import { oisSchema, type OIS, type Endpoint as oisEndpoint } from '@api3/ois'; import { goSync } from '@api3/promise-utils'; @@ -174,14 +174,14 @@ const validateTriggerReferences: SuperRefinement<{ const ois = oises.find((o) => o.title === endpoint.oisTitle)!; const oisEndpoint = ois.endpoints.find((e) => e.name === endpoint.endpointName)!; - const apiCallParameters = template.parameters.reduce((acc, parameter) => { + const endpointParameters = template.parameters.reduce((acc, parameter) => { return { ...acc, [parameter.name]: parameter.value, }; }, {}); - return preProcessApiCallParameters(oisEndpoint, apiCallParameters); + return preProcessEndpointParameters(oisEndpoint, endpointParameters); }); const operationsPayloads = await Promise.all(operationPayloadPromises); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ba7a2e3..e025d577 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -137,8 +137,8 @@ importers: specifier: ^0.12.0 version: 0.12.0 '@api3/commons': - specifier: ^0.3.0 - version: 0.3.0(eslint@8.51.0)(jest@29.7.0)(typescript@5.2.2) + specifier: file:../../../commons + version: file:../commons(eslint@8.51.0)(jest@29.7.0)(typescript@5.2.2) '@api3/ois': specifier: ^2.2.1 version: 2.2.1 @@ -6856,3 +6856,53 @@ packages: /zod@3.22.4: resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==} + + file:../commons(eslint@8.51.0)(jest@29.7.0)(typescript@5.2.2): + resolution: {directory: ../commons, type: directory} + id: file:../commons + name: '@api3/commons' + engines: {node: ^18.14.0, pnpm: ^8.8.0} + peerDependencies: + eslint: ^8.50.0 + dependencies: + '@api3/ois': file:../ois + '@api3/promise-utils': 0.4.0 + '@typescript-eslint/eslint-plugin': 6.8.0(@typescript-eslint/parser@6.8.0)(eslint@8.51.0)(typescript@5.2.2) + '@typescript-eslint/parser': 6.8.0(eslint@8.51.0)(typescript@5.2.2) + eslint: 8.51.0 + eslint-config-next: 13.5.5(eslint@8.51.0)(typescript@5.2.2) + eslint-plugin-check-file: 2.6.2(eslint@8.51.0) + eslint-plugin-cypress: 2.15.1(eslint@8.51.0) + eslint-plugin-deprecation: 2.0.0(eslint@8.51.0)(typescript@5.2.2) + eslint-plugin-functional: 6.0.0(eslint@8.51.0)(typescript@5.2.2) + eslint-plugin-import: 2.28.1(@typescript-eslint/parser@6.8.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.51.0) + eslint-plugin-jest: 27.4.2(@typescript-eslint/eslint-plugin@6.8.0)(eslint@8.51.0)(jest@29.7.0)(typescript@5.2.2) + eslint-plugin-jest-formatting: 3.1.0(eslint@8.51.0) + eslint-plugin-lodash: 7.4.0(eslint@8.51.0) + eslint-plugin-no-only-tests: 3.1.0 + eslint-plugin-promise: 6.1.1(eslint@8.51.0) + eslint-plugin-react: 7.33.2(eslint@8.51.0) + eslint-plugin-react-hooks: 4.6.0(eslint@8.51.0) + eslint-plugin-unicorn: 48.0.1(eslint@8.51.0) + ethers: 5.7.2 + lodash: 4.17.21 + winston: 3.11.0 + winston-console-format: 1.0.8 + zod: 3.22.4 + transitivePeerDependencies: + - bufferutil + - eslint-import-resolver-typescript + - eslint-import-resolver-webpack + - jest + - supports-color + - typescript + - utf-8-validate + dev: false + + file:../ois: + resolution: {directory: ../ois, type: directory} + name: '@api3/ois' + dependencies: + lodash: 4.17.21 + zod: 3.22.4 + dev: false