Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-abortable-fetch

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-abortable-fetch - npm Package Compare versions

Comparing version 2.4.0 to 2.6.0

.cache/03/3b21237774c0ad8cb85c2c948fc6f1.json

29

package.json
{
"name": "use-abortable-fetch",
"version": "2.4.0",
"version": "2.6.0",
"main": "release/index.js",

@@ -12,3 +12,4 @@ "umd:main": "release/index.umd.js",

"prettier": "prettier --write {src,example}/**/*.{js,ts,jsx,tsx}",
"test": "jest"
"test": "jest",
"eslint": "eslint ./src/**/*.{ts,tsx}"
},

@@ -19,12 +20,21 @@ "repository": "git@github.com:mauricedb/use-abortable-fetch.git",

"devDependencies": {
"@types/react": "16.8.7",
"@types/react-dom": "16.8.2",
"@types/content-type": "1.1.3",
"@types/jest": "^24.0.9",
"@types/media-typer": "0.3.0",
"@types/react": "16.8.8",
"@types/react-dom": "16.8.3",
"@typescript-eslint/eslint-plugin": "^1.4.2",
"@typescript-eslint/parser": "^1.4.2",
"eslint": "^5.15.1",
"eslint-config-prettier": "4.1.0",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-react-hooks": "^1.5.1",
"jest": "24.3.1",
"jest-fetch-mock": "2.1.1",
"microbundle": "0.11.0",
"parcel-bundler": "1.12.0",
"parcel-bundler": "1.12.3",
"prettier": "1.16.4",
"react": "16.8.4",
"react-dom": "16.8.4",
"react-testing-library": "6.0.0",
"react": "16.8.5",
"react-dom": "16.8.5",
"react-testing-library": "6.0.2",
"ts-jest": "24.0.0"

@@ -36,5 +46,2 @@ },

