@cocreate/api 1.19.1 → 1.19.2

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,10 @@
1
+ ## [1.19.2](https://github.com/CoCreate-app/CoCreate-api/compare/v1.19.1...v1.19.2) (2024-02-02)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * handling of methods ([9807e74](https://github.com/CoCreate-app/CoCreate-api/commit/9807e74983c28482e280edd003ad371344758efa))
7
+
1
8
  ## [1.19.1](https://github.com/CoCreate-app/CoCreate-api/compare/v1.19.0...v1.19.1) (2024-01-30)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/api",
3
- "version": "1.19.1",
3
+ "version": "1.19.2",
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",
package/src/client.js CHANGED
@@ -25,6 +25,10 @@ const CoCreateApi = {
25
25
  });
26
26
 
27
27
  for (const endPoint of Object.keys(endPoints)) {
28
+ socket.listen(name + '.' + endPoint, (data) => {
29
+ self.__response(name, data);
30
+ });
31
+
28
32
  let functions = endPoints[endPoint]
29
33
  if (typeof functions.request !== 'function') {
30
34
  functions.request = self.__request;
@@ -33,13 +37,12 @@ const CoCreateApi = {
33
37
  name: endPoint,
34
38
  endEvent: endPoint,
35
39
  callback: (data) => {
36
- let params
37
40
  const element = data.element
38
- let form = element.closest('form') || document
41
+ let form = data.form || document
39
42
  const selector = `[${name}^="${endPoint}."]`
40
43
  let el = form.querySelector(selector)
41
44
  if (!el) return
42
- params = self.parseParams({ name, element: el })
45
+ let params = self.parseParams({ name, element: el })
43
46
  if (params) {
44
47
  functions.request({ name, ...params, element, form, type: 'action' })
45
48
  }
@@ -97,33 +100,47 @@ const CoCreateApi = {
97
100
  }
98
101
  },
99
102
 
100
- __request: async function ({ name, endPoint, element }) {
101
- let form = element.closest('form');
102
- let data = await this.getData({ name, endPoint, form });
103
- this.send(name, endPoint, data[endPoint]);
103
+
104
+ __request: async function (object) {
105
+ if (object.type !== 'action')
106
+ return
107
+ if (!object.form)
108
+ object.form = element.closest('form');
109
+ let data = await CoCreateApi.getData(object);
110
+ CoCreateApi.send(object.name, object.endPoint, data);
104
111
  },
105
112
 
106
113
  __response: function (name, data) {
107
- const { endPoint, response } = data;
114
+ const endPoint = data.method.substring(name.length + 1);
108
115
  const component = this.components[name];
109
116
  const functions = component.endPoints[endPoint]
110
117
  if (functions.listen !== false) {
111
118
  if (typeof functions.response === 'function') {
112
- functions.response(response);
113
- } else
114
- this.setData({ name, endPoint, data })
119
+ functions.response(data[name]);
120
+ } else if (data.error) {
121
+ render({
122
+ selector: `[template*='${name}']`,
123
+ data: [{
124
+ type: name,
125
+ status: 'failed',
126
+ message: data.error
127
+ }]
128
+ });
129
+ } else {
130
+ CoCreateApi.setData({ name, endPoint, data })
115
131
 
116
- document.dispatchEvent(new CustomEvent(endPoint, {
117
- detail: {
118
- data: response
119
- }
120
- }));
132
+ document.dispatchEvent(new CustomEvent(endPoint, {
133
+ detail: {
134
+ data: data[name]
135
+ }
136
+ }));
137
+ }
121
138
  }
122
139
  },
123
140
 
124
141
  send: function (name, endPoint, data) {
125
142
  let method = name + '.' + endPoint
126
- socket.send({ method, endPoint, data, broadcastBrowser: false });
143
+ socket.send({ method, endPoint, [name]: data, broadcastBrowser: false, status: 'await' });
127
144
  },
128
145
 
129
146
  parseParams: function ({ name, endPoint, element }) {
@@ -131,11 +148,16 @@ const CoCreateApi = {
131
148
  if (!attribute)
132
149
  return false
133
150
 
134
- let dotNotation = attribute
135
- let params = attribute.split('.')
136
- endPoint = params[0]
137
- let key = params[1]
138
- return { endPoint, key, dotNotation, params }
151
+ let endPoints = this.components[name].endPoints
152
+ for (let k of Object.keys(endPoints)) {
153
+ if (attribute.startsWith(k)) {
154
+ endPoint = k
155
+ break;
156
+ }
157
+ }
158
+
159
+ let key = attribute.substring(endPoint.length + 1);
160
+ return { endPoint, key, dotNotation: attribute }
139
161
 
140
162
  },
141
163
 
@@ -150,9 +172,10 @@ const CoCreateApi = {
150
172
  elements = [element]
151
173
  for (let el of elements) {
152
174
  if (!el || el.closest('[template]')) continue
153
- let attribute = el.getAttribute(name)
154
- if (attribute)
155
- data[attribute] = await el.getValue()
175
+ let params = this.parseParams({ name, element: el })
176
+ if (params.key) {
177
+ data[params.key] = await el.getValue()
178
+ }
156
179
  }
157
180
 
158
181
  return dotNotationToObject(data);