Skip to content

Commit

Permalink
Fix TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Nov 25, 2023
1 parent 58d937e commit 777f987
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/pusher/src/api-requests/data-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildAndExecuteRequest, extractAndEncodeResponse } from '@api3/airnode-adapter';
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';
Expand Down Expand Up @@ -27,7 +27,7 @@ export const callApi = async (
const response = await buildAndExecuteRequest(
{
endpointName: endpoint.name,
ois: ois as any, // TODO: The OIS type from Airnode does not match the one from @api3/ois.
ois: ois as BuildRequestOptions['ois'], // TS doesn't realize the types are the same because of https://github.com/microsoft/TypeScript/issues/26627#issuecomment-416046113.
parameters: processedApiCallParameters,
metadata: null,
apiCredentials,
Expand Down Expand Up @@ -96,7 +96,7 @@ export const makeTemplateRequests = async (signedApiUpdate: SignedApiUpdate): Pr

const goEncodedResponse = goSync(() => {
const { _type, _path, _times } = getReservedParameters(
oisEndpoint as any, // TODO: The Endpoint type from @api3/ois does not match the one from Airnode.
oisEndpoint as Parameters<typeof getReservedParameters>[0], // TS doesn't realize the types are the same because of https://github.com/microsoft/TypeScript/issues/26627#issuecomment-416046113.
apiCallParameters
);
return extractAndEncodeResponse(goPostProcess.data, {
Expand Down
3 changes: 0 additions & 3 deletions packages/pusher/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export const SIGNED_DATA_PUSH_POLLING_INTERVAL = 2500;

export const NO_SIGNED_API_UPDATE_EXIT_CODE = 1;
export const NO_FETCH_EXIT_CODE = 2;

export const API_CALL_TIMEOUT = 10_000;
13 changes: 1 addition & 12 deletions packages/pusher/src/fetch-beacon-data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { go } from '@api3/promise-utils';
import { isEmpty } from 'lodash';

import { makeTemplateRequests } from './api-requests/data-provider';
import { NO_FETCH_EXIT_CODE } from './constants';
import { logger } from './logger';
import { signTemplateResponses } from './sign-template-data';
import { getState } from './state';
Expand All @@ -13,16 +11,7 @@ export const initiateFetchingBeaconData = () => {
logger.debug('Initiating fetching all beacon data');
const { config } = getState();

const { signedApiUpdates } = config.triggers;

// TODO: Validate using zod schema
if (isEmpty(signedApiUpdates)) {
logger.error('No signed API updates found. Stopping.');
// eslint-disable-next-line unicorn/no-process-exit
process.exit(NO_FETCH_EXIT_CODE);
}

return signedApiUpdates.map(async (element) => fetchBeaconDataInLoop(element));
return config.triggers.signedApiUpdates.map(async (element) => fetchBeaconDataInLoop(element));
};

const fetchBeaconDataInLoop = async (signedApiUpdate: SignedApiUpdate) => {
Expand Down
9 changes: 1 addition & 8 deletions packages/pusher/src/update-signed-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get, isEmpty, uniq } from 'lodash';

import { postSignedApiData } from './api-requests/signed-api';
import { NO_SIGNED_API_UPDATE_EXIT_CODE, SIGNED_DATA_PUSH_POLLING_INTERVAL } from './constants';
import { SIGNED_DATA_PUSH_POLLING_INTERVAL } from './constants';
import { logger } from './logger';
import { getState } from './state';
import { sleep } from './utils';
Expand Down Expand Up @@ -46,13 +46,6 @@ export const initiateUpdatingSignedApi = () => {
}))
);

// TODO: Validate using zod schema
if (isEmpty(signedApiUpdateDelayGroups)) {
logger.error('No signed API updates found. Stopping.');
// eslint-disable-next-line unicorn/no-process-exit
process.exit(NO_SIGNED_API_UPDATE_EXIT_CODE);
}

signedApiUpdateDelayGroups.map(async (element) => updateSignedApiInLoop(element));
};

Expand Down
27 changes: 15 additions & 12 deletions packages/pusher/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const signedApiUpdateSchema = z.strictObject({
});

export const triggersSchema = z.strictObject({
signedApiUpdates: z.array(signedApiUpdateSchema),
signedApiUpdates: z.array(signedApiUpdateSchema).nonempty(),
});

const validateTemplatesReferences: SuperRefinement<{ templates: Templates; endpoints: Endpoints }> = (config, ctx) => {
Expand Down Expand Up @@ -203,18 +203,21 @@ export const signedApiSchema = z.strictObject({
url: z.string().url(),
});

export const signedApisSchema = z.array(signedApiSchema).superRefine((apis, ctx) => {
const names = apis.map((api) => api.name);
const uniqueNames = [...new Set(names)];
export const signedApisSchema = z
.array(signedApiSchema)
.nonempty()
.superRefine((apis, ctx) => {
const names = apis.map((api) => api.name);
const uniqueNames = [...new Set(names)];

if (names.length !== uniqueNames.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Signed API names must be unique`,
path: ['signedApis'],
});
}
});
if (names.length !== uniqueNames.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Signed API names must be unique`,
path: ['signedApis'],
});
}
});

export const oisesSchema = z.array(oisSchema);

Expand Down

0 comments on commit 777f987

Please sign in to comment.