diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..3bb9642 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,16 @@ +name: Compressed Size + +on: [pull_request] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2-beta + with: + fetch-depth: 1 + - uses: preactjs/compressed-size-action@v1 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/README.md b/README.md index 6255592..8a9d040 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ Unfetch will account for the following properties in `options`: 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. + * `body`: The content to be transmitted 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: diff --git a/package.json b/package.json index 7e2ffdb..760273f 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "description": "Bare minimum fetch polyfill in 500 bytes", "unpkg": "polyfill/index.js", "main": "dist/unfetch.js", - "module": "dist/unfetch.mjs", - "jsnext:main": "dist/unfetch.mjs", + "module": "dist/unfetch.module.js", + "jsnext:main": "dist/unfetch.module.js", "umd:main": "dist/unfetch.umd.js", "scripts": { "test": "eslint src test && jest", - "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", + "build": "microbundle src/index.mjs && microbundle -f cjs polyfill/polyfill.mjs -o polyfill/index.js --no-sourcemap && cp dist/unfetch.module.js 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" }, diff --git a/packages/isomorphic-unfetch/browser.js b/packages/isomorphic-unfetch/browser.js index 13b55c2..ed13eef 100644 --- a/packages/isomorphic-unfetch/browser.js +++ b/packages/isomorphic-unfetch/browser.js @@ -1 +1 @@ -module.exports = window.fetch || (window.fetch = require('unfetch').default || require('unfetch')); +module.exports = self.fetch || (self.fetch = require('unfetch').default || require('unfetch')); diff --git a/packages/isomorphic-unfetch/index.js b/packages/isomorphic-unfetch/index.js index 6ae374c..dacdb66 100644 --- a/packages/isomorphic-unfetch/index.js +++ b/packages/isomorphic-unfetch/index.js @@ -1,5 +1,6 @@ +function r(m){return m && m.default || m;} module.exports = global.fetch = global.fetch || ( - typeof process=='undefined' ? (require('unfetch').default || require('unfetch')) : (function(url, opts) { - return require('node-fetch')(url.replace(/^\/\//g,'https://'), opts); + typeof process=='undefined' ? r(require('unfetch')) : (function(url, opts) { + return r(require('node-fetch'))(String(url).replace(/^\/\//g,'https://'), opts); }) ); diff --git a/packages/isomorphic-unfetch/package-lock.json b/packages/isomorphic-unfetch/package-lock.json new file mode 100644 index 0000000..3330abe --- /dev/null +++ b/packages/isomorphic-unfetch/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "isomorphic-unfetch", + "version": "3.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" + } + } +} diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index 0d544d0..ae66aa7 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -7,12 +7,13 @@ "index.d.ts", "browser.js" ], + "license": "MIT", "repository": "developit/unfetch", "browser": "browser.js", "main": "index.js", "types": "index.d.ts", "dependencies": { - "node-fetch": "^2.2.0", + "node-fetch": "^2.6.1", "unfetch": "^4.0.0" } } diff --git a/polyfill/package.json b/polyfill/package.json index edcbbda..9811fbc 100644 --- a/polyfill/package.json +++ b/polyfill/package.json @@ -1,5 +1,5 @@ { "name": "unfetch-polyfill", "main": "index.js", - "module": "polyfill.mjs" + "module": "polyfill.module.js" } diff --git a/polyfill/polyfill.mjs b/polyfill/polyfill.mjs index e705e65..3ed0e73 100644 --- a/polyfill/polyfill.mjs +++ b/polyfill/polyfill.mjs @@ -1,2 +1,2 @@ -import unfetch from '../src/index.mjs'; +import unfetch from '..'; if (!self.fetch) self.fetch = unfetch; diff --git a/src/index.d.ts b/src/index.d.ts index 39162e8..7d582fa 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -14,6 +14,33 @@ declare namespace unfetch { export type IsomorphicRequestInit = RequestInit | NodeRequestInit; } -declare const unfetch: typeof fetch; +type UnfetchResponse = { + ok: boolean, + statusText: string, + status: number, + url: string, + text: () => Promise, + json: () => Promise, + blob: () => Promise, + clone: () => UnfetchResponse, + headers: { + keys: () => string[], + entries: () => Array<[string, string]>, + get: (key: string) => string | undefined, + has: (key: string) => boolean, + } +} + +type Unfetch = ( + url: string, + options?: { + method?: string, + headers?: Record, + credentials?: 'include' | 'omit', + body?: Parameters[0] + } +) => Promise + +declare const unfetch: Unfetch; export default unfetch; diff --git a/src/index.mjs b/src/index.mjs index 783ad42..96066f6 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -12,7 +12,7 @@ export default function(url, options) { status: request.status, url: request.responseURL, text: () => Promise.resolve(request.responseText), - json: () => Promise.resolve(JSON.parse(request.responseText)), + json: () => Promise.resolve(request.responseText).then(JSON.parse), blob: () => Promise.resolve(new Blob([request.response])), clone: response, headers: {