From 67a5dea95f24171f5070dfafbd12ea1e05014212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20S=C3=A1rai?= Date: Tue, 31 Oct 2023 17:20:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Use=20downloadFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JsonStorage.ts | 25 +++++++++++++------------ src/index.ts | 5 +++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/JsonStorage.ts b/src/JsonStorage.ts index 5d5ae19..a179199 100644 --- a/src/JsonStorage.ts +++ b/src/JsonStorage.ts @@ -45,7 +45,7 @@ export class JsonStorage { this.addToStorage("empty.json", {}), ]).then(() => {}) await this.initPromise; - console.log('feed URL:', this.feedURLString()) + console.log('feed URL:', `${this.beeURL.toString()}/bzz/${this.feedManifestResult?.reference}`) } } @@ -63,7 +63,10 @@ export class JsonStorage { this.mantarayNode.addFork( this.pathToBytes(key), - this.hexStringToReference(beeUploadResult.reference) + this.hexStringToReference(beeUploadResult.reference), { + 'Content Type': 'application/json', + 'Filename': `${key}.json` + } ) const savedMantaray = await this.mantarayNode.save( @@ -84,24 +87,22 @@ export class JsonStorage { return new TextEncoder().encode(string); } - private feedURLString(): string{ - return `${this.beeURL.toString()}/bzz/${this.feedManifestResult?.reference}` - } - async put(key: string, value: Object) { await this.initAsyncResources(); await this.addToStorage(key, value); - console.log('put: ', `${this.feedURLString()}/${key}`) + console.log('put: ', key) console.log(value) } - async get(key: string) { + async get(key: string): Promise { await this.initAsyncResources(); - const urlString = `${this.beeURL.toString()}/bzz/${this.feedManifestResult?.reference}/${key}` - const res = await fetch(urlString) - const value = res.ok? await res.json() : {} - console.log('get: ', urlString) + const res = await this.bee.downloadFile( + this.feedManifestResult!.reference as any, key + ) + const value = JSON.parse(res.data.text()) + console.log('get: ', key) console.log(value) + return value } } diff --git a/src/index.ts b/src/index.ts index 7fee282..bf7c8be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ async function main() { const def = { first: 'def1', second: 'def2' } await jsonStorage.put('def', def) - await jsonStorage.get("abc") - await jsonStorage.get("def") + const r1 = await jsonStorage.get("abc") + const r2 = await jsonStorage.get("def") + console.log({r1}) }