@andrewcaires/fetch 1.0.2 → 1.0.4

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.4
3
+ * Plugin for fetch api
4
+ * (c) 2023 Andrew Caires
5
+ * @license: MIT
6
+ */
7
+ "use strict";var t=require("@andrewcaires/utils.js");function e(t,e,o,r){return new(o||(o=Promise))((function(n,i){function s(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(s,a)}u((r=r.apply(t,e||[])).next())}))}const o={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class r extends t.EventEmitter{constructor(t={}){super(),this._options=Object.assign({headers:{},timeout:5e3},t)}delete(t,o){return e(this,void 0,void 0,(function*(){return this.fetch({method:"delete",path:t,query:o})}))}get(t,o){return e(this,void 0,void 0,(function*(){return this.fetch({method:"get",path:t,query:o})}))}head(t,o){return e(this,void 0,void 0,(function*(){return this.fetch({method:"head",path:t,query:o})}))}options(t,o){return e(this,void 0,void 0,(function*(){return this.fetch({method:"options",path:t,query:o})}))}patch(t,o,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"patch",path:t,query:r,body:o})}))}post(t,o,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"post",path:t,query:r,body:o})}))}put(t,o,r){return e(this,void 0,void 0,(function*(){return this.fetch({method:"put",path:t,query:r,body:o})}))}request(t,o){return e(this,void 0,void 0,(function*(){return this.fetch({method:"request",path:t,query:o})}))}fetch(t){return e(this,void 0,void 0,(function*(){const e=this._request(t),o={};try{this.emit("before",e),o.raw=yield fetch(e.url,e),o.data=yield this._data(o.raw),o.raw.ok||(o.error=this._status(o.raw)),this.emit("complete",o)}catch(t){this.emit("error",t),o.error=t.message}return o}))}_body(e){const o=e instanceof Blob,r=e instanceof FormData,n=!(o||r||t.isString(e));return{json:n,parse:n?e?JSON.stringify(e):null:e}}_data(t){var e;const o=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",r=o.indexOf("text/html")>=0,n=o.indexOf("application/json")>=0;return!o||r?t.text():n?t.json():t.blob()}_headers(t){return Object.assign(Object.assign({},this._options.headers),t)}_request({url:t,path:e,query:o,method:r,body:n,headers:i,timeout:s}){t=this._url(t,e,o),r=r.toUpperCase(),i=this._headers(i);const{json:a,parse:u}=this._body(n);a&&(i["Content-Type"]="application/json");return{url:t,method:r,body:u,headers:i,signal:this._signal(s)}}_signal(e){const o=new AbortController;return t.isUndefined(e)&&(e=this._options.timeout),e&&setTimeout((()=>o.abort()),e),o.signal}_status(t){return t.statusText||o[t.status]||"ERROR"}_url(e,o,r){const n=new URL(o||"",e||this._options.url);return r&&t.forEachKey(r,((t,e)=>n.searchParams.set(e,t))),n.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.4
3
+ * Plugin for fetch api
4
+ * (c) 2023 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>;
@@ -52,6 +59,8 @@ export declare class Fetch extends EventEmitter {
52
59
  private _headers;
53
60
  private _request;
54
61
  private _signal;
62
+ private _status;
55
63
  private _url;
56
64
  }
57
- export default Fetch;
65
+
66
+ 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
3
- * (c) 2022 Andrew Caires
2
+ * @andrewcaires/fetch v1.0.4
3
+ * Plugin for fetch api
4
+ * (c) 2023 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 o,forEachKey as r}from"@andrewcaires/utils.js";function n(t,e,o,r){return new(o||(o=Promise))((function(n,i){function s(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(s,a)}u((r=r.apply(t,e||[])).next())}))}const i={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class s 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,o){return n(this,void 0,void 0,(function*(){return this.fetch({method:"patch",path:t,query:o,body:e})}))}post(t,e,o){return n(this,void 0,void 0,(function*(){return this.fetch({method:"post",path:t,query:o,body:e})}))}put(t,e,o){return n(this,void 0,void 0,(function*(){return this.fetch({method:"put",path:t,query:o,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),o={};try{this.emit("before",e),o.raw=yield fetch(e.url,e),o.data=yield this._data(o.raw),o.raw.ok||(o.error=this._status(o.raw)),this.emit("complete",o)}catch(t){this.emit("error",t),o.error=t.message}return o}))}_body(t){const o=t instanceof Blob,r=t instanceof FormData,n=!(o||r||e(t));return{json:n,parse:n?t?JSON.stringify(t):null:t}}_data(t){var e;const o=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",r=o.indexOf("text/html")>=0,n=o.indexOf("application/json")>=0;return!o||r?t.text():n?t.json():t.blob()}_headers(t){return Object.assign(Object.assign({},this._options.headers),t)}_request({url:t,path:e,query:o,method:r,body:n,headers:i,timeout:s}){t=this._url(t,e,o),r=r.toUpperCase(),i=this._headers(i);const{json:a,parse:u}=this._body(n);a&&(i["Content-Type"]="application/json");return{url:t,method:r,body:u,headers:i,signal:this._signal(s)}}_signal(t){const e=new AbortController;return o(t)&&(t=this._options.timeout),t&&setTimeout((()=>e.abort()),t),e.signal}_status(t){return t.statusText||i[t.status]||"ERROR"}_url(t,e,o){const n=new URL(e||"",t||this._options.url);return o&&r(o,((t,e)=>n.searchParams.set(e,t))),n.toString()}}export{s as Fetch};
package/dist/index.min.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/fetch v1.0.2
3
- * (c) 2022 Andrew Caires
2
+ * @andrewcaires/fetch v1.0.4
3
+ * Plugin for fetch api
4
+ * (c) 2023 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 o(t,e,o,n){return new(o||(o=Promise))((function(r,i){function s(t){try{u(n.next(t))}catch(t){i(t)}}function a(t){try{u(n.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?r(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(s,a)}u((n=n.apply(t,e||[])).next())}))}const n={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",511:"Network Authentication Required"};class r extends e.EventEmitter{constructor(t={}){super(),this._options=Object.assign({headers:{},timeout:5e3},t)}delete(t,e){return o(this,void 0,void 0,(function*(){return this.fetch({method:"delete",path:t,query:e})}))}get(t,e){return o(this,void 0,void 0,(function*(){return this.fetch({method:"get",path:t,query:e})}))}head(t,e){return o(this,void 0,void 0,(function*(){return this.fetch({method:"head",path:t,query:e})}))}options(t,e){return o(this,void 0,void 0,(function*(){return this.fetch({method:"options",path:t,query:e})}))}patch(t,e,n){return o(this,void 0,void 0,(function*(){return this.fetch({method:"patch",path:t,query:n,body:e})}))}post(t,e,n){return o(this,void 0,void 0,(function*(){return this.fetch({method:"post",path:t,query:n,body:e})}))}put(t,e,n){return o(this,void 0,void 0,(function*(){return this.fetch({method:"put",path:t,query:n,body:e})}))}request(t,e){return o(this,void 0,void 0,(function*(){return this.fetch({method:"request",path:t,query:e})}))}fetch(t){return o(this,void 0,void 0,(function*(){const e=this._request(t),o={};try{this.emit("before",e),o.raw=yield fetch(e.url,e),o.data=yield this._data(o.raw),o.raw.ok||(o.error=this._status(o.raw)),this.emit("complete",o)}catch(t){this.emit("error",t),o.error=t.message}return o}))}_body(t){const o=t instanceof Blob,n=t instanceof FormData,r=!(o||n||e.isString(t));return{json:r,parse:r?t?JSON.stringify(t):null:t}}_data(t){var e;const o=(null===(e=t.headers.get("Content-Type"))||void 0===e?void 0:e.toLowerCase())||"",n=o.indexOf("text/html")>=0,r=o.indexOf("application/json")>=0;return!o||n?t.text():r?t.json():t.blob()}_headers(t){return Object.assign(Object.assign({},this._options.headers),t)}_request({url:t,path:e,query:o,method:n,body:r,headers:i,timeout:s}){t=this._url(t,e,o),n=n.toUpperCase(),i=this._headers(i);const{json:a,parse:u}=this._body(r);a&&(i["Content-Type"]="application/json");return{url:t,method:n,body:u,headers:i,signal:this._signal(s)}}_signal(t){const o=new AbortController;return e.isUndefined(t)&&(t=this._options.timeout),t&&setTimeout((()=>o.abort()),t),o.signal}_status(t){return t.statusText||n[t.status]||"ERROR"}_url(t,o,n){const r=new URL(o||"",t||this._options.url);return n&&e.forEachKey(n,((t,e)=>r.searchParams.set(e,t))),r.toString()}}return t.Fetch=r,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.4",
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.9"
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": "^24.0.1",
43
+ "@rollup/plugin-terser": "^0.4.0",
44
+ "@rollup/plugin-typescript": "^11.0.0",
45
+ "rollup": "^3.18.0",
46
+ "rollup-plugin-dts": "^5.2.0",
47
+ "tslib": "^2.5.0"
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;