typizator-client
Version:
Client library for a generic server API creator with typizator and cdk-typescript-lib
2 lines (1 loc) • 2.23 kB
JavaScript
;var T=Object.create;var p=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var I=(i,e)=>{for(var r in e)p(i,r,{get:e[r],enumerable:!0})},m=(i,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of b(e))!x.call(i,a)&&a!==r&&p(i,a,{get:()=>e[a],enumerable:!(o=c(e,a))||o.enumerable});return i};var $=(i,e,r)=>(r=i!=null?T(w(i)):{},m(e||!i||!i.__esModule?p(r,"default",{value:i,enumerable:!0}):r,i)),z=i=>m(p({},"__esModule",{value:!0}),i);var y={};I(y,{API_URL_PARAM:()=>M,connectTsApi:()=>S});module.exports=z(y);var f=$(require("json-bigint")),M="ApiUrl",U=i=>i.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`),u=(i,e,r)=>{if(i.hidden)return{};let o=e.url.endsWith("/")?e.url:`${e.url}/`,a={};return Object.keys(i.implementation).forEach(d=>{let s=i.implementation[d].metadata,h=U(d);if(s.dataType==="api"){let n=e.children?.[d];a[d]=u(s,{url:n?.url??o,path:`${e.path?`${e.path}/`:""}${h}`,children:n?.children,freeze:n?.freeze??e.freeze,unfreeze:n?.unfreeze??e.unfreeze,wildcardCors:n?.wildcardCors??e.wildcardCors},r)}else{if(s.hidden)return;let n=`${o}${e.path?`${e.path}/`:""}${h}`;a[d]=async(...A)=>{e.freeze?.();let g=!navigator||/^((?!chrome|android).)*safari/i.test(navigator.userAgent),t=await fetch(n,{method:"POST",credentials:e.wildcardCors?void 0:g?"same-origin":"include",headers:{Accept:"application/json","Content-Type":"application/json","x-security-token":r?.()??"",Origin:window.location.origin,"X-Requested-With":"XMLHttpRequest","X-Custom-Header":"1",Authorization:"Bearer rand"},body:f.default.stringify(A)}).then(l=>l.status===401?{errorMessage:"Unauthorized"}:l.json()).catch(l=>{throw new Error(`Error in fetch: ${l.message}`)}).finally(()=>e.unfreeze?.());if(t?.errorMessage)throw new Error(`Server error: ${t.errorMessage}`);if(t?.message)throw new Error(`Server error: ${t.message}`);if(s.retVal){if(t?.data===void 0)throw new Error(`There must be a data field in the received JSON: ${f.default.stringify(t)}`);return typeof t.data=="string"&&t.data.startsWith('"')?s.retVal?.unbox(t.data.substring(1,t.data.length-1)):s.retVal?.unbox(t.data)}}}}),a},S=(i,e,r)=>u(i,e,r);