UNPKG

@map.ir/services-sdk

Version:

JavaScript/TypeScript SDK for map.ir services

3 lines (2 loc) 1.87 kB
const e=e=>Object.fromEntries(Object.entries(e).map(([e,t])=>[e,String(t)])),t=t=>new URLSearchParams(e(((e,t=!0)=>Object.fromEntries(Object.entries(e).filter(([e,n])=>t?Boolean(n):null!=n)))(t))).toString();function n(n,i="car",o={}){const r={alternatives:!0,steps:!0,overview:!1},s={car:"route",walking:"walking",bicycle:"bicycle"}[i];try{const i=n.map(e=>[e.lng,e.lat].join()).join(";"),a=Object.assign(r,o),c=t(e(a)),h=new URL(`/routes/${s}/v1/driving/${i}?${c}`,this.baseURL);return fetch(h,{method:"GET",headers:{"x-api-key":this.apiKey,"content-type":"application/json"}}).then(e=>e.json()).then(e=>e)}catch(e){return void console.error("🚀 ~ file: route.ts:34 ~ err:",e)}}class i{constructor(e){this.apiKey="",this.baseURL="https://map.ir",this.route=n;const{apiKey:t,baseURL:i}=null!=e?e:{};t&&(this.apiKey=t),i&&(this.baseURL=i)}search(e,t,n){const i=new URL("/search"+(n?"/autocomplete":""),this.baseURL);return fetch(i,{method:"POST",headers:{"x-api-key":this.apiKey,"content-type":"application/json"},body:JSON.stringify({text:e,returnid:!0,location:{type:"Point",coordinates:[t.lng,t.lat]}})}).then(e=>e.json()).then(e=>e)}reverseGeocode(e){const n=t({lat:e.lat.toString(),lon:e.lng.toString()}),i=new URL(`/reverse?${n}`,this.baseURL);return fetch(i,{method:"GET",headers:{"x-api-key":this.apiKey,"content-type":"application/json"}}).then(e=>e.json()).then(e=>e)}staticMap(e,n){const{width:i=700,height:o=500,zoom:r=13,colors:s=["red","red"],labels:a}=null!=n?n:{},c=e.map((e,t)=>[`color:${s[t]}`,[e.lng,e.lat].join(),null==a?void 0:a[t]].filter(Boolean).join("|")).join(),h=t({markers:c,width:String(i),height:String(o),zoom_level:String(r)}),l=new URL(`/static?${h}`,this.baseURL);return fetch(l,{method:"GET",headers:{"x-api-key":this.apiKey}}).then(e=>e.blob()).then(e=>e)}}export{i as default}; //# sourceMappingURL=mapir_services_sdk.modern.js.map