Skip to content

Commit

Permalink
de-duplicate types/interfaces for dwn rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Apr 11, 2024
1 parent c9c5194 commit 4f36a93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 59 deletions.
31 changes: 23 additions & 8 deletions packages/agent/src/prototyping/clients/dwn-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,49 @@ export interface SerializableDwnMessage {
export type DwnEventSubscriptionHandler = (event: MessageEvent) => void;

/**
* Interface that can be implemented to communicate with
* {@link https://github.com/TBD54566975/dwn-server | DWN Servers} via JSON-RPC.
* Interface for communicating with {@link https://github.com/TBD54566975/dwn-server | DWN Servers}
* via JSON-RPC, supporting operations like sending DWN requests.
*/
export interface DwnRpc {
/**
* TODO: add jsdoc
* Lists the transport protocols supported by the DWN RPC client, such as HTTP or HTTPS.
* @returns An array of strings representing the supported transport protocols.
*/
get transportProtocols(): string[]

/**
* TODO: add jsdoc
* @param request
* Sends a request to a DWN Server using the specified DWN RPC request parameters.
*
* @param request - The DWN RPC request containing the URL, target DID, message, and optional data.
* @returns A promise that resolves to the response from the DWN server.
*/
sendDwnRequest(request: DwnRpcRequest): Promise<DwnRpcResponse>
}


/**
* TODO: add jsdoc
* Represents a JSON RPC request to a DWN server, including the URL, target DID, the message to be
* processed, and optional data.
*/
export type DwnRpcRequest = {
/** Optional data to be sent with the request. */
data?: any;
subscriptionHandler?: DwnEventSubscriptionHandler;

/** The URL of the DWN server to which the request is sent. */
dwnUrl: string;

/** The message to be processed by the DWN server, which can be a serializable DWN message. */
message: SerializableDwnMessage | any;

/** The DID of the target to which the message is addressed. */
targetDid: string;

/** Optional subscription handler for DWN events. */
subscriptionHandler?: DwnEventSubscriptionHandler;
}

/**
* TODO: add jsdoc
* Represents the JSON RPC response from a DWN server to a request, combining the results of various
* DWN operations.
*/
export type DwnRpcResponse = UnionMessageReply & RecordsReadReply;
55 changes: 4 additions & 51 deletions packages/agent/src/rpc-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { utils as cryptoUtils } from '@web5/crypto';
import { RecordsReadReply, UnionMessageReply } from '@tbd54566975/dwn-sdk-js';


import type { DwnRpc, DwnRpcRequest, DwnRpcResponse } from './prototyping/clients/dwn-rpc-types.js';
import type { JsonRpcResponse } from './prototyping/clients/json-rpc.js';

import { createJsonRpcRequest } from './prototyping/clients/json-rpc.js';
import { HttpDwnRpcClient } from './prototyping/clients/http-dwn-rpc-client.js';
import { WebSocketDwnRpcClient } from './prototyping/clients/web-socket-clients.js';
import { JsonRpcResponse, createJsonRpcRequest } from './prototyping/clients/json-rpc.js';

/**
* Interface that can be implemented to communicate with {@link Web5Agent | Web5 Agent}
Expand Down Expand Up @@ -32,60 +34,11 @@ export type DidRpcResponse = {
status: RpcStatus;
}

/**
* Interface for communicating with {@link https://github.com/TBD54566975/dwn-server | DWN Servers}
* via JSON-RPC, supporting operations like sending DWN requests.
*/
export interface DwnRpc {
/**
* Lists the transport protocols supported by the DWN RPC client, such as HTTP or HTTPS.
* @returns An array of strings representing the supported transport protocols.
*/
get transportProtocols(): string[]

/**
* Sends a request to a DWN Server using the specified DWN RPC request parameters.
*
* @param request - The DWN RPC request containing the URL, target DID, message, and optional data.
* @returns A promise that resolves to the response from the DWN server.
*/
sendDwnRequest(request: DwnRpcRequest): Promise<DwnRpcResponse>
}


/**
* Represents a JSON RPC request to a DWN server, including the URL, target DID, the message to be
* processed, and optional data.
*/
export type DwnRpcRequest = {
/** Optional data to be sent with the request. */
data?: any;

/** The URL of the DWN server to which the request is sent. */
dwnUrl: string;

/** The message to be processed by the DWN server, which can be a serializable DWN message. */
message: SerializableDwnMessage | any;

/** The DID of the target to which the message is addressed. */
targetDid: string;
}

/**
* Represents the JSON RPC response from a DWN server to a request, combining the results of various
* DWN operations.
*/
export type DwnRpcResponse = UnionMessageReply & RecordsReadReply;

export type RpcStatus = {
code: number;
message: string;
};

export interface SerializableDwnMessage {
toJSON(): string;
}

export interface Web5Rpc extends DwnRpc, DidRpc {}

/**
Expand Down

0 comments on commit 4f36a93

Please sign in to comment.