"dependencies": {
"@types/content-type": "1.1.3",
"@types/jest": "^24.0.9",
"@types/media-typer": "0.3.0",
"content-type": "1.0.4",

@@ -41,0 +48,0 @@ "media-typer": "1.0.1"

import { Dispatch, SetStateAction } from 'react';
import { FetchState } from './types';
declare const fetchData: <T>(url: string, init: RequestInit, signal: AbortSignal, setState: Dispatch<SetStateAction<FetchState<T>>>) => Promise<void>;
declare const fetchData: <T>(url: string, init: RequestInit | undefined, signal: AbortSignal, setState: Dispatch<SetStateAction<FetchState<T>>>) => Promise<void>;
export default fetchData;
import { AbortableFetchResult } from './types';
declare const useAbortableFetch: <T>(url: string | null, init?: RequestInit) => AbortableFetchResult<T>;
declare const useAbortableFetch: <T>(url: string | null, init?: RequestInit | undefined) => AbortableFetchResult<T>;
export default useAbortableFetch;

@@ -1,2 +0,2 @@

function n(n){return n&&"object"==typeof n&&"default"in n?n.default:n}var t=require("react"),e=n(require("media-typer")),r=n(require("content-type"));const o=function(){function n(){}return n.prototype.then=function(t,e){const r=new n,o=this.s;if(o){const n=1&o?t:e;if(n){try{u(r,1,n(this.v))}catch(n){u(r,2,n)}return r}return this}return this.o=function(n){try{const o=n.v;1&n.s?u(r,1,t?t(o):o):e?u(r,1,e(o)):u(r,2,o)}catch(n){u(r,2,n)}},r},n}();function u(n,t,e){if(!n.s){if(e instanceof o){if(!e.s)return void(e.o=u.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(u.bind(null,n,t),u.bind(null,n,2));n.s=t,n.v=e;const r=n.o;r&&r(n)}}const i={};!function(){function n(n){this._entry=n,this._pact=null,this._resolve=null,this._return=null,this._promise=null}function t(n){return{value:n,done:!0}}function e(n){return{value:n,done:!1}}n.prototype[Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator"))]=function(){return this},n.prototype._yield=function(n){return this._resolve(n&&n.then?n.then(e):e(n)),this._pact=new o},n.prototype.next=function(n){const e=this;return e._promise=new Promise(function(r){const l=e._pact;if(null===l){const n=e._entry;if(null===n)return r(e._promise);function s(n){e._resolve(n&&n.then?n.then(t):t(n)),e._pact=null,e._resolve=null}e._entry=null,e._resolve=r,n(e).then(s,function(n){if(n===i)s(e._return);else{const t=new o;e._resolve(t),e._pact=null,e._resolve=null,_resolve(t,2,n)}})}else e._pact=null,e._resolve=r,u(l,1,n)})},n.prototype.return=function(n){const e=this;return e._promise=new Promise(function(r){const o=e._pact;if(null===o)return null===e._entry?r(e._promise):(e._entry=null,r(n&&n.then?n.then(t):t(n)));e._return=n,e._resolve=r,e._pact=null,u(o,2,i)})},n.prototype.throw=function(n){const t=this;return t._promise=new Promise(function(e,r){const o=t._pact;if(null===o)return null===t._entry?e(t._promise):(t._entry=null,r(n));t._resolve=e,t._pact=null,u(o,2,n)})}}(),module.exports=function(n,o){void 0===o&&(o={});var u=t.useState({data:null,loading:0,error:null,controller:null}),i=u[0],l=u[1],s=t.useRef(!1);return t.useLayoutEffect(function(){return s.current=!0,function(){s.current=!1}},[]),t.useEffect(function(){var t=new AbortController;return n&&(l(function(n){return{data:null,loading:n.loading+1,error:null,controller:t}}),function(n,t,o,u){try{var i=Object.assign({},t,{signal:o});Promise.resolve(function(t,o){try{var l=Promise.resolve(fetch(n,i)).then(function(n){function t(){u(function(n){return Object.assign({},n,{data:i,loading:n.loading-1})})}if(!n.ok){var o=new Error(n.statusText);throw o.status=n.status,o}var i=null,l=function(n){if(n){var t=r.parse(n),o=e.parse(t.type);if("json"===o.subtype)return!0;if("json"===o.suffix)return!0;if(o.suffix&&/\bjson\b/i.test(o.suffix))return!0;if(o.subtype&&/\bjson\b/i.test(o.subtype))return!0}return!1}(n.headers.get("content-type"))?Promise.resolve(n.json()).then(function(n){i=n}):Promise.resolve(n.text()).then(function(n){i=n});return l&&l.then?l.then(t):t()})}catch(n){return o(n)}return l&&l.then?l.then(void 0,o):l}(0,function(n){var t="AbortError"!==n.name?n:null;u(function(n){return Object.assign({},n,{error:t,loading:n.loading-1})})}))}catch(n){return Promise.reject(n)}}(n,o,t.signal,function(n){s.current&&l(n)})),function(){return t.abort()}},[n]),{data:i.data,loading:!!i.loading,error:i.error,abort:function(){return i.controller&&i.controller.abort()}}};
function n(n){return n&&"object"==typeof n&&"default"in n?n.default:n}var t=require("react"),e=n(require("media-typer")),r=n(require("content-type"));const o=function(){function n(){}return n.prototype.then=function(t,e){const r=new n,o=this.s;if(o){const n=1&o?t:e;if(n){try{u(r,1,n(this.v))}catch(n){u(r,2,n)}return r}return this}return this.o=function(n){try{const o=n.v;1&n.s?u(r,1,t?t(o):o):e?u(r,1,e(o)):u(r,2,o)}catch(n){u(r,2,n)}},r},n}();function u(n,t,e){if(!n.s){if(e instanceof o){if(!e.s)return void(e.o=u.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(u.bind(null,n,t),u.bind(null,n,2));n.s=t,n.v=e;const r=n.o;r&&r(n)}}const i={};!function(){function n(n){this._entry=n,this._pact=null,this._resolve=null,this._return=null,this._promise=null}function t(n){return{value:n,done:!0}}function e(n){return{value:n,done:!1}}n.prototype[Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator"))]=function(){return this},n.prototype._yield=function(n){return this._resolve(n&&n.then?n.then(e):e(n)),this._pact=new o},n.prototype.next=function(n){const e=this;return e._promise=new Promise(function(r){const l=e._pact;if(null===l){const n=e._entry;if(null===n)return r(e._promise);function s(n){e._resolve(n&&n.then?n.then(t):t(n)),e._pact=null,e._resolve=null}e._entry=null,e._resolve=r,n(e).then(s,function(n){if(n===i)s(e._return);else{const t=new o;e._resolve(t),e._pact=null,e._resolve=null,_resolve(t,2,n)}})}else e._pact=null,e._resolve=r,u(l,1,n)})},n.prototype.return=function(n){const e=this;return e._promise=new Promise(function(r){const o=e._pact;if(null===o)return null===e._entry?r(e._promise):(e._entry=null,r(n&&n.then?n.then(t):t(n)));e._return=n,e._resolve=r,e._pact=null,u(o,2,i)})},n.prototype.throw=function(n){const t=this;return t._promise=new Promise(function(e,r){const o=t._pact;if(null===o)return null===t._entry?e(t._promise):(t._entry=null,r(n));t._resolve=e,t._pact=null,u(o,2,n)})}}(),module.exports=function(n,o){var u=t.useState({data:null,loading:0,error:null,controller:null}),i=u[0],l=u[1],s=t.useRef(!1);return t.useLayoutEffect(function(){return s.current=!0,function(){s.current=!1}},[]),t.useEffect(function(){var t=new AbortController;return n&&(l(function(n){return{data:null,loading:n.loading+1,error:null,controller:t}}),function(n,t,o,u){void 0===t&&(t={});try{var i=Object.assign({},t,{signal:o});Promise.resolve(function(t,o){try{var l=Promise.resolve(fetch(n,i)).then(function(n){function t(){u(function(n){return Object.assign({},n,{data:i,loading:n.loading-1})})}if(!n.ok){var o=new Error(n.statusText);throw o.status=n.status,o}var i=null,l=function(n){if(n){var t=r.parse(n),o=e.parse(t.type);if("json"===o.subtype)return!0;if("json"===o.suffix)return!0;if(o.suffix&&/\bjson\b/i.test(o.suffix))return!0;if(o.subtype&&/\bjson\b/i.test(o.subtype))return!0}return!1}(n.headers.get("content-type"))?Promise.resolve(n.json()).then(function(n){i=n}):Promise.resolve(n.text()).then(function(n){i=n});return l&&l.then?l.then(t):t()})}catch(n){return o(n)}return l&&l.then?l.then(void 0,o):l}(0,function(n){var t="AbortError"!==n.name?n:null;u(function(n){return Object.assign({},n,{error:t,loading:n.loading-1})})}))}catch(n){return Promise.reject(n)}}(n,o,t.signal,function(n){s.current&&l(n)})),function(){return t.abort()}},[o,n]),{data:i.data,loading:!!i.loading,error:i.error,abort:function(){return i.controller&&i.controller.abort()}}};
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("media-typer"),require("content-type")):"function"==typeof define&&define.amd?define(["react","media-typer","content-type"],t):n.useAbortableFetch=t(n.react,n.mediaTyper,n.contentType)}(this,function(n,t,e){t=t&&t.hasOwnProperty("default")?t.default:t,e=e&&e.hasOwnProperty("default")?e.default:e;const r=function(){function n(){}return n.prototype.then=function(t,e){const r=new n,u=this.s;if(u){const n=1&u?t:e;if(n){try{o(r,1,n(this.v))}catch(n){o(r,2,n)}return r}return this}return this.o=function(n){try{const u=n.v;1&n.s?o(r,1,t?t(u):u):e?o(r,1,e(u)):o(r,2,u)}catch(n){o(r,2,n)}},r},n}();function o(n,t,e){if(!n.s){if(e instanceof r){if(!e.s)return void(e.o=o.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(o.bind(null,n,t),o.bind(null,n,2));n.s=t,n.v=e;const u=n.o;u&&u(n)}}const u={};return function(){function n(n){this._entry=n,this._pact=null,this._resolve=null,this._return=null,this._promise=null}function t(n){return{value:n,done:!0}}function e(n){return{value:n,done:!1}}n.prototype[Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator"))]=function(){return this},n.prototype._yield=function(n){return this._resolve(n&&n.then?n.then(e):e(n)),this._pact=new r},n.prototype.next=function(n){const e=this;return e._promise=new Promise(function(i){const l=e._pact;if(null===l){const n=e._entry;if(null===n)return i(e._promise);function s(n){e._resolve(n&&n.then?n.then(t):t(n)),e._pact=null,e._resolve=null}e._entry=null,e._resolve=i,n(e).then(s,function(n){if(n===u)s(e._return);else{const t=new r;e._resolve(t),e._pact=null,e._resolve=null,_resolve(t,2,n)}})}else e._pact=null,e._resolve=i,o(l,1,n)})},n.prototype.return=function(n){const e=this;return e._promise=new Promise(function(r){const i=e._pact;if(null===i)return null===e._entry?r(e._promise):(e._entry=null,r(n&&n.then?n.then(t):t(n)));e._return=n,e._resolve=r,e._pact=null,o(i,2,u)})},n.prototype.throw=function(n){const t=this;return t._promise=new Promise(function(e,r){const u=t._pact;if(null===u)return null===t._entry?e(t._promise):(t._entry=null,r(n));t._resolve=e,t._pact=null,o(u,2,n)})}}(),function(r,o){void 0===o&&(o={});var u=n.useState({data:null,loading:0,error:null,controller:null}),i=u[0],l=u[1],s=n.useRef(!1);return n.useLayoutEffect(function(){return s.current=!0,function(){s.current=!1}},[]),n.useEffect(function(){var n=new AbortController;return r&&(l(function(t){return{data:null,loading:t.loading+1,error:null,controller:n}}),function(n,r,o,u){try{var i=Object.assign({},r,{signal:o});Promise.resolve(function(r,o){try{var l=Promise.resolve(fetch(n,i)).then(function(n){function r(){u(function(n){return Object.assign({},n,{data:i,loading:n.loading-1})})}if(!n.ok){var o=new Error(n.statusText);throw o.status=n.status,o}var i=null,l=function(n){if(n){var r=e.parse(n),o=t.parse(r.type);if("json"===o.subtype)return!0;if("json"===o.suffix)return!0;if(o.suffix&&/\bjson\b/i.test(o.suffix))return!0;if(o.subtype&&/\bjson\b/i.test(o.subtype))return!0}return!1}(n.headers.get("content-type"))?Promise.resolve(n.json()).then(function(n){i=n}):Promise.resolve(n.text()).then(function(n){i=n});return l&&l.then?l.then(r):r()})}catch(n){return o(n)}return l&&l.then?l.then(void 0,o):l}(0,function(n){var t="AbortError"!==n.name?n:null;u(function(n){return Object.assign({},n,{error:t,loading:n.loading-1})})}))}catch(n){return Promise.reject(n)}}(r,o,n.signal,function(n){s.current&&l(n)})),function(){return n.abort()}},[r]),{data:i.data,loading:!!i.loading,error:i.error,abort:function(){return i.controller&&i.controller.abort()}}}});
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("media-typer"),require("content-type")):"function"==typeof define&&define.amd?define(["react","media-typer","content-type"],t):n.useAbortableFetch=t(n.react,n.mediaTyper,n.contentType)}(this,function(n,t,e){t=t&&t.hasOwnProperty("default")?t.default:t,e=e&&e.hasOwnProperty("default")?e.default:e;const r=function(){function n(){}return n.prototype.then=function(t,e){const r=new n,u=this.s;if(u){const n=1&u?t:e;if(n){try{o(r,1,n(this.v))}catch(n){o(r,2,n)}return r}return this}return this.o=function(n){try{const u=n.v;1&n.s?o(r,1,t?t(u):u):e?o(r,1,e(u)):o(r,2,u)}catch(n){o(r,2,n)}},r},n}();function o(n,t,e){if(!n.s){if(e instanceof r){if(!e.s)return void(e.o=o.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(o.bind(null,n,t),o.bind(null,n,2));n.s=t,n.v=e;const u=n.o;u&&u(n)}}const u={};return function(){function n(n){this._entry=n,this._pact=null,this._resolve=null,this._return=null,this._promise=null}function t(n){return{value:n,done:!0}}function e(n){return{value:n,done:!1}}n.prototype[Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator"))]=function(){return this},n.prototype._yield=function(n){return this._resolve(n&&n.then?n.then(e):e(n)),this._pact=new r},n.prototype.next=function(n){const e=this;return e._promise=new Promise(function(i){const l=e._pact;if(null===l){const n=e._entry;if(null===n)return i(e._promise);function s(n){e._resolve(n&&n.then?n.then(t):t(n)),e._pact=null,e._resolve=null}e._entry=null,e._resolve=i,n(e).then(s,function(n){if(n===u)s(e._return);else{const t=new r;e._resolve(t),e._pact=null,e._resolve=null,_resolve(t,2,n)}})}else e._pact=null,e._resolve=i,o(l,1,n)})},n.prototype.return=function(n){const e=this;return e._promise=new Promise(function(r){const i=e._pact;if(null===i)return null===e._entry?r(e._promise):(e._entry=null,r(n&&n.then?n.then(t):t(n)));e._return=n,e._resolve=r,e._pact=null,o(i,2,u)})},n.prototype.throw=function(n){const t=this;return t._promise=new Promise(function(e,r){const u=t._pact;if(null===u)return null===t._entry?e(t._promise):(t._entry=null,r(n));t._resolve=e,t._pact=null,o(u,2,n)})}}(),function(r,o){var u=n.useState({data:null,loading:0,error:null,controller:null}),i=u[0],l=u[1],s=n.useRef(!1);return n.useLayoutEffect(function(){return s.current=!0,function(){s.current=!1}},[]),n.useEffect(function(){var n=new AbortController;return r&&(l(function(t){return{data:null,loading:t.loading+1,error:null,controller:n}}),function(n,r,o,u){void 0===r&&(r={});try{var i=Object.assign({},r,{signal:o});Promise.resolve(function(r,o){try{var l=Promise.resolve(fetch(n,i)).then(function(n){function r(){u(function(n){return Object.assign({},n,{data:i,loading:n.loading-1})})}if(!n.ok){var o=new Error(n.statusText);throw o.status=n.status,o}var i=null,l=function(n){if(n){var r=e.parse(n),o=t.parse(r.type);if("json"===o.subtype)return!0;if("json"===o.suffix)return!0;if(o.suffix&&/\bjson\b/i.test(o.suffix))return!0;if(o.subtype&&/\bjson\b/i.test(o.subtype))return!0}return!1}(n.headers.get("content-type"))?Promise.resolve(n.json()).then(function(n){i=n}):Promise.resolve(n.text()).then(function(n){i=n});return l&&l.then?l.then(r):r()})}catch(n){return o(n)}return l&&l.then?l.then(void 0,o):l}(0,function(n){var t="AbortError"!==n.name?n:null;u(function(n){return Object.assign({},n,{error:t,loading:n.loading-1})})}))}catch(n){return Promise.reject(n)}}(r,o,n.signal,function(n){s.current&&l(n)})),function(){return n.abort()}},[o,r]),{data:i.data,loading:!!i.loading,error:i.error,abort:function(){return i.controller&&i.controller.abort()}}}});
//# sourceMappingURL=index.umd.js.map

@@ -1,5 +0,5 @@

declare type SharedFetchState<T> = {
interface SharedFetchState<T> {
data: T | string | null;
error: Error | null;
};
}
export declare type FetchState<T> = SharedFetchState<T> & {

@@ -6,0 +6,0 @@ controller: AbortController | null;

@@ -5,9 +5,10 @@ import { Dispatch, SetStateAction } from 'react';

import isJSON from './isJSON';
import { CustomConsole } from '@jest/console';
const fetchData = async <T>(
url: string,
init: RequestInit,
init: RequestInit = {},
signal: AbortSignal,
setState: Dispatch<SetStateAction<FetchState<T>>>
) => {
): Promise<void> => {
const actualInit: RequestInit = { ...init, signal };

@@ -17,3 +18,18 @@

const rsp = await fetch(url, actualInit);
const contentTypeHeader = rsp.headers.get('content-type');
if (contentTypeHeader) {
let data: T | string | null = null;
if (isJSON(contentTypeHeader)) {
data = await rsp.json();
} else {
data = await rsp.text();
}
setState((oldState: FetchState<T>) => ({
...oldState,
data
}));
}
if (!rsp.ok) {

@@ -24,14 +40,5 @@ const err: any = new Error(rsp.statusText);

}
let data: T | string | null = null;
const contentTypeHeader = rsp.headers.get('content-type');
if (isJSON(contentTypeHeader)) {
data = await rsp.json();
} else {
data = await rsp.text();
}
setState((oldState: FetchState<T>) => ({
...oldState,
data,
loading: oldState.loading - 1

@@ -38,0 +45,0 @@ }));

@@ -8,3 +8,3 @@ import { useState, useEffect, useLayoutEffect, useRef } from 'react';

url: string | null,
init: RequestInit = {}
init?: RequestInit
): AbortableFetchResult<T> => {

@@ -44,3 +44,3 @@ const [state, setState] = useState<FetchState<T>>({

return () => controller.abort();
}, [url]);
}, [init, url]);

@@ -47,0 +47,0 @@ return {

import mediaTyper from 'media-typer';
import contentType from 'content-type';
export default function isJSON(contentTypeHeader: string | null) {
export default function isJSON(contentTypeHeader: string | null): boolean {
if (contentTypeHeader) {

@@ -6,0 +6,0 @@ const ct = contentType.parse(contentTypeHeader);

@@ -1,5 +0,5 @@

type SharedFetchState<T> = {
interface SharedFetchState<T> {
data: T | string | null;
error: Error | null;
};
}

@@ -6,0 +6,0 @@ export type FetchState<T> = SharedFetchState<T> & {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc