From b308d71cd418cc5c47098f8d0da72bdc3f3ac2f0 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 7 Mar 2024 22:11:02 +0530 Subject: [PATCH] setup a generic type (DRY) --- packages/core/src/lib/light_push/index.ts | 11 ++--------- packages/interfaces/src/metadata.ts | 12 ++---------- packages/interfaces/src/protocols.ts | 10 ++++++++++ 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index a077a44f6e..5cd9a8ff29 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -6,6 +6,7 @@ import { Libp2p, ProtocolCreateOptions, ProtocolError, + ProtocolResult, SendResult } from "@waku/interfaces"; import { PushResponse } from "@waku/proto"; @@ -28,15 +29,7 @@ const log = new Logger("light-push"); export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1"; export { PushResponse }; -type PreparePushMessageResult = - | { - query: PushRpc; - error: null; - } - | { - query: null; - error: ProtocolError; - }; +type PreparePushMessageResult = ProtocolResult<"query", PushRpc>; /** * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/). diff --git a/packages/interfaces/src/metadata.ts b/packages/interfaces/src/metadata.ts index 469a3fc5ab..12ff521477 100644 --- a/packages/interfaces/src/metadata.ts +++ b/packages/interfaces/src/metadata.ts @@ -3,19 +3,11 @@ import type { PeerId } from "@libp2p/interface"; import type { ShardInfo } from "./enr.js"; import type { IBaseProtocol, - ProtocolError, + ProtocolResult, ShardingParams } from "./protocols.js"; -export type QueryResult = - | { - shardInfo: ShardInfo; - error: null; - } - | { - shardInfo: null; - error: ProtocolError; - }; +export type QueryResult = ProtocolResult<"shardInfo", ShardInfo>; // IMetadata always has shardInfo defined while it is optionally undefined in IBaseProtocol export interface IMetadata extends Omit { diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 340a5f97cf..3597511b6c 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -97,6 +97,16 @@ export type Callback = ( msg: T ) => void | Promise; +// K = key name +// T = value type +export type ProtocolResult = + | ({ + [key in K]: T; + } & { error: null }) + | ({ + [key in K]: null; + } & { error: ProtocolError }); + export enum ProtocolError { /** Could not determine the origin of the fault. Best to check connectivity and try again */ GENERIC_FAIL = "Generic error",