@andrewcaires/fetch 1.0.2 → 1.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  [![npm](https://img.shields.io/npm/v/@andrewcaires/fetch?color=blue&logo=npm)](https://www.npmjs.com/package/@andrewcaires/fetch)
2
2
  [![downloads](https://img.shields.io/npm/dt/@andrewcaires/fetch?color=blue)](https://www.npmjs.com/package/@andrewcaires/fetch)
3
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)
4
+ [![license](https://img.shields.io/npm/l/@andrewcaires/fetch?color=blue)](https://www.npmjs.com/package/@andrewcaires/fetch)
5
5
 
6
6
  # fetch
7
7
 
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * @andrewcaires/fetch v1.0.3
3
+ * Plugin for fetch api
4
+ * (c) 2022 Andrew Caires
5
+ * @license: MIT
6
+ */
7
+ "use strict";var t=require("@andrewcaires/utils.js");function e(t,e,r,n){return new(r||(r=Promise))((function(o,i){function s(t){try{u(n.next(t))}catch(t){i(t)}}function h(t){try{u(n.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,h)}u((n=n.apply(t,e||[])).next())}))}class r extends t.EventEmitter{constructor(t={}){super(),this._options=Object.assign({headers:{},timeout:5e3},t)}delete(t,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"delete",path:t,query:r})}))}get(t,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"get",path:t,query:r})}))}head(t,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"head",path:t,query:r})}))}options(t,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"options",path:t,query:r})}))}patch(t,r,n){return e(this,void 0,void 0,(function*(){return this.fetch({method:"patch",path:t,query:n,body:r})}))}post(t,r,n){return e(this,void 0,void 0,(function*(){return this.fetch({method:"post",path:t,query:n,body:r})}))}put(t,r,n){return e(this,void 0,void 0,(function*(){return this.fetch({method:"put",path:t,query:n,body:r})}))}request(t,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"request",path:t,query:r})}))}fetch(t){return e(this,void 0,void 0,(function*(){const e=this._request(t),r={};try{this.emit("before",e),r.raw=yield fetch(e.url,e),r.data=yield this._data(r.raw),r.raw.ok||(r.error=r.raw.statusText),this.emit("complete",r)}catch(t){this.emit("error",t),r.error=t.message}return r}))}_body(e){const 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}}_data(t){var e;const 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()}_headers(t){return Object.assign(Object.assign({},this._options.headers),t)}_request({url:t,path:e,query:r,method:n,body:o,headers:i,timeout:s}){t=this._url(t,e,r),n=n.toUpperCase(),i=this._headers(i);const{json:h,parse:u}=this._body(o);h&&(i["Content-Type"]="application/json");return{url:t,method:n,body:u,headers:i,signal:this._signal(s)}}_signal(e){const r=new AbortController;return t.isUndefined(e)&&(e=this._options.timeout),e&&setTimeout((()=>r.abort()),e),r.signal}_url(e,r,n){const o=new URL(r||"",e||this._options.url);return n&&t.forEachKey(n,((t,e)=>o.searchParams.set(e,t))),o.toString()}}exports.Fetch=r;
package/dist/index.d.ts CHANGED
@@ -1,17 +1,24 @@
1
- import { EventEmitter } from "@andrewcaires/utils.js";
2
- export interface FetchOptions {
1
+ /*!
2
+ * @andrewcaires/fetch v1.0.3
3
+ * Plugin for fetch api
4
+ * (c) 2022 Andrew Caires
5
+ * @license: MIT
6
+ */
7
+ import { EventEmitter } from '@andrewcaires/utils.js';
8
+
9
+ interface FetchOptions {
3
10
  url?: string;
4
11
  headers?: FetchHeaders;
5
12
  timeout?: number;
6
13
  }
7
- export interface FetchBody {
14
+ interface FetchBody {
8
15
  json: boolean;
9
16
  parse: any;
10
17
  }
11
- export declare type FetchHeaders = {
18
+ type FetchHeaders = {
12
19
  [key: string]: string;
13
20
  };
14
- export interface FetchInit {
21
+ interface FetchInit {
15
22
  url?: string;
16
23
  path: string;
17
24
  query?: FetchQuery;
@@ -20,22 +27,22 @@ export interface FetchInit {
20
27
  headers?: FetchHeaders;
21
28
  timeout?: number;
22
29
  }
23
- export declare type FetchQuery = {
30
+ type FetchQuery = {
24
31
  [key: string]: string;
25
32
  };
26
- export interface FetchRequest {
33
+ interface FetchRequest {
27
34
  url: string;
28
35
  method: string;
29
36
  body: any;
30
37
  signal: AbortSignal;
31
38
  headers: FetchHeaders;
32
39
  }
33
- export interface FetchResponse {
40
+ interface FetchResponse {
34
41
  data?: any;
35
42
  error?: string;
36
43
  raw?: Response;
37
44
  }
38
- export declare class Fetch extends EventEmitter {
45
+ declare class Fetch extends EventEmitter {
39
46
  private _options;
40
47
  constructor(options?: FetchOptions);
41
48
  delete(path: string, query?: FetchQuery): Promise<FetchResponse>;
@@ -54,4 +61,5 @@ export declare class Fetch extends EventEmitter {
54
61
  private _signal;
55
62
  private _url;
56
63
  }
57
- export default Fetch;
64
+
65
+ export { Fetch, FetchBody, FetchHeaders, FetchInit, FetchOptions, FetchQuery, FetchRequest, FetchResponse };
package/dist/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.0.2
2
+ * @andrewcaires/fetch v1.0.3
3
+ * Plugin for fetch api
3
4
  * (c) 2022 Andrew Caires
4
5
  * @license: MIT
5
6
  */
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};
7
+ import{EventEmitter as t,isString as e,isUndefined as r,forEachKey as o}from"@andrewcaires/utils.js";function n(t,e,r,o){return new(r||(r=Promise))((function(n,i){function s(t){try{u(o.next(t))}catch(t){i(t)}}function h(t){try{u(o.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,h)}u((o=o.apply(t,e||[])).next())}))}class i extends t{constructor(t={}){super(),this._options=Object.assign({headers:{},timeout:5e3},t)}delete(t,e){return n(this,void 0,void 0,(function*(){return this.fetch({method:"delete",path:t,query:e})}))}get(t,e){return n(this,void 0,void 0,(function*(){return this.fetch({method:"get",path:t,query:e})}))}head(t,e){return n(this,void 0,void 0,(function*(){return this.fetch({method:"head",path:t,query:e})}))}options(t,e){return n(this,void 0,void 0,(function*(){return this.fetch({method:"options",path:t,query:e})}))}patch(t,e,r){return n(this,void 0,void 0,(function*(){return this.fetch({method:"patch",path:t,query:r,body:e})}))}post(t,e,r){return n(this,void 0,void 0,(function*(){return this.fetch({method:"post",path:t,query:r,body:e})}))}put(t,e,r){return n(this,void 0,void 0,(function*(){return this.fetch({method:"put",path:t,query:r,body:e})}))}request(t,e){return n(this,void 0,void 0,(function*(){return this.fetch({method:"request",path:t,query:e})}))}fetch(t){return n(this,void 0,void 0,(function*(){const e=this._request(t),r={};try{this.emit("before",e),r.raw=yield fetch(e.url,e),r.data=yield this._data(r.raw),r.raw.ok||(r.error=r.raw.statusText),this.emit("complete",r)}catch(t){this.emit("error",t),r.error=t.message}return r}))}_body(t){const r=t instanceof Blob,o=t instanceof FormData,n=!(r||o||e(t));return{json:n,parse:n?t?JSON.stringify(t):null:t}}_data(t){var e;const r=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",o=r.indexOf("text/html")>=0,n=r.indexOf("application/json")>=0;return!r||o?t.text():n?t.json():t.blob()}_headers(t){return Object.assign(Object.assign({},this._options.headers),t)}_request({url:t,path:e,query:r,method:o,body:n,headers:i,timeout:s}){t=this._url(t,e,r),o=o.toUpperCase(),i=this._headers(i);const{json:h,parse:u}=this._body(n);h&&(i["Content-Type"]="application/json");return{url:t,method:o,body:u,headers:i,signal:this._signal(s)}}_signal(t){const e=new AbortController;return r(t)&&(t=this._options.timeout),t&&setTimeout((()=>e.abort()),t),e.signal}_url(t,e,r){const n=new URL(e||"",t||this._options.url);return r&&o(r,((t,e)=>n.searchParams.set(e,t))),n.toString()}}export{i as Fetch};
package/dist/index.min.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.0.2
2
+ * @andrewcaires/fetch v1.0.3
3
+ * Plugin for fetch api
3
4
  * (c) 2022 Andrew Caires
4
5
  * @license: MIT
5
6
  */
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);
7
+ var Fetch=function(t,e){"use strict";function r(t,e,r,n){return new(r||(r=Promise))((function(o,i){function s(t){try{u(n.next(t))}catch(t){i(t)}}function h(t){try{u(n.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,h)}u((n=n.apply(t,e||[])).next())}))}class n extends e.EventEmitter{constructor(t={}){super(),this._options=Object.assign({headers:{},timeout:5e3},t)}delete(t,e){return r(this,void 0,void 0,(function*(){return this.fetch({method:"delete",path:t,query:e})}))}get(t,e){return r(this,void 0,void 0,(function*(){return this.fetch({method:"get",path:t,query:e})}))}head(t,e){return r(this,void 0,void 0,(function*(){return this.fetch({method:"head",path:t,query:e})}))}options(t,e){return r(this,void 0,void 0,(function*(){return this.fetch({method:"options",path:t,query:e})}))}patch(t,e,n){return r(this,void 0,void 0,(function*(){return this.fetch({method:"patch",path:t,query:n,body:e})}))}post(t,e,n){return r(this,void 0,void 0,(function*(){return this.fetch({method:"post",path:t,query:n,body:e})}))}put(t,e,n){return r(this,void 0,void 0,(function*(){return this.fetch({method:"put",path:t,query:n,body:e})}))}request(t,e){return r(this,void 0,void 0,(function*(){return this.fetch({method:"request",path:t,query:e})}))}fetch(t){return r(this,void 0,void 0,(function*(){const e=this._request(t),r={};try{this.emit("before",e),r.raw=yield fetch(e.url,e),r.data=yield this._data(r.raw),r.raw.ok||(r.error=r.raw.statusText),this.emit("complete",r)}catch(t){this.emit("error",t),r.error=t.message}return r}))}_body(t){const r=t instanceof Blob,n=t instanceof FormData,o=!(r||n||e.isString(t));return{json:o,parse:o?t?JSON.stringify(t):null:t}}_data(t){var e;const 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()}_headers(t){return Object.assign(Object.assign({},this._options.headers),t)}_request({url:t,path:e,query:r,method:n,body:o,headers:i,timeout:s}){t=this._url(t,e,r),n=n.toUpperCase(),i=this._headers(i);const{json:h,parse:u}=this._body(o);h&&(i["Content-Type"]="application/json");return{url:t,method:n,body:u,headers:i,signal:this._signal(s)}}_signal(t){const r=new AbortController;return e.isUndefined(t)&&(t=this._options.timeout),t&&setTimeout((()=>r.abort()),t),r.signal}_url(t,r,n){const o=new URL(r||"",t||this._options.url);return n&&e.forEachKey(n,((t,e)=>o.searchParams.set(e,t))),o.toString()}}return t.Fetch=n,t}({},UtilsJS);
package/package.json CHANGED
@@ -1,13 +1,26 @@
1
1
  {
2
2
  "name": "@andrewcaires/fetch",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Plugin for fetch api",
5
- "main": "dist/index.umd.js",
6
- "module": "dist/index.esm.js",
7
- "unpkg": "dist/index.min.js",
8
- "types": "dist/index.d.ts",
5
+ "main": "./dist/index.cjs.js",
6
+ "unpkg": "./dist/index.min.js",
7
+ "types": "./dist/index.d.ts",
8
+ "module": "./dist/index.esm.js",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.esm.js",
13
+ "browser": "./dist/index.min.js",
14
+ "require": "./dist/index.cjs.js",
15
+ "default": "./dist/index.cjs.js"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
9
22
  "scripts": {
10
- "build": "rollup -c build/rollup.config.js"
23
+ "build": "rollup --bundleConfigAsCjs -c rollup.config.js"
11
24
  },
12
25
  "repository": {
13
26
  "type": "git",
@@ -23,13 +36,14 @@
23
36
  },
24
37
  "homepage": "https://github.com/andrewcaires/npm#readme",
25
38
  "dependencies": {
26
- "@andrewcaires/utils.js": "^0.1.7"
39
+ "@andrewcaires/utils.js": "^0.2.7"
27
40
  },
28
41
  "devDependencies": {
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"
42
+ "@rollup/plugin-commonjs": "^23.0.4",
43
+ "@rollup/plugin-terser": "^0.2.0",
44
+ "@rollup/plugin-typescript": "^10.0.1",
45
+ "rollup": "^3.7.3",
46
+ "rollup-plugin-dts": "^5.0.0",
47
+ "tslib": "^2.4.1"
34
48
  }
35
49
  }
package/dist/index.umd.js DELETED
@@ -1,6 +0,0 @@
1
- /*!
2
- * @andrewcaires/fetch v1.0.2
3
- * (c) 2022 Andrew Caires
4
- * @license: MIT
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=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;