@mapcreator/api 2.4.0 → 2.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapcreator/api",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "Mapcreator JavaScript API",
5
5
  "main": "dist/bundle.js",
6
6
  "repository": "https://gitlab.com/mapcreator/api-wrapper.git",
package/src/Mapcreator.js CHANGED
@@ -58,6 +58,7 @@ import {
58
58
  Layer,
59
59
  Mapstyle,
60
60
  MapstyleSet,
61
+ Message,
61
62
  Notification,
62
63
  Organisation,
63
64
  Permission,
@@ -559,6 +560,15 @@ export default class Mapcreator extends mix(EventEmitter, Injectable) {
559
560
  return this.static(Notification);
560
561
  }
561
562
 
563
+ /**
564
+ * Message accessor
565
+ * @see {@link Message}
566
+ * @returns {ResourceProxy} - A proxy for accessing the resource
567
+ */
568
+ get messages () {
569
+ return this.static(Message);
570
+ }
571
+
562
572
  /**
563
573
  * Organisation accessor
564
574
  * @see {@link Organisation}
@@ -216,8 +216,11 @@ export default class ImplicitFlow extends OAuth {
216
216
  }
217
217
 
218
218
  const params = this._getAnchorParams();
219
- const message = params.message ?? titleCase(params.error);
220
219
 
221
- return new OAuthError(params.error, message);
220
+ if (params.message) {
221
+ return new OAuthError(params.error, params.message);
222
+ }
223
+
224
+ return new OAuthError(params.error, titleCase(params.error));
222
225
  }
223
226
  }
@@ -0,0 +1,94 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2020, Mapcreator
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * Redistributions of source code must retain the above copyright notice, this
11
+ * list of conditions and the following disclaimer.
12
+ *
13
+ * Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ import CrudBase from './base/CrudBase';
34
+ import MessageVariant from './MessageVariant';
35
+ import { makeCancelable } from '../utils/helpers';
36
+ import { camel as camelCase, snake as snakeCase } from 'case';
37
+
38
+ export default class Message extends CrudBase {
39
+ static get resourceName () {
40
+ return 'messages';
41
+ }
42
+
43
+ toObject (camelCaseKeys = false) {
44
+ const superObject = super.toObject(camelCaseKeys);
45
+
46
+ superObject.variants = superObject.variants.map(variant => {
47
+ if (variant instanceof MessageVariant) {
48
+ return variant.toObject(camelCaseKeys);
49
+ }
50
+
51
+ const caseFn = camelCaseKeys ? camelCase : snakeCase;
52
+ const res = {};
53
+ const fields = Object.keys(variant);
54
+
55
+ for (const field of fields) {
56
+ res[caseFn(field)] = variant[field];
57
+ }
58
+
59
+ return res;
60
+ });
61
+
62
+ return superObject;
63
+ }
64
+
65
+ _guessType (name, value) {
66
+ if (name === 'variants') {
67
+ return Array.from(value).map(data => new MessageVariant(this.api, data));
68
+ }
69
+
70
+ return super._guessType(name, value);
71
+ }
72
+
73
+ _buildCreateData () {
74
+ return this.toObject();
75
+ }
76
+
77
+ _update () {
78
+ return makeCancelable(async signal => {
79
+ const json = this.toObject();
80
+
81
+ await this.api.ky.patch(this.url, { json, signal });
82
+
83
+ // Reset changes
84
+ Object.assign(this._baseProperties, this._properties);
85
+ this._properties = {};
86
+
87
+ if ('updated_at' in this._baseProperties) {
88
+ this._baseProperties['updated_at'] = new Date();
89
+ }
90
+
91
+ return this;
92
+ });
93
+ }
94
+ }
@@ -0,0 +1,37 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2020, Mapcreator
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * Redistributions of source code must retain the above copyright notice, this
11
+ * list of conditions and the following disclaimer.
12
+ *
13
+ * Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ import ResourceBase from './base/ResourceBase';
35
+
36
+ export default class MessageVariant extends ResourceBase {
37
+ }
@@ -46,6 +46,7 @@ import Language from './Language';
46
46
  import Layer from './Layer';
47
47
  import Mapstyle from './Mapstyle';
48
48
  import MapstyleSet from './MapstyleSet';
49
+ import Message from './Message';
49
50
  import Notification from './Notification';
50
51
  import Organisation from './Organisation';
51
52
  import Permission from './Permission';
@@ -101,6 +102,14 @@ export default class User extends CrudBase {
101
102
  return this._proxyResourceList(Notification);
102
103
  }
103
104
 
105
+ /**
106
+ * Get the list messagse linked to the user
107
+ * @returns {SimpleResourceProxy} - A proxy for accessing the resource
108
+ */
109
+ get messages () {
110
+ return this._proxyResourceList(Message);
111
+ }
112
+
104
113
  /**
105
114
  * Get the list mapstyle sets linked to the user
106
115
  * @returns {SimpleResourceProxy} - A proxy for accessing the resource
@@ -111,7 +111,7 @@ export default class CrudBase extends ResourceBase {
111
111
  * @returns {CancelablePromise<CrudBase>} - Current instance
112
112
  * @throws {ApiError} - If the api returns errors
113
113
  * @throws {ValidationError} - If the submitted data isn't valid
114
- * @private
114
+ * @protected
115
115
  */
116
116
  _update () {
117
117
  this._updateProperties();
@@ -37,7 +37,7 @@ import SimpleResourceProxy from '../../proxy/SimpleResourceProxy';
37
37
  import Injectable from '../../traits/Injectable';
38
38
  import { fnv32b } from '../../utils/hash';
39
39
  import { isParentOf, mix } from '../../utils/reflection';
40
- import { makeCancelable } from '../../utils/helpers';
40
+ import { clone, makeCancelable } from '../../utils/helpers';
41
41
 
42
42
  function unique (input) {
43
43
  return input.filter((v, i) => input.findIndex(vv => vv === v) === i);
@@ -62,7 +62,7 @@ export default class ResourceBase extends mix(null, Injectable) {
62
62
  this.api = api;
63
63
 
64
64
  // De-reference
65
- data = { ...data };
65
+ data = clone(data);
66
66
 
67
67
  // Normalize keys to snake_case
68
68
  // Fix data types
@@ -75,7 +75,7 @@ export default class ResourceBase extends mix(null, Injectable) {
75
75
  continue;
76
76
  }
77
77
 
78
- data[newKey] = this.constructor._guessType(newKey, data[key]);
78
+ data[newKey] = this._guessType(newKey, data[key]);
79
79
 
80
80
  if (newKey !== key) {
81
81
  delete data[key];
@@ -295,7 +295,7 @@ export default class ResourceBase extends mix(null, Injectable) {
295
295
 
296
296
  if (!this.constructor.protectedFields.includes(key) && !this.constructor.readonly) {
297
297
  desc.set = val => {
298
- this._properties[key] = ResourceBase._guessType(key, val);
298
+ this._properties[key] = this._guessType(key, val);
299
299
  delete this._url; // Clears url cache
300
300
  };
301
301
  }
@@ -309,10 +309,10 @@ export default class ResourceBase extends mix(null, Injectable) {
309
309
  * Guess type based on property name
310
310
  * @param {string} name - Field name
311
311
  * @param {*} value - Field Value
312
- * @private
312
+ * @protected
313
313
  * @returns {*} - Original or converted value
314
314
  */
315
- static _guessType (name, value) {
315
+ _guessType (name, value) {
316
316
  if (name.endsWith('_at') || name.startsWith('date_')) {
317
317
  return typeof value === 'string' ? new Date(value.replace(' ', 'T')) : value;
318
318
  } else if (/(_|^)id$/.test(name) && value !== null) {
@@ -51,6 +51,7 @@ export Language from './Language';
51
51
  export Layer from './Layer';
52
52
  export Mapstyle from './Mapstyle';
53
53
  export MapstyleSet from './MapstyleSet';
54
+ export Message from './Message';
54
55
  export Notification from './Notification';
55
56
  export Organisation from './Organisation';
56
57
  export Permission from './Permission';
@@ -67,15 +67,6 @@ export default class FileDriver extends DataStoreContract {
67
67
  this._path = value;
68
68
  }
69
69
 
70
- get realPath () {
71
- if (this.path.startsWith('/')) {
72
- return this.path;
73
- }
74
-
75
- // eslint-disable-next-line no-undef
76
- return this._fs.realpathSync(this.path);
77
- }
78
-
79
70
  /**
80
71
  * @inheritDoc
81
72
  */
@@ -142,7 +133,7 @@ export default class FileDriver extends DataStoreContract {
142
133
  let data;
143
134
 
144
135
  try {
145
- data = this._fs.readFileSync(this.realPath).toString();
136
+ data = this._fs.readFileSync(this.path).toString();
146
137
  } catch (e) {
147
138
  data = '{}';
148
139
  }
@@ -161,7 +152,7 @@ export default class FileDriver extends DataStoreContract {
161
152
  */
162
153
  _write (obj) {
163
154
  const data = JSON.stringify(obj);
164
- const fd = this._fs.openSync(this.realPath, 'w');
155
+ const fd = this._fs.openSync(this.path, 'w');
165
156
 
166
157
  this._fs.writeSync(fd, data);
167
158
  this._fs.closeSync(fd);
@@ -154,3 +154,25 @@ export function serializeUTCDate (date) {
154
154
 
155
155
  return out;
156
156
  }
157
+
158
+ export function clone (input, clonePrivate = true) {
159
+ const _clone = value => clone(value, clonePrivate);
160
+
161
+ if (typeof input !== 'object' || input === null) {
162
+ return input;
163
+ } else if (Array.isArray(input)) {
164
+ return input.map(_clone);
165
+ }
166
+
167
+ const output = {};
168
+
169
+ for (const key of Object.keys(input)) {
170
+ if (!clonePrivate && key.startsWith('_')) {
171
+ continue;
172
+ }
173
+
174
+ output[key] = _clone(input[key]);
175
+ }
176
+
177
+ return output;
178
+ }