Skip to content

Commit 20936ba

Browse files
committed
feat: add excluding urls and cleanup
1 parent d20f8d7 commit 20936ba

File tree

7 files changed

+61
-38
lines changed

7 files changed

+61
-38
lines changed
File renamed without changes.
File renamed without changes.

example/src/App.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import useNetworkStatusCode from 'use-network-status-code';
33
import axios from 'axios';
44

55
const App = () => {
6-
const { networkStatusCode, clearStatus } = useNetworkStatusCode({
7-
urls: [
6+
const { networkStatusCode } = useNetworkStatusCode({
7+
baseURLs: [
88
'https://ghibliapi.herokuapp.com/film',
99
'https://ghibliapi.herokuapp.com/filmsd'
10-
]
10+
],
11+
excludingURLs: ['https://ghibliapi.herokuapp.com/login']
1112
});
1213

1314
useEffect(() => {
@@ -21,11 +22,15 @@ const App = () => {
2122

2223
const id = setTimeout(async () => {
2324
try {
24-
await axios.get('https://ghibliapi.herokuapp.com/filmsd');
25+
await axios.get('https://ghibliapi.herokuapp.com/login');
2526
} catch (error) {
2627
console.log('error ');
2728
}
2829
}, 4000);
30+
31+
return () => {
32+
clearTimeout(id);
33+
};
2934
}, []);
3035

3136
return <div>See this {JSON.stringify(networkStatusCode)} </div>;

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2-
collectCoverage: true,
3-
collectCoverageFrom: ['**/*.{js,jsx}', '!**/node_modules/**', '!**/vendor/**']
2+
moduleFileExtensions: ['ts', 'js'],
3+
testRegex: '/__tests__/.*\\.(ts|js)$',
4+
transformIgnorePatterns: ['<rootDir>/node_modules/']
45
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"npm": ">=5"
1818
},
1919
"scripts": {
20-
"test": "react-scripts test",
20+
"test": "cross-env CI=1 react-scripts test --env=jsdom",
2121
"test:watch": "react-scripts test --env=jsdom",
2222
"build": "rollup -c && tsc -d --emitDeclarationOnly --noEmit false --declarationDir dist",
2323
"start": "rollup -c -w",

src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import interceptNetwork from './interceptNetwork';
44

55
declare namespace useNetworkStatusCodeNamespace {
66
export type Props = {
7-
urls: Array<string>;
7+
baseURLs: Array<string>;
8+
excludingURLs?: Array<string>;
89
};
910

1011
export type response = {
@@ -21,13 +22,16 @@ const convertArrayOfURLsToObject = (urls: Array<string>) => {
2122

2223
const useNetworkStatusCode = (props: useNetworkStatusCodeNamespace.Props) => {
2324
const [networkStatusCode, setNetworkStatusCode] = React.useState(() => {
24-
return convertArrayOfURLsToObject(props.urls);
25+
return convertArrayOfURLsToObject(props.baseURLs);
2526
});
2627

2728
const handleStatusAndUrl = (url: string, statusCode: number) => {
28-
const isValidURL = props.urls.some((validURL) => {
29+
const isValidURL = props.baseURLs.some((validURL) => {
2930
const validURLLength = validURL.length;
30-
return url.substr(0, validURLLength) === validURL;
31+
return (
32+
url.substr(0, validURLLength) === validURL &&
33+
!props?.excludingURLs?.includes(url)
34+
);
3135
});
3236

3337
if (isValidURL) {
@@ -49,7 +53,7 @@ const useNetworkStatusCode = (props: useNetworkStatusCodeNamespace.Props) => {
4953
});
5054
} else {
5155
setNetworkStatusCode(() => {
52-
return convertArrayOfURLsToObject(props.urls);
56+
return convertArrayOfURLsToObject(props.baseURLs);
5357
});
5458
}
5559
};

tsconfig.json

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
{
2-
"compilerOptions": {
3-
"module": "esnext",
4-
"target": "es5",
5-
"lib": ["es6", "dom", "es2016", "es2017"],
6-
"sourceMap": true,
7-
"allowJs": false,
8-
"jsx": "preserve",
9-
"moduleResolution": "node",
10-
"forceConsistentCasingInFileNames": true,
11-
"noImplicitReturns": true,
12-
"noImplicitThis": true,
13-
"noImplicitAny": true,
14-
"strictNullChecks": true,
15-
"suppressImplicitAnyIndexErrors": true,
16-
"noUnusedLocals": true,
17-
"noUnusedParameters": true,
18-
"skipLibCheck": true,
19-
"esModuleInterop": true,
20-
"allowSyntheticDefaultImports": true,
21-
"strict": true,
22-
"resolveJsonModule": true,
23-
"isolatedModules": true,
24-
"noEmit": true
25-
},
26-
"include": ["src", "tests"],
27-
"exclude": ["node_modules", "build", "dist", "example", "rollup.config.js"]
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "es5",
5+
"lib": [
6+
"es6",
7+
"dom",
8+
"es2016",
9+
"es2017"
10+
],
11+
"sourceMap": true,
12+
"allowJs": false,
13+
"jsx": "preserve",
14+
"moduleResolution": "node",
15+
"forceConsistentCasingInFileNames": true,
16+
"noImplicitReturns": true,
17+
"noImplicitThis": true,
18+
"noImplicitAny": true,
19+
"strictNullChecks": true,
20+
"suppressImplicitAnyIndexErrors": true,
21+
"noUnusedLocals": true,
22+
"noUnusedParameters": true,
23+
"skipLibCheck": true,
24+
"esModuleInterop": true,
25+
"allowSyntheticDefaultImports": true,
26+
"strict": true,
27+
"resolveJsonModule": true,
28+
"isolatedModules": true,
29+
"noEmit": true
30+
},
31+
"exclude": [
32+
"node_modules",
33+
"build",
34+
"dist",
35+
"example",
36+
"rollup.config.js"
37+
],
38+
"include": [
39+
"src"
40+
]
2841
}

0 commit comments

Comments
 (0)