geoserver-node-client 1.2.4 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ import fetch from 'node-fetch';
2
+ import { getGeoServerResponseText, GeoServerResponseError } from './util/geoserver.js';
3
+ import AboutClient from './about.js'
4
+ import LayerClient from './layer.js';
5
+
6
+ /**
7
+ * Client for GeoServer layergroups
8
+ *
9
+ * @module LayerGroupClient
10
+ */
11
+ export default class LayerGroupClient {
12
+ /**
13
+ * Creates a GeoServer REST LayerGroupClient instance.
14
+ *
15
+ * @param {String} url The URL of the GeoServer REST API endpoint
16
+ * @param {String} auth The Basic Authentication string
17
+ */
18
+ constructor (url, auth) {
19
+ this.url = url;
20
+ this.auth = auth;
21
+ }
22
+
23
+ /**
24
+ * @typedef {object} bounds
25
+ * @property {number} minx The minimum x coordinates. Default: -180
26
+ * @property {number} miny The minimum y coordinates. Default: -90
27
+ * @property {number} maxx The maximum x coordinates. Default: 180
28
+ * @property {number} maxy The maximum y coordinates. Default: 90
29
+ * @property {String} crs The crs of the bounds. Default: 'EPSG:4326'
30
+ */
31
+
32
+ /**
33
+ * Create a GeoServer layergroup by the given workspace, layerGroupName, layers and options
34
+ * @param {String} workspace The name of the workspace
35
+ * @param {String} layerGroupName The name of the layer group
36
+ * @param {Array.<String>} layers List of layers to be added to the group. Must be in same workspace as layergroup
37
+ * @param {String} options.mode The mode of the layergroup. Default to SINGLE
38
+ * @param {String} options.layerGroupTitle The title of the layergroup.
39
+ * @param {bounds} options.bounds The bounds of the layer group.
40
+ *
41
+ * @throws Error if request fails
42
+ *
43
+ * @returns {string} A string with layer group location or undefined if not found
44
+ */
45
+ async create (workspace, layerGroupName, layers, layerGroupOptions) {
46
+ const options = {
47
+ mode: 'SINGLE',
48
+ layerGroupTitle: '',
49
+ abstractTxt: '',
50
+ bounds: {
51
+ minx: -180,
52
+ maxx: -90,
53
+ miny: 180,
54
+ maxy: 90,
55
+ crs: 'EPSG:4326'
56
+ },
57
+ ...layerGroupOptions
58
+ };
59
+ const publishedLayers = [];
60
+ const styles = [];
61
+ for (const l of layers) {
62
+ publishedLayers.push({
63
+ '@type': 'layer',
64
+ name: `${workspace}:${l}`,
65
+ href: `${this.url}/workspaces/${workspace}/layers/${l}.json`
66
+ });
67
+ // use default style by define empty string
68
+ styles.push('');
69
+ }
70
+ const body = {
71
+ layerGroup: {
72
+ name: layerGroupName,
73
+ workspace: {
74
+ name: workspace
75
+ },
76
+ publishables: {
77
+ published: publishedLayers
78
+ },
79
+ styles: {
80
+ style: styles
81
+ },
82
+ ...options
83
+ }
84
+ };
85
+ const response = await fetch(
86
+ `${this.url}/workspaces/${workspace}/layergroups`, {
87
+ credentials: 'include',
88
+ method: 'POST',
89
+ headers: {
90
+ Authorization: this.auth,
91
+ 'Content-Type': 'application/json'
92
+ },
93
+ body: JSON.stringify(body)
94
+ }
95
+ );
96
+
97
+ if (!response.ok) {
98
+ const grc = new AboutClient(this.url, this.auth);
99
+ if (await grc.exists()) {
100
+ // GeoServer exists, but requested item does not exist, we return empty
101
+ return;
102
+ } else {
103
+ // There was a general problem with GeoServer
104
+ const geoServerResponse = await getGeoServerResponseText(response);
105
+ throw new GeoServerResponseError(null, geoServerResponse);
106
+ }
107
+ }
108
+ // get resource location from response header
109
+ const location = response.headers.get('location');
110
+
111
+ // return resource location
112
+ return location;
113
+ }
114
+
115
+ /**
116
+ * Returns a GeoServer layergroup by the given workspace and layergroup name,
117
+ * e.g. "myWs:myLayergroup".
118
+ *
119
+ * @param {String} workspace The name of the workspace
120
+ * @param {String} layerGroupName The name of the layer group to query
121
+ *
122
+ * @throws Error if request fails
123
+ *
124
+ * @returns {Object} An object with layer group information or undefined if it cannot be found
125
+ */
126
+ async get (workspace, layerGroupName) {
127
+ const response = await fetch(
128
+ `${this.url}/workspaces/${workspace}/layergroups/${layerGroupName}.json`, {
129
+ credentials: 'include',
130
+ method: 'GET',
131
+ headers: {
132
+ Authorization: this.auth
133
+ }
134
+ });
135
+
136
+ if (!response.ok) {
137
+ const grc = new AboutClient(this.url, this.auth);
138
+ if (await grc.exists()) {
139
+ // GeoServer exists, but requested item does not exist, we return empty
140
+ return;
141
+ } else {
142
+ // There was a general problem with GeoServer
143
+ const geoServerResponse = await getGeoServerResponseText(response);
144
+ throw new GeoServerResponseError(null, geoServerResponse);
145
+ }
146
+ }
147
+ return response.json();
148
+ }
149
+
150
+ /**
151
+ * Updates an existing GeoServer layergroup
152
+ *
153
+ * @param {String} workspace The name of the workspace
154
+ * @param {String} layerName The name of the layergroup to update
155
+ * @param {Object} layerGroupDefinition The updated definiton of the layergroup
156
+ *
157
+ * @throws Error if request fails
158
+ */
159
+ async update (workspace, layerGroupName, layerGroupDefinition) {
160
+ const url = `${this.url}/workspaces/${workspace}/layergroups/${layerGroupName}.json`;
161
+ const response = await fetch(url, {
162
+ credentials: 'include',
163
+ method: 'PUT',
164
+ headers: {
165
+ Authorization: this.auth,
166
+ 'Content-Type': 'application/json'
167
+ },
168
+ body: JSON.stringify(layerGroupDefinition)
169
+ });
170
+
171
+ if (!response.ok) {
172
+ const geoServerResponse = await getGeoServerResponseText(response);
173
+ throw new GeoServerResponseError(null, geoServerResponse);
174
+ }
175
+ }
176
+ }
package/src/workspace.js CHANGED
@@ -86,7 +86,7 @@ export default class WorkspaceClient {
86
86
  async create (name) {
87
87
  const body = {
88
88
  workspace: {
89
- name: name
89
+ name
90
90
  }
91
91
  };
92
92
 
@@ -109,8 +109,9 @@ export default class WorkspaceClient {
109
109
  throw new GeoServerResponseError(null, geoServerResponse);
110
110
  }
111
111
  }
112
-
113
- return response.text();
112
+ // query the WS object again to return the name of the created WS
113
+ const wsObject = await this.get(name);
114
+ return wsObject.workspace.name;
114
115
  }
115
116
 
116
117
  /**