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

refactor!: no generic BeeResponse returned from Bee class #342

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 22 additions & 21 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import * as bytes from './modules/bytes'
import * as pss from './modules/pss'
import * as status from './modules/status'
import * as stamps from './modules/stamps'

import { BeeArgumentError, BeeError } from './utils/error'
import { prepareWebsocketData } from './utils/data'
import { fileArrayBuffer, isFile } from './utils/file'
import { AxiosRequestConfig } from 'axios'
import { makeFeedReader, makeFeedWriter } from './feed'
import { makeSigner } from './chunk/signer'
import { assertFeedType, DEFAULT_FEED_TYPE, FeedType } from './feed/type'
import { downloadSingleOwnerChunk, uploadSingleOwnerChunkData } from './chunk/soc'
import { makeTopic, makeTopicFromString } from './feed/topic'
import { createFeedManifest } from './modules/feed'
import { assertBeeUrl, stripLastSlash } from './utils/url'
import { EthAddress, makeEthAddress, makeHexEthAddress } from './utils/eth'
import { wrapBytesWithHelpers } from './utils/bytes'
import { assertBatchId, assertNonNegativeInteger, assertReference } from './utils/type'
import { setJsonData, getJsonData } from './feed/json'
import { makeCollectionFromFS, makeCollectionFromFileList } from './utils/collection'
import { PostageBatchOptions, STAMPS_DEPTH_MAX, STAMPS_DEPTH_MIN } from './types'

