diff --git a/src/api/bytes.ts b/src/api/bytes.ts index e2265459..84022cf0 100644 --- a/src/api/bytes.ts +++ b/src/api/bytes.ts @@ -1,5 +1,6 @@ import { BatchId, Bytes, Reference } from '@ethersphere/core-sdk' import { Optional } from 'cafe-utility' +import { Readable } from 'stream' import type { BeeRequestOptions, DownloadOptions, RedundantUploadOptions, ReferenceInformation } from '../types' import { UploadResult } from '../types' import { UploadResultBody } from '../types/schema/upload' @@ -17,7 +18,7 @@ const endpoint = 'bytes' export async function upload( requestOptions: BeeRequestOptions, - data: string | Uint8Array, + data: string | Uint8Array | Blob | Readable, postageBatchId: BatchId, options?: RedundantUploadOptions, ): Promise { diff --git a/src/modules/data.ts b/src/modules/data.ts index e947f657..e70b108f 100644 --- a/src/modules/data.ts +++ b/src/modules/data.ts @@ -1,4 +1,7 @@ import { BatchId, Bytes, Reference } from '@ethersphere/core-sdk' +import { Readable } from 'stream' +import * as bytes from '../api/bytes' +import * as stewardship from '../api/stewardship' import type { BeeRequestOptions, DownloadOptions, @@ -9,8 +12,6 @@ import type { import { ResourceLocator } from '../utils/resource-locator' import { DownloadOptionsSchema, RedundantUploadOptionsSchema } from '../utils/schema' import { assertData } from '../utils/type' -import * as bytes from '../api/bytes' -import * as stewardship from '../api/stewardship' import type { BeeContext } from './context' /** @@ -33,7 +34,7 @@ export class Data { */ async upload( postageBatchId: BatchId | Uint8Array | string, - data: string | Uint8Array, + data: string | Uint8Array | Blob | Readable, options?: RedundantUploadOptions, requestOptions?: BeeRequestOptions, ): Promise { diff --git a/src/utils/http.ts b/src/utils/http.ts index 9ab5a631..94d3be2d 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -39,7 +39,13 @@ export interface BeeRequestConfig extends RequestInit { * @param config Internal settings and/or Bee settings */ export async function http(options: BeeRequestOptions, config: BeeRequestConfig): Promise> { - const requestConfig: BeeRequestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options) + const rawData = config.data + const requestConfig: BeeRequestConfig = Objects.deepMerge3( + DEFAULT_HTTP_CONFIG, + { ...config, data: undefined }, + options, + ) + requestConfig.data = rawData if (options.signal) { requestConfig.signal = options.signal diff --git a/src/utils/type.ts b/src/utils/type.ts index b21888fe..7949f940 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -36,9 +36,9 @@ export function isTag(value: unknown): value is Tag { * @param value * @throws TypeError if not valid */ -export function assertData(value: unknown): asserts value is string | Uint8Array { - if (typeof value !== 'string' && !(value instanceof Uint8Array)) { - throw new TypeError('Data must be either string or Uint8Array!') +export function assertData(value: unknown): asserts value is string | Uint8Array | Blob | stream.Readable { + if (typeof value !== 'string' && !(value instanceof Uint8Array) && !(value instanceof Blob) && !isReadable(value)) { + throw new TypeError('Data must be either string, Uint8Array, Blob or Readable!') } }