geoserver-node-client 0.0.7 → 1.2.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.
package/src/workspace.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import fetch from 'node-fetch';
2
+ import { getGeoServerResponseText, GeoServerResponseError } from './util/geoserver.js';
3
+ import AboutClient from './about.js'
2
4
 
3
5
  /**
4
6
  * Client for GeoServer workspaces
@@ -9,64 +11,67 @@ export default class WorkspaceClient {
9
11
  /**
10
12
  * Creates a GeoServer REST WorkspaceClient instance.
11
13
  *
14
+ * WARNING: For most cases the 'NameSpaceClient' seems to fit better.
15
+ *
12
16
  * @param {String} url The URL of the GeoServer REST API endpoint
13
- * @param {String} user The user for the GeoServer REST API
14
- * @param {String} password The password for the GeoServer REST API
17
+ * @param {String} auth The Basic Authentication string
15
18
  */
16
- constructor (url, user, password) {
17
- this.url = url.endsWith('/') ? url : url + '/';
18
- this.user = user;
19
- this.password = password;
19
+ constructor (url, auth) {
20
+ this.url = url;
21
+ this.auth = auth;
20
22
  }
21
23
 
22
24
  /**
23
25
  * Returns all workspaces.
24
26
  *
25
- * @returns {Object|Boolean} An Object describing the workspaces or 'false'
27
+ * @throws Error if request fails
28
+ *
29
+ * @returns {Object} An Object describing the workspaces
26
30
  */
27
31
  async getAll () {
28
- try {
29
- const auth =
30
- Buffer.from(this.user + ':' + this.password).toString('base64');
31
- const response = await fetch(this.url + 'workspaces.json', {
32
- credentials: 'include',
33
- method: 'GET',
34
- headers: {
35
- Authorization: 'Basic ' + auth
36
- }
37
- });
38
- const json = await response.json();
39
- return json;
40
- } catch (error) {
41
- return false;
32
+ const response = await fetch(this.url + 'workspaces.json', {
33
+ credentials: 'include',
34
+ method: 'GET',
35
+ headers: {
36
+ Authorization: this.auth
37
+ }
38
+ });
39
+ if (!response.ok) {
40
+ const geoServerResponse = await getGeoServerResponseText(response);
41
+ throw new GeoServerResponseError(null, geoServerResponse);
42
42
  }
43
+ return response.json();
43
44
  }
44
45
 
45
46
  /**
46
47
  * Returns a workspace.
47
48
  *
48
49
  * @param {String} name Name of the workspace
49
- * @returns {Object|Boolean} An object describing the workspaces or 'false'
50
+ *
51
+ * @throws Error if request fails
52
+ *
53
+ * @returns {Object} An object describing the workspaces
50
54
  */
51
55
  async get (name) {
52
- try {
53
- const auth =
54
- Buffer.from(this.user + ':' + this.password).toString('base64');
55
- const response = await fetch(this.url + 'workspaces/' + name + '.json', {
56
- credentials: 'include',
57
- method: 'GET',
58
- headers: {
59
- Authorization: 'Basic ' + auth
60
- }
61
- });
62
- if (response.status === 200) {
63
- return await response.json();
56
+ const response = await fetch(this.url + 'workspaces/' + name + '.json', {
57
+ credentials: 'include',
58
+ method: 'GET',
59
+ headers: {
60
+ Authorization: this.auth
61
+ }
62
+ });
63
+ if (!response.ok) {
64
+ const grc = new AboutClient(this.url, this.auth);
65
+ if (await grc.exists()) {
66
+ // GeoServer exists, but requested item does not exist, we return empty
67
+ return;
64
68
  } else {
65
- return false;
69
+ // There was a general problem with GeoServer
70
+ const geoServerResponse = await getGeoServerResponseText(response);
71
+ throw new GeoServerResponseError(null, geoServerResponse);
66
72
  }
67
- } catch (error) {
68
- return false;
69
73
  }
74
+ return response.json();
70
75
  }
71
76
 
72
77
  /**
@@ -74,38 +79,38 @@ export default class WorkspaceClient {
74
79
  *
75
80
  * @param {String} name Name of the new workspace
76
81
  *
77
- * @returns {String|Boolean} The name of the created workspace or 'false'
82
+ * @throws Error if request fails
83
+ *
84
+ * @returns {String} The name of the created workspace
78
85
  */
79
86
  async create (name) {
80
- try {
81
- const body = {
82
- workspace: {
83
- name: name
84
- }
85
- };
86
-
87
- const auth =
88
- Buffer.from(this.user + ':' + this.password).toString('base64');
87
+ const body = {
88
+ workspace: {
89
+ name: name
90
+ }
91
+ };
89
92
 
90
- const response = await fetch(this.url + 'workspaces', {
91
- credentials: 'include',
92
- method: 'POST',
93
- headers: {
94
- Authorization: 'Basic ' + auth,
95
- 'Content-Type': 'application/json'
96
- },
97
- body: JSON.stringify(body)
98
- });
93
+ const response = await fetch(this.url + 'workspaces', {
94
+ credentials: 'include',
95
+ method: 'POST',
96
+ headers: {
97
+ Authorization: this.auth,
98
+ 'Content-Type': 'application/json'
99
+ },
100
+ body: JSON.stringify(body)
101
+ });
99
102
 
100
- if (response.status === 201) {
101
- const responseText = await response.text();
102
- return responseText;
103
- } else {
104
- return false;
103
+ if (!response.ok) {
104
+ const geoServerResponse = await getGeoServerResponseText(response);
105
+ switch (response.status) {
106
+ case 409:
107
+ throw new GeoServerResponseError('Unable to add workspace as it already exists', geoServerResponse);
108
+ default:
109
+ throw new GeoServerResponseError(null, geoServerResponse);
105
110
  }
106
- } catch (error) {
107
- return false;
108
111
  }
112
+
113
+ return response.text();
109
114
  }
110
115
 
111
116
  /**
@@ -114,28 +119,31 @@ export default class WorkspaceClient {
114
119
  * @param {String} name Name of the workspace to delete
115
120
  * @param {Boolean} recurse Flag to enable recursive deletion
116
121
  *
117
- * @returns {Boolean} If Deletion was successful
122
+ * @throws Error if request fails
118
123
  */
119
124
  async delete (name, recurse) {
120
- try {
121
- const auth =
122
- Buffer.from(this.user + ':' + this.password).toString('base64');
123
- const response = await fetch(this.url + 'workspaces/' + name + '?recurse=' + recurse, {
124
- credentials: 'include',
125
- method: 'DELETE',
126
- headers: {
127
- Authorization: 'Basic ' + auth
128
- }
129
- });
125
+ const response = await fetch(this.url + 'workspaces/' + name + '?recurse=' + recurse, {
126
+ credentials: 'include',
127
+ method: 'DELETE',
128
+ headers: {
129
+ Authorization: this.auth
130
+ }
131
+ });
130
132
 
131
- // TODO map other HTTP status
132
- if (response.status === 200) {
133
- return true;
134
- } else {
135
- return false;
133
+ if (!response.ok) {
134
+ const geoServerResponse = await getGeoServerResponseText(response);
135
+ switch (response.status) {
136
+ case 400:
137
+ // the docs say code 403, but apparently it is code 400
138
+ // https://docs.geoserver.org/latest/en/api/#1.0.0/workspaces.yaml
139
+ throw new GeoServerResponseError(
140
+ 'Workspace or related Namespace is not empty (and recurse not true)',
141
+ geoServerResponse);
142
+ case 404:
143
+ throw new GeoServerResponseError('Workspace doesn\'t exist', geoServerResponse);
144
+ default:
145
+ throw new GeoServerResponseError(null, geoServerResponse);
136
146
  }
137
- } catch (error) {
138
- return false;
139
147
  }
140
148
  }
141
149
  }
package/.eslintrc.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "es6": true
5
- },
6
- "extends": [
7
- "standard"
8
- ],
9
- "globals": {
10
- "Atomics": "readonly",
11
- "SharedArrayBuffer": "readonly"
12
- },
13
- "parserOptions": {
14
- "ecmaVersion": 11,
15
- "sourceType": "module"
16
- },
17
- "rules": {
18
- "semi": "off"
19
- }
20
- }
@@ -1,54 +0,0 @@
1
- name: ci-geoserver-node-client
2
- on:
3
- push:
4
- branches:
5
- - master
6
- pull_request:
7
- branches:
8
- - master
9
- jobs:
10
- run-tests-maintenance:
11
- runs-on: ubuntu-latest
12
- env:
13
- GEOSERVER_VERSION: 2.19.3
14
- steps:
15
- - uses: actions/checkout@v2
16
-
17
- # In this step, this action saves a list of existing images,
18
- # the cache is created without them in the post run.
19
- # It also restores the cache if it exists.
20
- - uses: satackey/action-docker-layer-caching@v0.0.11
21
- # Ignore the failure of a step and avoid terminating the job.
22
- continue-on-error: true
23
-
24
- - run: docker run -d -p 8080:8080 meggsimum/geoserver:${GEOSERVER_VERSION}
25
- # finishes when tomcat of GeoServer is running
26
- - run: .github/workflows/wait-for.sh "localhost:8080"
27
- # we use wget to ensure that GeoServer is running
28
- - run: wget http://localhost:8080/geoserver/web
29
-
30
- - run: npm install
31
- - run: npm run test
32
-
33
- run-tests-stable:
34
- runs-on: ubuntu-latest
35
- env:
36
- GEOSERVER_VERSION: 2.20.1
37
- steps:
38
- - uses: actions/checkout@v2
39
-
40
- # In this step, this action saves a list of existing images,
41
- # the cache is created without them in the post run.
42
- # It also restores the cache if it exists.
43
- - uses: satackey/action-docker-layer-caching@v0.0.11
44
- # Ignore the failure of a step and avoid terminating the job.
45
- continue-on-error: true
46
-
47
- - run: docker run -d -p 8080:8080 meggsimum/geoserver:${GEOSERVER_VERSION}
48
- # finishes when tomcat of GeoServer is running
49
- - run: .github/workflows/wait-for.sh "localhost:8080"
50
- # we use wget to ensure that GeoServer is running
51
- - run: wget http://localhost:8080/geoserver/web
52
-
53
- - run: npm install
54
- - run: npm run test
@@ -1,24 +0,0 @@
1
- name: publish-docs
2
- on:
3
- push:
4
- branches:
5
- - master
6
- jobs:
7
- build-and-publish-docs:
8
- runs-on: ubuntu-latest
9
- steps:
10
- - name: Checkout
11
- uses: actions/checkout@v2.3.1
12
-
13
- - name: Install and Build
14
- run: |
15
- npm install
16
- npm run docs
17
- # prevents Jekyll defaults by GitHub
18
- touch out/.nojekyll
19
-
20
- - name: Publish docs
21
- uses: JamesIves/github-pages-deploy-action@4.0.0
22
- with:
23
- branch: gh-pages # The branch the action should deploy to.
24
- folder: out # The folder the action should deploy.
@@ -1,145 +0,0 @@
1
- #!/bin/sh
2
-
3
- # The MIT License (MIT)
4
- #
5
- # Copyright (c) 2017 Eficode Oy
6
- #
7
- # Permission is hereby granted, free of charge, to any person obtaining a copy
8
- # of this software and associated documentation files (the "Software"), to deal
9
- # in the Software without restriction, including without limitation the rights
10
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- # copies of the Software, and to permit persons to whom the Software is
12
- # furnished to do so, subject to the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be included in all
15
- # copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- # SOFTWARE.
24
-
25
- set -- "$@" -- "$TIMEOUT" "$QUIET" "$HOST" "$PORT" "$result"
26
- TIMEOUT=15
27
- QUIET=0
28
-
29
- echoerr() {
30
- if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
31
- }
32
-
33
- usage() {
34
- exitcode="$1"
35
- cat << USAGE >&2
36
- Usage:
37
- $cmdname host:port [-t timeout] [-- command args]
38
- -q | --quiet Do not output any status messages
39
- -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
40
- -- COMMAND ARGS Execute command with args after the test finishes
41
- USAGE
42
- exit "$exitcode"
43
- }
44
-
45
- wait_for() {
46
- if ! command -v nc >/dev/null; then
47
- echoerr 'nc command is missing!'
48
- exit 1
49
- fi
50
-
51
- while :; do
52
- nc -z "$HOST" "$PORT" > /dev/null 2>&1
53
-
54
- result=$?
55
- if [ $result -eq 0 ] ; then
56
- if [ $# -gt 6 ] ; then
57
- for result in $(seq $(($# - 6))); do
58
- result=$1
59
- shift
60
- set -- "$@" "$result"
61
- done
62
-
63
- TIMEOUT=$2 QUIET=$3 HOST=$4 PORT=$5 result=$6
64
- shift 6
65
- exec "$@"
66
- fi
67
- exit 0
68
- fi
69
-
70
- if [ "$TIMEOUT" -le 0 ]; then
71
- break
72
- fi
73
- TIMEOUT=$((TIMEOUT - 1))
74
-
75
- sleep 1
76
- done
77
- echo "Operation timed out" >&2
78
- exit 1
79
- }
80
-
81
- while :; do
82
- case "$1" in
83
- *:* )
84
- HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
85
- PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
86
- shift 1
87
- ;;
88
- -q | --quiet)
89
- QUIET=1
90
- shift 1
91
- ;;
92
- -q-*)
93
- QUIET=0
94
- echoerr "Unknown option: $1"
95
- usage 1
96
- ;;
97
- -q*)
98
- QUIET=1
99
- result=$1
100
- shift 1
101
- set -- -"${result#-q}" "$@"
102
- ;;
103
- -t | --timeout)
104
- TIMEOUT="$2"
105
- shift 2
106
- ;;
107
- -t*)
108
- TIMEOUT="${1#-t}"
109
- shift 1
110
- ;;
111
- --timeout=*)
112
- TIMEOUT="${1#*=}"
113
- shift 1
114
- ;;
115
- --)
116
- shift
117
- break
118
- ;;
119
- --help)
120
- usage 0
121
- ;;
122
- -*)
123
- QUIET=0
124
- echoerr "Unknown option: $1"
125
- usage 1
126
- ;;
127
- *)
128
- QUIET=0
129
- echoerr "Unknown argument: $1"
130
- usage 1
131
- ;;
132
- esac
133
- done
134
-
135
- if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
136
- echoerr "Error: invalid timeout '$TIMEOUT'"
137
- usage 3
138
- fi
139
-
140
- if [ "$HOST" = "" -o "$PORT" = "" ]; then
141
- echoerr "Error: you need to provide a host and port to test."
142
- usage 2
143
- fi
144
-
145
- wait_for "$@"
@@ -1,3 +0,0 @@
1
- {
2
- "eslint.enable": false
3
- }
package/DOCS_HOME.md DELETED
@@ -1,26 +0,0 @@
1
- # GeoServer Node Client
2
-
3
- Node.js / JavaScript Client for the [GeoServer REST API](https://docs.geoserver.org/stable/en/user/rest/).
4
-
5
- **CAUTION: This is highly bleeding edge, heavily under development and therefore breaking changes can be made at every time!**
6
-
7
- ### Meta information
8
-
9
- Compatible with [GeoServer](https://geoserver.org)
10
-
11
- - v2.20.x
12
- - v2.19.x
13
- - v2.18.x (no more maintained and officially deprecated)
14
- - v2.17.x (no more maintained and officially deprecated)
15
-
16
- ### Who do I talk to? ###
17
-
18
- * meggsimum (Christian Mayer) - info __at## meggsimum ~~dot** de
19
-
20
- ### Credits
21
-
22
- This project was initiated by [meggsimum](https://meggsimum.de) within the [mFund](https://www.bmvi.de/EN/Topics/Digital-Matters/mFund/mFund.html) research project [SAUBER](https://sauber-projekt.de/).
23
- <p><img src="https://sauber-projekt.de/wp-content/uploads/2018/12/SAG_SAUBER_Logo_Dez3_transparent-1-e1543843688935.png" alt="SAUBER Logo" width="200"/></p>.
24
-
25
- <img src="https://sauber-projekt.de/wp-content/uploads/2018/12/mfund-logo-download-e1547545420815-300x77.jpg" alt="mFund Logo" width="300"/>
26
- <img src="https://sauber-projekt.de/wp-content/uploads/2019/06/BMVI_Fz_2017_Office_Farbe_de_Bundestag-400x402.png" alt="BMVI Logo" height="200"/>