@mapcreator/api 2.5.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.5.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",
@@ -32,12 +32,36 @@
32
32
 
33
33
  import CrudBase from './base/CrudBase';
34
34
  import MessageVariant from './MessageVariant';
35
+ import { makeCancelable } from '../utils/helpers';
36
+ import { camel as camelCase, snake as snakeCase } from 'case';
35
37
 
36
38
  export default class Message extends CrudBase {
37
39
  static get resourceName () {
38
40
  return 'messages';
39
41
  }
40
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
+
41
65
  _guessType (name, value) {
42
66
  if (name === 'variants') {
43
67
  return Array.from(value).map(data => new MessageVariant(this.api, data));
@@ -45,4 +69,26 @@ export default class Message extends CrudBase {
45
69
 
46
70
  return super._guessType(name, value);
47
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
+ }
48
94
  }
@@ -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();