@cocreate/lazy-loader 1.23.5 → 1.23.6

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,11 @@
1
+ ## [1.23.6](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.5...v1.23.6) (2025-09-01)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improve request handling and error reporting in executeEndpoint ([b452259](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/b452259fb28b97421badf7aeb21c24d99d5599c0))
7
+ * replace valideUrl with urlObject for URL handling consistency ([d53fe8a](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/d53fe8a78f2120805c1a9a60a72a5ac13bb361be))
8
+
1
9
  ## [1.23.5](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.4...v1.23.5) (2025-05-01)
2
10
 
3
11
 
package/package.json CHANGED
@@ -1,18 +1,10 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.23.5",
3
+ "version": "1.23.6",
4
4
  "description": "A simple lazy-loader component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "lazy-loader",
7
- "cocreate",
8
- "low-code-framework",
9
- "no-code-framework",
10
- "cocreatejs",
11
- "cocreatejs-component",
12
- "cocreate-framework",
13
- "no-code",
14
7
  "low-code",
15
- "collaborative-framework",
16
8
  "realtime",
17
9
  "realtime-framework",
18
10
  "collaboration",
package/src/server.js CHANGED
@@ -47,8 +47,8 @@ class CoCreateLazyLoader {
47
47
  async request(req, res) {
48
48
  try {
49
49
  // TODO: track usage
50
- const valideUrl = new URL(`http://${req.headers.host}${req.url}`);
51
- const hostname = valideUrl.hostname;
50
+ const urlObject = new URL(`http://${req.headers.host}${req.url}`);
51
+ const hostname = urlObject.hostname;
52
52
  let organization;
53
53
 
54
54
  try {
@@ -61,11 +61,11 @@ class CoCreateLazyLoader {
61
61
  res,
62
62
  this.crud,
63
63
  organization,
64
- valideUrl
64
+ urlObject
65
65
  );
66
66
  }
67
67
 
68
- if (valideUrl.pathname.startsWith("/webhooks/")) {
68
+ if (urlObject.pathname.startsWith("/webhooks/")) {
69
69
  let name = req.url.split("/")[2]; // Assuming URL structure is /webhooks/name/...
70
70
  if (this.modules[name]) {
71
71
  this.executeScriptWithTimeout(name, {
@@ -73,7 +73,7 @@ class CoCreateLazyLoader {
73
73
  res,
74
74
  host: hostname,
75
75
  organization,
76
- valideUrl,
76
+ urlObject,
77
77
  organization_id: organization._id
78
78
  });
79
79
  } else {
@@ -82,7 +82,7 @@ class CoCreateLazyLoader {
82
82
  res.end(JSON.stringify({ error: "Not found" }));
83
83
  }
84
84
  } else {
85
- this.files.send(req, res, this.crud, organization, valideUrl);
85
+ this.files.send(req, res, this.crud, organization, urlObject);
86
86
  }
87
87
  } catch (error) {
88
88
  res.writeHead(400, { "Content-Type": "text/plain" });
@@ -129,21 +129,47 @@ class CoCreateLazyLoader {
129
129
  headers = { ...headers, ...override.headers }; // Correct idea for merging
130
130
  }
131
131
 
132
- let body = formatRequestBody(data[name]);
132
+ // let body = formatRequestBody(data[name]);
133
133
 
134
- let options = { method, headers, body, timeout };
134
+ let formatType = data.formatType || "json";
135
+ const timeout = 10000; // Set default timeout in ms (e.g., 10 seconds)
136
+ let options = { method, headers, timeout };
135
137
 
136
- const response = await makeHttpRequest(url, options);
137
- data[name] = parseResponse(response);
138
+ // Only add body for methods that support it (not GET or HEAD)
139
+ if (!["GET", "HEAD"].includes(method)) {
140
+ let { body } = this.formatRequestBody(data[name], formatType);
141
+ options.body = body;
142
+ }
143
+ // For GET/HEAD, do not create or send a body; all params should be in the URL
144
+
145
+ const response = await this.makeHttpRequest(url, options);
146
+
147
+ // If the response is not ok, makeHttpRequest will throw and be caught below.
148
+ // If you want to include more info in the error, you can log or attach response details here.
149
+
150
+ data[name] = await response.json();
138
151
 
139
152
  this.wsManager.send(data);
140
153
  } catch (error) {
154
+ // Add more detail to the error for debugging 404s
141
155
  data.error = error.message;
156
+ if (error.response) {
157
+ data.status = error.response.status;
158
+ data.statusText = error.response.statusText;
159
+ data.responseData = error.response.data;
160
+ }
142
161
  if (data.req) {
143
162
  data.res.writeHead(400, {
144
- "Content-Type": "text/plain"
163
+ "Content-Type": "application/json"
145
164
  });
146
- data.res.end(`Lazyload Error: ${error.message}`);
165
+ data.res.end(
166
+ JSON.stringify({
167
+ error: data.error,
168
+ status: data.status,
169
+ statusText: data.statusText,
170
+ responseData: data.responseData
171
+ })
172
+ );
147
173
  }
148
174
  if (data.socket) {
149
175
  this.wsManager.send(data);
@@ -255,13 +281,12 @@ class CoCreateLazyLoader {
255
281
  * @throws {Error} If the request fails or returns a non-ok status.
256
282
  */
257
283
  async makeHttpRequest(url, options) {
258
- if (!this.server.AbortController) {
259
- console.log("makeHttpRequest test");
260
- return {};
284
+ let controller, timeoutId;
285
+ if (this.server.AbortController) {
286
+ controller = new this.server.AbortController();
287
+ timeoutId = setTimeout(() => controller.abort(), options.timeout);
288
+ options.signal = controller.signal;
261
289
  }
262
- const controller = new this.server.AbortController();
263
- const timeoutId = setTimeout(() => controller.abort(), options.timeout);
264
- options.signal = controller.signal;
265
290
 
266
291
  // Remove Content-Type header if there's no body (relevant for GET, DELETE etc.)
267
292
  if (
@@ -272,46 +297,32 @@ class CoCreateLazyLoader {
272
297
  delete options.headers["Content-Type"];
273
298
  }
274
299
 
300
+ const fetchFn = this.server.fetch || global.fetch;
301
+ if (typeof fetchFn !== "function") {
302
+ throw new Error("No fetch implementation available.");
303
+ }
304
+
275
305
  try {
276
- const response = await this.server.fetch(url, options);
277
- clearTimeout(timeoutId); // Request finished, clear timeout
306
+ const response = await fetchFn(url, options);
307
+ if (timeoutId) clearTimeout(timeoutId);
278
308
 
279
309
  if (!response.ok) {
280
- // status >= 200 && status < 300
310
+ const text = await response.text();
281
311
  const error = new Error(
282
312
  `HTTP error! Status: ${response.status} ${response.statusText}`
283
313
  );
284
- // Attach structured response info to the error
285
314
  error.response = {
286
315
  status: response.status,
287
316
  statusText: response.statusText,
288
317
  headers: Object.fromEntries(response.headers.entries()),
289
- data: parseResponse(response) // Include parsed error body
318
+ data: text
290
319
  };
291
320
  throw error;
292
321
  }
293
322
  return response;
294
323
  } catch (error) {
295
- clearTimeout(timeoutId);
296
- if (error.name === "AbortError") {
297
- console.error(
298
- `Request timed out after ${options.timeout}ms: ${options.method} ${url}`
299
- );
300
- throw new Error(
301
- `Request Timeout: API call exceeded ${options.timeout}ms`
302
- );
303
- }
304
-
305
- // If it already has response info (from !response.ok), rethrow it
306
- if (error.response) {
307
- throw error;
308
- }
309
- // Otherwise, wrap other errors (network, DNS, etc.)
310
- console.error(
311
- `Network/Request Error: ${options.method} ${url}`,
312
- error
313
- );
314
- throw new Error(`Network/Request Error: ${error.message}`);
324
+ if (timeoutId) clearTimeout(timeoutId);
325
+ throw error;
315
326
  }
316
327
  }
317
328