From 572253c173b4acc8c2e515677f66a68c8e5076b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Tue, 8 Jun 2021 15:52:40 +0200 Subject: [PATCH] refactor!: no single-property object returned (#341) Co-authored-by: nugaon <50576770+nugaon@users.noreply.github.com> --- src/bee.ts | 3 +-- src/chunk/soc.ts | 6 +++--- src/feed/index.ts | 5 ++--- src/feed/json.ts | 4 ++-- src/modules/chunk.ts | 7 ++++--- src/modules/soc.ts | 6 +++--- src/types/index.ts | 4 ++-- test/integration/bee-class.spec.ts | 6 +++--- test/integration/chunk/bmt.spec.ts | 4 ++-- test/integration/chunk/cac.spec.ts | 2 +- test/integration/chunk/soc.spec.ts | 2 +- test/integration/feed/index.spec.ts | 6 +++--- test/integration/modules/chunk.spec.ts | 4 ++-- test/integration/modules/feed.spec.ts | 15 ++++++++++++--- test/integration/modules/pinning.spec.ts | 16 ++++++++-------- test/unit/feed/json.spec.ts | 6 +++--- 16 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/bee.ts b/src/bee.ts index 9d3826d4..c2b608b6 100644 --- a/src/bee.ts +++ b/src/bee.ts @@ -44,7 +44,6 @@ import type { SOCReader, Topic, BeeOptions, - ReferenceResponse, JsonFeedOptions, AnyJson, Pin, @@ -492,7 +491,7 @@ export class Bee { topic: string, data: T, options?: JsonFeedOptions, - ): Promise { + ): Promise { assertBatchId(postageBatchId) const hashedTopic = this.makeFeedTopic(topic) diff --git a/src/chunk/soc.ts b/src/chunk/soc.ts index e65582b3..769077d9 100644 --- a/src/chunk/soc.ts +++ b/src/chunk/soc.ts @@ -13,7 +13,7 @@ import { MIN_PAYLOAD_SIZE, assertValidChunkData, } from './cac' -import { ReferenceResponse, UploadOptions, Signature, Signer, BatchId } from '../types' +import { UploadOptions, Signature, Signer, BatchId, Reference } from '../types' import { bytesToHex } from '../utils/hex' import * as socAPI from '../modules/soc' import * as chunkAPI from '../modules/chunk' @@ -137,7 +137,7 @@ export async function uploadSingleOwnerChunk( chunk: SingleOwnerChunk, postageBatchId: BatchId, options?: UploadOptions, -): Promise { +): Promise { const owner = bytesToHex(chunk.owner()) const identifier = bytesToHex(chunk.identifier()) const signature = bytesToHex(chunk.signature()) @@ -163,7 +163,7 @@ export async function uploadSingleOwnerChunkData( identifier: Identifier, data: Uint8Array, options?: UploadOptions, -): Promise { +): Promise { assertAddress(postageBatchId) const cac = makeContentAddressedChunk(data) const soc = await makeSingleOwnerChunk(cac, identifier, signer) diff --git a/src/feed/index.ts b/src/feed/index.ts index 7028e69b..32a26124 100644 --- a/src/feed/index.ts +++ b/src/feed/index.ts @@ -5,7 +5,6 @@ import { FeedUpdateOptions, fetchFeedUpdate } from '../modules/feed' import { REFERENCE_HEX_LENGTH, Reference, - ReferenceResponse, UploadOptions, ENCRYPTED_REFERENCE_HEX_LENGTH, ENCRYPTED_REFERENCE_BYTES_LENGTH, @@ -94,7 +93,7 @@ export async function uploadFeedUpdate( reference: ChunkReference, postageBatchId: BatchId, options?: FeedUploadOptions, -): Promise { +): Promise { const identifier = makeFeedIdentifier(topic, index) const at = options?.at ?? Date.now() / 1000.0 const timestamp = writeUint64BigEndian(at) @@ -128,7 +127,7 @@ export async function updateFeed( reference: ChunkReference, postageBatchId: BatchId, options?: FeedUploadOptions, -): Promise { +): Promise { const ownerHex = makeHexEthAddress(signer.address) const nextIndex = await findNextIndex(url, ownerHex, topic, options) diff --git a/src/feed/json.ts b/src/feed/json.ts index 246864c8..bd83d717 100644 --- a/src/feed/json.ts +++ b/src/feed/json.ts @@ -1,4 +1,4 @@ -import { FeedWriter, ReferenceResponse, FeedReader, AnyJson, Address } from '../types' +import { FeedWriter, FeedReader, AnyJson, Address, Reference } from '../types' import { Bee } from '../bee' import { assertAddress } from '../utils/type' @@ -25,7 +25,7 @@ export async function setJsonData( writer: FeedWriter, postageBatchId: string | Address, data: AnyJson, -): Promise { +): Promise { assertAddress(postageBatchId) const serializedData = serializeJson(data) diff --git a/src/modules/chunk.ts b/src/modules/chunk.ts index 4cafa57d..811e3d9b 100644 --- a/src/modules/chunk.ts +++ b/src/modules/chunk.ts @@ -3,6 +3,7 @@ import type { Readable } from 'stream' import type { BatchId, ReferenceResponse, UploadOptions } from '../types' import { extractUploadHeaders } from '../utils/headers' import { safeAxios } from '../utils/safe-axios' +import { Reference } from '../types' const endpoint = '/chunks' @@ -16,14 +17,14 @@ const endpoint = '/chunks' * @param url Bee URL * @param data Chunk data to be uploaded * @param postageBatchId Postage BatchId that will be assigned to uploaded data - * @param options Aditional options like tag, encryption, pinning + * @param options Additional options like tag, encryption, pinning */ export async function upload( url: string, data: Uint8Array, postageBatchId: BatchId, options?: UploadOptions, -): Promise { +): Promise { const response = await safeAxios({ ...options?.axiosOptions, method: 'post', @@ -36,7 +37,7 @@ export async function upload( responseType: 'json', }) - return response.data + return response.data.reference } /** diff --git a/src/modules/soc.ts b/src/modules/soc.ts index b9366637..e75110b9 100644 --- a/src/modules/soc.ts +++ b/src/modules/soc.ts @@ -1,4 +1,4 @@ -import { BatchId, ReferenceResponse, UploadOptions } from '../types' +import { BatchId, Reference, ReferenceResponse, UploadOptions } from '../types' import { extractUploadHeaders } from '../utils/headers' import { safeAxios } from '../utils/safe-axios' @@ -23,7 +23,7 @@ export async function upload( data: Uint8Array, postageBatchId: BatchId, options?: UploadOptions, -): Promise { +): Promise { const response = await safeAxios({ ...options?.axiosOptions, method: 'post', @@ -37,5 +37,5 @@ export async function upload( params: { sig: signature }, }) - return response.data + return response.data.reference } diff --git a/src/types/index.ts b/src/types/index.ts index 48018e41..dd7a61d4 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -204,7 +204,7 @@ export interface FeedWriter extends FeedReader { postageBatchId: string | BatchId, reference: ChunkReference | Reference, options?: FeedUploadOptions, - ): Promise + ): Promise } /** @@ -235,7 +235,7 @@ export interface SOCWriter extends SOCReader { identifier: Identifier, data: Uint8Array, options?: UploadOptions, - ) => Promise + ) => Promise } /** diff --git a/test/integration/bee-class.spec.ts b/test/integration/bee-class.spec.ts index b2c29484..86e18968 100644 --- a/test/integration/bee-class.spec.ts +++ b/test/integration/bee-class.spec.ts @@ -340,7 +340,7 @@ describe('Bee class', () => { const socWriter = bee.makeSOCWriter(testIdentity.privateKey) const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload) - expect(reference).toEqual({ reference: socHash }) + expect(reference).toEqual(socHash) const soc = await socWriter.download(identifier) const payload = soc.payload() @@ -376,7 +376,7 @@ describe('Bee class', () => { const socWriter = bee.makeSOCWriter() const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload) - expect(reference).toEqual({ reference: '00019ec85e8859aa641cf149fbd1147ac7965a9cad1dfe4ab7beaa12d5dc8027' }) + expect(reference).toEqual('00019ec85e8859aa641cf149fbd1147ac7965a9cad1dfe4ab7beaa12d5dc8027') }) it('should prioritize signer passed to method', async () => { @@ -393,7 +393,7 @@ describe('Bee class', () => { const socWriter = bee.makeSOCWriter(testIdentity.privateKey) const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload) - expect(reference).toEqual({ reference: 'd1a21cce4c86411f6af2f621ce9a3a0aa3cc5cea6cc9e1b28523d28411398cfb' }) + expect(reference).toEqual('d1a21cce4c86411f6af2f621ce9a3a0aa3cc5cea6cc9e1b28523d28411398cfb') }) it('should throw if no signers are passed', () => { diff --git a/test/integration/chunk/bmt.spec.ts b/test/integration/chunk/bmt.spec.ts index e19c48cd..fa414b8e 100644 --- a/test/integration/chunk/bmt.spec.ts +++ b/test/integration/chunk/bmt.spec.ts @@ -17,7 +17,7 @@ describe('bmt', () => { const reference = bytesToHex(bmtHash(data)) const response = await chunk.upload(beeUrl(), data, getPostageBatch()) - expect(response).toEqual({ reference }) + expect(response).toEqual(reference) } }) @@ -28,6 +28,6 @@ describe('bmt', () => { const reference = bytesToHex(bmtHash(data)) const response = await chunk.upload(beeUrl(), data, getPostageBatch()) - expect(response).toEqual({ reference }) + expect(response).toEqual(reference) }) }) diff --git a/test/integration/chunk/cac.spec.ts b/test/integration/chunk/cac.spec.ts index 685649db..f236fa57 100644 --- a/test/integration/chunk/cac.spec.ts +++ b/test/integration/chunk/cac.spec.ts @@ -14,7 +14,7 @@ describe('cac', () => { const reference = bytesToHex(address) const response = await chunkAPI.upload(beeUrl(), cac.data, getPostageBatch()) - expect(response).toEqual({ reference }) + expect(response).toEqual(reference) }) test('download content address chunk', async () => { diff --git a/test/integration/chunk/soc.spec.ts b/test/integration/chunk/soc.spec.ts index 811f1cd4..31ed69a5 100644 --- a/test/integration/chunk/soc.spec.ts +++ b/test/integration/chunk/soc.spec.ts @@ -23,7 +23,7 @@ describe('soc', () => { const response = await uploadSingleOwnerChunk(beeUrl(), soc, getPostageBatch()) - expect(response).toEqual({ reference: socAddress }) + expect(response).toEqual(socAddress) }) test('download single owner chunk', async () => { diff --git a/test/integration/feed/index.spec.ts b/test/integration/feed/index.spec.ts index e487edd2..9ff26f01 100644 --- a/test/integration/feed/index.spec.ts +++ b/test/integration/feed/index.spec.ts @@ -1,5 +1,5 @@ import { fetchFeedUpdate } from '../../../src/modules/feed' -import { HexString, hexToBytes, makeHexString } from '../../../src/utils/hex' +import { hexToBytes, makeHexString } from '../../../src/utils/hex' import { beeUrl, ERR_TIMEOUT, getPostageBatch, testIdentity } from '../../utils' import { ChunkReference, downloadFeedUpdate, findNextIndex, Index, uploadFeedUpdate } from '../../../src/feed' import { Bytes, assertBytes } from '../../../src/utils/bytes' @@ -15,9 +15,9 @@ function makeChunk(index: number) { async function uploadChunk(url: string, index: number): Promise { const chunk = makeChunk(index) - const referenceResponse = await chunkAPI.upload(url, chunk.data, getPostageBatch()) + const reference = await chunkAPI.upload(url, chunk.data, getPostageBatch()) - return hexToBytes(referenceResponse.reference as HexString) as ChunkReference + return hexToBytes(reference) as ChunkReference } // FIXME helper function for setting up test state for testing finding feed updates diff --git a/test/integration/modules/chunk.spec.ts b/test/integration/modules/chunk.spec.ts index fa77fb2f..826dedf1 100644 --- a/test/integration/modules/chunk.spec.ts +++ b/test/integration/modules/chunk.spec.ts @@ -13,9 +13,9 @@ describe('modules/chunk', () => { const reference = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' const response = await chunk.upload(BEE_URL, data, getPostageBatch()) - expect(response).toEqual({ reference }) + expect(response).toEqual(reference) - const downloadedData = await chunk.download(BEE_URL, response.reference) + const downloadedData = await chunk.download(BEE_URL, response) expect(downloadedData).toEqual(data) }) diff --git a/test/integration/modules/feed.spec.ts b/test/integration/modules/feed.spec.ts index 9c01745a..60f3244b 100644 --- a/test/integration/modules/feed.spec.ts +++ b/test/integration/modules/feed.spec.ts @@ -1,9 +1,18 @@ import { createFeedManifest, fetchFeedUpdate } from '../../../src/modules/feed' import { HexString, hexToBytes, makeHexString } from '../../../src/utils/hex' -import { beeUrl, ERR_TIMEOUT, getPostageBatch, testIdentity, tryDeleteChunkFromLocalStorage } from '../../utils' +import { + beeUrl, + commonMatchers, + ERR_TIMEOUT, + getPostageBatch, + testIdentity, + tryDeleteChunkFromLocalStorage, +} from '../../utils' import { upload as uploadSOC } from '../../../src/modules/soc' import type { Topic } from '../../../src/types' +commonMatchers() + describe('modules/feed', () => { const url = beeUrl() const owner = makeHexString(testIdentity.address, 40) @@ -42,10 +51,10 @@ describe('modules/feed', () => { await tryDeleteChunkFromLocalStorage(cacAddress) const socResponse = await uploadSOC(url, owner, identifier, signature, socData, getPostageBatch()) - expect(typeof socResponse.reference).toBe('string') + expect(socResponse).toBeType('string') const feedUpdate = await fetchFeedUpdate(url, owner, oneUpdateTopic) - expect(typeof feedUpdate.reference).toBe('string') + expect(feedUpdate.reference).toBeType('string') expect(feedUpdate.feedIndex).toEqual('0000000000000000') expect(feedUpdate.feedIndexNext).toEqual('0000000000000001') }, 21000) diff --git a/test/integration/modules/pinning.spec.ts b/test/integration/modules/pinning.spec.ts index 7affa191..3efd3b58 100644 --- a/test/integration/modules/pinning.spec.ts +++ b/test/integration/modules/pinning.spec.ts @@ -107,15 +107,15 @@ describe('modules/pin', () => { describe('should work with chunks', () => { it('should pin existing chunk', async () => { - const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) - expect(chunkResponse).toEqual({ reference: testChunkHash }) + const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) + expect(chunkReference).toEqual(testChunkHash) await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong }) it('should unpin existing chunk', async () => { - const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) - expect(chunkResponse).toEqual({ reference: testChunkHash }) + const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) + expect(chunkReference).toEqual(testChunkHash) await pinning.unpin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong }) @@ -133,8 +133,8 @@ describe('modules/pin', () => { }) it('should return pinning status of existing chunk', async () => { - const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) - expect(chunkResponse).toEqual({ reference: testChunkHash }) + const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) + expect(chunkReference).toEqual(testChunkHash) await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong const pinningStatus = await pinning.getPin(BEE_URL, testChunkHash) @@ -146,8 +146,8 @@ describe('modules/pin', () => { }) it('should return list of pinned chunks', async () => { - const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) - expect(chunkResponse).toEqual({ reference: testChunkHash }) + const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch()) + expect(chunkReference).toEqual(testChunkHash) await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong }) diff --git a/test/unit/feed/json.spec.ts b/test/unit/feed/json.spec.ts index e771683a..dacf1b93 100644 --- a/test/unit/feed/json.spec.ts +++ b/test/unit/feed/json.spec.ts @@ -12,7 +12,7 @@ interface CircularReference { describe('JsonFeed', () => { const DATA_REFERENCE = testChunkHash as Reference - const FEED_REFERENCE_HASH = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a1111' + const FEED_REFERENCE_HASH = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a1111' as Reference const FEED_REFERENCE = { reference: FEED_REFERENCE_HASH, } as FetchFeedUpdateResponse @@ -23,9 +23,9 @@ describe('JsonFeed', () => { bee.uploadData(Arg.all()).resolves(DATA_REFERENCE) const writer = Substitute.for() - writer.upload(Arg.all()).resolves(FEED_REFERENCE) + writer.upload(Arg.all()).resolves(FEED_REFERENCE_HASH) - await expect(setJsonData(bee, writer, testAddress, data as AnyJson)).resolves.toEqual(FEED_REFERENCE) + await expect(setJsonData(bee, writer, testAddress, data as AnyJson)).resolves.toEqual(FEED_REFERENCE_HASH) bee.received(1).uploadData(testAddress, expectedBytes) writer.received(1).upload(testAddress, DATA_REFERENCE) })