Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: adds support for Feed Manifest CID #745

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
AllTagsOptions,
CHUNK_SIZE,
Collection,
FeedManifestResult,
Ky,
Readable,
ReferenceCidOrEns,
Expand Down Expand Up @@ -892,23 +893,26 @@ 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,
type: FeedType,
topic: Topic | Uint8Array | string,
owner: EthAddress | Uint8Array | string,
options?: RequestOptions,
): Promise<Reference> {
): Promise<FeedManifestResult> {
assertRequestOptions(options)
assertFeedType(type)
assertBatchId(postageBatchId)

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)
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,23 @@ export const TOPIC_HEX_LENGTH = 64
*/
export type Topic = HexString<typeof TOPIC_HEX_LENGTH>

/**
* 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
*/
Expand Down
7 changes: 4 additions & 3 deletions src/utils/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import {
PostageBatchOptions,
CashoutOptions,
ReferenceOrEns,
UploadResult,
UploadResultWithCid,
} from '../types'
import { BeeArgumentError, BeeError } from './error'
import { isFile } from './file'
Expand Down Expand Up @@ -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<T extends { reference: string }>(
result: T,
cidType: ReferenceType,
): T & { cid: () => string } {
return {
...result,
cid() {
Expand Down
11 changes: 8 additions & 3 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down