From fc89f798454bb79373799e03d2d2d463fac7a5ed Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Fri, 1 Mar 2024 01:06:26 +0100 Subject: [PATCH] build: add umd bundle --- lightweight/index.umd.js | 1 + rollup.config.js | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 lightweight/index.umd.js diff --git a/lightweight/index.umd.js b/lightweight/index.umd.js new file mode 100644 index 0000000..06231d5 --- /dev/null +++ b/lightweight/index.umd.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).mql={})}(this,(function(e){"use strict";function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function r(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var r=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};r.prototype=t.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var s=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,s.get?s:{enumerable:!0,get:function(){return e[t]}})})),r}var s={exports:{}};const o=/^https?:\/\//i;var n={};function i(e,t,r,s,o){var n,a=o?o+r:o;if(null==s)t&&(e[o]=s);else if("object"!=typeof s)e[o]=s;else if(Array.isArray(s))for(n=0;nnull!==e&&"object"==typeof e;var l=({VERSION:e,MicrolinkError:t,urlHttp:r,got:s,flatten:o})=>{const n=e=>{if(!u(e))return;const t=o(e);return Object.keys(t).reduce(((e,r)=>(e[`data.${r}`]=t[r].toString(),e)),{})},i=async(e,r={})=>{try{const t=await s(e,r);return"buffer"===r.responseType?{body:t.body,response:t}:{...t.body,response:t}}catch(r){const{response:s={}}=r,{statusCode:n,body:i,headers:a={},url:l=e}=s,c=null!=(o=i)&&null!=o.constructor&&"function"==typeof o.constructor.isBuffer&&o.constructor.isBuffer(o),h=u(i)&&!c?i:((e,t,r)=>{try{return JSON.parse(e)}catch(s){const o=e||t.message;return{status:"error",data:{url:o},more:"https://microlink.io/efatalclient",code:"EFATALCLIENT",message:o,url:r}}})(c?i.toString():i,r,l);throw new t({...h,message:h.message,url:l,statusCode:n,headers:a})}var o},l=(e,{data:t,apiKey:r,endpoint:s,retry:i,cache:u,...l}={},{responseType:c="json",headers:h,...p}={})=>{const f=!!r;return[`${s||a[f?"PRO":"FREE"]}?${new URLSearchParams({url:e,...n(t),...o(l)}).toString()}`,{...p,responseType:c,cache:u,retry:i,headers:f?{...h,"x-api-key":r}:{...h}}]},c=e=>async(s,o,n)=>{((e="")=>{if(!r(e)){const r=`The \`url\` as \`${e}\` is not valid. Ensure it has protocol (http or https) and hostname.`;throw new t({status:"fail",data:{url:r},more:"https://microlink.io/docs/api/api-parameters/url",code:"EINVALURLCLIENT",message:r,url:e})}})(s);const[a,u]=l(s,o,{...e,...n});return i(a,u)},h=c();return h.extend=c,h.MicrolinkError=t,h.getApiUrl=l,h.fetchFromApi=i,h.mapRules=n,h.version=e,h.stream=s.stream,h};class c extends Error{constructor(e,t,r){const s=`${e.status||0===e.status?e.status:""} ${e.statusText||""}`.trim();super(`Request failed with ${s?`status code ${s}`:"an unknown error"}`),Object.defineProperty(this,"response",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"request",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.name="HTTPError",this.response=e,this.request=t,this.options=r}}class h extends Error{constructor(e){super("Request timed out"),Object.defineProperty(this,"request",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.name="TimeoutError",this.request=e}}const p=e=>null!==e&&"object"==typeof e,f=(...e)=>{for(const t of e)if((!p(t)||Array.isArray(t))&&void 0!==t)throw new TypeError("The `options` argument must be an object");return y({},...e)},d=(e={},t={})=>{const r=new globalThis.Headers(e),s=t instanceof globalThis.Headers,o=new globalThis.Headers(t);for(const[e,t]of o.entries())s&&"undefined"===t||void 0===t?r.delete(e):r.set(e,t);return r},y=(...e)=>{let t={},r={};for(const s of e)if(Array.isArray(s))Array.isArray(t)||(t=[]),t=[...t,...s];else if(p(s)){for(let[e,r]of Object.entries(s))p(r)&&e in t&&(r=y(t[e],r)),t={...t,[e]:r};p(s.headers)&&(r=d(r,s.headers),t.headers=r)}return t},b=(()=>{let e=!1,t=!1;const r="function"==typeof globalThis.ReadableStream,s="function"==typeof globalThis.Request;return r&&s&&(t=new globalThis.Request("https://empty.invalid",{body:new globalThis.ReadableStream,method:"POST",get duplex(){return e=!0,"half"}}).headers.has("Content-Type")),e&&!t})(),m="function"==typeof globalThis.AbortController,_="function"==typeof globalThis.ReadableStream,g="function"==typeof globalThis.FormData,w=["get","post","put","patch","head","delete"],T={json:"application/json",text:"text/*",formData:"multipart/form-data",arrayBuffer:"*/*",blob:"*/*"},R=2147483647,v=Symbol("stop"),x={json:!0,parseJson:!0,searchParams:!0,prefixUrl:!0,retry:!0,timeout:!0,hooks:!0,throwHttpErrors:!0,onDownloadProgress:!0,fetch:!0},E={method:!0,headers:!0,body:!0,mode:!0,credentials:!0,cache:!0,redirect:!0,referrer:!0,referrerPolicy:!0,integrity:!0,keepalive:!0,signal:!0,window:!0,dispatcher:!0,duplex:!0},q=e=>w.includes(e)?e.toUpperCase():e,P=[413,429,503],j={limit:2,methods:["get","put","head","delete","options","trace"],statusCodes:[408,413,429,500,502,503,504],afterStatusCodes:P,maxRetryAfter:Number.POSITIVE_INFINITY,backoffLimit:Number.POSITIVE_INFINITY,delay:e=>.3*2**(e-1)*1e3},A=(e={})=>{if("number"==typeof e)return{...j,limit:e};if(e.methods&&!Array.isArray(e.methods))throw new Error("retry.methods must be an array");if(e.statusCodes&&!Array.isArray(e.statusCodes))throw new Error("retry.statusCodes must be an array");return{...j,...e,afterStatusCodes:P}};class C{static create(e,t){const r=new C(e,t),s=async()=>{if("number"==typeof r._options.timeout&&r._options.timeout>R)throw new RangeError("The `timeout` option cannot be greater than 2147483647");await Promise.resolve();let e=await r._fetch();for(const t of r._options.hooks.afterResponse){const s=await t(r.request,r._options,r._decorateResponse(e.clone()));s instanceof globalThis.Response&&(e=s)}if(r._decorateResponse(e),!e.ok&&r._options.throwHttpErrors){let t=new c(e,r.request,r._options);for(const e of r._options.hooks.beforeError)t=await e(t);throw t}if(r._options.onDownloadProgress){if("function"!=typeof r._options.onDownloadProgress)throw new TypeError("The `onDownloadProgress` option must be a function");if(!_)throw new Error("Streams are not supported in your environment. `ReadableStream` is missing.");return r._stream(e.clone(),r._options.onDownloadProgress)}return e},o=r._options.retry.methods.includes(r.request.method.toLowerCase())?r._retry(s):s();for(const[e,s]of Object.entries(T))o[e]=async()=>{r.request.headers.set("accept",r.request.headers.get("accept")||s);const n=(await o).clone();if("json"===e){if(204===n.status)return"";if(0===(await n.clone().arrayBuffer()).byteLength)return"";if(t.parseJson)return t.parseJson(await n.text())}return n[e]()};return o}constructor(e,t={}){Object.defineProperty(this,"request",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"abortController",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_retryCount",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_input",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_options",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._input=e;const r="credentials"in Request.prototype;if(this._options={credentials:r?this._input.credentials:void 0,...t,headers:d(this._input.headers,t.headers),hooks:y({beforeRequest:[],beforeRetry:[],beforeError:[],afterResponse:[]},t.hooks),method:q(t.method??this._input.method),prefixUrl:String(t.prefixUrl||""),retry:A(t.retry),throwHttpErrors:!1!==t.throwHttpErrors,timeout:t.timeout??1e4,fetch:t.fetch??globalThis.fetch.bind(globalThis)},"string"!=typeof this._input&&!(this._input instanceof URL||this._input instanceof globalThis.Request))throw new TypeError("`input` must be a string, URL, or Request");if(this._options.prefixUrl&&"string"==typeof this._input){if(this._input.startsWith("/"))throw new Error("`input` must not begin with a slash when using `prefixUrl`");this._options.prefixUrl.endsWith("/")||(this._options.prefixUrl+="/"),this._input=this._options.prefixUrl+this._input}if(m){if(this.abortController=new globalThis.AbortController,this._options.signal){const e=this._options.signal;this._options.signal.addEventListener("abort",(()=>{this.abortController.abort(e.reason)}))}this._options.signal=this.abortController.signal}if(b&&(this._options.duplex="half"),this.request=new globalThis.Request(this._input,this._options),this._options.searchParams){const e="?"+("string"==typeof this._options.searchParams?this._options.searchParams.replace(/^\?/,""):new URLSearchParams(this._options.searchParams).toString()),t=this.request.url.replace(/(?:\?.*?)?(?=#|$)/,e);!(g&&this._options.body instanceof globalThis.FormData||this._options.body instanceof URLSearchParams)||this._options.headers&&this._options.headers["content-type"]||this.request.headers.delete("content-type"),this.request=new globalThis.Request(new globalThis.Request(t,{...this.request}),this._options)}void 0!==this._options.json&&(this._options.body=JSON.stringify(this._options.json),this.request.headers.set("content-type",this._options.headers.get("content-type")??"application/json"),this.request=new globalThis.Request(this.request,{body:this._options.body}))}_calculateRetryDelay(e){if(this._retryCount++,this._retryCount<=this._options.retry.limit&&!(e instanceof h)){if(e instanceof c){if(!this._options.retry.statusCodes.includes(e.response.status))return 0;const t=e.response.headers.get("Retry-After");if(t&&this._options.retry.afterStatusCodes.includes(e.response.status)){let e=Number(t);return Number.isNaN(e)?e=Date.parse(t)-Date.now():e*=1e3,void 0!==this._options.retry.maxRetryAfter&&e>this._options.retry.maxRetryAfter?0:e}if(413===e.response.status)return 0}const t=this._options.retry.delay(this._retryCount);return Math.min(this._options.retry.backoffLimit,t)}return 0}_decorateResponse(e){return this._options.parseJson&&(e.json=async()=>this._options.parseJson(await e.text())),e}async _retry(e){try{return await e()}catch(t){const r=Math.min(this._calculateRetryDelay(t),R);if(0!==r&&this._retryCount>0){await async function(e,{signal:t}){return new Promise(((r,s)=>{function o(){clearTimeout(n),s(t.reason)}t&&(t.throwIfAborted(),t.addEventListener("abort",o,{once:!0}));const n=setTimeout((()=>{t?.removeEventListener("abort",o),r()}),e)}))}(r,{signal:this._options.signal});for(const e of this._options.hooks.beforeRetry){if(await e({request:this.request,options:this._options,error:t,retryCount:this._retryCount})===v)return}return this._retry(e)}throw t}}async _fetch(){for(const e of this._options.hooks.beforeRequest){const t=await e(this.request,this._options);if(t instanceof Request){this.request=t;break}if(t instanceof Response)return t}const e=((e,t)=>{const r={};for(const s in t)s in E||s in x||s in e||(r[s]=t[s]);return r})(this.request,this._options);return!1===this._options.timeout?this._options.fetch(this.request.clone(),e):async function(e,t,r,s){return new Promise(((o,n)=>{const i=setTimeout((()=>{r&&r.abort(),n(new h(e))}),s.timeout);s.fetch(e,t).then(o).catch(n).then((()=>{clearTimeout(i)}))}))}(this.request.clone(),e,this.abortController,this._options)}_stream(e,t){const r=Number(e.headers.get("content-length"))||0;let s=0;return 204===e.status?(t&&t({percent:1,totalBytes:r,transferredBytes:s},new Uint8Array),new globalThis.Response(null,{status:e.status,statusText:e.statusText,headers:e.headers})):new globalThis.Response(new globalThis.ReadableStream({async start(o){const n=e.body.getReader();t&&t({percent:0,transferredBytes:0,totalBytes:r},new Uint8Array),await async function e(){const{done:i,value:a}=await n.read();if(i)o.close();else{if(t){s+=a.byteLength;t({percent:0===r?0:s/r,transferredBytes:s,totalBytes:r},a)}o.enqueue(a),await e()}}()}}),{status:e.status,statusText:e.statusText,headers:e.headers})}}const O=e=>{const t=(t,r)=>C.create(t,f(e,r));for(const r of w)t[r]=(t,s)=>C.create(t,f(e,s,{method:r}));return t.create=e=>O(f(e)),t.extend=t=>O(f(e,t)),t.stop=v,t},k=O();var S=r(Object.freeze({__proto__:null,HTTPError:c,TimeoutError:h,default:k}));const U=e=>{try{const{href:t}=new URL(e);return o.test(t)&&t}catch(e){return!1}},{flattie:L}=n,N=l,{default:I}=S;class B extends Error{constructor(e){super(),this.name="MicrolinkError",Object.assign(this,e),this.description=this.message,this.message=this.code?`${this.code}, ${this.description}`:this.description}}const D=async(e,{responseType:t,...r})=>{try{void 0===r.timeout&&(r.timeout=!1);const s=await I(e,r),o=await s[t](),{headers:n,status:i}=s;return{url:s.url,body:o,headers:n,statusCode:i}}catch(e){if(e.response){const{response:t}=e;e.response={...t,headers:Array.from(t.headers.entries()).reduce(((e,[t,r])=>(e[t]=r,e)),{}),statusCode:t.status,body:await t.text()}}throw e}};D.stream=(...e)=>I(...e).then((e=>e.body));const M=N({MicrolinkError:B,urlHttp:U,got:D,flatten:L,VERSION:"0.12.2"});s.exports=M;var F=s.exports.arrayBuffer=M.extend({responseType:"arrayBuffer"}),H=s.exports.extend=M.extend,$=s.exports.fetchFromApi=M.fetchFromApi,J=s.exports.getApiUrl=M.getApiUrl,V=s.exports.mapRules=M.mapRules,W=s.exports.MicrolinkError=M.MicrolinkError,Y=s.exports.version=M.version,z=t(s.exports);e.MicrolinkError=W,e.arrayBuffer=F,e.default=z,e.extend=H,e.fetchFromApi=$,e.getApiUrl=J,e.mapRules=V,e.version=Y,Object.defineProperty(e,"__esModule",{value:!0})})); diff --git a/rollup.config.js b/rollup.config.js index f0f738b..45719fc 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -53,6 +53,17 @@ const builds = [ }), commonjs() ] + }), + build({ + compress: true, + input: 'src/lightweight.js', + output: { name: 'mql', file: 'lightweight/index.umd.js', format: 'umd' }, + plugins: [ + nodeResolve({ + mainFields: ['browser', 'module', 'main'] + }), + commonjs() + ] }) ]