@cocreate/api 1.10.13 → 1.10.14

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/client.js CHANGED
@@ -1,190 +1,190 @@
1
- /*globals CustomEvent, config*/
2
- import {getAttributes, getValueFromObject, dotNotationToObject} from "@cocreate/utils";
3
- import observer from "@cocreate/observer";
4
- import socket from "@cocreate/socket-client";
5
- import action from '@cocreate/actions';
6
- import render from '@cocreate/render';
7
- import '@cocreate/element-prototype';
8
-
9
- const CoCreateApi = {
10
- components: {},
11
-
12
- init: function({name, endPoints, options}) {
13
- this.register({name, endPoints, options});
14
- if (options.socket !== false && !socket.sockets.size)
15
- socket.create({prefix: 'api'});
16
- },
17
-
18
- register: function({name, endPoints, options}) {
19
- const self = this;
20
- if (typeof this.components[name] === 'undefined') {
21
- this.components[name] = {name, endPoints, options};
22
-
23
- socket.listen(name, (data) => {
24
- self.__response(name, data);
25
- });
26
-
27
- for (const endPoint of Object.keys(endPoints)) {
28
- let functions = endPoints[endPoint]
29
- if (typeof functions.request !== 'function') {
30
- functions.request = self.__request;
31
- }
32
- action.init({
33
- name: endPoint,
34
- endEvent: endPoint,
35
- callback: (element) => {
36
- let params
37
- let form = element.closest('form') || document
38
- const selector = `[${name}^="${endPoint}."]`
39
- let el = form.querySelector(selector)
40
- if (!el) return
41
- params = self.parseParams({name, element: el})
42
- if (params) {
43
- functions.request({name, ...params, element, form, type: 'action'})
44
- }
45
- },
46
- });
47
- }
48
-
49
- const inputEvent = (element) => {
50
- const self = this
51
- element.addEventListener('input', (e) => {
52
- if (element.hasAttribute(name)) {
53
- let params = self.parseParams({name, element})
54
- if (params) {
55
- if (!e.detail || e.detail && e.detail.skip != true) {
56
- endPoints[params.endPoint].request({name, ...params, element, type: 'input'})
57
- }
58
- }
59
- }
60
- });
61
- }
62
-
63
- let elements = document.querySelectorAll(`[${name}]`)
64
- for (let element of elements) {
65
- let params = this.parseParams({name, element})
66
- if (params) {
67
- endPoints[params.endPoint].request({name, ...params, element, type:'onload'})
68
- inputEvent(element)
69
- }
70
- }
71
-
72
- observer.init({
73
- name: `${name}NodeObserver`,
74
- observe: ['addedNodes'],
75
- target: `[${name}]`,
76
- callback: function(mutation) {
77
- let params = self.parseParams({name, element: mutation.target})
78
- if (params) {
79
- endPoints[params.endPoint].request({name, ...params, element: mutation.target, type: 'nodeObserver'});
80
- inputEvent(mutation.target)
81
- }
82
- }
83
- });
84
-
85
- observer.init({
86
- name: `${name}AttributeObserver`,
87
- observe: ['attributes'],
88
- attributeName: [name],
89
- callback: function(mutation) {
90
- let params = self.parseParams({name, element: mutation.target})
91
- if (params) {
92
- endPoints[params.endPoint].request({name, ...params, element: mutation.target, type: 'attributeObserver'});
93
- }
94
- }
95
- });
96
- }
97
- },
98
-
99
- __request: function({name, endPoint, element}) {
100
- let form = element.closest('form');
101
- let data = this.getData({name, endPoint, form});
102
- this.send(name, endPoint, data[endPoint]);
103
- },
104
-
105
- __response: function(name, data) {
106
- const {endPoint, response} = data;
107
- const component = this.components[name];
108
- const functions = component.endPoints[endPoint]
109
- if (functions.listen !== false) {
110
- if (typeof functions.response === 'function') {
111
- functions.response(response);
112
- } else
113
- this.setData({name, endPoint, data})
114
-
115
- document.dispatchEvent(new CustomEvent(endPoint, {
116
- detail: {
117
- data: response
118
- }
119
- }));
120
- }
121
- },
122
-
123
- send: function(name, endPoint, data) {
124
- socket.send(name, {endPoint, data, broadcastBrowser: false});
125
- },
126
-
127
- parseParams: function({name, endPoint, element}) {
128
- let attribute = element.getAttribute(name)
129
- if (!attribute)
130
- return false
131
-
132
- let dotNotation = attribute
133
- let params = attribute.split('.')
134
- endPoint = params[0]
135
- let key = params[1]
136
- return {endPoint, key, dotNotation, params}
137
-
138
- },
139
-
140
-
141
- getData: function({name, endPoint, element, form}) {
142
- const data = {}
143
- const selector = `[${name}^="${endPoint}."]`
144
- let elements
145
- if (form)
146
- elements = form.querySelectorAll(selector);
147
- if (!elements || elements.length == 0)
148
- elements = [element]
149
- for (let el of elements) {
150
- if (!el || el.closest('[template]')) continue
151
- let attribute = el.getAttribute(name)
152
- if (attribute)
153
- data[attribute] = el.getValue()
154
- }
155
-
156
- return dotNotationToObject(data);
157
- },
158
-
159
- setData: function({name, endPoint, data, form}) {
160
- const selector = `[${name}^="${endPoint}."]`
161
- if (!form)
162
- form = document;
163
- let elements = form.querySelectorAll(selector);
164
- if (!elements || elements.length == 0) return
165
-
166
- for (let el of elements) {
167
- let attribute = el.getAttribute(name)
168
- if (attribute) {
169
- if (el.hasAttribute('actions')) continue;
170
- let templateid = el.getAttribute('template_id')
171
- if (templateid) {
172
- let items = document.querySelectorAll(`[templateid="${templateid}"]`)
173
- for (let item of items)
174
- item.remove()
175
- render.data({
176
- selector: `[template="${templateid}"]`,
177
- data
178
- });
179
- } else {
180
- let value = getValueFromObject(data, attribute);
181
- el.setValue(value);
182
- }
183
- }
184
- }
185
-
186
- }
187
-
188
- };
189
-
1
+ /*globals CustomEvent, config*/
2
+ import {getAttributes, getValueFromObject, dotNotationToObject} from "@cocreate/utils";
3
+ import observer from "@cocreate/observer";
4
+ import socket from "@cocreate/socket-client";
5
+ import action from '@cocreate/actions';
6
+ import render from '@cocreate/render';
7
+ import '@cocreate/element-prototype';
8
+
9
+ const CoCreateApi = {
10
+ components: {},
11
+
12
+ init: function({name, endPoints, options}) {
13
+ this.register({name, endPoints, options});
14
+ if (options.socket !== false && !socket.sockets.size)
15
+ socket.create({prefix: 'api'});
16
+ },
17
+
18
+ register: function({name, endPoints, options}) {
19
+ const self = this;
20
+ if (typeof this.components[name] === 'undefined') {
21
+ this.components[name] = {name, endPoints, options};
22
+
23
+ socket.listen(name, (data) => {
24
+ self.__response(name, data);
25
+ });
26
+
27
+ for (const endPoint of Object.keys(endPoints)) {
28
+ let functions = endPoints[endPoint]
29
+ if (typeof functions.request !== 'function') {
30
+ functions.request = self.__request;
31
+ }
32
+ action.init({
33
+ name: endPoint,
34
+ endEvent: endPoint,
35
+ callback: (element) => {
36
+ let params
37
+ let form = element.closest('form') || document
38
+ const selector = `[${name}^="${endPoint}."]`
39
+ let el = form.querySelector(selector)
40
+ if (!el) return
41
+ params = self.parseParams({name, element: el})
42
+ if (params) {
43
+ functions.request({name, ...params, element, form, type: 'action'})
44
+ }
45
+ },
46
+ });
47
+ }
48
+
49
+ const inputEvent = (element) => {
50
+ const self = this
51
+ element.addEventListener('input', (e) => {
52
+ if (element.hasAttribute(name)) {
53
+ let params = self.parseParams({name, element})
54
+ if (params) {
55
+ if (!e.detail || e.detail && e.detail.skip != true) {
56
+ endPoints[params.endPoint].request({name, ...params, element, type: 'input'})
57
+ }
58
+ }
59
+ }
60
+ });
61
+ }
62
+
63
+ let elements = document.querySelectorAll(`[${name}]`)
64
+ for (let element of elements) {
65
+ let params = this.parseParams({name, element})
66
+ if (params) {
67
+ endPoints[params.endPoint].request({name, ...params, element, type:'onload'})
68
+ inputEvent(element)
69
+ }
70
+ }
71
+
72
+ observer.init({
73
+ name: `${name}NodeObserver`,
74
+ observe: ['addedNodes'],
75
+ target: `[${name}]`,
76
+ callback: function(mutation) {
77
+ let params = self.parseParams({name, element: mutation.target})
78
+ if (params) {
79
+ endPoints[params.endPoint].request({name, ...params, element: mutation.target, type: 'nodeObserver'});
80
+ inputEvent(mutation.target)
81
+ }
82
+ }
83
+ });
84
+
85
+ observer.init({
86
+ name: `${name}AttributeObserver`,
87
+ observe: ['attributes'],
88
+ attributeName: [name],
89
+ callback: function(mutation) {
90
+ let params = self.parseParams({name, element: mutation.target})
91
+ if (params) {
92
+ endPoints[params.endPoint].request({name, ...params, element: mutation.target, type: 'attributeObserver'});
93
+ }
94
+ }
95
+ });
96
+ }
97
+ },
98
+
99
+ __request: function({name, endPoint, element}) {
100
+ let form = element.closest('form');
101
+ let data = this.getData({name, endPoint, form});
102
+ this.send(name, endPoint, data[endPoint]);
103
+ },
104
+
105
+ __response: function(name, data) {
106
+ const {endPoint, response} = data;
107
+ const component = this.components[name];
108
+ const functions = component.endPoints[endPoint]
109
+ if (functions.listen !== false) {
110
+ if (typeof functions.response === 'function') {
111
+ functions.response(response);
112
+ } else
113
+ this.setData({name, endPoint, data})
114
+
115
+ document.dispatchEvent(new CustomEvent(endPoint, {
116
+ detail: {
117
+ data: response
118
+ }
119
+ }));
120
+ }
121
+ },
122
+
123
+ send: function(name, endPoint, data) {
124
+ socket.send(name, {endPoint, data, broadcastBrowser: false});
125
+ },
126
+
127
+ parseParams: function({name, endPoint, element}) {
128
+ let attribute = element.getAttribute(name)
129
+ if (!attribute)
130
+ return false
131
+
132
+ let dotNotation = attribute
133
+ let params = attribute.split('.')
134
+ endPoint = params[0]
135
+ let key = params[1]
136
+ return {endPoint, key, dotNotation, params}
137
+
138
+ },
139
+
140
+
141
+ getData: function({name, endPoint, element, form}) {
142
+ const data = {}
143
+ const selector = `[${name}^="${endPoint}."]`
144
+ let elements
145
+ if (form)
146
+ elements = form.querySelectorAll(selector);
147
+ if (!elements || elements.length == 0)
148
+ elements = [element]
149
+ for (let el of elements) {
150
+ if (!el || el.closest('[template]')) continue
151
+ let attribute = el.getAttribute(name)
152
+ if (attribute)
153
+ data[attribute] = el.getValue()
154
+ }
155
+
156
+ return dotNotationToObject(data);
157
+ },
158
+
159
+ setData: function({name, endPoint, data, form}) {
160
+ const selector = `[${name}^="${endPoint}."]`
161
+ if (!form)
162
+ form = document;
163
+ let elements = form.querySelectorAll(selector);
164
+ if (!elements || elements.length == 0) return
165
+
166
+ for (let el of elements) {
167
+ let attribute = el.getAttribute(name)
168
+ if (attribute) {
169
+ if (el.hasAttribute('actions')) continue;
170
+ let templateid = el.getAttribute('template_id')
171
+ if (templateid) {
172
+ let items = document.querySelectorAll(`[templateid="${templateid}"]`)
173
+ for (let item of items)
174
+ item.remove()
175
+ render.data({
176
+ selector: `[template="${templateid}"]`,
177
+ data
178
+ });
179
+ } else {
180
+ let value = getValueFromObject(data, attribute);
181
+ el.setValue(value);
182
+ }
183
+ }
184
+ }
185
+
186
+ }
187
+
188
+ };
189
+
190
190
  export default CoCreateApi;
