Source: src/about.js

  1. import fetch from 'node-fetch';
  2. import { GeoServerResponseError, getGeoServerResponseText } from './util/geoserver.js';
  3. /**
  4. * Client for GeoServer "about" endpoint
  5. *
  6. * @module AboutClient
  7. */
  8. export default class AboutClient {
  9. /**
  10. * Creates a GeoServer REST AboutClient instance.
  11. *
  12. * @param {String} url The URL of the GeoServer REST API endpoint
  13. * @param {String} auth The Basic Authentication string
  14. */
  15. constructor (url, auth) {
  16. this.url = url;
  17. this.auth = auth;
  18. }
  19. /**
  20. * Get the GeoServer version.
  21. *
  22. * @throws Error if request fails
  23. *
  24. * @returns {Object} The version of GeoServer
  25. */
  26. async getVersion () {
  27. const url = this.url + 'about/version.json';
  28. const response = await fetch(url, {
  29. credentials: 'include',
  30. method: 'GET',
  31. headers: {
  32. Authorization: this.auth
  33. }
  34. });
  35. if (!response.ok) {
  36. const geoServerResponse = await getGeoServerResponseText(response);
  37. throw new GeoServerResponseError(null, geoServerResponse);
  38. }
  39. return response.json();
  40. }
  41. /**
  42. * Checks if the configured GeoServer REST connection exists.
  43. *
  44. * @returns {Boolean} If the connection exists
  45. */
  46. async exists () {
  47. let versionInfo;
  48. try {
  49. versionInfo = await this.getVersion();
  50. return !!versionInfo
  51. } catch (error) {
  52. return false;
  53. }
  54. }
  55. }