@karpeleslab/klbfw 0.2.12 → 0.2.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/index.d.ts CHANGED
@@ -32,6 +32,7 @@ declare function setCookie(name: string, value: string, expires?: Date | number,
32
32
  declare function rest(name: string, verb: string, params?: Record<string, any>, context?: Record<string, any>): Promise<any>;
33
33
  declare function rest_get(name: string, params?: Record<string, any>): Promise<any>; // Backward compatibility
34
34
  declare function restGet(name: string, params?: Record<string, any>): Promise<any>;
35
+ declare function restSSE(name: string, method: 'GET', params?: Record<string, any>, context?: Record<string, any>): EventSource;
35
36
 
36
37
  // Upload module types
37
38
  interface UploadOptions {
@@ -75,6 +76,7 @@ export {
75
76
  rest,
76
77
  rest_get,
77
78
  restGet,
79
+ restSSE,
78
80
  upload,
79
81
  getI18N,
80
82
  trimPrefix
package/index.js CHANGED
@@ -42,6 +42,7 @@ module.exports.setCookie = cookies.setCookie;
42
42
  module.exports.rest = rest.rest;
43
43
  module.exports.rest_get = rest.rest_get; // Backward compatibility
44
44
  module.exports.restGet = rest.restGet; // New camelCase name
45
+ module.exports.restSSE = rest.restSSE;
45
46
 
46
47
  // Upload module exports
47
48
  module.exports.upload = upload.upload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karpeleslab/klbfw",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "description": "Frontend Framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/rest.js CHANGED
@@ -153,9 +153,70 @@ const restGet = (name, params) => {
153
153
  });
154
154
  };
155
155
 
156
+ /**
157
+ * Creates a Server-Sent Events (SSE) connection to a REST API endpoint
158
+ * @param {string} name - API endpoint name
159
+ * @param {string} method - HTTP method (must be GET)
160
+ * @param {Object} params - Request parameters
161
+ * @param {Object} context - Context object with additional parameters
162
+ * @returns {EventSource} EventSource instance for the SSE connection
163
+ */
164
+ const restSSE = (name, method, params, context) => {
165
+ // EventSource only supports GET requests
166
+ if (method !== 'GET') {
167
+ throw new Error('EventSource only supports GET method');
168
+ }
169
+
170
+ // EventSource only works in browsers
171
+ if (typeof EventSource === 'undefined') {
172
+ throw new Error('EventSource is not supported in this environment');
173
+ }
174
+
175
+ if (!internal.checkSupport()) {
176
+ throw new Error('Environment not supported');
177
+ }
178
+
179
+ params = params || {};
180
+ context = context || {};
181
+
182
+ // Add timezone data if in browser
183
+ if (typeof window !== 'undefined') {
184
+ context['t'] = internal.getTimezoneData();
185
+ }
186
+
187
+ // Build URL with authentication and context
188
+ let callUrl = internal.buildRestUrl(name, true, context);
189
+
190
+ // Add params to the URL
191
+ if (params) {
192
+ const glue = callUrl.indexOf('?') === -1 ? '?' : '&';
193
+ if (typeof params === 'string') {
194
+ callUrl += glue + '_=' + encodeURIComponent(params);
195
+ } else {
196
+ callUrl += glue + '_=' + encodeURIComponent(JSON.stringify(params));
197
+ }
198
+ }
199
+
200
+ // Create and return EventSource instance
201
+ // Note: EventSource doesn't support custom headers directly,
202
+ // but authentication is handled via URL parameters or cookies
203
+ const eventSource = new EventSource(callUrl, {
204
+ withCredentials: true
205
+ });
206
+
207
+ // Handle errors and server-side closures
208
+ eventSource.onerror = (error) => {
209
+ console.error('EventSource error:', error);
210
+ eventSource.close();
211
+ };
212
+
213
+ return eventSource;
214
+ };
215
+
156
216
  // Export new camelCase API
157
217
  module.exports.rest = rest;
158
218
  module.exports.restGet = restGet;
219
+ module.exports.restSSE = restSSE;
159
220
 
160
221
  // Backward compatibility
161
222
  module.exports.rest_get = restGet;