From 82823145f09ae7c3b8a2e61bad5078d8c9652006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Wed, 27 Jul 2022 16:34:38 +0200 Subject: [PATCH 1/2] feat!: adds support for Feed Manifest CID --- src/bee.ts | 10 +++++++--- src/types/index.ts | 17 +++++++++++++++++ src/utils/type.ts | 7 ++++--- test/integration/bee-class.spec.ts | 11 ++++++++--- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/bee.ts b/src/bee.ts index 067d7ac7..3a3f9996 100644 --- a/src/bee.ts +++ b/src/bee.ts @@ -69,6 +69,7 @@ import { AllTagsOptions, CHUNK_SIZE, Collection, + FeedManifestResult, Ky, Readable, ReferenceCidOrEns, @@ -892,7 +893,6 @@ export class Bee { * * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/dapps-on-swarm/feeds) * @see [Bee API reference - `POST /feeds`](https://docs.ethswarm.org/api/#tag/Feed/paths/~1feeds~1{owner}~1{topic}/post) - * TODO: Once breaking add support for Feed CID */ async createFeedManifest( postageBatchId: string | BatchId, @@ -900,7 +900,7 @@ export class Bee { topic: Topic | Uint8Array | string, owner: EthAddress | Uint8Array | string, options?: RequestOptions, - ): Promise { + ): Promise { assertRequestOptions(options) assertFeedType(type) assertBatchId(postageBatchId) @@ -908,7 +908,11 @@ export class Bee { const canonicalTopic = makeTopic(topic) const canonicalOwner = makeHexEthAddress(owner) - return createFeedManifest(this.getKy(options), canonicalOwner, canonicalTopic, postageBatchId, { type }) + const reference = await createFeedManifest(this.getKy(options), canonicalOwner, canonicalTopic, postageBatchId, { + type, + }) + + return addCidConversionFunction({ reference }, ReferenceType.FEED) } /** diff --git a/src/types/index.ts b/src/types/index.ts index d8b3b0d8..047bbc8d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -404,6 +404,23 @@ export const TOPIC_HEX_LENGTH = 64 */ export type Topic = HexString +/** + * Result of upload calls. + */ +export interface FeedManifestResult { + /** + * Reference of the uploaded data + */ + reference: Reference + + /** + * Function that converts the reference into Swarm Feed CID. + * + * @see https://github.com/ethersphere/swarm-cid-js + */ + cid: () => string +} + /** * FeedReader is an interface for downloading feed updates */ diff --git a/src/utils/type.ts b/src/utils/type.ts index 9d8eac6a..0d0ca588 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -25,8 +25,6 @@ import { PostageBatchOptions, CashoutOptions, ReferenceOrEns, - UploadResult, - UploadResultWithCid, } from '../types' import { BeeArgumentError, BeeError } from './error' import { isFile } from './file' @@ -199,7 +197,10 @@ export function makeReferenceOrEns(value: unknown, expectedCidType: ReferenceTyp * @param result * @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType */ -export function addCidConversionFunction(result: UploadResult, cidType: ReferenceType): UploadResultWithCid { +export function addCidConversionFunction( + result: T, + cidType: ReferenceType, +): T & { cid: () => string } { return { ...result, cid() { diff --git a/test/integration/bee-class.spec.ts b/test/integration/bee-class.spec.ts index a6c15d8e..920ea1c2 100644 --- a/test/integration/bee-class.spec.ts +++ b/test/integration/bee-class.spec.ts @@ -578,12 +578,17 @@ describe('Bee class', () => { const feed = bee.makeFeedWriter('sequence', topic, signer) await feed.upload(getPostageBatch(), cacResult.reference) - const manifestReference = await bee.createFeedManifest(getPostageBatch(), 'sequence', topic, owner) + const manifestResult = await bee.createFeedManifest(getPostageBatch(), 'sequence', topic, owner) - expect(typeof manifestReference).toBe('string') + expect(manifestResult).toEqual( + expect.objectContaining({ + reference: String, + cid: Function, + }), + ) // this calls /bzz endpoint that should resolve the manifest and the feed returning the latest feed's content - const file = await bee.downloadFile(manifestReference, 'index.html') + const file = await bee.downloadFile(manifestResult.reference, 'index.html') expect(new TextDecoder().decode(file.data)).toEqual('some data') }, FEED_TIMEOUT, From 7532566b16ea0e0b9d0ca895c0e2df1bbbbafde3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Thu, 28 Jul 2022 15:15:27 +0200 Subject: [PATCH 2/2] test: fixes testing --- test/integration/bee-class.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/bee-class.spec.ts b/test/integration/bee-class.spec.ts index 920ea1c2..900d6d2f 100644 --- a/test/integration/bee-class.spec.ts +++ b/test/integration/bee-class.spec.ts @@ -582,8 +582,8 @@ describe('Bee class', () => { expect(manifestResult).toEqual( expect.objectContaining({ - reference: String, - cid: Function, + reference: expect.any(String), + cid: expect.any(Function), }), )