Skip to content

Commit

Permalink
Merge branch 'master' into topic/export-request-init-type
Browse files Browse the repository at this point in the history
  • Loading branch information
developit authored Sep 29, 2020
2 parents cfd6bff + aff2b9d commit 1583aad
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 12 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/isomorphic-unfetch/browser.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = window.fetch || (window.fetch = require('unfetch').default || require('unfetch'));
module.exports = self.fetch || (self.fetch = require('unfetch').default || require('unfetch'));
5 changes: 3 additions & 2 deletions packages/isomorphic-unfetch/index.js
Original file line number Diff line number Diff line change
@@ -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);
})
);
13 changes: 13 additions & 0 deletions packages/isomorphic-unfetch/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/isomorphic-unfetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion polyfill/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "unfetch-polyfill",
"main": "index.js",
"module": "polyfill.mjs"
"module": "polyfill.module.js"
}
2 changes: 1 addition & 1 deletion polyfill/polyfill.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import unfetch from '../src/index.mjs';
import unfetch from '..';
if (!self.fetch) self.fetch = unfetch;
29 changes: 28 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
json: () => Promise<any>,
blob: () => Promise<Blob>,
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<string, string>,
credentials?: 'include' | 'omit',
body?: Parameters<XMLHttpRequest["send"]>[0]
}
) => Promise<UnfetchResponse>

declare const unfetch: Unfetch;

export default unfetch;
2 changes: 1 addition & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 1583aad

Please sign in to comment.