import type {
Tag,
FileData,
Expand All @@ -15,7 +34,6 @@ import type {
AddressPrefix,
PssMessageHandler,
PssSubscription,
BeeResponse,
CollectionUploadOptions,
FileUploadOptions,
Data,
Expand All @@ -33,23 +51,6 @@ import type {
PostageBatch,
BatchId,
} from './types'
import { BeeArgumentError, BeeError } from './utils/error'
import { prepareWebsocketData } from './utils/data'
import { fileArrayBuffer, isFile } from './utils/file'
import { AxiosRequestConfig } from 'axios'
import { makeFeedReader, makeFeedWriter } from './feed'
import { makeSigner } from './chunk/signer'
import { assertFeedType, DEFAULT_FEED_TYPE, FeedType } from './feed/type'
import { downloadSingleOwnerChunk, uploadSingleOwnerChunkData } from './chunk/soc'
import { makeTopic, makeTopicFromString } from './feed/topic'
import { createFeedManifest } from './modules/feed'
import { assertBeeUrl, stripLastSlash } from './utils/url'
import { EthAddress, makeEthAddress, makeHexEthAddress } from './utils/eth'
import { wrapBytesWithHelpers } from './utils/bytes'
import { assertBatchId, assertNonNegativeInteger, assertReference } from './utils/type'
import { setJsonData, getJsonData } from './feed/json'
import { makeCollectionFromFS, makeCollectionFromFileList } from './utils/collection'
import { PostageBatchOptions, STAMPS_DEPTH_MAX, STAMPS_DEPTH_MIN } from './types'

/**
* The Bee class provides a way of interacting with the Bee APIs based on the provided url
Expand Down Expand Up @@ -241,7 +242,7 @@ export class Bee {
*
* @param reference Bee data reference
*/
async pin(reference: Reference | string): Promise<BeeResponse> {
async pin(reference: Reference | string): Promise<void> {
assertReference(reference)

return pinning.pin(this.url, reference)
Expand All @@ -252,7 +253,7 @@ export class Bee {
*
* @param reference Bee data reference
*/
async unpin(reference: Reference | string): Promise<BeeResponse> {
async unpin(reference: Reference | string): Promise<void> {
assertReference(reference)

return pinning.unpin(this.url, reference)
Expand Down Expand Up @@ -314,7 +315,7 @@ export class Bee {
target: AddressPrefix,
data: string | Uint8Array,
recipient?: PublicKey,
): Promise<BeeResponse> {
): Promise<void> {
assertBatchId(postageBatchId)

return pss.send(this.url, topic, target, data, postageBatchId, recipient)
Expand Down
12 changes: 4 additions & 8 deletions src/modules/pinning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ export interface GetAllPinResponse {
* @param url Bee URL
* @param reference Bee data reference
*/
export async function pin(url: string, reference: Reference): Promise<BeeResponse> {
const response = await safeAxios<BeeResponse>({
export async function pin(url: string, reference: Reference): Promise<void> {
await safeAxios<BeeResponse>({
method: 'post',
responseType: 'json',
url: `${url}${PINNING_ENDPOINT}/${reference}`,
})

return response.data
}

/**
Expand All @@ -29,14 +27,12 @@ export async function pin(url: string, reference: Reference): Promise<BeeRespons
* @param url Bee URL
* @param reference Bee data reference
*/
export async function unpin(url: string, reference: Reference): Promise<BeeResponse> {
const response = await safeAxios<BeeResponse>({
export async function unpin(url: string, reference: Reference): Promise<void> {
await safeAxios<BeeResponse>({
method: 'delete',
responseType: 'json',
url: `${url}${PINNING_ENDPOINT}/${reference}`,
})

return response.data
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/modules/pss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ export async function send(
data: string | Uint8Array,
postageBatchId: BatchId,
recipient?: PublicKey,
): Promise<BeeResponse> {
const response = await safeAxios<BeeResponse>({
): Promise<void> {
await safeAxios<BeeResponse>({
method: 'post',
url: `${url}${endpoint}/send/${topic}/${target.slice(0, 4)}`,
data: await prepareData(data),
responseType: 'json',
params: { recipient },
headers: extractUploadHeaders(postageBatchId),
})

return response.data
}

/**
Expand Down
18 changes: 3 additions & 15 deletions test/integration/bee-class.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { join } from 'path'
import {
beeDebugUrl,
beePeerUrl,
beeUrl,
commonMatchers,
createdResponse,
getPostageBatch,
okResponse,
PSS_TIMEOUT,
} from '../utils'
import { beeDebugUrl, beePeerUrl, beeUrl, commonMatchers, getPostageBatch, PSS_TIMEOUT } from '../utils'
import '../../src'
import type { Address } from '../../src/types'

Expand Down Expand Up @@ -83,7 +74,7 @@ describe('Bee class - in browser', () => {
)
expect(fileHash).toBeHashReference()
//pinning
const pinResult = await page.evaluate(
await page.evaluate(
async (BEE_URL, fileHash) => {
const bee = new window.BeeJs.Bee(BEE_URL)

Expand All @@ -92,10 +83,9 @@ describe('Bee class - in browser', () => {
BEE_URL,
fileHash,
)
expect(pinResult).toBeOneOf([createdResponse, okResponse])

//unpinning
const unpinResult = await page.evaluate(
await page.evaluate(
async (BEE_URL, fileHash) => {
const bee = new window.BeeJs.Bee(BEE_URL)

Expand All @@ -104,8 +94,6 @@ describe('Bee class - in browser', () => {
BEE_URL,
fileHash,
)
expect(pinResult).toBeOneOf([createdResponse, okResponse])
expect(unpinResult).toEqual(okResponse)
})

it('should get state of uploading on uploading file', async () => {
Expand Down
29 changes: 8 additions & 21 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import {
beePeerUrl,
beeUrl,
commonMatchers,
createdResponse,
FEED_TIMEOUT,
getPostageBatch,
okResponse,
POSTAGE_BATCH_TIMEOUT,
PSS_TIMEOUT,
randomByteArray,
Expand Down Expand Up @@ -127,8 +125,7 @@ describe('Bee class', () => {
const content = new Uint8Array([1, 2, 3])
const hash = await bee.uploadFile(getPostageBatch(), content)

const pinResponse = await bee.pin(hash)
expect(pinResponse).toBeOneOf([createdResponse, okResponse])
await bee.pin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong

const pinnedChunks = await bee.getAllPins()
expect(pinnedChunks).toBeType('array')
Expand All @@ -144,8 +141,7 @@ describe('Bee class', () => {
const statusBeforePinning = bee.getPin(hash)
await expect(statusBeforePinning).rejects.toThrowError('Not Found')

const pinResponse = await bee.pin(hash)
expect(pinResponse).toBeOneOf([createdResponse, okResponse])
await bee.pin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong

const statusAfterPinning = await bee.getPin(hash)
expect(statusAfterPinning).toHaveProperty('reference', hash)
Expand All @@ -156,34 +152,25 @@ describe('Bee class', () => {

const hash = await bee.uploadFile(getPostageBatch(), content)

const pinResponse = await bee.pin(hash)
expect(pinResponse).toBeOneOf([createdResponse, okResponse])

const unpinResponse = await bee.unpin(hash)
expect(unpinResponse).toEqual(okResponse)
await bee.pin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
await bee.unpin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it('should pin and unpin collection', async () => {
const path = './test/data/'
const hash = await bee.uploadFilesFromDirectory(getPostageBatch(), path)

const pinResponse = await bee.pin(hash)
expect(pinResponse).toBeOneOf([createdResponse, okResponse])

const unpinResponse = await bee.unpin(hash)
expect(unpinResponse).toEqual(okResponse)
await bee.pin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
await bee.unpin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it('should pin and unpin data', async () => {
const content = new Uint8Array([1, 2, 3])

const hash = await bee.uploadData(getPostageBatch(), content)

const pinResponse = await bee.pin(hash)
expect(pinResponse).toBeOneOf([createdResponse, okResponse])

const unpinResponse = await bee.unpin(hash)
expect(unpinResponse).toEqual(okResponse)
await bee.pin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
await bee.unpin(hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})
})

Expand Down
39 changes: 10 additions & 29 deletions test/integration/modules/pinning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import * as chunk from '../../../src/modules/chunk'
import {
beeUrl,
invalidReference,
okResponse,
randomByteArray,
testChunkData,
testChunkHash,
ERR_TIMEOUT,
getPostageBatch,
createdResponse,
commonMatchers,
} from '../../utils'
import { Collection } from '../../../src/types'
Expand All @@ -25,16 +23,12 @@ describe('modules/pin', () => {

it('should pin an existing file', async () => {
const hash = await bzz.uploadFile(BEE_URL, randomData, getPostageBatch())
const response = await pinning.pin(BEE_URL, hash)

expect(response).toBeOneOf([createdResponse, okResponse])
await pinning.pin(BEE_URL, hash)
})

it('should unpin an existing file', async () => {
const hash = await bzz.uploadFile(BEE_URL, randomData, getPostageBatch())
const response = await pinning.unpin(BEE_URL, hash)

expect(response).toEqual(okResponse)
await pinning.unpin(BEE_URL, hash)
})

it(
Expand Down Expand Up @@ -64,16 +58,12 @@ describe('modules/pin', () => {

it('should pin an existing collection', async () => {
const hash = await bzz.uploadCollection(BEE_URL, testCollection, getPostageBatch())
const response = await pinning.pin(BEE_URL, hash)

expect(response).toBeOneOf([createdResponse, okResponse])
await pinning.pin(BEE_URL, hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it('should unpin an existing collections', async () => {
const hash = await bzz.uploadCollection(BEE_URL, testCollection, getPostageBatch())
const response = await pinning.unpin(BEE_URL, hash)

expect(response).toEqual(okResponse)
await pinning.unpin(BEE_URL, hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it(
Expand All @@ -94,16 +84,12 @@ describe('modules/pin', () => {

it('should pin existing data', async () => {
const hash = await bytes.upload(BEE_URL, randomData, getPostageBatch())
const response = await pinning.pin(BEE_URL, hash)

expect(response).toBeOneOf([createdResponse, okResponse])
await pinning.pin(BEE_URL, hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it('should unpin existing data', async () => {
const hash = await bytes.upload(BEE_URL, randomData, getPostageBatch())
const response = await pinning.pin(BEE_URL, hash)

expect(response).toBeOneOf([createdResponse, okResponse])
await pinning.pin(BEE_URL, hash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it(
Expand All @@ -124,16 +110,14 @@ describe('modules/pin', () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })

const pinningResponse = await pinning.pin(BEE_URL, testChunkHash)
expect(pinningResponse).toBeOneOf([createdResponse, okResponse])
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 pinningResponse = await pinning.unpin(BEE_URL, testChunkHash)
expect(pinningResponse).toEqual(okResponse)
await pinning.unpin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it(
Expand All @@ -152,9 +136,7 @@ describe('modules/pin', () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })

const pinningResponse = await pinning.pin(BEE_URL, testChunkHash)
expect(pinningResponse).toBeOneOf([createdResponse, okResponse])

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)
expect(pinningStatus.reference).toEqual(testChunkHash)
})
Expand All @@ -167,8 +149,7 @@ describe('modules/pin', () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })

const pinningResponse = await pinning.pin(BEE_URL, testChunkHash)
expect(pinningResponse).toBeOneOf([createdResponse, okResponse])
await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})
})
})
6 changes: 2 additions & 4 deletions test/integration/modules/pss.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as pss from '../../../src/modules/pss'
import * as connectivity from '../../../src/modules/debug/connectivity'
import { beeDebugUrl, beePeerUrl, beeUrl, createdResponse, getPostageBatch, PSS_TIMEOUT } from '../../utils'
import { beeDebugUrl, beePeerUrl, beeUrl, getPostageBatch, PSS_TIMEOUT } from '../../utils'

const BEE_URL = beeUrl()
const BEE_PEER_URL = beePeerUrl()
Expand All @@ -18,9 +18,7 @@ describe('modules/pss', () => {
expect(peers.length).toBeGreaterThan(0)

const target = peers[0].address
const response = await pss.send(BEE_URL, topic, target, message, getPostageBatch())

expect(response).toEqual(createdResponse)
await pss.send(BEE_URL, topic, target, message, getPostageBatch()) // Nothing is asserted as nothing is returned, will throw error if something is wrong
},
PSS_TIMEOUT,
)
Expand Down