diff --git a/src/ExposedTypes.tsx b/src/ExposedTypes.tsx index 63694afc..507a11e5 100644 --- a/src/ExposedTypes.tsx +++ b/src/ExposedTypes.tsx @@ -4,6 +4,10 @@ * This source code is licensed under the MPL-2.0 license found in the * LICENSE file in the root directory of this source tree. */ +export declare interface BlobRequestBody { + readonly body?: BlobBodyData; +} + export declare interface BlobRequestHeaders { readonly headers?: { [key: string]: string }; } @@ -33,15 +37,30 @@ export declare interface BlobBaseRequest BlobRequestOnProgress, BlobRequestUrl {} -export declare interface BlobFetchRequest - extends BlobBaseRequest, - BlobRequestMimeType, - BlobRequestMethod, - AndroidFetchSettings, - IOSFetchSettings { +export declare interface BlobRequestFilename { readonly filename: string; } +export declare interface BlobRawFetchRequest + extends BlobBaseRequest, + BlobRequestBody, + BlobRequestFilename, + BlobRequestMethod, + BlobRequestMimeType, + AndroidFetchWithoutManagerSettings, + IOSFetchSettings {} + +export declare interface BlobAndroidManagerFetchRequest + extends BlobBaseRequest, + BlobRequestFilename, + BlobRequestMimeType, + AndroidFetchWithManagerSettings, + IOSFetchSettings {} + +export type BlobFetchRequest = + | BlobRawFetchRequest + | BlobAndroidManagerFetchRequest; + export declare interface AndroidDownloadManagerToggle { readonly useDownloadManager?: boolean; } @@ -60,12 +79,17 @@ export type TargetType = 'cache' | 'data'; export declare interface TargetSettings { target?: TargetType; } -export declare interface AndroidFetchSettings { + +export declare interface AndroidFetchWithManagerSettings { readonly android?: AndroidDownloadManagerToggle & AndroidDownloadManager & TargetSettings; } +export declare interface AndroidFetchWithoutManagerSettings { + readonly android?: TargetSettings; +} + export declare interface IOSFetchSettings { readonly ios?: TargetSettings; } @@ -76,6 +100,7 @@ export declare interface BlobProgressEvent { } export declare type BlobMultipartFormData = string | { [key: string]: any }; +export declare type BlobBodyData = string | { [key: string]: any }; export declare type BlobMultipartType = 'file' | 'string'; export declare type BlobMultipart = { payload: BlobMultipartFormData | BlobMultipartFormDataFile; @@ -96,6 +121,7 @@ export declare interface BlobMultipartFormDataFile { export declare interface BlobUploadRequest extends BlobBaseRequest, + BlobRequestBody, BlobRequestMimeType, BlobRequestMethod, BlobRequestReturnResponse { @@ -159,7 +185,6 @@ export declare interface BlobUploadResponse extends BlobUnmanagedData {} export type BlobFetchInput = BlobFetchRequest & BlobRequestSettings & - AndroidFetchSettings & IOSFetchSettings; export type BlobUploadInput = BlobUploadRequest & BlobRequestSettings; diff --git a/src/index.tsx b/src/index.tsx index 107a6ad0..dedce6c4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -42,6 +42,8 @@ type BlobFetchNativeInput = BlobFetchInput & BlobRequestTask; type BlobUploadNativeInput = BlobUploadInput & BlobRequestTask; +type BlobUploadType = 'raw' | 'multipart'; + type BlobUploadMultipartInput = BlobMultipartMapUploadRequest & BlobRequestSettings; @@ -248,9 +250,16 @@ const uploadParts = ( input.signal ); -const uploadBlob = (input: Readonly) => { +const uploadBlob = ( + input: Readonly, + type: BlobUploadType +) => { const { absoluteFilePath, filename, mimeType, multipartName } = input; + if (type !== 'multipart') { + throw new Error('Raw uploadBlob not implemented'); + } + return uploadParts({ ...input, parts: { @@ -265,7 +274,6 @@ const uploadBlob = (input: Readonly) => { }, }); }; - const onProgress = ( taskId: string, fn: (e: BlobProgressEvent) => void, @@ -278,13 +286,19 @@ const onProgress = ( onProgress: fn, taskId, }), - uploadBlob: (input: BlobUploadRequest) => - uploadBlob({ - ...input, - ...requestSettings, - onProgress: fn, - taskId, - }), + /** + * @deprecated This behavior of this method will change from multipart to raw in a future version. For multipart upload use `uploadBlobMultipart` instead. + */ + uploadBlob: (input: BlobUploadRequest, type: BlobUploadType = 'multipart') => + uploadBlob( + { + ...input, + ...requestSettings, + onProgress: fn, + taskId, + }, + type + ), uploadParts: (input: BlobUploadMultipartInput) => uploadParts({ ...input, @@ -327,12 +341,18 @@ const settings = (taskId: string, requestSettings: BlobRequestSettings) => ({ }), onProgress: (fn: (e: BlobProgressEvent) => void) => onProgress(taskId, fn, requestSettings), - uploadBlob: (input: BlobUploadRequest) => - uploadBlob({ - ...input, - ...requestSettings, - taskId, - }), + /** + * @deprecated This behavior of this method will change from multipart to raw in a future version. For multipart upload use `uploadBlobMultipart` instead. + */ + uploadBlob: (input: BlobUploadRequest, type: BlobUploadType = 'multipart') => + uploadBlob( + { + ...input, + ...requestSettings, + taskId, + }, + type + ), uploadParts: (input: BlobUploadMultipartInput) => uploadParts({ ...input, @@ -355,8 +375,11 @@ export default { onProgress: (fn: (e: BlobProgressEvent) => void) => onProgress(createTaskId(), fn), settings: (input: BlobRequestSettings) => settings(createTaskId(), input), - uploadBlob: (input: BlobUploadInput) => - uploadBlob({ ...input, taskId: createTaskId() }), + /** + * @deprecated This behavior of this method will change from multipart to raw in a future version. For multipart upload use `uploadBlobMultipart` instead. + */ + uploadBlob: (input: BlobUploadInput, type: BlobUploadType = 'multipart') => + uploadBlob({ ...input, taskId: createTaskId() }, type), uploadParts: (input: BlobUploadMultipartInput) => uploadParts({ ...input, taskId: createTaskId() }), useDownloadManagerOnAndroid: (