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

Add raw upload #191

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
41 changes: 33 additions & 8 deletions src/ExposedTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -96,6 +121,7 @@ export declare interface BlobMultipartFormDataFile {

export declare interface BlobUploadRequest
extends BlobBaseRequest,
BlobRequestBody,
BlobRequestMimeType,
BlobRequestMethod,
BlobRequestReturnResponse {
Expand Down Expand Up @@ -159,7 +185,6 @@ export declare interface BlobUploadResponse extends BlobUnmanagedData {}

export type BlobFetchInput = BlobFetchRequest &
BlobRequestSettings &
AndroidFetchSettings &
IOSFetchSettings;

export type BlobUploadInput = BlobUploadRequest & BlobRequestSettings;
57 changes: 40 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type BlobFetchNativeInput = BlobFetchInput & BlobRequestTask;

type BlobUploadNativeInput = BlobUploadInput & BlobRequestTask;

type BlobUploadType = 'raw' | 'multipart';

type BlobUploadMultipartInput = BlobMultipartMapUploadRequest &
BlobRequestSettings;

Expand Down Expand Up @@ -248,9 +250,16 @@ const uploadParts = <T extends BlobUploadMultipartInputWithTask>(
input.signal
);

const uploadBlob = <T extends BlobUploadNativeInput>(input: Readonly<T>) => {
const uploadBlob = <T extends BlobUploadNativeInput>(
input: Readonly<T>,
type: BlobUploadType
) => {
const { absoluteFilePath, filename, mimeType, multipartName } = input;

if (type !== 'multipart') {
throw new Error('Raw uploadBlob not implemented');
}

return uploadParts({
...input,
parts: {
Expand All @@ -265,7 +274,6 @@ const uploadBlob = <T extends BlobUploadNativeInput>(input: Readonly<T>) => {
},
});
};

const onProgress = (
taskId: string,
fn: (e: BlobProgressEvent) => void,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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: (
Expand Down