UNPKG

geography-apis-sdk

Version:

SDK for making requests to Geography APIs

1 lines 7.64 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.GeographyAPISDK=e():t.GeographyAPISDK=e()}(this,(()=>{return t={291:t=>{t.exports=class{constructor(t,e,s){this.baseURL=t,this.apiKey=e,this.apiHost=s}async request(t,e={}){try{const s={method:e.method||"GET",headers:{"X-RapidAPI-Key":this.apiKey,"X-RapidAPI-Host":this.apiHost,"Access-Control-Allow-Headers":"X-Requested-With",...e.headers}};delete s.method;const r=new URL(`${this.baseURL}${t}`);e.params&&Object.entries(e.params).forEach((([t,e])=>{r.searchParams.append(t,e)}));const a=await fetch(r.toString(),s),o=a.headers;if(204===a.status)return{body:[],headers:o};if(a.ok||206===a.status)return{body:await a.json(),headers:o};{let t;try{t=await a.json()}catch(t){throw new Error(`Request failed with status: ${a.status}`)}const e=t?.message||a.statusText;throw new Error(e)}}catch(t){throw new Error(`Error making request: ${t.message}`)}}async get(t,e={},s={}){const r=new URL(`${this.baseURL}${t}`);Object.entries(e).forEach((([t,e])=>{r.searchParams.append(t,e)}));try{const t=await fetch(r.toString(),{headers:{"X-RapidAPI-Key":this.apiKey,"X-RapidAPI-Host":this.apiHost,...e}}),e=t.headers;if(204===t.status)return{body:[],headers:e};if(t.ok||206===t.status)return{body:await t.json(),headers:e};{let e;try{e=await t.json()}catch(e){throw new Error(`Request failed with status: ${t.status}`)}const s=e?.message||t.statusText;throw new Error(s)}}catch(t){throw new Error(`Error making GET request: ${t.message}`)}}async call(t,e,s,r){try{const a={...s,method:"GET"},o=await this.request(`${t}${e}`,a),i=o.headers;if(Array.isArray(o.body))return{body:o.body.map((t=>new r(t))),headers:i};if("object"==typeof o&&null!==o)return{body:new r(o),headers:i};throw new Error("No data found")}catch(t){throw new Error(`Failed to call Geography API: ${t.message}`)}}}},230:(t,e,s)=>{const r=s(299),a=s(291);t.exports=class{constructor(t,e,s){this.apiKey=t,this.apiHost=e,this.restClient=new a(s,t,e)}async getAllCountries(t={}){try{const e="/apis/geography/v1/country";return await this.restClient.call(e,"",t,r)}catch(t){throw new Error(`Failed to get all countries : ${t.message}`)}}async searchCountriesByName(t,e={}){try{const s="/apis/geography/v1/country/name/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by name : ${t.message}`)}}async searchCountriesByCapital(t,e={}){try{const s="/apis/geography/v1/country/capital/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by capital: ${t.message}`)}}async searchNeighboringCountries(t,e={}){try{const s="/apis/geography/v1/country/neighbour/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search neighboring countries for given country code: ${t.message}`)}}async searchCountriesByPopulation(t={}){try{const e="/apis/geography/v1/country/population";return await this.restClient.call(e,"",t,r)}catch(t){throw new Error(`Failed to search countries for given population threshold: ${t.message}`)}}async searchCountriesByLanguage(t,e={}){try{const s="/apis/geography/v1/country/language/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by language : ${t.message}`)}}async searchCountriesByCurrency(t,e={}){try{const s="/apis/geography/v1/country/currency/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by Currency : ${t.message}`)}}async searchCountriesByContinent(t,e={}){try{const s="/apis/geography/v1/country/continent/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by continent : ${t.message}`)}}async searchCountriesByRegion(t,e={}){try{const s="/apis/geography/v1/country/region/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by region : ${t.message}`)}}async searchCountriesBySubregion(t,e={}){try{const s="/apis/geography/v1/country/subregion/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by sub-region : ${t.message}`)}}async searchCountriesByTimezone(t,e={}){try{const s="/apis/geography/v1/country/timezone/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by timezone : ${t.message}`)}}async searchCountryByFifaCode(t,e={}){try{const s="/apis/geography/v1/country/fifacode/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by fifaCode : ${t.message}`)}}async getCountryByIsoCode(t,e={}){try{const s="/apis/geography/v1/country/";return await this.restClient.call(s,t,e,r)}catch(t){throw new Error(`Failed to search countries by countryCode : ${t.message}`)}}}},664:(t,e,s)=>{const r=s(434);t.exports=class{constructor(t){void 0!==t&&(this.name=t.name,this.latLng=new r(t.latLng))}}},332:t=>{t.exports=class{constructor(t){void 0!==t&&(this.drivingSide=t.drivingSide,this.roadSigns=t.roadSigns||[])}}},601:t=>{t.exports=class{constructor(t){void 0!==t&&(this.svg=t.svg)}}},299:(t,e,s)=>{const r=s(153),a=s(884),o=s(112),i=s(664),n=s(130),c=s(434),h=s(847),l=s(61),u=s(601),y=s(814),d=s(568),g=s(332),p=s(980);t.exports=class{constructor(t){void 0!==t&&(this.href=t.href,this.name=new r(t.name),this.tld=t.tld||[],this.cca2=t.cca2,this.ccn3=t.ccn3,this.cca3=t.cca3,this.cioc=t.cioc,this.fifa=t.fifa,this.independent=t.independent,this.status=t.status,this.unMember=t.unMember,this.currencies=(t.currencies||[]).map((t=>new a(t))),this.idd=new o(t.idd),this.capital=(t.capital||[]).map((t=>new i(t))),this.altSpellings=t.altSpellings||[],this.region=t.region,this.subregion=t.subregion,this.continents=t.continents||[],this.languages=(t.languages||[]).map((t=>new n(t))),this.latLng=new c(t.latLng),this.landlocked=t.landlocked,this.borders=t.borders||[],this.area=t.area,this.flag=t.flag,this.flags=new h(t.flags),this.demonyms=(t.demonyms||[]).map((t=>new l(t))),this.coatOfArms=new u(t.coatOfArms),this.population=t.population,this.maps=new y(t.maps),this.gini=new d(t.gini),this.car=new g(t.car),this.timezones=t.timezones||[],this.startOfWeek=t.startOfWeek,this.postalCode=new p(t.postalCode))}}},884:t=>{t.exports=class{constructor(t){void 0!==t&&(this.code=t.code,this.name=t.name,this.symbol=t.symbol)}}},61:t=>{t.exports=class{constructor(t){void 0!==t&&(this.male=t.male,this.female=t.female,this.demonym=t.demonym)}}},847:t=>{t.exports=class{constructor(t){void 0!==t&&(this.svg=t.svg)}}},568:t=>{t.exports=class{constructor(t){void 0!==t&&(this.value=t.value,this.year=t.year)}}},112:t=>{t.exports=class{constructor(t){void 0!==t&&(this.root=t.root,this.suffixes=t.suffixes||[])}}},130:t=>{t.exports=class{constructor(t){void 0!==t&&(this.iso639_1=t.iso639_1,this.iso639_2=t.iso639_2,this.name=t.name,this.nativeName=t.nativeName)}}},434:t=>{t.exports=class{constructor(t){void 0!==t&&(this.lat=t.lat,this.lng=t.lng)}}},814:(t,e,s)=>{s(434),t.exports=class{constructor(t){void 0!==t&&(this.svg=t.svg,this.png=t.png)}}},153:(t,e,s)=>{const r=s(903);t.exports=class{constructor(t){void 0!==t&&(this.common=t.common,this.official=t.official,this.native=new r(t.native))}}},903:t=>{t.exports=class{constructor(t){void 0!==t&&(this.common=t?.common,this.official=t?.official)}}},980:t=>{t.exports=class{constructor(t){void 0!==t&&(this.format=t.format,this.regex=t.regex)}}}},e={},function s(r){var a=e[r];if(void 0!==a)return a.exports;var o=e[r]={exports:{}};return t[r](o,o.exports,s),o.exports}(230);var t,e}));