This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2017 Jason Miller
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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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.
+247
View File
@@ -0,0 +1,247 @@
<p align="center">
<img src="https://i.imgur.com/0cSIPzP.png" width="300" height="300" alt="unfetch">
<br>
<a href="https://www.npmjs.org/package/unfetch"><img src="https://img.shields.io/npm/v/unfetch.svg?style=flat" alt="npm"></a>
<a href="https://unpkg.com/unfetch/polyfill"><img src="https://img.badgesize.io/https://unpkg.com/unfetch/polyfill/index.js?compression=gzip" alt="gzip size"></a>
<a href="https://www.npmjs.com/package/unfetch"><img src="https://img.shields.io/npm/dt/unfetch.svg" alt="downloads" ></a>
<a href="https://travis-ci.org/developit/unfetch"><img src="https://travis-ci.org/developit/unfetch.svg?branch=master" alt="travis"></a>
</p>
# unfetch
> Tiny 500b fetch "barely-polyfill"
- **Tiny:** about **500 bytes** of [ES3](https://unpkg.com/unfetch) gzipped
- **Minimal:** just `fetch()` with headers and text/json responses
- **Familiar:** a subset of the full API
- **Supported:** supports IE8+ _(assuming `Promise` is polyfilled of course!)_
- **Standalone:** one function, no dependencies
- **Modern:** written in ES2015, transpiled to 500b of old-school JS
> 🤔 **What's Missing?**
>
> - Uses simple Arrays instead of Iterables, since Arrays _are_ iterables
> - No streaming, just Promisifies existing XMLHttpRequest response bodies
> - Use in Node.JS is handled by [isomorphic-unfetch](https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch)
* * *
- [Unfetch](#unfetch)
- [Installation](#installation)
- [Usage: As a Polyfill](#usage-as-a-polyfill)
- [Usage: As a Ponyfill](#usage-as-a-ponyfill)
- [Examples & Demos](#examples--demos)
- [API](#api)
- [Caveats](#caveats)
- [Contribute](#contribute)
- [License](#license)
* * *
## Installation
For use with [node](http://nodejs.org) and [npm](https://npmjs.com):
```sh
npm install --save unfetch
```
Otherwise, grab it from [unpkg.com/unfetch](https://unpkg.com/unfetch/).
* * *
## Usage: As a [Polyfill](https://ponyfill.com/#polyfill)
This automatically "installs" unfetch as `window.fetch()` if it detects Fetch isn't supported:
```js
import 'unfetch/polyfill'
// fetch is now available globally!
fetch('/foo.json')
.then( r => r.json() )
.then( data => console.log(data) )
```
This polyfill version is particularly useful for hotlinking from [unpkg](https://unpkg.com):
```html
<script src="https://unpkg.com/unfetch/polyfill"></script>
<script>
// now our page can use fetch!
fetch('/foo')
</script>
```
* * *
## Usage: As a [Ponyfill](https://github.com/sindresorhus/ponyfill)
With a module bundler like [rollup](http://rollupjs.org) or [webpack](https://webpack.js.org),
you can import unfetch to use in your code without modifying any globals:
```js
// using JS Modules:
import fetch from 'unfetch'
// or using CommonJS:
var fetch = require('unfetch')
// usage:
fetch('/foo.json')
.then( r => r.json() )
.then( data => console.log(data) )
```
The above will always return `unfetch()`. _(even if `window.fetch` exists!)_
There's also a UMD bundle available as [unfetch/dist/unfetch.umd.js](https://unpkg.com/unfetch/dist/unfetch.umd.js), which doesn't automatically install itself as `window.fetch`.
* * *
## Examples & Demos
[**Real Example on JSFiddle**](https://jsfiddle.net/developit/qrh7tLc0/) ➡️
```js
// simple GET request:
fetch('/foo')
.then( r => r.text() )
.then( txt => console.log(txt) )
// complex POST request with JSON, headers:
fetch('/bear', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ hungry: true })
}).then( r => {
open(r.headers.get('location'));
return r.json();
})
```
* * *
## API
While one of Unfetch's goals is to provide a familiar interface, its API may differ from other `fetch` polyfills/ponyfills.
One of the key differences is that Unfetch focuses on implementing the [`fetch()` API](https://fetch.spec.whatwg.org/#fetch-api), while offering minimal (yet functional) support to the other sections of the [Fetch spec](https://fetch.spec.whatwg.org/), like the [Headers class](https://fetch.spec.whatwg.org/#headers-class) or the [Response class](https://fetch.spec.whatwg.org/#response-class).
Unfetch's API is organized as follows:
### `fetch(url: string, options: Object)`
This function is the heart of Unfetch. It will fetch resources from `url` according to the given `options`, returning a Promise that will eventually resolve to the response.
Unfetch will account for the following properties in `options`:
* `method`: Indicates the request method to be performed on the
target resource (The most common ones being `GET`, `POST`, `PUT`, `PATCH`, `HEAD`, `OPTIONS` or `DELETE`).
* `headers`: An `Object` containing additional information to be sent with the request, e.g. `{ 'Content-Type': 'application/json' }` to indicate a JSON-typed request body.
* `credentials`: ⚠ Accepts a `"include"` string, which will allow both CORS and same origin requests to work with cookies. As pointed in the ['Caveats' section](#caveats), Unfetch won't send or receive cookies otherwise. The `"same-origin"` value is not supported. ⚠
* `body`: The content to be transmited in request's body. Common content types include `FormData`, `JSON`, `Blob`, `ArrayBuffer` or plain text.
### `response` Methods and Attributes
These methods are used to handle the response accordingly in your Promise chain. Instead of implementing full spec-compliant [Response Class](https://fetch.spec.whatwg.org/#response-class) functionality, Unfetch provides the following methods and attributes:
#### `response.ok`
Returns `true` if the request received a status in the `OK` range (200-299).
#### `response.status`
Contains the status code of the response, e.g. `404` for a not found resource, `200` for a success.
#### `response.statusText`
A message related to the `status` attribute, e.g. `OK` for a status `200`.
#### `response.clone()`
Will return another `Object` with the same shape and content as `response`.
#### `response.text()`, `response.json()`, `response.blob()`
Will return the response content as plain text, JSON and `Blob`, respectively.
#### `response.headers`
Again, Unfetch doesn't implement a full spec-compliant [`Headers Class`](https://fetch.spec.whatwg.org/#headers), emulating some of the Map-like functionality through its own functions:
* `headers.keys`: Returns an `Array` containing the `key` for every header in the response.
* `headers.entries`: Returns an `Array` containing the `[key, value]` pairs for every `Header` in the response.
* `headers.get(key)`: Returns the `value` associated with the given `key`.
* `headers.has(key)`: Returns a `boolean` asserting the existence of a `value` for the given `key` among the response headers.
## Caveats
_Adapted from the GitHub fetch polyfill [**readme**](https://github.com/github/fetch#caveats)._
The `fetch` specification differs from `jQuery.ajax()` in mainly two ways that
bear keeping in mind:
* By default, `fetch` **won't send or receive any cookies** from the server,
resulting in unauthenticated requests if the site relies on maintaining a user
session.
```javascript
fetch('/users', {
credentials: 'include'
});
```
* The Promise returned from `fetch()` **won't reject on HTTP error status**
even if the response is an HTTP 404 or 500. Instead, it will resolve normally,
and it will only reject on network failure or if anything prevented the
request from completing.
To have `fetch` Promise reject on HTTP error statuses, i.e. on any non-2xx
status, define a custom response handler:
```javascript
fetch('/users')
.then( checkStatus )
.then( r => r.json() )
.then( data => {
console.log(data);
});
function checkStatus(response) {
if (response.ok) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
return Promise.reject(error);
}
}
```
* * *
## Contribute
First off, thanks for taking the time to contribute!
Now, take a moment to be sure your contributions make sense to everyone else.
### Reporting Issues
Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).
If it hasn't, just open a [new clear and descriptive issue](../../issues/new).
### Submitting pull requests
Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
> 💁 **Remember: size is the #1 priority.**
>
> Every byte counts! PR's can't be merged if they increase the output size much.
- Fork it!
- Clone your fork: `git clone https://github.com/<your-username>/unfetch`
- Navigate to the newly cloned directory: `cd unfetch`
- Create a new branch for the new feature: `git checkout -b my-new-feature`
- Install the tools necessary for development: `npm install`
- Make your changes.
- `npm run build` to verify your change doesn't increase output size.
- `npm test` to make sure your change doesn't break anything.
- Commit your changes: `git commit -am 'Add some feature'`
- Push to the branch: `git push origin my-new-feature`
- Submit a pull request with full remarks documenting your changes.
## License
[MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)
+2
View File
@@ -0,0 +1,2 @@
export default function(e,n){return n=n||{},new Promise(function(t,r){var s=new XMLHttpRequest,o=[],u=[],i={},a=function(){return{ok:2==(s.status/100|0),statusText:s.statusText,status:s.status,url:s.responseURL,text:function(){return Promise.resolve(s.responseText)},json:function(){return Promise.resolve(JSON.parse(s.responseText))},blob:function(){return Promise.resolve(new Blob([s.response]))},clone:a,headers:{keys:function(){return o},entries:function(){return u},get:function(e){return i[e.toLowerCase()]},has:function(e){return e.toLowerCase()in i}}}};for(var l in s.open(n.method||"get",e,!0),s.onload=function(){s.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,function(e,n,t){o.push(n=n.toLowerCase()),u.push([n,t]),i[n]=i[n]?i[n]+","+t:t}),t(a())},s.onerror=r,s.withCredentials="include"==n.credentials,n.headers)s.setRequestHeader(l,n.headers[l]);s.send(n.body||null)})}
//# sourceMappingURL=unfetch.mjs.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"unfetch.es.js","sources":["../src/index.js"],"sourcesContent":["export default typeof fetch=='function' ? fetch.bind() : function(url, options) {\n\toptions = options || {};\n\treturn new Promise( (resolve, reject) => {\n\t\tlet request = new XMLHttpRequest();\n\n\t\trequest.open(options.method || 'get', url, true);\n\n\t\tfor (let i in options.headers) {\n\t\t\trequest.setRequestHeader(i, options.headers[i]);\n\t\t}\n\n\t\trequest.withCredentials = options.credentials=='include';\n\n\t\trequest.onload = () => {\n\t\t\tresolve(response());\n\t\t};\n\n\t\trequest.onerror = reject;\n\n\t\trequest.send(options.body || null);\n\n\t\tfunction response() {\n\t\t\tlet keys = [],\n\t\t\t\tall = [],\n\t\t\t\theaders = {},\n\t\t\t\theader;\n\n\t\t\trequest.getAllResponseHeaders().replace(/^(.*?):[^\\S\\n]*([\\s\\S]*?)$/gm, (m, key, value) => {\n\t\t\t\tkeys.push(key = key.toLowerCase());\n\t\t\t\tall.push([key, value]);\n\t\t\t\theader = headers[key];\n\t\t\t\theaders[key] = header ? `${header},${value}` : value;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tok: (request.status/100|0) == 2,\t\t// 200-299\n\t\t\t\tstatus: request.status,\n\t\t\t\tstatusText: request.statusText,\n\t\t\t\turl: request.responseURL,\n\t\t\t\tclone: response,\n\t\t\t\ttext: () => Promise.resolve(request.responseText),\n\t\t\t\tjson: () => Promise.resolve(request.responseText).then(JSON.parse),\n\t\t\t\tblob: () => Promise.resolve(new Blob([request.response])),\n\t\t\t\theaders: {\n\t\t\t\t\tkeys: () => keys,\n\t\t\t\t\tentries: () => all,\n\t\t\t\t\tget: n => headers[n.toLowerCase()],\n\t\t\t\t\thas: n => n.toLowerCase() in headers\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t});\n}\n"],"names":["let"],"mappings":"AAAA,YAAe,OAAO,KAAK,EAAE,UAAU,GAAG,KAAK,CAAC,IAAI,EAAE,GAAG,SAAS,GAAG,EAAE,OAAO,EAAE;CAC/E,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;CACxB,OAAO,IAAI,OAAO,EAAE,UAAC,OAAO,EAAE,MAAM,EAAE;EACrCA,IAAI,OAAO,GAAG,IAAI,cAAc,EAAE,CAAC;;EAEnC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;;EAEjD,KAAKA,IAAI,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE;GAC9B,OAAO,CAAC,gBAAgB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;GAChD;;EAED,OAAO,CAAC,eAAe,GAAG,OAAO,CAAC,WAAW,EAAE,SAAS,CAAC;;EAEzD,OAAO,CAAC,MAAM,GAAG,YAAG;GACnB,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;GACpB,CAAC;;EAEF,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC;;EAEzB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC;;EAEnC,SAAS,QAAQ,GAAG;GACnBA,IAAI,IAAI,GAAG,EAAE;IACZ,GAAG,GAAG,EAAE;IACR,OAAO,GAAG,EAAE;IACZ,MAAM,CAAC;;GAER,OAAO,CAAC,qBAAqB,EAAE,CAAC,OAAO,CAAC,8BAA8B,EAAE,UAAC,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE;IACvF,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC;IACnC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;IACvB,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACtB,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,IAAG,MAAS,MAAE,GAAE,KAAK,IAAK,KAAK,CAAC;IACrD,CAAC,CAAC;;GAEH,OAAO;IACN,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;IAC/B,MAAM,EAAE,OAAO,CAAC,MAAM;IACtB,UAAU,EAAE,OAAO,CAAC,UAAU;IAC9B,GAAG,EAAE,OAAO,CAAC,WAAW;IACxB,KAAK,EAAE,QAAQ;IACf,IAAI,EAAE,YAAG,SAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,GAAA;IACjD,IAAI,EAAE,YAAG,SAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAA;IAClE,IAAI,EAAE,YAAG,SAAG,OAAO,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAA;IACzD,OAAO,EAAE;KACR,IAAI,EAAE,YAAG,SAAG,IAAI,GAAA;KAChB,OAAO,EAAE,YAAG,SAAG,GAAG,GAAA;KAClB,GAAG,EAAE,UAAA,CAAC,EAAC,SAAG,OAAO,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,GAAA;KAClC,GAAG,EAAE,UAAA,CAAC,EAAC,SAAG,CAAC,CAAC,WAAW,EAAE,IAAI,OAAO,GAAA;KACpC;IACD,CAAC;GACF;EACD,CAAC,CAAC;CACH,CAAA;;"}
+2
View File
@@ -0,0 +1,2 @@
module.exports=function(e,n){return n=n||{},new Promise(function(t,r){var s=new XMLHttpRequest,o=[],u=[],i={},a=function(){return{ok:2==(s.status/100|0),statusText:s.statusText,status:s.status,url:s.responseURL,text:function(){return Promise.resolve(s.responseText)},json:function(){return Promise.resolve(JSON.parse(s.responseText))},blob:function(){return Promise.resolve(new Blob([s.response]))},clone:a,headers:{keys:function(){return o},entries:function(){return u},get:function(e){return i[e.toLowerCase()]},has:function(e){return e.toLowerCase()in i}}}};for(var l in s.open(n.method||"get",e,!0),s.onload=function(){s.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,function(e,n,t){o.push(n=n.toLowerCase()),u.push([n,t]),i[n]=i[n]?i[n]+","+t:t}),t(a())},s.onerror=r,s.withCredentials="include"==n.credentials,n.headers)s.setRequestHeader(l,n.headers[l]);s.send(n.body||null)})};
//# sourceMappingURL=unfetch.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"unfetch.js","sources":["../src/index.mjs"],"sourcesContent":["export default function(url, options) {\n\toptions = options || {};\n\treturn new Promise( (resolve, reject) => {\n\t\tconst request = new XMLHttpRequest();\n\t\tconst keys = [];\n\t\tconst all = [];\n\t\tconst headers = {};\n\n\t\tconst response = () => ({\n\t\t\tok: (request.status/100|0) == 2,\t\t// 200-299\n\t\t\tstatusText: request.statusText,\n\t\t\tstatus: request.status,\n\t\t\turl: request.responseURL,\n\t\t\ttext: () => Promise.resolve(request.responseText),\n\t\t\tjson: () => Promise.resolve(JSON.parse(request.responseText)),\n\t\t\tblob: () => Promise.resolve(new Blob([request.response])),\n\t\t\tclone: response,\n\t\t\theaders: {\n\t\t\t\tkeys: () => keys,\n\t\t\t\tentries: () => all,\n\t\t\t\tget: n => headers[n.toLowerCase()],\n\t\t\t\thas: n => n.toLowerCase() in headers\n\t\t\t}\n\t\t});\n\n\t\trequest.open(options.method || 'get', url, true);\n\n\t\trequest.onload = () => {\n\t\t\trequest.getAllResponseHeaders().replace(/^(.*?):[^\\S\\n]*([\\s\\S]*?)$/gm, (m, key, value) => {\n\t\t\t\tkeys.push(key = key.toLowerCase());\n\t\t\t\tall.push([key, value]);\n\t\t\t\theaders[key] = headers[key] ? `${headers[key]},${value}` : value;\n\t\t\t});\n\t\t\tresolve(response());\n\t\t};\n\n\t\trequest.onerror = reject;\n\n\t\trequest.withCredentials = options.credentials=='include';\n\n\t\tfor (const i in options.headers) {\n\t\t\trequest.setRequestHeader(i, options.headers[i]);\n\t\t}\n\n\t\trequest.send(options.body || null);\n\t});\n}\n"],"names":["url","options","Promise","resolve","reject","request","XMLHttpRequest","keys","all","headers","response","ok","status","statusText","responseURL","text","responseText","json","JSON","parse","blob","Blob","clone","entries","get","n","toLowerCase","has","const","i","open","method","onload","getAllResponseHeaders","replace","m","key","value","push","onerror","withCredentials","credentials","setRequestHeader","send","body"],"mappings":"eAAe,SAASA,EAAKC,UAC5BA,EAAUA,GAAW,GACd,IAAIC,iBAAUC,EAASC,OACvBC,EAAU,IAAIC,eACdC,EAAO,GACPC,EAAM,GACNC,EAAU,GAEVC,oBACLC,GAA8B,IAAzBN,EAAQO,OAAO,IAAI,GACxBC,WAAYR,EAAQQ,WACpBD,OAAQP,EAAQO,OAChBZ,IAAKK,EAAQS,YACbC,uBAAYb,QAAQC,QAAQE,EAAQW,eACpCC,uBAAYf,QAAQC,QAAQe,KAAKC,MAAMd,EAAQW,gBAC/CI,uBAAYlB,QAAQC,QAAQ,IAAIkB,KAAK,CAAChB,EAAQK,aAC9CY,MAAOZ,EACPD,QAAS,CACRF,uBAAYA,GACZgB,0BAAef,GACfgB,aAAKC,UAAKhB,EAAQgB,EAAEC,gBACpBC,aAAKF,UAAKA,EAAEC,gBAAiBjB,UAmB1BmB,IAAMC,KAfXxB,EAAQyB,KAAK7B,EAAQ8B,QAAU,MAAO/B,GAAK,GAE3CK,EAAQ2B,kBACP3B,EAAQ4B,wBAAwBC,QAAQ,wCAAiCC,EAAGC,EAAKC,GAChF9B,EAAK+B,KAAKF,EAAMA,EAAIV,eACpBlB,EAAI8B,KAAK,CAACF,EAAKC,IACf5B,EAAQ2B,GAAO3B,EAAQ2B,GAAU3B,EAAQ2B,OAAQC,EAAUA,IAE5DlC,EAAQO,MAGTL,EAAQkC,QAAUnC,EAElBC,EAAQmC,gBAAuC,WAArBvC,EAAQwC,YAElBxC,EAAQQ,QACvBJ,EAAQqC,iBAAiBb,EAAG5B,EAAQQ,QAAQoB,IAG7CxB,EAAQsC,KAAK1C,EAAQ2C,MAAQ"}
+2
View File
@@ -0,0 +1,2 @@
export default function(e,n){return n=n||{},new Promise(function(t,r){var s=new XMLHttpRequest,o=[],u=[],i={},a=function(){return{ok:2==(s.status/100|0),statusText:s.statusText,status:s.status,url:s.responseURL,text:function(){return Promise.resolve(s.responseText)},json:function(){return Promise.resolve(JSON.parse(s.responseText))},blob:function(){return Promise.resolve(new Blob([s.response]))},clone:a,headers:{keys:function(){return o},entries:function(){return u},get:function(e){return i[e.toLowerCase()]},has:function(e){return e.toLowerCase()in i}}}};for(var l in s.open(n.method||"get",e,!0),s.onload=function(){s.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,function(e,n,t){o.push(n=n.toLowerCase()),u.push([n,t]),i[n]=i[n]?i[n]+","+t:t}),t(a())},s.onerror=r,s.withCredentials="include"==n.credentials,n.headers)s.setRequestHeader(l,n.headers[l]);s.send(n.body||null)})}
//# sourceMappingURL=unfetch.mjs.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"unfetch.mjs","sources":["../src/index.mjs"],"sourcesContent":["export default function(url, options) {\n\toptions = options || {};\n\treturn new Promise( (resolve, reject) => {\n\t\tconst request = new XMLHttpRequest();\n\t\tconst keys = [];\n\t\tconst all = [];\n\t\tconst headers = {};\n\n\t\tconst response = () => ({\n\t\t\tok: (request.status/100|0) == 2,\t\t// 200-299\n\t\t\tstatusText: request.statusText,\n\t\t\tstatus: request.status,\n\t\t\turl: request.responseURL,\n\t\t\ttext: () => Promise.resolve(request.responseText),\n\t\t\tjson: () => Promise.resolve(JSON.parse(request.responseText)),\n\t\t\tblob: () => Promise.resolve(new Blob([request.response])),\n\t\t\tclone: response,\n\t\t\theaders: {\n\t\t\t\tkeys: () => keys,\n\t\t\t\tentries: () => all,\n\t\t\t\tget: n => headers[n.toLowerCase()],\n\t\t\t\thas: n => n.toLowerCase() in headers\n\t\t\t}\n\t\t});\n\n\t\trequest.open(options.method || 'get', url, true);\n\n\t\trequest.onload = () => {\n\t\t\trequest.getAllResponseHeaders().replace(/^(.*?):[^\\S\\n]*([\\s\\S]*?)$/gm, (m, key, value) => {\n\t\t\t\tkeys.push(key = key.toLowerCase());\n\t\t\t\tall.push([key, value]);\n\t\t\t\theaders[key] = headers[key] ? `${headers[key]},${value}` : value;\n\t\t\t});\n\t\t\tresolve(response());\n\t\t};\n\n\t\trequest.onerror = reject;\n\n\t\trequest.withCredentials = options.credentials=='include';\n\n\t\tfor (const i in options.headers) {\n\t\t\trequest.setRequestHeader(i, options.headers[i]);\n\t\t}\n\n\t\trequest.send(options.body || null);\n\t});\n}\n"],"names":["url","options","Promise","resolve","reject","request","XMLHttpRequest","keys","all","headers","response","ok","status","statusText","responseURL","text","responseText","json","JSON","parse","blob","Blob","clone","entries","get","n","toLowerCase","has","const","i","open","method","onload","getAllResponseHeaders","replace","m","key","value","push","onerror","withCredentials","credentials","setRequestHeader","send","body"],"mappings":"eAAe,SAASA,EAAKC,UAC5BA,EAAUA,GAAW,GACd,IAAIC,iBAAUC,EAASC,OACvBC,EAAU,IAAIC,eACdC,EAAO,GACPC,EAAM,GACNC,EAAU,GAEVC,oBACLC,GAA8B,IAAzBN,EAAQO,OAAO,IAAI,GACxBC,WAAYR,EAAQQ,WACpBD,OAAQP,EAAQO,OAChBZ,IAAKK,EAAQS,YACbC,uBAAYb,QAAQC,QAAQE,EAAQW,eACpCC,uBAAYf,QAAQC,QAAQe,KAAKC,MAAMd,EAAQW,gBAC/CI,uBAAYlB,QAAQC,QAAQ,IAAIkB,KAAK,CAAChB,EAAQK,aAC9CY,MAAOZ,EACPD,QAAS,CACRF,uBAAYA,GACZgB,0BAAef,GACfgB,aAAKC,UAAKhB,EAAQgB,EAAEC,gBACpBC,aAAKF,UAAKA,EAAEC,gBAAiBjB,UAmB1BmB,IAAMC,KAfXxB,EAAQyB,KAAK7B,EAAQ8B,QAAU,MAAO/B,GAAK,GAE3CK,EAAQ2B,kBACP3B,EAAQ4B,wBAAwBC,QAAQ,wCAAiCC,EAAGC,EAAKC,GAChF9B,EAAK+B,KAAKF,EAAMA,EAAIV,eACpBlB,EAAI8B,KAAK,CAACF,EAAKC,IACf5B,EAAQ2B,GAAO3B,EAAQ2B,GAAU3B,EAAQ2B,OAAQC,EAAUA,IAE5DlC,EAAQO,MAGTL,EAAQkC,QAAUnC,EAElBC,EAAQmC,gBAAuC,WAArBvC,EAAQwC,YAElBxC,EAAQQ,QACvBJ,EAAQqC,iBAAiBb,EAAG5B,EAAQQ,QAAQoB,IAG7CxB,EAAQsC,KAAK1C,EAAQ2C,MAAQ"}
+2
View File
@@ -0,0 +1,2 @@
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.unfetch=n()}(this,function(){return function(e,n){return n=n||{},new Promise(function(t,o){var r=new XMLHttpRequest,s=[],u=[],i={},f=function(){return{ok:2==(r.status/100|0),statusText:r.statusText,status:r.status,url:r.responseURL,text:function(){return Promise.resolve(r.responseText)},json:function(){return Promise.resolve(JSON.parse(r.responseText))},blob:function(){return Promise.resolve(new Blob([r.response]))},clone:f,headers:{keys:function(){return s},entries:function(){return u},get:function(e){return i[e.toLowerCase()]},has:function(e){return e.toLowerCase()in i}}}};for(var a in r.open(n.method||"get",e,!0),r.onload=function(){r.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,function(e,n,t){s.push(n=n.toLowerCase()),u.push([n,t]),i[n]=i[n]?i[n]+","+t:t}),t(f())},r.onerror=o,r.withCredentials="include"==n.credentials,n.headers)r.setRequestHeader(a,n.headers[a]);r.send(n.body||null)})}});
//# sourceMappingURL=unfetch.umd.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"unfetch.umd.js","sources":["../src/index.mjs"],"sourcesContent":["export default function(url, options) {\n\toptions = options || {};\n\treturn new Promise( (resolve, reject) => {\n\t\tconst request = new XMLHttpRequest();\n\t\tconst keys = [];\n\t\tconst all = [];\n\t\tconst headers = {};\n\n\t\tconst response = () => ({\n\t\t\tok: (request.status/100|0) == 2,\t\t// 200-299\n\t\t\tstatusText: request.statusText,\n\t\t\tstatus: request.status,\n\t\t\turl: request.responseURL,\n\t\t\ttext: () => Promise.resolve(request.responseText),\n\t\t\tjson: () => Promise.resolve(JSON.parse(request.responseText)),\n\t\t\tblob: () => Promise.resolve(new Blob([request.response])),\n\t\t\tclone: response,\n\t\t\theaders: {\n\t\t\t\tkeys: () => keys,\n\t\t\t\tentries: () => all,\n\t\t\t\tget: n => headers[n.toLowerCase()],\n\t\t\t\thas: n => n.toLowerCase() in headers\n\t\t\t}\n\t\t});\n\n\t\trequest.open(options.method || 'get', url, true);\n\n\t\trequest.onload = () => {\n\t\t\trequest.getAllResponseHeaders().replace(/^(.*?):[^\\S\\n]*([\\s\\S]*?)$/gm, (m, key, value) => {\n\t\t\t\tkeys.push(key = key.toLowerCase());\n\t\t\t\tall.push([key, value]);\n\t\t\t\theaders[key] = headers[key] ? `${headers[key]},${value}` : value;\n\t\t\t});\n\t\t\tresolve(response());\n\t\t};\n\n\t\trequest.onerror = reject;\n\n\t\trequest.withCredentials = options.credentials=='include';\n\n\t\tfor (const i in options.headers) {\n\t\t\trequest.setRequestHeader(i, options.headers[i]);\n\t\t}\n\n\t\trequest.send(options.body || null);\n\t});\n}\n"],"names":["url","options","Promise","resolve","reject","request","XMLHttpRequest","keys","all","headers","response","ok","status","statusText","responseURL","text","responseText","json","JSON","parse","blob","Blob","clone","entries","get","n","toLowerCase","has","const","i","open","method","onload","getAllResponseHeaders","replace","m","key","value","push","onerror","withCredentials","credentials","setRequestHeader","send","body"],"mappings":"6KAAe,SAASA,EAAKC,UAC5BA,EAAUA,GAAW,GACd,IAAIC,iBAAUC,EAASC,OACvBC,EAAU,IAAIC,eACdC,EAAO,GACPC,EAAM,GACNC,EAAU,GAEVC,oBACLC,GAA8B,IAAzBN,EAAQO,OAAO,IAAI,GACxBC,WAAYR,EAAQQ,WACpBD,OAAQP,EAAQO,OAChBZ,IAAKK,EAAQS,YACbC,uBAAYb,QAAQC,QAAQE,EAAQW,eACpCC,uBAAYf,QAAQC,QAAQe,KAAKC,MAAMd,EAAQW,gBAC/CI,uBAAYlB,QAAQC,QAAQ,IAAIkB,KAAK,CAAChB,EAAQK,aAC9CY,MAAOZ,EACPD,QAAS,CACRF,uBAAYA,GACZgB,0BAAef,GACfgB,aAAKC,UAAKhB,EAAQgB,EAAEC,gBACpBC,aAAKF,UAAKA,EAAEC,gBAAiBjB,UAmB1BmB,IAAMC,KAfXxB,EAAQyB,KAAK7B,EAAQ8B,QAAU,MAAO/B,GAAK,GAE3CK,EAAQ2B,kBACP3B,EAAQ4B,wBAAwBC,QAAQ,wCAAiCC,EAAGC,EAAKC,GAChF9B,EAAK+B,KAAKF,EAAMA,EAAIV,eACpBlB,EAAI8B,KAAK,CAACF,EAAKC,IACf5B,EAAQ2B,GAAO3B,EAAQ2B,GAAU3B,EAAQ2B,OAAQC,EAAUA,IAE5DlC,EAAQO,MAGTL,EAAQkC,QAAUnC,EAElBC,EAAQmC,gBAAuC,WAArBvC,EAAQwC,YAElBxC,EAAQQ,QACvBJ,EAAQqC,iBAAiBb,EAAG5B,EAAQQ,QAAQoB,IAG7CxB,EAAQsC,KAAK1C,EAAQ2C,MAAQ"}
+92
View File
@@ -0,0 +1,92 @@
{
"_args": [
[
"unfetch@4.1.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "unfetch@4.1.0",
"_id": "unfetch@4.1.0",
"_inBundle": false,
"_integrity": "sha512-crP/n3eAPUJxZXM9T80/yv0YhkTEx2K1D3h7D1AJM6fzsWZrxdyRuLN0JH/dkZh1LNH8LxCnBzoPFCPbb2iGpg==",
"_location": "/unfetch",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "unfetch@4.1.0",
"name": "unfetch",
"escapedName": "unfetch",
"rawSpec": "4.1.0",
"saveSpec": null,
"fetchSpec": "4.1.0"
},
"_requiredBy": [
"/@nuxt/vue-app"
],
"_resolved": "https://registry.npmjs.org/unfetch/-/unfetch-4.1.0.tgz",
"_spec": "4.1.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"authors": [
"Jason Miller <jason@developit.ca>"
],
"bugs": {
"url": "https://github.com/developit/unfetch/issues"
},
"description": "Bare minimum fetch polyfill in 500 bytes",
"devDependencies": {
"babel-preset-env": "^1.7.0",
"cross-var": "^1.1.0",
"eslint": "^3.13.1",
"eslint-config-developit": "^1.1.1",
"jest": "^23.6.0",
"microbundle": "^0.10.1"
},
"eslintConfig": {
"extends": "developit"
},
"files": [
"src",
"dist",
"polyfill"
],
"homepage": "https://github.com/developit/unfetch",
"jest": {
"testURL": "http://localhost/",
"testMatch": [
"<rootDir>/test/**/*.?(m)js?(x)"
],
"moduleFileExtensions": [
"mjs",
"js"
],
"transform": {
"^.+\\.m?jsx?$": "babel-jest"
}
},
"jsnext:main": "dist/unfetch.mjs",
"keywords": [
"fetch",
"polyfill",
"xhr",
"ajax"
],
"license": "MIT",
"main": "dist/unfetch.js",
"module": "dist/unfetch.mjs",
"name": "unfetch",
"repository": {
"type": "git",
"url": "git+https://github.com/developit/unfetch.git"
},
"scripts": {
"build": "microbundle src/index.mjs && microbundle -f cjs polyfill/polyfill.mjs -o polyfill/index.js --no-sourcemap && cp dist/unfetch.mjs dist/unfetch.es.js",
"prepare": "npm run -s build",
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish",
"test": "eslint src test && jest"
},
"types": "src/index.d.ts",
"umd:main": "dist/unfetch.umd.js",
"unpkg": "polyfill/index.js",
"version": "4.1.0"
}
+1
View File
@@ -0,0 +1 @@
self.fetch||(self.fetch=function(e,n){return n=n||{},new Promise(function(t,s){var r=new XMLHttpRequest,o=[],u=[],i={},a=function(){return{ok:2==(r.status/100|0),statusText:r.statusText,status:r.status,url:r.responseURL,text:function(){return Promise.resolve(r.responseText)},json:function(){return Promise.resolve(JSON.parse(r.responseText))},blob:function(){return Promise.resolve(new Blob([r.response]))},clone:a,headers:{keys:function(){return o},entries:function(){return u},get:function(e){return i[e.toLowerCase()]},has:function(e){return e.toLowerCase()in i}}}};for(var c in r.open(n.method||"get",e,!0),r.onload=function(){r.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm,function(e,n,t){o.push(n=n.toLowerCase()),u.push([n,t]),i[n]=i[n]?i[n]+","+t:t}),t(a())},r.onerror=s,r.withCredentials="include"==n.credentials,n.headers)r.setRequestHeader(c,n.headers[c]);r.send(n.body||null)})});
+5
View File
@@ -0,0 +1,5 @@
{
"name": "unfetch-polyfill",
"main": "index.js",
"module": "polyfill.mjs"
}
+2
View File
@@ -0,0 +1,2 @@
import unfetch from '../src/index.mjs';
if (!self.fetch) self.fetch = unfetch;
+17
View File
@@ -0,0 +1,17 @@
import {
Body as NodeBody,
Headers as NodeHeaders,
Request as NodeRequest,
Response as NodeResponse
} from "node-fetch";
declare namespace unfetch {
export type IsomorphicHeaders = Headers | NodeHeaders;
export type IsomorphicBody = Body | NodeBody;
export type IsomorphicResponse = Response | NodeResponse;
export type IsomorphicRequest = Request | NodeRequest
}
declare const unfetch: typeof fetch;
export default unfetch;
+47
View File
@@ -0,0 +1,47 @@
export default function(url, options) {
options = options || {};
return new Promise( (resolve, reject) => {
const request = new XMLHttpRequest();
const keys = [];
const all = [];
const headers = {};
const response = () => ({
ok: (request.status/100|0) == 2, // 200-299
statusText: request.statusText,
status: request.status,
url: request.responseURL,
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(JSON.parse(request.responseText)),
blob: () => Promise.resolve(new Blob([request.response])),
clone: response,
headers: {
keys: () => keys,
entries: () => all,
get: n => headers[n.toLowerCase()],
has: n => n.toLowerCase() in headers
}
});
request.open(options.method || 'get', url, true);
request.onload = () => {
request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push(key = key.toLowerCase());
all.push([key, value]);
headers[key] = headers[key] ? `${headers[key]},${value}` : value;
});
resolve(response());
};
request.onerror = reject;
request.withCredentials = options.credentials=='include';
for (const i in options.headers) {
request.setRequestHeader(i, options.headers[i]);
}
request.send(options.body || null);
});
}