@cocreate/api 1.6.12 → 1.7.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [1.7.1](https://github.com/CoCreate-app/CoCreate-api/compare/v1.7.0...v1.7.1) (2023-02-12)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * setData missng data param ([ff9c158](https://github.com/CoCreate-app/CoCreate-api/commit/ff9c1586ea2cabe0f8bb1428b4046afa20bedfcd))
7
+
8
+ # [1.7.0](https://github.com/CoCreate-app/CoCreate-api/compare/v1.6.12...v1.7.0) (2023-02-11)
9
+
10
+
11
+ ### Features
12
+
13
+ * getData and setData functions, added @cocreate/utils functions getValueFromObject, dotNotationToObject ([88e8170](https://github.com/CoCreate-app/CoCreate-api/commit/88e81703f3138c80b3060e07ccf428a19c09f381))
14
+ * spport realtime api request. defaults to true ([21f9621](https://github.com/CoCreate-app/CoCreate-api/commit/21f9621b6a4e724f4b21539521df38214183b871))
15
+
1
16
  ## [1.6.12](https://github.com/CoCreate-app/CoCreate-api/compare/v1.6.11...v1.6.12) (2023-02-01)
2
17
 
3
18
 
package/demo/client.js ADDED
@@ -0,0 +1,20 @@
1
+ import api from "@cocreate/api";
2
+
3
+ const CoCreateExample = {
4
+ name: "example",
5
+ actions: {
6
+ 'tokens.create':{},
7
+ create:{
8
+ realtime: true,
9
+ request: function (data) {
10
+ },
11
+ response: function (data) {
12
+
13
+ },
14
+ }
15
+ }
16
+ };
17
+
18
+ api.init(CoCreateExample);
19
+
20
+ export default CoCreateExample;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/api",
3
- "version": "1.6.12",
3
+ "version": "1.7.1",
4
4
  "description": "A simple api helper component in vanilla javascript used by JavaScript developers to create thirdparty api intergrations. CoCreate-api includes the client component and server side for api processing. Thirdparty apis can be accessible using HTML5 attributes and/or JavaScript API. ",
5
5
  "keywords": [
6
6
  "thirdparty-api-intergration",
@@ -67,6 +67,7 @@
67
67
  "@cocreate/element-prototype": "^1.4.10",
68
68
  "@cocreate/hosting": "^1.7.13",
69
69
  "@cocreate/render": "^1.21.6",
70
- "@cocreate/socket-client": "^1.15.6"
70
+ "@cocreate/socket-client": "^1.15.6",
71
+ "@cocreate/utils": "^1.18.3"
71
72
  }
72
73
  }
package/src/client.js CHANGED
@@ -1,28 +1,46 @@
1
1
  /*globals CustomEvent, config*/
2
+ import { getValueFromObject, dotNotationToObject} from "@cocreate/utils";
2
3
  import socket from "@cocreate/socket-client";
3
4
  import CoCreateAction from '@cocreate/actions';
4
5
  import CoCreateRender from '@cocreate/render';
5
6
  import '@cocreate/element-prototype';
6
7
 
7
8
  const CoCreateApi = {
8
- components: { },
9
+ components: {},
9
10
 
10
- init: function({name, component}) {
11
- this.register(name || component.name, component);
11
+ init: function({name, actions, realtime}) {
12
+ this.register({name, actions});
12
13
  if (!socket.sockets.size)
13
14
  socket.create({prefix: 'api'});
15
+ document.addEventListener('input', (e)=> {
16
+ let action = e.target.getAttribute(name)
17
+ if (action) {
18
+ action = action.split('.', 1)
19
+ action = action[0]
20
+ if (actions[action].realtime !== false) {
21
+ if (realtime !== false && actions[action].realtime !== undefined) {
22
+ let functions = actions[action]
23
+ if (typeof functions.request === 'function') {
24
+ functions.request(name, action, e.target)
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ })
14
31
  },
15
32
 
16
- register: function(name, component) {
33
+ register: function({name, actions}) {
17
34
  const self = this;
18
35
  if (typeof this.components[name] === 'undefined') {
19
- this.components[name] = component;
36
+ this.components[name] = {name, actions};
20
37
 
21
38
  socket.listen(name, (data) => {
22
39
  self.__response(name, data);
23
40
  });
24
41
 
25
- for (const [action, functions] of Object.entries(component['actions'])){
42
+ for (const action of Object.keys(actions)) {
43
+ let functions = actions[action]
26
44
  if (typeof functions.request !== 'function') {
27
45
  functions.request = self.__request;
28
46
  }
@@ -30,22 +48,22 @@ const CoCreateApi = {
30
48
  name: action,
31
49
  endEvent: action,
32
50
  callback: (element) => {
33
- functions.request(component.name, action, element);
51
+ functions.request(name, action, element);
34
52
  },
35
53
  });
36
54
  }
37
55
  }
38
56
  },
39
57
 
40
- __request: function(id, action, element) {
41
- const form = element.closest("form") || document;
42
- let data = CoCreateApi.getFormData(id, action, form);
43
- CoCreateApi.send(id, action, data);
58
+ __request: function(name, action, element) {
59
+ let form = element.closest('form');
60
+ let data = this.getData(name, action, form);
61
+ this.send(name, action, data[action]);
44
62
  },
45
63
 
46
- __response: function(id, data) {
64
+ __response: function(name, data) {
47
65
  const {action, response} = data;
48
- const component = this.components[id];
66
+ const component = this.components[name];
49
67
  const functions = component.actions[action]
50
68
  if (typeof functions.response === 'function') {
51
69
  functions.response(response);
@@ -58,84 +76,10 @@ const CoCreateApi = {
58
76
  data: response
59
77
  }
60
78
  }));
61
- },
62
-
63
- getFormData: function(id, action, form){
64
- const mainAttr = id;
65
- const self = this;
66
- const elements = form.querySelectorAll(`[${mainAttr}^="${action}."]`);
67
-
68
- let data = {};
69
- elements.forEach(element => {
70
- let name = element.getAttribute(mainAttr);
71
- if (!name) return;
72
-
73
- let array_name = element.getAttribute(mainAttr + "_array");
74
-
75
- let value;
76
- if (element.getValue)
77
- value = element.getValue();
78
-
79
- if (action) {
80
- let re = new RegExp(`^${action}.`, 'i');
81
- if (re.test(name)) {
82
- name = name.replace(re, "");
83
- } else {
84
- return;
85
- }
86
- }
87
-
88
- if (array_name) {
89
- if (!data[name]) {
90
- data[name] = [];
91
- }
92
- data[name].push(self.getFormData(id, array_name, element));
93
- } else if (value != null) {
94
- data[name] = value;
95
- }
96
- });
97
-
98
- let keys = Object.keys(data);
99
- let objectData = {};
100
- keys.forEach((k) => {
101
- if (k.split('.').length > 1) {
102
- let newData = self.__createObject(data[k], k);
103
- delete data[k];
104
-
105
- objectData = self.__mergeObject(objectData, newData);
106
- } else {
107
- objectData[k] = data[k];
108
- }
109
- });
110
- return objectData;
111
79
  },
112
-
113
- __mergeObject: function(target, source) {
114
- target = target || {};
115
- for (let key of Object.keys(source)) {
116
- if (source[key] instanceof Object) {
117
- Object.assign(source[key], this.__mergeObject(target[key], source[key]));
118
- }
119
- }
120
-
121
- Object.assign(target || {}, source);
122
- return target;
123
- },
124
-
125
- __createObject: function (data, path) {
126
- if (!path) return data;
127
-
128
- let keys = path.split('.');
129
- let newObject = data;
130
80
 
131
- for (var i = keys.length - 1; i >= 0; i--) {
132
- newObject = {[keys[i]]: newObject};
133
- }
134
- return newObject;
135
- },
136
-
137
- send: function(component, action, data) {
138
- socket.send(component, {action, data, broadcastBrowser: false});
81
+ send: function(name, action, data) {
82
+ socket.send(name, {action, data, broadcastBrowser: false});
139
83
  },
140
84
 
141
85
  render: function(action, data) {
@@ -145,6 +89,43 @@ const CoCreateApi = {
145
89
  });
146
90
  },
147
91
 
92
+ getData: function(attribute, action, form) {
93
+ let data = {}
94
+ const selector = `[${attribute}^="${action}."]`
95
+ if (!form)
96
+ form = document;
97
+ let elements = form.querySelectorAll(selector);
98
+ if (!elements || elements.length == 0) return
99
+
100
+ for (let el of elements) {
101
+ let name = el.getAttribute(attribute)
102
+ let value = el.getValue();
103
+ if (name)
104
+ data[name] = value
105
+ }
106
+ return dotNotationToObject(data);
107
+ },
108
+
109
+ setData: function(attribute, action, data, form) {
110
+ const selector = `[${attribute}^="${action}."]`
111
+ if (!form)
112
+ form = document;
113
+ let elements = form.querySelectorAll(selector);
114
+ if (!elements || elements.length == 0) return
115
+
116
+ for (let el of elements) {
117
+ let name = el.getAttribute(attribute)
118
+ const { isRead, isUpdate, isCrdt } = crud.getAttributes(el);
119
+ if (!name || isRead == "false" || isUpdate == "false" || isCrdt == "true") continue;
120
+
121
+ if (el.hasAttribute('actions')) continue;
122
+
123
+ let value = getValueFromObject({[action]: data}, name);
124
+ el.setValue(value);
125
+ }
126
+
127
+ }
128
+
148
129
  };
149
130
 
150
131
  export default CoCreateApi;