diff --git a/index.d.ts b/index.d.ts index e69de29..16b778b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -0,0 +1,62 @@ +declare module "ar-wrapper" { + type Arweave = import("arweave") + type LRUMap = import("lru_map").LRUMap + + interface BlockStatusI { + status: number + confirmed: { + block_height: number + block_indep_hash: string + number_of_confirmations: number + } + } + + export interface BlockDocument { + name: string + content: string + version: number + tags: Record + } + + export interface OptionsI { + host: string + port: number + protocol: string + timeout: number + logging: boolean + } + + export const DEFAULT_OPTIONS: OptionsI + + export class Document { + txID: string + client: ArweaveClient + posted: boolean + timestamp: string + + name: string + content: any + version: number + tags: Record + + constructor(parentClient: ArweaveClient, name: string, content: any, tags: Record, version?: number) + data(): BlockDocument + + update(content: any): Promise + bumpTimestamp(dateMs: number) + } + + export class ArweaveClient { + adminAddr: string + client: Arweave + cache: LRUMap + + constructor(adminAddress: string, keyFile: string, cacheSize?: number, options?: OptionsI) + isCached(documentName: string, desiredVersion?: number): boolean + addDocument(name: string, content: any, tags: Record): Promise + updateDocument(document: Document): Promise + pollForConfirmation(txId: string, maxRetries?: number): Promise + getDocumentByName(name: string, version?: number, maxRetries?: number, verifiedOnly?: boolean): Promise + getDocumentByName(txId: string, maxRetries?: number, verifiedOnly?: boolean): Promise + } +} \ No newline at end of file diff --git a/index.js b/index.js index d2dffbb..1c8ca6e 100644 --- a/index.js +++ b/index.js @@ -92,11 +92,11 @@ class ArweaveClient { // Construct a new client given the address of the admin account, // keys to the wallet, and a set of options for connecting to an Arweave network. // Options are identical to the ones supported by the official `arweave-js` library - constructor(adminAddress, key, cache_size = 500, options = DEFAULT_OPTIONS) { - this.#key = key + constructor(adminAddress, keyFile, cacheSize = 500, options = DEFAULT_OPTIONS) { + this.#key = keyFile this.adminAddr = adminAddress this.client = ArweaveLib.init(options) - this.cache = new LRUMap(500) + this.cache = new LRUMap(cacheSize) } // Internal function for adding single document to permaweb @@ -124,13 +124,6 @@ class ArweaveClient { return doc } - // Add a new document - async addDocument(name, content, tags) { - // create document + transaction - const doc = new Document(this, name, content, tags) - return this.#insert(doc) - } - // Internal function to see if given document is cached. // Optionally define desired version to match against. isCached(documentName, desiredVersion) { @@ -144,6 +137,13 @@ class ArweaveClient { return cached.synced && versionMatch } + // Add a new document + async addDocument(name, content, tags) { + // create document + transaction + const doc = new Document(this, name, content, tags) + return this.#insert(doc) + } + // Update existing document object and send to chain async updateDocument(document) { // check if cache has latest version of document @@ -192,6 +192,7 @@ class ArweaveClient { }) } + // TODO: handle pagination/cursor here return { query: ` query { @@ -249,7 +250,7 @@ class ArweaveClient { } // fetch document, update cache - const doc = await this.getDocumentByTxId(txId) + const doc = await this.getDocumentByTxId(txId, maxRetries) this.cache.set(doc.name, doc) return doc }