2020-12-01 20:19:48 +00:00
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
import { IFileUpload } from './interfaces';
|
|
|
|
|
|
|
|
type TMethods = 'POST' | 'GET' | 'DELETE' | 'PUT' | 'post' | 'get' | 'delete' | 'put';
|
|
|
|
|
2020-12-01 20:19:48 +00:00
|
|
|
class FileUpload {
|
2022-01-12 12:54:04 +00:00
|
|
|
fetch = (method: TMethods, url: string, headers: { [key: string]: string }, data: IFileUpload[]) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const formData = data.map(item => {
|
2020-12-01 20:19:48 +00:00
|
|
|
if (item.uri) {
|
|
|
|
return {
|
|
|
|
name: item.name,
|
|
|
|
type: item.type,
|
|
|
|
filename: item.filename,
|
|
|
|
data: RNFetchBlob.wrap(decodeURI(item.uri))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
|
|
|
|
return RNFetchBlob.fetch(method, url, headers, formData);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-12-01 20:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fileUpload = new FileUpload();
|
|
|
|
export default fileUpload;
|