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

se agrego el metodo downloadFile #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
96 changes: 95 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import {isString, URLMaker, parsePathParams} from './utils/helpers.js';
import RNFS from 'react-native-fs';
import {
isString,
URLMaker,
parsePathParams,
promiseWrapper,
isObject,
isFunction,
isNumber,
} from './utils/helpers.js';
import crashlytics from './utils/crashlytics.js';
import updateHeaders from './utils/updateHeaders.js';
import parseQueryParams from './utils/parseQueryParams.js';
Expand Down Expand Up @@ -381,6 +390,91 @@ class Request {
return Promise.reject(formattedError);
}
}

/**
* @name downloadFile
* @description Download a file and save it in the phone using react-native-fs package
* @param {string} fileName - File name for the document with its extension | .zip .pdf etc
* @param {string} folderPath - The path for folder where the document is stored
* @param {string} url - url where file is stored
* @param {object} headers - An object of headers to be passed to the server
* @param {function} begin - callback it is invoked once at the beginning of the process with the response headers and download data
* @param {function} progress - callback that receives the download trace
* @param {number} progressInterval - it will return progress events in the maximum frequency of progressInterval
* @param {number} progressDivider - it will return progress events that divided by progressDivider
* @returns {Promise} Response from package | {jobId: number, statusCode: number, bytesWritten: number}
* @example
* const request = new Request({JANIS_ENV: 'janis_dev'})
* const pdfFile = await request.downloadFile({
url: `https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf`,
fileName: `dummy.pdf`,
folderPath: `${RNFS.DocumentDirectoryPath}/pdfs`,
progress: ({jobId, contentLength, bytesWritten }) => {},
progressDivider: 10
})
*/
async downloadFile({
fileName,
folderPath,
url,
headers,
begin,
progress,
progressInterval,
progressDivider,
...props
}) {
try {
if (!fileName || !isString(fileName)) throw new Error('fileName is not valid');
if (!folderPath || !isString(folderPath)) throw new Error('folderPath is not valid');
if (!url || !isString(url)) throw new Error('url is not valid');

const validHeaders = headers && isObject(headers);
const validBegin = begin && isFunction(begin);
const validProgress = progress && isFunction(progress);
const validProgressInterval = progressInterval && isNumber(progressInterval);
const validProgressDivider = progressDivider && isNumber(progressDivider);

const [downloadRes, downloadErr] = await promiseWrapper(
RNFS.downloadFile({
fromUrl: url,
toFile: `${folderPath}/${fileName}`,
...(validHeaders && {headers}),
...(validBegin && {begin}),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WilliamSaya-lvl30 acá no se tendrían que usar los mismos headers que en las demás request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GonzaFran para lo que se utiliza no suele requerir headers, se agrego esa validación solo para cubrir hipoteticos casos futuros.

...(validProgress && {progress}),
...(validProgressInterval && {progressInterval}),
...(validProgressDivider && {progressDivider}),
...{...props},
}).promise,
);

if (downloadErr) throw new Error(downloadErr.message);
if (
!isObject(downloadRes) ||
!downloadRes ||
downloadRes.bytesWritten === 0 ||
downloadRes.statusCode !== 200
)
throw new Error('The file download did not complete');

return {
result: downloadRes,
};
} catch (error) {
const formattedError = {
result: {
message: error.message,
},
};

crashlytics.recordError(
error,
`Error at downloadAndFile: url: ${url} reason: ${error.message}`,
);

return Promise.reject(formattedError);
}
}
}

export default Request;
2 changes: 2 additions & 0 deletions lib/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export const isArray = (arr) => !!(arr instanceof Array);

export const isNumber = (num) => typeof num === 'number' && !Number.isNaN(Number(num));

export const isFunction = (fn) => !!({}.toString.call(fn) === '[object Function]');

export const promiseWrapper = (promise) =>
promise.then((data) => [data, null]).catch((error) => Promise.resolve([null, error]));

Expand Down
Loading
Loading