UNPKG

locize-lastused

Version:

Using API lastUsed using in node.js, in the browser and for deno.

1 lines 4.78 kB
var locizeLastUsed=(function(){let e=[],t=e.forEach,n=e.slice;function r(e){return t.call(n.call(arguments,1),t=>{if(t)for(let n in t)e[n]===void 0&&(e[n]=t[n])}),e}function i(e,t,n){let r;return function(){let i=this,a=arguments,o=function(){r=null,n||e.apply(i,a)},s=n&&!r;clearTimeout(r),r=setTimeout(o,t),s&&e.apply(i,a)}}function a(e,t){return t.reduce((t,n)=>{if(t)return t;if(!e||!e[n]||typeof e[n]!=`string`||!e[n].toLowerCase()===n.toLowerCase()){let t=`i18next-lastused :: got "${e[n]}" in options for ${n} which is invalid.`;return console.warn(t),t}return!1},!1)}function o(e,t,n){let r=e;return t.forEach(e=>{let t=RegExp(`{{${e}}}`,`g`);r=r.replace(t,n[e])}),r}let s=typeof globalThis<`u`?globalThis:typeof global<`u`?global:typeof window<`u`?window:void 0,c;typeof fetch==`function`?c=fetch:s&&typeof s.fetch==`function`&&(c=s.fetch);let l=(typeof XMLHttpRequest==`function`||typeof XMLHttpRequest==`object`)&&s?s.XMLHttpRequest:void 0,u=typeof ActiveXObject==`function`&&s?s.ActiveXObject:void 0,d=(e,t,n,r)=>{let i=e=>{let t=e.headers&&e.headers.get(`x-cache`)===`Error from cloudfront`;if(!e.ok)return r(e.statusText||`Error`,{status:e.status,resourceNotExisting:t});e.text().then(n=>{r(null,{status:e.status,data:n,resourceNotExisting:t})}).catch(r)},a={Authorization:e.authorize&&e.apiKey?e.apiKey:void 0,"Content-Type":`application/json`};typeof window>`u`&&typeof global<`u`&&global.process!==void 0&&global.process.versions&&global.process.versions.node&&(a[`User-Agent`]=`locize-lastused (node/${global.process.version}; ${global.process.platform} ${global.process.arch})`),typeof fetch==`function`?fetch(t,{method:n?`POST`:`GET`,body:n?JSON.stringify(n):void 0,headers:a}).then(i).catch(r):c(t,{method:n?`POST`:`GET`,body:n?JSON.stringify(n):void 0,headers:a}).then(i).catch(r)},f=(e,t,n,r)=>{try{let i=l?new l:new u(`MSXML2.XMLHTTP.3.0`);i.open(n?`POST`:`GET`,t,1),e.crossDomain||i.setRequestHeader(`X-Requested-With`,`XMLHttpRequest`),e.authorize&&e.apiKey&&i.setRequestHeader(`Authorization`,e.apiKey),(n||e.setContentTypeJSON)&&i.setRequestHeader(`Content-Type`,`application/json`),i.onreadystatechange=()=>{let e=i.getResponseHeader(`x-cache`)===`Error from cloudfront`;i.readyState>3&&r(i.status>=400?i.statusText:null,{status:i.status,data:i.responseText,resourceNotExisting:e})},i.send(JSON.stringify(n))}catch(e){console&&console.log(e)}},p=(e,t,n,r)=>{if(typeof n==`function`&&(r=n,n=void 0),r=r||(()=>{}),c)return d(e,t,n,r);if(l||u)return f(e,t,n,r);r(Error(`No fetch and no xhr implementation found!`))},m=()=>({lastUsedPath:`https://api.locize.app/used/{{projectId}}/{{version}}/{{lng}}/{{ns}}`,referenceLng:`en`,crossDomain:!0,setContentTypeJSON:!1,version:`latest`,debounceSubmit:9e4,allowedHosts:[`localhost`]}),h={init(e){let t=e.t&&typeof e.t==`function`;t&&!e.options.locizeLastUsed.projectId&&e.options.backend.projectId&&(e.options.locizeLastUsed.projectId=e.options.backend.projectId),t&&!e.options.locizeLastUsed.version&&e.options.backend.version&&(e.options.locizeLastUsed.version=e.options.backend.version),t&&!e.options.locizeLastUsed.apiKey&&e.options.backend.apiKey&&(e.options.locizeLastUsed.apiKey=e.options.backend.apiKey),t&&!e.options.locizeLastUsed.referenceLng&&e.options.backend.referenceLng&&(e.options.locizeLastUsed.referenceLng=e.options.backend.referenceLng),t&&!e.options.locizeLastUsed.referenceLng&&e.options.fallbackLng&&Array.isArray(e.options.fallbackLng)&&e.options.fallbackLng[0]!==`dev`&&(e.options.locizeLastUsed.referenceLng=e.options.fallbackLng[0]),this.options=r(t?e.options.locizeLastUsed:e,this.options||{},m());let n=typeof window<`u`&&window.location&&window.location.hostname;n?this.isAllowed=this.options.allowedHosts.indexOf(n)>-1:this.isAllowed=!0,this.submitting=null,this.pending={},this.done={},this.submit=i(this.submit,this.options.debounceSubmit),t&&this.interceptI18next(e)},interceptI18next(e){let t=e.services.resourceStore.getResource;e.services.resourceStore.getResource=(n,r,i,a)=>(i&&this.used(r,i),t.call(e.services.resourceStore,n,r,i,a))},used(e,t,n){[`pending`,`done`].forEach(n=>{this.done[e]&&this.done[e][t]||(this[n][e]||(this[n][e]={}),this[n][e][t]=!0)}),this.submit(n)},submit(e){if(!this.isAllowed)return e&&e(Error(`not allowed`));if(this.submitting)return this.submit(e);let t=a(this.options,[`projectId`,`version`,`apiKey`,`referenceLng`]);if(t)return e&&e(Error(t));this.submitting=this.pending,this.pending={};let n=Object.keys(this.submitting),i=n.length,s=t=>{i--,i||(this.submitting=null,e&&e(t))};n.forEach(e=>{let t=Object.keys(this.submitting[e]),n=o(this.options.lastUsedPath,[`projectId`,`version`,`lng`,`ns`],r({lng:this.options.referenceLng,ns:e},this.options));t.length?p(r({authorize:!0},this.options),n,t,s):s()})}};return h.type=`3rdParty`,h})();