@deltares/fews-wms-requests
Version:
Library for making requests to the FEWS WMS-T webservice
1 lines • 2.76 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["fews-wms-requests"]=t():e["fews-wms-requests"]=t()}(self,(()=>(()=>{"use strict";var e,t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},s={};function r(e){const t=Object.entries(e).flatMap((([e,t])=>void 0===t?[]:[`${e}=${encodeURIComponent(t)}`]));return t.length?"?"+t.join("&"):""}function a(e,t){return r({request:e,...t})}t.r(s),t.d(s,{WMSProvider:()=>c,WMSRequestType:()=>e,filterToParams:()=>r,filterToParamsWMS:()=>a}),function(e){e.GetCapabilities="GetCapabilities",e.GetLegendGraphic="GetLegendGraphic"}(e||(e={}));class n{_mode="cors";_relativeUrl=!0;get relativeUrl(){return this._relativeUrl}set relativeUrl(e){this._relativeUrl=e}get mode(){return this._mode}set mode(e){this._mode=e}}class i{async parse(e){return await e.json()}}class o{webserviceUrl;transformRequest;constructor(e,t){if(this.webserviceUrl=e,void 0!==t)this.transformRequest=t;else{async function s(e){return e}this.transformRequest=s}}async getDataWithParser(e,t,s){const r=t.relativeUrl?this.webserviceUrl+e:e,a=new Request(r,{method:"GET"}),n=await fetch(await this.transformRequest(a));return await this.processResponse({},n,r,s)}async postDataWithParser(e,t,s,r,a){const n=t.relativeUrl?this.webserviceUrl+e:e,i={method:"POST"};i.body=r,i.headers=a;const o=new Request(n,i),c=await fetch(await this.transformRequest(o));return await this.processResponse({},c,n,s)}async getData(e){const t=new n;return t.relativeUrl=!e.startsWith("http"),this.getDataWithParser(e,t,new i)}async postData(e,t,s={"Content-Type":"application/json"}){const r=new n;return r.relativeUrl=!e.startsWith("http"),this.postDataWithParser(e,r,new i,t,s)}async processResponse(e,t,s,r){if(e.responseCode=t.status,e.contentType=t.headers.get("content-type"),200!=t.status)return e.errorMessage=t.statusText,e;try{e.data=await r.parse(t)}catch(e){throw e.message+=`\n When loading ${s}.`,e}return e}}class c{baseUrl;webservice;constructor(e,t={}){this.baseUrl=e,this.webservice=new o(e,t.transformRequestFn)}async getCapabilities(t){return this.executeWMSRequest(e.GetCapabilities,t)}async getLegendGraphic(t){return t={service:"WMS",version:"1.3",...t},this.executeWMSRequest(e.GetLegendGraphic,t)}async executeWMSRequest(e,t){const s=a(e,{format:"application/json",...t}),r=`${this.baseUrl}${s}`;return(await this.webservice.getData(r)).data}}return s})()));