JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 811
  • Score
    100M100P100Q109922F
  • License ISC

Custom headers on Leaflet TileLayer WMS

Package Exports

  • leaflet-wms-header

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (leaflet-wms-header) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

leaflet-wms-header

Custom headers on Leaflet TileLayer WMS. It's a simple plugin that allow to set custom header for WMS interface.

It works with javascript and typescript without any dependencies!

Based on https://github.com/Leaflet/Leaflet/issues/2091#issuecomment-302706529.

Javascript

$ npm install leaflet leaflet-wms-header --save
<!-- Assuming your project root is "../" -->
<script src="../node_modules/leaflet/dist/leaflet.js"></script> 
<script src="../node_modules/leaflet-wms-header/index.js"></script> 
// YOUR LEAFLET CODE

var wmsLayer = L.TileLayer.wmsHeader(
    'https://GEOSERVER_PATH/geoserver/wms?',
    {
        layers: 'ne:ne',
        format: 'image/png',
        transparent: true,
    },
    [
        { header: 'Authorization', value: 'JWT ' + MYAUTHTOKEN },
        { header: 'content-type', value: 'text/plain'},
    ],
    null
).addTo(map);

Typescript

$ npm install leaflet @types/leaflet leaflet-wms-header --save
import * as L from 'leaflet';
import 'leaflet-wms-header';

// YOUR LEAFLET CODE

let wmsLayer: L.TileLayer.WMSHeader = L.TileLayer.wmsHeader(
    'https://GEOSERVER_PATH/geoserver/wms?',
    {
        layers: layers,
        format: 'image/png',
        transparent: true,
    }, [
        { header: 'Authorization', value: 'JWT ' + MYAUTHTOKEN },
        { header: 'content-type', value: 'text/plain'},
    ],
    null
).addTo(map);

Abort parameter

Abort parameter allow to abort the http request through a Promise. This optimization function might be usefull to stop the http request when it is not necessary anymore, mostly if many requests are pending.

See below an example using an Observable as "abort" parameter.

let tileLayer: L.TileLayer.WMSHeader = L.TileLayer.wmsHeader(
    'https://GEOSERVER_PATH/geoserver/wms?',
    {
        layers: layers,
        format: 'image/png',
        transparent: true,
    }, [
        { header: 'Authorization', value: 'JWT ' + MYAUTHTOKEN },
        { header: 'content-type', value: 'text/plain'},
    ],
    new Promise((resolve, reject) => {
        this.abortWMSObservable$
            .pipe(
                take(1)
            )
            .subscribe(x => {
                resolve();
            });
        })
);