@andrewcaires/fetch 1.0.0 → 1.0.2

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/README.md ADDED
@@ -0,0 +1,22 @@
1
+ [![npm](https://img.shields.io/npm/v/@andrewcaires/fetch?color=blue&logo=npm)](https://www.npmjs.com/package/@andrewcaires/fetch)
2
+ [![downloads](https://img.shields.io/npm/dt/@andrewcaires/fetch?color=blue)](https://www.npmjs.com/package/@andrewcaires/fetch)
3
+ [![size](https://img.shields.io/bundlephobia/min/@andrewcaires/fetch?color=blue)](https://www.npmjs.com/package/@andrewcaires/fetch)
4
+ [![license](https://img.shields.io/github/license/andrewcaires/npm?color=blue)](https://github.com/andrewcaires/npm/blob/main/LICENSE)
5
+
6
+ # fetch
7
+
8
+ Plugin for fetch api
9
+
10
+ ## Installation
11
+
12
+ `npm i @andrewcaires/fetch`
13
+
14
+ ### Links
15
+
16
+ * [Docs](https://github.com/andrewcaires/npm/blob/main/fetch/README.md)
17
+ * [GitHub](https://github.com/andrewcaires/npm/tree/main/fetch)
18
+ * [npm](https://www.npmjs.com/package/@andrewcaires/fetch)
19
+
20
+ ## License
21
+
22
+ * [MIT](https://github.com/andrewcaires/npm/blob/main/LICENSE)
package/dist/index.d.ts CHANGED
@@ -38,7 +38,6 @@ export interface FetchResponse {
38
38
  export declare class Fetch extends EventEmitter {
39
39
  private _options;
40
40
  constructor(options?: FetchOptions);
41
- create(options?: FetchOptions): Fetch;
42
41
  delete(path: string, query?: FetchQuery): Promise<FetchResponse>;
43
42
  get(path: string, query?: FetchQuery): Promise<FetchResponse>;
44
43
  head(path: string, query?: FetchQuery): Promise<FetchResponse>;
@@ -51,7 +50,6 @@ export declare class Fetch extends EventEmitter {
51
50
  private _body;
52
51
  private _data;
53
52
  private _headers;
54
- private _query;
55
53
  private _request;
56
54
  private _signal;
57
55
  private _url;
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.0.0
2
+ * @andrewcaires/fetch v1.0.2
3
3
  * (c) 2022 Andrew Caires
4
4
  * @license: MIT
5
5
  */
6
- import{isString as t,isDef as e,EventEmitter as n}from"@andrewcaires/utils.js";var r=function(t,e){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},r(t,e)};var o=function(){return o=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},o.apply(this,arguments)};function i(t,e,n,r){return new(n||(n=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function s(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,s)}a((r=r.apply(t,e||[])).next())}))}function u(t,e){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var s={headers:{},timeout:5e3},a=function(n){function a(t){void 0===t&&(t={});var e=n.call(this)||this;return e._options=o(o({},s),t),e}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(a,n),a.prototype.create=function(t){return void 0===t&&(t={}),new a(o(o({},this._options),t))},a.prototype.delete=function(t,e){return i(this,void 0,void 0,(function(){return u(this,(function(n){return[2,this.fetch({method:"delete",path:t,query:e})]}))}))},a.prototype.get=function(t,e){return i(this,void 0,void 0,(function(){return u(this,(function(n){return[2,this.fetch({method:"get",path:t,query:e})]}))}))},a.prototype.head=function(t,e){return i(this,void 0,void 0,(function(){return u(this,(function(n){return[2,this.fetch({method:"head",path:t,query:e})]}))}))},a.prototype.options=function(t,e){return i(this,void 0,void 0,(function(){return u(this,(function(n){return[2,this.fetch({method:"options",path:t,query:e})]}))}))},a.prototype.patch=function(t,e,n){return i(this,void 0,void 0,(function(){return u(this,(function(r){return[2,this.fetch({method:"patch",path:t,query:n,body:e})]}))}))},a.prototype.post=function(t,e,n){return i(this,void 0,void 0,(function(){return u(this,(function(r){return[2,this.fetch({method:"post",path:t,query:n,body:e})]}))}))},a.prototype.put=function(t,e,n){return i(this,void 0,void 0,(function(){return u(this,(function(r){return[2,this.fetch({method:"put",path:t,query:n,body:e})]}))}))},a.prototype.request=function(t,e){return i(this,void 0,void 0,(function(){return u(this,(function(n){return[2,this.fetch({method:"request",path:t,query:e})]}))}))},a.prototype.fetch=function(t){return i(this,void 0,void 0,(function(){var e,n,r,o,i;return u(this,(function(u){switch(u.label){case 0:e=this._request(t),n={},u.label=1;case 1:return u.trys.push([1,4,,5]),this.emit("before",e),r=n,[4,fetch(e.url,e)];case 2:return r.raw=u.sent(),o=n,[4,this._data(n.raw)];case 3:return o.data=u.sent(),n.raw.ok||(n.error=n.raw.statusText),this.emit("complete",n),[3,5];case 4:return i=u.sent(),this.emit("error",i),n.error=i.message,[3,5];case 5:return[2,n]}}))}))},a.prototype._body=function(e){var n=e instanceof Blob,r=e instanceof FormData,o=!(n||r||t(e));return{json:o,parse:o?e?JSON.stringify(e):null:e}},a.prototype._data=function(t){var e,n=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",r=n.indexOf("text/html")>=0,o=n.indexOf("application/json")>=0;return!n||r?t.text():o?t.json():t.blob()},a.prototype._headers=function(t){return o(o({},this._options.headers),t)},a.prototype._query=function(t){var e=Object.keys(t||{}),n=new URLSearchParams;return t&&e.forEach((function(e){return n.set(e,t[e])})),e.length?"?"+n.toString():void 0},a.prototype._request=function(t){var e=t.url,n=t.path,r=t.query,o=t.method,i=t.body,u=t.headers,s=t.timeout,a=this._query(r);e=this._url(e,n,a),o=o.toUpperCase(),u=this._headers(u);var c=this._body(i),h=c.json,p=c.parse;return h&&(u["Content-Type"]="application/json"),{url:e,method:o,body:p,headers:u,signal:this._signal(s)}},a.prototype._signal=function(t){var e=new AbortController;return(t||this._options.timeout)&&setTimeout((function(){return e.abort()}),t||this._options.timeout),e.signal},a.prototype._url=function(t,n,r){return[t=t||this._options.url,n,r].filter(e).map((function(t){return null==t?void 0:t.toString().replace(/(^\/+|\/+$)/gm,"")})).join("/")},a}(n);export{a as Fetch,a as default};
6
+ import{isString as t,isUndefined as e,forEachKey as r,EventEmitter as n}from"@andrewcaires/utils.js";var o=function(t,e){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},o(t,e)};var i=function(){return i=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},i.apply(this,arguments)};function u(t,e,r,n){return new(r||(r=Promise))((function(o,i){function u(t){try{a(n.next(t))}catch(t){i(t)}}function s(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(u,s)}a((n=n.apply(t,e||[])).next())}))}function s(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var a=function(n){function a(t){void 0===t&&(t={});var e=n.call(this)||this;return e._options=i({headers:{},timeout:5e3},t),e}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(a,n),a.prototype.delete=function(t,e){return u(this,void 0,void 0,(function(){return s(this,(function(r){return[2,this.fetch({method:"delete",path:t,query:e})]}))}))},a.prototype.get=function(t,e){return u(this,void 0,void 0,(function(){return s(this,(function(r){return[2,this.fetch({method:"get",path:t,query:e})]}))}))},a.prototype.head=function(t,e){return u(this,void 0,void 0,(function(){return s(this,(function(r){return[2,this.fetch({method:"head",path:t,query:e})]}))}))},a.prototype.options=function(t,e){return u(this,void 0,void 0,(function(){return s(this,(function(r){return[2,this.fetch({method:"options",path:t,query:e})]}))}))},a.prototype.patch=function(t,e,r){return u(this,void 0,void 0,(function(){return s(this,(function(n){return[2,this.fetch({method:"patch",path:t,query:r,body:e})]}))}))},a.prototype.post=function(t,e,r){return u(this,void 0,void 0,(function(){return s(this,(function(n){return[2,this.fetch({method:"post",path:t,query:r,body:e})]}))}))},a.prototype.put=function(t,e,r){return u(this,void 0,void 0,(function(){return s(this,(function(n){return[2,this.fetch({method:"put",path:t,query:r,body:e})]}))}))},a.prototype.request=function(t,e){return u(this,void 0,void 0,(function(){return s(this,(function(r){return[2,this.fetch({method:"request",path:t,query:e})]}))}))},a.prototype.fetch=function(t){return u(this,void 0,void 0,(function(){var e,r,n,o,i;return s(this,(function(u){switch(u.label){case 0:e=this._request(t),r={},u.label=1;case 1:return u.trys.push([1,4,,5]),this.emit("before",e),n=r,[4,fetch(e.url,e)];case 2:return n.raw=u.sent(),o=r,[4,this._data(r.raw)];case 3:return o.data=u.sent(),r.raw.ok||(r.error=r.raw.statusText),this.emit("complete",r),[3,5];case 4:return i=u.sent(),this.emit("error",i),r.error=i.message,[3,5];case 5:return[2,r]}}))}))},a.prototype._body=function(e){var r=e instanceof Blob,n=e instanceof FormData,o=!(r||n||t(e));return{json:o,parse:o?e?JSON.stringify(e):null:e}},a.prototype._data=function(t){var e,r=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",n=r.indexOf("text/html")>=0,o=r.indexOf("application/json")>=0;return!r||n?t.text():o?t.json():t.blob()},a.prototype._headers=function(t){return i(i({},this._options.headers),t)},a.prototype._request=function(t){var e=t.url,r=t.path,n=t.query,o=t.method,i=t.body,u=t.headers,s=t.timeout;e=this._url(e,r,n),o=o.toUpperCase(),u=this._headers(u);var a=this._body(i),c=a.json,h=a.parse;return c&&(u["Content-Type"]="application/json"),{url:e,method:o,body:h,headers:u,signal:this._signal(s)}},a.prototype._signal=function(t){var r=new AbortController;return e(t)&&(t=this._options.timeout),t&&setTimeout((function(){return r.abort()}),t),r.signal},a.prototype._url=function(t,e,n){var o=new URL(e||"",t||this._options.url);return n&&r(n,(function(t,e){return o.searchParams.set(e,t)})),o.toString()},a}(n);export{a as Fetch,a as default};
package/dist/index.min.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.0.0
2
+ * @andrewcaires/fetch v1.0.2
3
3
  * (c) 2022 Andrew Caires
4
4
  * @license: MIT
5
5
  */
6
- var Fetch=function(t,e){"use strict";var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},n(t,e)};var r=function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};function o(t,e,n,r){return new(n||(n=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function s(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,s)}a((r=r.apply(t,e||[])).next())}))}function i(t,e){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var u={headers:{},timeout:5e3},s=function(t){function s(e){void 0===e&&(e={});var n=t.call(this)||this;return n._options=r(r({},u),e),n}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(s,t),s.prototype.create=function(t){return void 0===t&&(t={}),new s(r(r({},this._options),t))},s.prototype.delete=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"delete",path:t,query:e})]}))}))},s.prototype.get=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"get",path:t,query:e})]}))}))},s.prototype.head=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"head",path:t,query:e})]}))}))},s.prototype.options=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"options",path:t,query:e})]}))}))},s.prototype.patch=function(t,e,n){return o(this,void 0,void 0,(function(){return i(this,(function(r){return[2,this.fetch({method:"patch",path:t,query:n,body:e})]}))}))},s.prototype.post=function(t,e,n){return o(this,void 0,void 0,(function(){return i(this,(function(r){return[2,this.fetch({method:"post",path:t,query:n,body:e})]}))}))},s.prototype.put=function(t,e,n){return o(this,void 0,void 0,(function(){return i(this,(function(r){return[2,this.fetch({method:"put",path:t,query:n,body:e})]}))}))},s.prototype.request=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"request",path:t,query:e})]}))}))},s.prototype.fetch=function(t){return o(this,void 0,void 0,(function(){var e,n,r,o,u;return i(this,(function(i){switch(i.label){case 0:e=this._request(t),n={},i.label=1;case 1:return i.trys.push([1,4,,5]),this.emit("before",e),r=n,[4,fetch(e.url,e)];case 2:return r.raw=i.sent(),o=n,[4,this._data(n.raw)];case 3:return o.data=i.sent(),n.raw.ok||(n.error=n.raw.statusText),this.emit("complete",n),[3,5];case 4:return u=i.sent(),this.emit("error",u),n.error=u.message,[3,5];case 5:return[2,n]}}))}))},s.prototype._body=function(t){var n=t instanceof Blob,r=t instanceof FormData,o=!(n||r||e.isString(t));return{json:o,parse:o?t?JSON.stringify(t):null:t}},s.prototype._data=function(t){var e,n=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",r=n.indexOf("text/html")>=0,o=n.indexOf("application/json")>=0;return!n||r?t.text():o?t.json():t.blob()},s.prototype._headers=function(t){return r(r({},this._options.headers),t)},s.prototype._query=function(t){var e=Object.keys(t||{}),n=new URLSearchParams;return t&&e.forEach((function(e){return n.set(e,t[e])})),e.length?"?"+n.toString():void 0},s.prototype._request=function(t){var e=t.url,n=t.path,r=t.query,o=t.method,i=t.body,u=t.headers,s=t.timeout,a=this._query(r);e=this._url(e,n,a),o=o.toUpperCase(),u=this._headers(u);var c=this._body(i),h=c.json,p=c.parse;return h&&(u["Content-Type"]="application/json"),{url:e,method:o,body:p,headers:u,signal:this._signal(s)}},s.prototype._signal=function(t){var e=new AbortController;return(t||this._options.timeout)&&setTimeout((function(){return e.abort()}),t||this._options.timeout),e.signal},s.prototype._url=function(t,n,r){return[t=t||this._options.url,n,r].filter(e.isDef).map((function(t){return null==t?void 0:t.toString().replace(/(^\/+|\/+$)/gm,"")})).join("/")},s}(e.EventEmitter);return t.Fetch=s,t.default=s,Object.defineProperty(t,"__esModule",{value:!0}),t}({},UtilsJS);
6
+ var Fetch=function(t,e){"use strict";var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},n(t,e)};var r=function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};function o(t,e,n,r){return new(n||(n=Promise))((function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function s(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,s)}a((r=r.apply(t,e||[])).next())}))}function i(t,e){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var u=function(t){function u(e){void 0===e&&(e={});var n=t.call(this)||this;return n._options=r({headers:{},timeout:5e3},e),n}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(u,t),u.prototype.delete=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"delete",path:t,query:e})]}))}))},u.prototype.get=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"get",path:t,query:e})]}))}))},u.prototype.head=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"head",path:t,query:e})]}))}))},u.prototype.options=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"options",path:t,query:e})]}))}))},u.prototype.patch=function(t,e,n){return o(this,void 0,void 0,(function(){return i(this,(function(r){return[2,this.fetch({method:"patch",path:t,query:n,body:e})]}))}))},u.prototype.post=function(t,e,n){return o(this,void 0,void 0,(function(){return i(this,(function(r){return[2,this.fetch({method:"post",path:t,query:n,body:e})]}))}))},u.prototype.put=function(t,e,n){return o(this,void 0,void 0,(function(){return i(this,(function(r){return[2,this.fetch({method:"put",path:t,query:n,body:e})]}))}))},u.prototype.request=function(t,e){return o(this,void 0,void 0,(function(){return i(this,(function(n){return[2,this.fetch({method:"request",path:t,query:e})]}))}))},u.prototype.fetch=function(t){return o(this,void 0,void 0,(function(){var e,n,r,o,u;return i(this,(function(i){switch(i.label){case 0:e=this._request(t),n={},i.label=1;case 1:return i.trys.push([1,4,,5]),this.emit("before",e),r=n,[4,fetch(e.url,e)];case 2:return r.raw=i.sent(),o=n,[4,this._data(n.raw)];case 3:return o.data=i.sent(),n.raw.ok||(n.error=n.raw.statusText),this.emit("complete",n),[3,5];case 4:return u=i.sent(),this.emit("error",u),n.error=u.message,[3,5];case 5:return[2,n]}}))}))},u.prototype._body=function(t){var n=t instanceof Blob,r=t instanceof FormData,o=!(n||r||e.isString(t));return{json:o,parse:o?t?JSON.stringify(t):null:t}},u.prototype._data=function(t){var e,n=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",r=n.indexOf("text/html")>=0,o=n.indexOf("application/json")>=0;return!n||r?t.text():o?t.json():t.blob()},u.prototype._headers=function(t){return r(r({},this._options.headers),t)},u.prototype._request=function(t){var e=t.url,n=t.path,r=t.query,o=t.method,i=t.body,u=t.headers,s=t.timeout;e=this._url(e,n,r),o=o.toUpperCase(),u=this._headers(u);var a=this._body(i),c=a.json,h=a.parse;return c&&(u["Content-Type"]="application/json"),{url:e,method:o,body:h,headers:u,signal:this._signal(s)}},u.prototype._signal=function(t){var n=new AbortController;return e.isUndefined(t)&&(t=this._options.timeout),t&&setTimeout((function(){return n.abort()}),t),n.signal},u.prototype._url=function(t,n,r){var o=new URL(n||"",t||this._options.url);return r&&e.forEachKey(r,(function(t,e){return o.searchParams.set(e,t)})),o.toString()},u}(e.EventEmitter);return t.Fetch=u,t.default=u,Object.defineProperty(t,"__esModule",{value:!0}),t}({},UtilsJS);
package/dist/index.umd.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.0.0
2
+ * @andrewcaires/fetch v1.0.2
3
3
  * (c) 2022 Andrew Caires
4
4
  * @license: MIT
5
5
  */
6
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@andrewcaires/utils.js"),e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},e(t,r)};var r=function(){return r=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};function n(t,e,r,n){return new(r||(r=Promise))((function(o,i){function u(t){try{a(n.next(t))}catch(t){i(t)}}function s(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(u,s)}a((n=n.apply(t,e||[])).next())}))}function o(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var i={headers:{},timeout:5e3},u=function(u){function s(t){void 0===t&&(t={});var e=u.call(this)||this;return e._options=r(r({},i),t),e}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}(s,u),s.prototype.create=function(t){return void 0===t&&(t={}),new s(r(r({},this._options),t))},s.prototype.delete=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"delete",path:t,query:e})]}))}))},s.prototype.get=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"get",path:t,query:e})]}))}))},s.prototype.head=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"head",path:t,query:e})]}))}))},s.prototype.options=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"options",path:t,query:e})]}))}))},s.prototype.patch=function(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(n){return[2,this.fetch({method:"patch",path:t,query:r,body:e})]}))}))},s.prototype.post=function(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(n){return[2,this.fetch({method:"post",path:t,query:r,body:e})]}))}))},s.prototype.put=function(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(n){return[2,this.fetch({method:"put",path:t,query:r,body:e})]}))}))},s.prototype.request=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"request",path:t,query:e})]}))}))},s.prototype.fetch=function(t){return n(this,void 0,void 0,(function(){var e,r,n,i,u;return o(this,(function(o){switch(o.label){case 0:e=this._request(t),r={},o.label=1;case 1:return o.trys.push([1,4,,5]),this.emit("before",e),n=r,[4,fetch(e.url,e)];case 2:return n.raw=o.sent(),i=r,[4,this._data(r.raw)];case 3:return i.data=o.sent(),r.raw.ok||(r.error=r.raw.statusText),this.emit("complete",r),[3,5];case 4:return u=o.sent(),this.emit("error",u),r.error=u.message,[3,5];case 5:return[2,r]}}))}))},s.prototype._body=function(e){var r=e instanceof Blob,n=e instanceof FormData,o=!(r||n||t.isString(e));return{json:o,parse:o?e?JSON.stringify(e):null:e}},s.prototype._data=function(t){var e,r=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",n=r.indexOf("text/html")>=0,o=r.indexOf("application/json")>=0;return!r||n?t.text():o?t.json():t.blob()},s.prototype._headers=function(t){return r(r({},this._options.headers),t)},s.prototype._query=function(t){var e=Object.keys(t||{}),r=new URLSearchParams;return t&&e.forEach((function(e){return r.set(e,t[e])})),e.length?"?"+r.toString():void 0},s.prototype._request=function(t){var e=t.url,r=t.path,n=t.query,o=t.method,i=t.body,u=t.headers,s=t.timeout,a=this._query(n);e=this._url(e,r,a),o=o.toUpperCase(),u=this._headers(u);var c=this._body(i),h=c.json,p=c.parse;return h&&(u["Content-Type"]="application/json"),{url:e,method:o,body:p,headers:u,signal:this._signal(s)}},s.prototype._signal=function(t){var e=new AbortController;return(t||this._options.timeout)&&setTimeout((function(){return e.abort()}),t||this._options.timeout),e.signal},s.prototype._url=function(e,r,n){return[e=e||this._options.url,r,n].filter(t.isDef).map((function(t){return null==t?void 0:t.toString().replace(/(^\/+|\/+$)/gm,"")})).join("/")},s}(t.EventEmitter);exports.Fetch=u,exports.default=u;
6
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@andrewcaires/utils.js"),e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},e(t,r)};var r=function(){return r=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};function n(t,e,r,n){return new(r||(r=Promise))((function(o,i){function u(t){try{a(n.next(t))}catch(t){i(t)}}function s(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(u,s)}a((n=n.apply(t,e||[])).next())}))}function o(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var i=function(i){function u(t){void 0===t&&(t={});var e=i.call(this)||this;return e._options=r({headers:{},timeout:5e3},t),e}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}(u,i),u.prototype.delete=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"delete",path:t,query:e})]}))}))},u.prototype.get=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"get",path:t,query:e})]}))}))},u.prototype.head=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"head",path:t,query:e})]}))}))},u.prototype.options=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"options",path:t,query:e})]}))}))},u.prototype.patch=function(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(n){return[2,this.fetch({method:"patch",path:t,query:r,body:e})]}))}))},u.prototype.post=function(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(n){return[2,this.fetch({method:"post",path:t,query:r,body:e})]}))}))},u.prototype.put=function(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(n){return[2,this.fetch({method:"put",path:t,query:r,body:e})]}))}))},u.prototype.request=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(r){return[2,this.fetch({method:"request",path:t,query:e})]}))}))},u.prototype.fetch=function(t){return n(this,void 0,void 0,(function(){var e,r,n,i,u;return o(this,(function(o){switch(o.label){case 0:e=this._request(t),r={},o.label=1;case 1:return o.trys.push([1,4,,5]),this.emit("before",e),n=r,[4,fetch(e.url,e)];case 2:return n.raw=o.sent(),i=r,[4,this._data(r.raw)];case 3:return i.data=o.sent(),r.raw.ok||(r.error=r.raw.statusText),this.emit("complete",r),[3,5];case 4:return u=o.sent(),this.emit("error",u),r.error=u.message,[3,5];case 5:return[2,r]}}))}))},u.prototype._body=function(e){var r=e instanceof Blob,n=e instanceof FormData,o=!(r||n||t.isString(e));return{json:o,parse:o?e?JSON.stringify(e):null:e}},u.prototype._data=function(t){var e,r=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",n=r.indexOf("text/html")>=0,o=r.indexOf("application/json")>=0;return!r||n?t.text():o?t.json():t.blob()},u.prototype._headers=function(t){return r(r({},this._options.headers),t)},u.prototype._request=function(t){var e=t.url,r=t.path,n=t.query,o=t.method,i=t.body,u=t.headers,s=t.timeout;e=this._url(e,r,n),o=o.toUpperCase(),u=this._headers(u);var a=this._body(i),c=a.json,h=a.parse;return c&&(u["Content-Type"]="application/json"),{url:e,method:o,body:h,headers:u,signal:this._signal(s)}},u.prototype._signal=function(e){var r=new AbortController;return t.isUndefined(e)&&(e=this._options.timeout),e&&setTimeout((function(){return r.abort()}),e),r.signal},u.prototype._url=function(e,r,n){var o=new URL(r||"",e||this._options.url);return n&&t.forEachKey(n,(function(t,e){return o.searchParams.set(e,t)})),o.toString()},u}(t.EventEmitter);exports.Fetch=i,exports.default=i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/fetch",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Plugin for fetch api",
5
5
  "main": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
@@ -23,12 +23,13 @@
23
23
  },
24
24
  "homepage": "https://github.com/andrewcaires/npm#readme",
25
25
  "dependencies": {
26
- "@andrewcaires/utils.js": "^0.1.1"
26
+ "@andrewcaires/utils.js": "^0.1.7"
27
27
  },
28
28
  "devDependencies": {
29
- "@rollup/plugin-commonjs": "^22.0.0",
30
- "@rollup/plugin-typescript": "^8.3.2",
31
- "rollup": "^2.74.1",
32
- "rollup-plugin-terser": "^7.0.2"
29
+ "@rollup/plugin-commonjs": "^22.0.2",
30
+ "@rollup/plugin-typescript": "^8.5.0",
31
+ "rollup": "^2.79.1",
32
+ "rollup-plugin-terser": "^7.0.2",
33
+ "tslib": "^2.4.0"
33
34
  }
34
35
  }