UNPKG

i18next-http-backend

Version:

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.

1 lines 6.78 kB
var i18nextHttpBackend=(function(){let e=[];e.forEach,e.slice;let t=[`__proto__`,`constructor`,`prototype`];function n(e){return!(typeof e!=`string`||e.length===0||e.length>128||t.indexOf(e)>-1||e.indexOf(`..`)>-1||e.indexOf(`\\`)>-1||/[?#%\s@]/.test(e)||/[\x00-\x1F\x7F]/.test(e))}function r(e){return!(!n(e)||e.indexOf(`/`)>-1)}function i(e){return n(e)}let a={lng:r,ns:i};function o(e){return typeof e==`string`?e.replace(/[\r\n\x00-\x1F\x7F]/g,` `):e}function s(e){if(typeof e!=`string`||e.length===0)return e;try{let t=new URL(e);return t.username||t.password?(t.username=``,t.password=``,t.toString()):e}catch{return e.replace(/(\/\/)[^/@\s]+@/g,`$1`)}}function c(){return typeof XMLHttpRequest==`function`||typeof XMLHttpRequest==`object`}function l(e){return!!e&&typeof e.then==`function`}function u(e){return l(e)?e:Promise.resolve(e)}let d=/\{\{(.+?)\}\}/g;function f(e,n){let i=!1,o=e.replace(d,(e,o)=>{let s=o.trim();if(t.indexOf(s)>-1)return e;let c=n[s];if(c==null)return e;let l=a[s]||r,u=String(c).split(`+`);for(let t of u)if(!l(t))return i=!0,e;return u.join(`+`)});return i?null:o}let p=typeof globalThis<`u`?globalThis:typeof global<`u`?global:typeof window<`u`?window:void 0,m;typeof fetch==`function`?m=fetch:p&&typeof p.fetch==`function`&&(m=p.fetch);let h=c()&&p?p.XMLHttpRequest:void 0,g=typeof ActiveXObject==`function`&&p?p.ActiveXObject:void 0,_=[`__proto__`,`constructor`,`prototype`],v=(e,t)=>{if(t&&typeof t==`object`){let n=``;for(let e of Object.keys(t))_.indexOf(e)>-1||(n+=`&`+encodeURIComponent(e)+`=`+encodeURIComponent(t[e]));if(!n)return e;e=e+(e.indexOf(`?`)===-1?`?`:`&`)+n.slice(1)}return e},y=(e,t,n,r)=>{let i=e=>{if(!e.ok)return n(e.statusText||`Error`,{status:e.status});e.text().then(t=>{n(null,{status:e.status,data:t})}).catch(n)};if(r){let a=r(e,t);if(a instanceof Promise){a.then(i).catch(n);return}}typeof fetch==`function`?fetch(e,t).then(i).catch(n):m(e,t).then(i).catch(n)},b=(e,t,n,r)=>{e.queryStringParams&&(t=v(t,e.queryStringParams));let i={...typeof e.customHeaders==`function`?e.customHeaders():e.customHeaders};typeof window>`u`&&typeof global<`u`&&global.process!==void 0&&global.process.versions&&global.process.versions.node&&(i[`User-Agent`]=`i18next-http-backend (node/${global.process.version}; ${global.process.platform} ${global.process.arch})`),n&&(i[`Content-Type`]=`application/json`);let a=typeof e.requestOptions==`function`?e.requestOptions(n):e.requestOptions,o={method:n?`POST`:`GET`,body:n?e.stringify(n):void 0,headers:i,...e._omitFetchOptions?{}:a},s=typeof e.alternateFetch==`function`&&e.alternateFetch.length>=1?e.alternateFetch:void 0;try{y(t,o,r,s)}catch(n){if(!a||Object.keys(a).length===0||!n.message||n.message.indexOf(`not implemented`)<0)return r(n);try{Object.keys(a).forEach(e=>{delete o[e]}),y(t,o,r,s),e._omitFetchOptions=!0}catch(e){r(e)}}},x=(e,t,n,r)=>{n&&typeof n==`object`&&(n=v(``,n).slice(1)),e.queryStringParams&&(t=v(t,e.queryStringParams));try{let i=h?new h:new g(`MSXML2.XMLHTTP.3.0`);i.open(n?`POST`:`GET`,t,1),e.crossDomain||i.setRequestHeader(`X-Requested-With`,`XMLHttpRequest`),i.withCredentials=!!e.withCredentials,n&&i.setRequestHeader(`Content-Type`,`application/x-www-form-urlencoded`),i.overrideMimeType&&i.overrideMimeType(`application/json`);let a=e.customHeaders;if(a=typeof a==`function`?a():a,a)for(let e of Object.keys(a))_.indexOf(e)>-1||i.setRequestHeader(e,a[e]);i.onreadystatechange=()=>{i.readyState>3&&r(i.status>=400?i.statusText:null,{status:i.status,data:i.responseText})},i.send(n)}catch(e){console&&console.log(e)}},S=(e,t,n,r)=>{if(typeof n==`function`&&(r=n,n=void 0),r=r||(()=>{}),m&&t.indexOf(`file:`)!==0)return b(e,t,n,r);if(c()||typeof ActiveXObject==`function`)return x(e,t,n,r);r(Error(`No fetch and no xhr implementation found!`))},C=()=>({loadPath:`/locales/{{lng}}/{{ns}}.json`,addPath:`/locales/add/{{lng}}/{{ns}}`,parse:e=>JSON.parse(e),stringify:JSON.stringify,parsePayload:(e,t,n)=>({[t]:n||``}),parseLoadPayload:(e,t)=>void 0,request:S,reloadInterval:typeof window<`u`?!1:3600*1e3,customHeaders:{},queryStringParams:{},crossDomain:!1,withCredentials:!1,overrideMimeType:!1,requestOptions:{mode:`cors`,credentials:`same-origin`,cache:`default`}});var w=class{constructor(e,t={},n={}){this.services=e,this.options=t,this.allOptions=n,this.type=`backend`,this.init(e,t,n)}init(e,t={},n={}){if(this.services=e,this.options={...C(),...this.options||{},...t},this.allOptions=n,this.services&&this.options.reloadInterval){let e=setInterval(()=>this.reload(),this.options.reloadInterval);typeof e==`object`&&typeof e.unref==`function`&&e.unref()}}readMulti(e,t,n){this._readAny(e,e,t,t,n)}read(e,t,n){this._readAny([e],e,[t],t,n)}_readAny(e,t,n,r,i){let a=this.options.loadPath;typeof this.options.loadPath==`function`&&(a=this.options.loadPath(e,n)),a=u(a),a.then(a=>{if(!a)return i(null,{});let s=f(a,{lng:e.join(`+`),ns:n.join(`+`)});if(s==null){let t=e.map(o).join(`, `),r=n.map(o).join(`, `);return i(Error(`i18next-http-backend: unsafe lng/ns value — refusing to build request URL for languages=[`+t+`] namespaces=[`+r+`]`),!1)}this.loadUrl(s,i,t,r)})}loadUrl(e,t,n,r){let i=typeof n==`string`?[n]:n,a=typeof r==`string`?[r]:r,c=this.options.parseLoadPayload(i,a),l=o(s(e));this.options.request(this.options,e,c,(e,i)=>{if(i&&(i.status>=500&&i.status<600||!i.status))return t(`failed loading `+l+`; status code: `+i.status,!0);if(i&&i.status>=400&&i.status<500)return t(`failed loading `+l+`; status code: `+i.status,!1);if(!i&&e&&e.message){let n=e.message.toLowerCase();if([`failed`,`fetch`,`network`,`load`].find(e=>n.indexOf(e)>-1))return t(`failed loading `+l+`: `+o(e.message),!0)}if(e)return t(e,!1);let a,s;try{a=typeof i.data==`string`?this.options.parse(i.data,n,r):i.data}catch{s=`failed parsing `+l+` to json`}if(s)return t(s,!1);t(null,a)})}create(e,t,n,r,i){if(!this.options.addPath)return;typeof e==`string`&&(e=[e]);let a=this.options.parsePayload(t,n,r),o=0,s=[],c=[];e.forEach(n=>{let r=this.options.addPath;typeof this.options.addPath==`function`&&(r=this.options.addPath(n,t));let l=f(r,{lng:n,ns:t});if(l==null){o+=1,i&&o===e.length&&i(s,c);return}this.options.request(this.options,l,a,(t,n)=>{o+=1,s.push(t),c.push(n),o===e.length&&typeof i==`function`&&i(s,c)})})}reload(){let{backendConnector:e,languageUtils:t,logger:n}=this.services,r=e.language;if(r&&r.toLowerCase()===`cimode`)return;let i=[],a=e=>{t.toResolveHierarchy(e).forEach(e=>{i.indexOf(e)<0&&i.push(e)})};a(r),this.allOptions.preload&&this.allOptions.preload.forEach(e=>a(e)),i.forEach(t=>{this.allOptions.ns.forEach(r=>{e.read(t,r,`read`,null,null,(i,a)=>{i&&n.warn(`loading namespace ${r} for language ${t} failed`,i),!i&&a&&n.log(`loaded namespace ${r} for language ${t}`,a),e.loaded(`${t}|${r}`,i,a)})})})}};return w.type=`backend`,w})();