Skip to content

Commit

Permalink
chore: doc updated, bable parser replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-j22 committed Aug 11, 2022
1 parent 44913bb commit e134441
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 48 deletions.
55 changes: 47 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ A file download utility for react native
npm install react-native-du
```

or

```sh
yarn add react-native-du
```

## Usage

```js
Expand All @@ -23,14 +29,14 @@ import {
export default function App() {
const downloadPdf = async () => {
try {
const id: any = await downloadFile(
'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg',
'cat.jpg',
'This is a cat '
);
const id = await downloadFile({
url: 'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg',
fileName: 'cat.jpg',
desc: 'This is a cat ',
});
console.log({ id });
const status = await downloadStatus(id[0]);
const idx: string = status[0];
const status = await downloadStatus(id?.[0] || '');
const idx: string = status?.[0] || '';
console.log({ status: DOWNLOAD_MANAGER_STATUS[idx] });
} catch (err) {
console.log(err);
Expand All @@ -56,8 +62,41 @@ const styles = StyleSheet.create({
marginVertical: 20,
},
});
```
## Functions
```js
async function downloadFile({
url,
fileName,
desc = '',
onPermissionDenied,
onError,
})
// params
{
url : 'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg' // request url,
fileName: 'cat.jpg' // file Name with extension
desc: 'Description of download manager' // optional
onPermissionDenied: ()=>{} // optional , this function gets called if storgae permission is denied
onError: (error)=>{} // optional , this function gets called if any exception is thrown
}

// returns
['downloadId'] or undefined


async function downloadStatus(downloadId: string)
// params
{
downloadId : 'id' // download id returned by downloadFile function,
}

// returns
['download_manager_status'] or undefined


// ...

```
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ PODS:
- React-jsinspector (0.68.2)
- React-logger (0.68.2):
- glog
- react-native-du (0.1.0):
- react-native-du (0.3.0):
- React-Core
- React-perflogger (0.68.2)
- React-RCTActionSheet (0.68.2):
Expand Down Expand Up @@ -554,7 +554,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: b7b553412f2ec768fe6c8f27cd6bafdb9d8719e6
React-jsinspector: c5989c77cb89ae6a69561095a61cce56a44ae8e8
React-logger: a0833912d93b36b791b7a521672d8ee89107aff1
react-native-du: 0bc489e24ad0daf7bdde52d7e05fee1ba9cc3c8a
react-native-du: 534db3510d63b4550e458df87eafe8eff2a87cde
React-perflogger: a18b4f0bd933b8b24ecf9f3c54f9bf65180f3fe6
React-RCTActionSheet: 547fe42fdb4b6089598d79f8e1d855d7c23e2162
React-RCTAnimation: bc9440a1c37b06ae9ebbb532d244f607805c6034
Expand Down
14 changes: 7 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
export default function App() {
const downloadPdf = async () => {
try {
const id: any = await downloadFile(
'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg',
'cat.jpg',
'This is a cat '
);
const id = await downloadFile({
url: 'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg',
fileName: 'cat.jpg',
desc: 'This is a cat ',
});
console.log({ id });
const status = await downloadStatus(id[0]);
const idx: string = status[0];
const status = await downloadStatus(id?.[0] || '');
const idx: string = status?.[0] || '';
console.log({ status: DOWNLOAD_MANAGER_STATUS[idx] });
} catch (err) {
console.log(err);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
},
"devDependencies": {
"@arkweid/lefthook": "^0.7.7",
"@babel/eslint-parser": "^7.18.2",
"@commitlint/config-conventional": "^17.0.2",
"@react-native-community/eslint-config": "^3.0.2",
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
"@types/react-native": "0.68.0",
"@typescript-eslint/parser": "^5.33.0",
"commitlint": "^17.0.2",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",
Expand Down Expand Up @@ -108,7 +108,7 @@
},
"eslintConfig": {
"root": true,
"parser": "@babel/eslint-parser",
"parser": "@typescript-eslint/parser",
"extends": [
"@react-native-community",
"prettier"
Expand Down
37 changes: 18 additions & 19 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
export type DOWNLOAD_MANAGER_STATUS_TYPE = {
[x: string]: string | undefined
}

export const DOWNLOAD_MANAGER_STATUS: DOWNLOAD_MANAGER_STATUS_TYPE = {
'1008':'ERROR_CANNOT_RESUME',
'1007':'ERROR_DEVICE_NOT_FOUND',
'1009':'ERROR_FILE_ALREADY_EXISTS',
'1001':'ERROR_FILE_ERROR',
'1004':'ERROR_HTTP_DATA_ERROR',
'1006':'ERROR_INSUFFICIENT_SPACE',
'1005':'ERROR_TOO_MANY_REDIRECTS',
'1002':'ERROR_UNHANDLED_HTTP_CODE',
'1000':'ERROR_UNKNOWN',
'3':'PAUSED_QUEUED_FOR_WIFI',
'4':'PAUSED_UNKNOWN',
'2':'PAUSED_WAITING_FOR_NETWORK',
'1':'PAUSED_WAITING_TO_RETRY',
'16':'STATUS_FAILED',
'8':'STATUS_SUCCESSFUL',
[x: string]: string | undefined;
};

export const DOWNLOAD_MANAGER_STATUS: DOWNLOAD_MANAGER_STATUS_TYPE = {
'1008': 'ERROR_CANNOT_RESUME',
'1007': 'ERROR_DEVICE_NOT_FOUND',
'1009': 'ERROR_FILE_ALREADY_EXISTS',
'1001': 'ERROR_FILE_ERROR',
'1004': 'ERROR_HTTP_DATA_ERROR',
'1006': 'ERROR_INSUFFICIENT_SPACE',
'1005': 'ERROR_TOO_MANY_REDIRECTS',
'1002': 'ERROR_UNHANDLED_HTTP_CODE',
'1000': 'ERROR_UNKNOWN',
'3': 'PAUSED_QUEUED_FOR_WIFI',
'4': 'PAUSED_UNKNOWN',
'2': 'PAUSED_WAITING_FOR_NETWORK',
'1': 'PAUSED_WAITING_TO_RETRY',
'16': 'STATUS_FAILED',
'8': 'STATUS_SUCCESSFUL',
};
28 changes: 19 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'react-native';

const LINKING_ERROR =
`The package 'react-native-download-util' doesn't seem to be linked. Make sure: \n\n` +
`The package 'react-native-du' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
Expand All @@ -34,13 +34,21 @@ function onErrorFn(error: any) {
console.log(error);
}

export async function downloadFile(
url: string,
fileName: string,
desc: string = '',
onPermissionDenied: () => void = onPermissionDeniedFn,
onError: (error: any) => void = onErrorFn
) {
export type downloadFileFnType = {
url: string;
fileName: string;
desc?: string;
onPermissionDenied?: () => void;
onError?: (error: any) => void;
};

export async function downloadFile({
url,
fileName,
desc = '',
onPermissionDenied = onPermissionDeniedFn,
onError = onErrorFn,
}: downloadFileFnType): Promise<string[] | void> {
if (isiOS) {
Linking.openURL(url);
} else {
Expand All @@ -54,7 +62,9 @@ export async function downloadFile(
}
}

export async function downloadStatus(downloadId: string) {
export async function downloadStatus(
downloadId: string
): Promise<string[] | void> {
return DownloadUtil?.getDownloadStatus(downloadId);
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@
semver "^7.3.7"
tsutils "^3.21.0"

"@typescript-eslint/parser@^5.30.5":
"@typescript-eslint/parser@^5.30.5", "@typescript-eslint/parser@^5.33.0":
version "5.33.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.33.0.tgz#26ec3235b74f0667414613727cb98f9b69dc5383"
integrity sha512-cgM5cJrWmrDV2KpvlcSkelTBASAs1mgqq+IUGKJvFxWrapHpaRy5EXPQz9YaKF3nZ8KY18ILTiVpUtbIac86/w==
Expand Down

0 comments on commit e134441

Please sign in to comment.