package/src/index.js CHANGED
@@ -1,14 +1,14 @@
1
- (function (root, factory) {
2
- if (typeof define === 'function' && define.amd) {
3
- define(["./client"], function(CoCreateApi) {
4
- return factory(CoCreateApi)
5
- });
6
- } else if (typeof module === 'object' && module.exports) {
7
- const CoCreateApi = require("./server.js")
8
- module.exports = factory(CoCreateApi);
9
- } else {
10
- root.returnExports = factory(root["./client.js"]);
11
- }
12
- }(typeof self !== 'undefined' ? self : this, function (CoCreateApi) {
13
- return CoCreateApi;
1
+ (function (root, factory) {
2
+ if (typeof define === 'function' && define.amd) {
3
+ define(["./client"], function(CoCreateApi) {
4
+ return factory(CoCreateApi)
5
+ });
6
+ } else if (typeof module === 'object' && module.exports) {
7
+ const CoCreateApi = require("./server.js")
8
+ module.exports = factory(CoCreateApi);
9
+ } else {
10
+ root.returnExports = factory(root["./client.js"]);
11
+ }
12
+ }(typeof self !== 'undefined' ? self : this, function (CoCreateApi) {
13
+ return CoCreateApi;
14
14
  }));
package/src/server.js CHANGED
@@ -1,109 +1,109 @@
1
- const CRUD = require('@cocreate/crud-client')
2
- const socketClient = require('@cocreate/socket-client')
3
- let socket = new socketClient("ws");
4
-
5
- crud.setSocket(socket);
6
-
7
- var api = ( ()=> {
8
- return {
9
- send_response: (wsManager, socket, obj, send_response) => {
10
- wsManager.send(socket, send_response, obj)
11
- },
12
-
13
- handleError: (wsManager, socket, action, error, component) => {
14
- const response = {
15
- 'object': 'error',
16
- 'data':error || error.response || error.response.data || error.response.body || error.message || error,
17
- };
18
- wsManager.send(socket, component, { action, response })
19
- },
20
-
21
- getOrg: async (config, component) => {
22
-
23
- socket.create({
24
- namespace: config["organization_id"],
25
- room: null,
26
- host: config["host"]
27
- })
28
-
29
- let org = await crud.readDocument({
30
- collection: "organizations",
31
- key: config["key"],
32
- organization_id: config["organization_id"],
33
- document: {
34
- _id: config["organization_id"]
35
- }
36
-
37
- });
38
-
39
- if (!org || !org.document && !org.document[0]) {
40
- console.log(component," Error GET ORG in : ",e);
41
- return false;
42
- }
43
-
44
- return org.document[0];
45
- },
46
-
47
- getOrgInRoutesbyHostname : async (config, hostname) => {
48
- var socket_config = {
49
- "config": {
50
- "key": config["config"]["key"],
51
- "organization_id": config["config"]["organization_id"],
52
- },
53
- "prefix": "ws",
54
- "host": "server.cocreate.app:8088"
55
- };
56
-
57
- socket.create({
58
- namespace: socket_config.config.organization_id,
59
- room: null,
60
- host: socket_config.host
61
- })
62
-
63
- let data2 = await crud.readDocument({
64
- collection: "organizations",
65
- filter: {
66
- query: [{
67
- name: 'domains',
68
- operator: "$in",
69
- value: [hostname]
70
- }],
71
- },
72
- key: config["config"]["key"],
73
- organization_id: config["config"]["organization_id"]
74
- });
75
-
76
- var org = data2.document[0]
77
-
78
- var socket_config = {
79
- "config": {
80
- "key": org["key"],
81
- "organization_id": org["_id"].toString(),
82
- },
83
- "prefix": "ws",
84
- "host": "server.cocreate.app:8088"
85
- }
86
-
87
- //other connection
88
- socket.create({
89
- namespace: socket_config.config.organization_id,
90
- room: null,
91
- host: socket_config.host
92
- })
93
-
94
- let myOrg = await crud.readDocument({
95
- collection: "organizations",
96
- key: org["key"],
97
- organization_id: org["_id"],
98
- document: {
99
- _id: org["_id"]
100
- }
101
- });
102
- let result = {'row':myOrg,'socket_config':socket_config};
103
- return result;
104
- }
105
-
106
- }
107
- })();
108
-
1
+ const CRUD = require('@cocreate/crud-client')
2
+ const socketClient = require('@cocreate/socket-client')
3
+ let socket = new socketClient("ws");
4
+
5
+ crud.setSocket(socket);
6
+
7
+ var api = ( ()=> {
8
+ return {
9
+ send_response: (wsManager, socket, obj, send_response) => {
10
+ wsManager.send(socket, send_response, obj)
11
+ },
12
+
13
+ handleError: (wsManager, socket, action, error, component) => {
14
+ const response = {
15
+ 'object': 'error',
16
+ 'data':error || error.response || error.response.data || error.response.body || error.message || error,
17
+ };
18
+ wsManager.send(socket, component, { action, response })
19
+ },
20
+
21
+ getOrg: async (config, component) => {
22
+
23
+ socket.create({
24
+ namespace: config["organization_id"],
25
+ room: null,
26
+ host: config["host"]
27
+ })
28
+
29
+ let org = await crud.readDocument({
30
+ collection: "organizations",
31
+ key: config["key"],
32
+ organization_id: config["organization_id"],
33
+ document: {
34
+ _id: config["organization_id"]
35
+ }
36
+
37
+ });
38
+
39
+ if (!org || !org.document && !org.document[0]) {
40
+ console.log(component," Error GET ORG in : ",e);
41
+ return false;
42
+ }
43
+
44
+ return org.document[0];
45
+ },
46
+
47
+ getOrgInRoutesbyHostname : async (config, hostname) => {
48
+ var socket_config = {
49
+ "config": {
50
+ "key": config["config"]["key"],
51
+ "organization_id": config["config"]["organization_id"],
52
+ },
53
+ "prefix": "ws",
54
+ "host": "server.cocreate.app:8088"
55
+ };
56
+
57
+ socket.create({
58
+ namespace: socket_config.config.organization_id,
59
+ room: null,
60
+ host: socket_config.host
61
+ })
62
+
63
+ let data2 = await crud.readDocument({
64
+ collection: "organizations",
65
+ filter: {
66
+ query: [{
67
+ name: 'domains',
68
+ operator: "$in",
69
+ value: [hostname]
70
+ }],
71
+ },
72
+ key: config["config"]["key"],
73
+ organization_id: config["config"]["organization_id"]
74
+ });
75
+
76
+ var org = data2.document[0]
77
+
78
+ var socket_config = {
79
+ "config": {
80
+ "key": org["key"],
81
+ "organization_id": org["_id"].toString(),
82
+ },
83
+ "prefix": "ws",
84
+ "host": "server.cocreate.app:8088"
85
+ }
86
+
87
+ //other connection
88
+ socket.create({
89
+ namespace: socket_config.config.organization_id,
90
+ room: null,
91
+ host: socket_config.host
92
+ })
93
+
94
+ let myOrg = await crud.readDocument({
95
+ collection: "organizations",
96
+ key: org["key"],
97
+ organization_id: org["_id"],
98
+ document: {
99
+ _id: org["_id"]
100
+ }
101
+ });
102
+ let result = {'row':myOrg,'socket_config':socket_config};
103
+ return result;
104
+ }
105
+
106
+ }
107
+ })();
108
+
109
109
  module.exports = api;