@merceas/cross-fetch 3.1.9
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/LICENSE +21 -0
- package/README.md +169 -0
- package/dist/browser-polyfill.js +532 -0
- package/dist/browser-ponyfill.js +554 -0
- package/dist/cross-fetch.js +2 -0
- package/dist/cross-fetch.js.map +1 -0
- package/dist/node-polyfill.js +11 -0
- package/dist/node-ponyfill.js +98 -0
- package/dist/react-native-polyfill.js +12 -0
- package/dist/react-native-ponyfill.js +6 -0
- package/index.d.ts +14 -0
- package/package.json +127 -0
- package/polyfill/package.json +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Leonardo Quixadá
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
cross-fetch<br>
|
|
2
|
+
[](https://www.npmjs.com/package/cross-fetch)
|
|
3
|
+
[](https://www.npmjs.com/package/cross-fetch)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://github.com/lquixada/cross-fetch/actions/workflows/ci.yml)
|
|
6
|
+
[](https://codecov.io/gh/lquixada/cross-fetch)
|
|
7
|
+
================
|
|
8
|
+
|
|
9
|
+
Universal WHATWG Fetch API for Node, Browsers and React Native. The scenario that cross-fetch really shines is when the same JavaScript codebase needs to run on different platforms.
|
|
10
|
+
|
|
11
|
+
- **Platform agnostic**: browsers, Node or React Native
|
|
12
|
+
- **Optional polyfill**: it's up to you if something is going to be added to the global object or not
|
|
13
|
+
- **Simple interface**: no instantiation, no configuration and no extra dependency
|
|
14
|
+
- **WHATWG compliant**: it works the same way wherever your code runs
|
|
15
|
+
- **TypeScript support**: better development experience with types.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
* * *
|
|
19
|
+
|
|
20
|
+
## Table of Contents
|
|
21
|
+
|
|
22
|
+
- [Install](#install)
|
|
23
|
+
- [Usage](#usage)
|
|
24
|
+
- [Demo & API](#demo--api)
|
|
25
|
+
- [FAQ](#faq)
|
|
26
|
+
- [Thanks](#thanks)
|
|
27
|
+
- [License](#license)
|
|
28
|
+
- [Author](#author)
|
|
29
|
+
|
|
30
|
+
* * *
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npm install --save cross-fetch
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
As a [ponyfill](https://github.com/sindresorhus/ponyfill):
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// Using ES6 modules with Babel or TypeScript
|
|
42
|
+
import fetch from 'cross-fetch';
|
|
43
|
+
|
|
44
|
+
// Using CommonJS modules
|
|
45
|
+
const fetch = require('cross-fetch');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
As a polyfill:
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
// Using ES6 modules
|
|
52
|
+
import 'cross-fetch/polyfill';
|
|
53
|
+
|
|
54
|
+
// Using CommonJS modules
|
|
55
|
+
require('cross-fetch/polyfill');
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
The CDN build is also available on unpkg:
|
|
60
|
+
|
|
61
|
+
```html
|
|
62
|
+
<script src="//unpkg.com/cross-fetch/dist/cross-fetch.js"></script>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
This adds the fetch function to the window object. Note that this is not UMD compatible.
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
* * *
|
|
69
|
+
|
|
70
|
+
## Usage
|
|
71
|
+
|
|
72
|
+
With [promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise):
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
import fetch from 'cross-fetch';
|
|
76
|
+
// Or just: import 'cross-fetch/polyfill';
|
|
77
|
+
|
|
78
|
+
fetch('//api.github.com/users/lquixada')
|
|
79
|
+
.then(res => {
|
|
80
|
+
if (res.status >= 400) {
|
|
81
|
+
throw new Error("Bad response from server");
|
|
82
|
+
}
|
|
83
|
+
return res.json();
|
|
84
|
+
})
|
|
85
|
+
.then(user => {
|
|
86
|
+
console.log(user);
|
|
87
|
+
})
|
|
88
|
+
.catch(err => {
|
|
89
|
+
console.error(err);
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
With [async/await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function):
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import fetch from 'cross-fetch';
|
|
97
|
+
// Or just: import 'cross-fetch/polyfill';
|
|
98
|
+
|
|
99
|
+
(async () => {
|
|
100
|
+
try {
|
|
101
|
+
const res = await fetch('//api.github.com/users/lquixada');
|
|
102
|
+
|
|
103
|
+
if (res.status >= 400) {
|
|
104
|
+
throw new Error("Bad response from server");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const user = await res.json();
|
|
108
|
+
|
|
109
|
+
console.log(user);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
console.error(err);
|
|
112
|
+
}
|
|
113
|
+
})();
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
> ⚠️ **Warning**: If you're in an environment that doesn't support Promises such as Internet Explorer, you must install an ES6 Promise compatible polyfill. [es6-promise](https://github.com/jakearchibald/es6-promise) is suggested.
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
## Demo & API
|
|
120
|
+
|
|
121
|
+
You can find a comprehensive doc at [Github's fetch](https://github.github.io/fetch/) page. If you want to play with cross-fetch, check our [**JSFiddle playground**](https://jsfiddle.net/lquixada/3ypqgacp/).
|
|
122
|
+
|
|
123
|
+
> **Tip**: Run the fiddle on various browsers and with different settings (for instance: cross-domain requests, wrong urls or text requests). Don't forget to open the console in the test suite page and play around.
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
## FAQ
|
|
127
|
+
|
|
128
|
+
#### Yet another fetch library?
|
|
129
|
+
|
|
130
|
+
I did a lot of research in order to find a fetch library that could be simple, cross-platform and provide polyfill as an option. There's a plethora of libs out there but none could match those requirements.
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
#### Why not isomorphic-fetch?
|
|
134
|
+
|
|
135
|
+
My preferred library used to be [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) but it has this [bug](https://github.com/matthew-andrews/isomorphic-fetch/issues/125) that prevents it from running in a react native environment. It seems unlikely to be fixed since there haven't been any new commits to it since 2016. That means dependencies are outdated as well.
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
#### Why polyfill might not be a good idea?
|
|
139
|
+
|
|
140
|
+
In a word? Risk. If the spec changes in the future, it might be problematic to debug. Read more about it on [sindresorhus's ponyfill](https://github.com/sindresorhus/ponyfill#how-are-ponyfills-better-than-polyfills) page. It's up to you if you're fine with it or not.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
#### How does cross-fetch work?
|
|
144
|
+
|
|
145
|
+
Just like isomorphic-fetch, it is just a proxy. If you're in node, it delivers you the [node-fetch](https://github.com/bitinn/node-fetch/) library, if you're in a browser or React Native, it delivers you the github's [whatwg-fetch](https://github.com/github/fetch/). The same strategy applies whether you're using polyfill or ponyfill.
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
## Who's Using It?
|
|
149
|
+
|
|
150
|
+
|[](https://www.nytimes.com/)|[](https://github.com/apollographql/apollo-client/)|[](https://github.com/facebook/fbjs/)|[](https://swagger.io/)|[](http://vulcanjs.org)|[](https://github.com/prisma/graphql-request)|
|
|
151
|
+
|:---:|:---:|:---:|:---:|:---:|:---:|
|
|
152
|
+
|The New York Times|Apollo GraphQL|Facebook|Swagger|VulcanJS|graphql-request|
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
## Thanks
|
|
156
|
+
|
|
157
|
+
Heavily inspired by the works of [matthew-andrews](https://github.com/matthew-andrews). Kudos to him!
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
cross-fetch is licensed under the [MIT license](https://github.com/lquixada/cross-fetch/blob/main/LICENSE) © [Leonardo Quixadá](https://twitter.com/lquixada/)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
## Author
|
|
166
|
+
|
|
167
|
+
|[](https://github.com/lquixada)|
|
|
168
|
+
|:---:|
|
|
169
|
+
|[@lquixada](http://www.github.com/lquixada)|
|