setup a generic type (DRY)

This commit is contained in:
danisharora099 2024-03-07 22:11:02 +05:30
parent dd7354462a
commit b308d71cd4
No known key found for this signature in database
GPG Key ID: FBD2BF500037F135
3 changed files with 14 additions and 19 deletions

View File

@ -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/).

View File

@ -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<IBaseProtocol, "shardInfo"> {

View File

@ -97,6 +97,16 @@ export type Callback<T extends IDecodedMessage> = (
msg: T
) => void | Promise<void>;
// K = key name
// T = value type
export type ProtocolResult<K extends string, T> =
| ({
[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",