diff --git a/src/network.ts b/src/network.ts index e827f57..80aa4c6 100644 --- a/src/network.ts +++ b/src/network.ts @@ -5,6 +5,8 @@ import RPC from "@lumeweb/rpc"; import { isPromise } from "./util.js"; import SimpleRpcQuery from "./query/simple.js"; import WisdomRpcQuery from "./query/wisdom.js"; +import ClearCacheRpcQuery from "./query/clearCache.js"; +import { RpcQueryOptions } from "./types.js"; export default class RpcNetwork { constructor(dht = new DHT()) { @@ -127,7 +129,7 @@ export default class RpcNetwork { module: string, data: object | any[] = {}, bypassCache: boolean = false, - options: {} + options: RpcQueryOptions = {} ): SimpleRpcQuery { return new SimpleRpcQuery( this, @@ -141,4 +143,23 @@ export default class RpcNetwork { options ).run(); } + + public clearCacheQuery( + relays: string[], + method: string, + module: string, + data: object | any[] = {}, + options: RpcQueryOptions = {} + ): SimpleRpcQuery { + return new ClearCacheRpcQuery( + this, + relays, + { + method, + module, + data, + }, + options + ).run(); + } } diff --git a/src/query/clearCache.ts b/src/query/clearCache.ts new file mode 100644 index 0000000..d92afa3 --- /dev/null +++ b/src/query/clearCache.ts @@ -0,0 +1,43 @@ +import RpcNetwork from "../network.js"; +import { RPCBroadcastRequest, RPCRequest } from "@lumeweb/relay-types"; +import { RpcQueryOptions } from "../types.js"; +import { hashQuery } from "../util.js"; +import { getActiveRelay, setupRelay } from "../sharedRelay.js"; +import SimpleRpcQuery from "./simple.js"; + +export default class ClearCacheRpcQuery extends SimpleRpcQuery { + protected _relays: string[]; + + constructor( + network: RpcNetwork, + relays: string[], + query: RPCRequest, + options: RpcQueryOptions + ) { + super(network, "", query, options); + this._relays = relays; + } + + protected async _run(): Promise { + await setupRelay(this._network); + // @ts-ignore + this._relay = getActiveRelay().stream.remotePublicKey; + await this.queryRelay(); + await this.checkResponses(); + } + + protected async queryRelay(): Promise { + return this.queryRpc(getActiveRelay(), { + module: "rpc", + method: "broadcast_request", + data: { + request: { + module: "rpc", + method: "clear_cached_item", + data: hashQuery(this._query), + }, + relays: this._relays, + } as RPCBroadcastRequest, + }); + } +}