@common.js/is-network-error 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/index.d.ts +26 -0
- package/index.js +34 -0
- package/license +9 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @common.js/is-network-error
|
|
2
|
+
|
|
3
|
+
The [is-network-error](https://www.npmjs.com/package/is-network-error) package exported as CommonJS modules.
|
|
4
|
+
|
|
5
|
+
Exported from [is-network-error@1.0.0](https://www.npmjs.com/package/is-network-error/v/1.0.0) using https://github.com/etienne-martin/common.js.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Check if an error is a [Fetch network error](https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions)
|
|
3
|
+
|
|
4
|
+
@return Returns `true` if the given value is a Fetch network error, otherwise `false`.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import isNetworkError from 'is-network-error';
|
|
9
|
+
|
|
10
|
+
async function getUnicorns() {
|
|
11
|
+
try {
|
|
12
|
+
const response = await fetch('unicorns.json');
|
|
13
|
+
return await response.json();
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (isNetworkError(error)) {
|
|
16
|
+
return localStorage.getItem('…');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(await getUnicorns());
|
|
24
|
+
```
|
|
25
|
+
*/
|
|
26
|
+
export default function isNetworkError(value: unknown): value is TypeError;
|
package/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return isNetworkError;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var objectToString = Object.prototype.toString;
|
|
12
|
+
var isError = function(value) {
|
|
13
|
+
return objectToString.call(value) === "[object Error]";
|
|
14
|
+
};
|
|
15
|
+
var errorMessages = new Set([
|
|
16
|
+
"Failed to fetch",
|
|
17
|
+
"NetworkError when attempting to fetch resource.",
|
|
18
|
+
"The Internet connection appears to be offline.",
|
|
19
|
+
"Load failed",
|
|
20
|
+
"Network request failed",
|
|
21
|
+
"fetch failed"
|
|
22
|
+
]);
|
|
23
|
+
function isNetworkError(error) {
|
|
24
|
+
var isValid = error && isError(error) && error.name === "TypeError" && typeof error.message === "string";
|
|
25
|
+
if (!isValid) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
// We do an extra check for Safari 17+ as it has a very generic error message.
|
|
29
|
+
// Network errors in Safari have no stack.
|
|
30
|
+
if (error.message === "Load failed") {
|
|
31
|
+
return error.stack === undefined;
|
|
32
|
+
}
|
|
33
|
+
return errorMessages.has(error.message);
|
|
34
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/is-network-error",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "is-network-error package exported as CommonJS modules",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "etienne-martin/common.js",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=16"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "xo && ava && tsd"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"ava": "^5.3.1",
|
|
21
|
+
"tsd": "^0.29.0",
|
|
22
|
+
"xo": "^0.56.0"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
25
|
+
"dependencies": {},
|
|
26
|
+
"types": "./index.d.ts",
|
|
27
|
+
"main": "./index.js"
|
|
28
|
+
}
|