wirejs-resources 0.1.85-realtime → 0.1.87-realtime

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.
@@ -84,7 +84,7 @@ async function callApi(INTERNAL_API_URL, method, ...args) {
84
84
  for (const subscriber of subscribers) {
85
85
  if (subscriber.onclose) {
86
86
  try {
87
- subscriber.onclose();
87
+ subscriber.onclose('closed');
88
88
  }
89
89
  catch (error) {
90
90
  console.error('Error in subscriber onclose:', error);
@@ -101,6 +101,7 @@ async function callApi(INTERNAL_API_URL, method, ...args) {
101
101
  if (index !== -1) {
102
102
  subscribers.splice(index, 1);
103
103
  }
104
+ subscriber.onclose?.('unsubscribed');
104
105
  if (subscribers.length === 0) {
105
106
  ws.close();
106
107
  }
@@ -0,0 +1 @@
1
+ export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
@@ -0,0 +1,68 @@
1
+ async function callApi(INTERNAL_API_URL, method, ...args) {
2
+ function isNode() {
3
+ return typeof args[0]?.cookies?.getAll === 'function';
4
+ }
5
+ function apiUrl() {
6
+ if (isNode()) {
7
+ return INTERNAL_API_URL;
8
+ }
9
+ else {
10
+ return "/api";
11
+ }
12
+ }
13
+ let cookieHeader = {};
14
+ if (isNode()) {
15
+ const context = args[0];
16
+ const cookies = context.cookies.getAll();
17
+ cookieHeader = typeof cookies === 'object'
18
+ ? {
19
+ Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
20
+ }
21
+ : {};
22
+ }
23
+ const response = await fetch(apiUrl(), {
24
+ method: 'POST',
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ ...cookieHeader
28
+ },
29
+ body: JSON.stringify([{ method, args: [...args] }]),
30
+ });
31
+ const body = await response.json();
32
+ if (isNode()) {
33
+ const context = args[0];
34
+ for (const c of response.headers.getSetCookie()) {
35
+ const parts = c.split(';').map(p => p.trim());
36
+ const flags = parts.slice(1);
37
+ const [name, value] = parts[0].split('=').map(decodeURIComponent);
38
+ const httpOnly = flags.includes('HttpOnly');
39
+ const secure = flags.includes('Secure');
40
+ const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
41
+ context.cookies.set({
42
+ name,
43
+ value,
44
+ httpOnly,
45
+ secure,
46
+ maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
47
+ });
48
+ }
49
+ }
50
+ const error = body[0].error;
51
+ if (error) {
52
+ throw new Error(error);
53
+ }
54
+ const value = body[0].data;
55
+ return value;
56
+ }
57
+ ;
58
+ export function apiTree(INTERNAL_API_URL, path = []) {
59
+ return new Proxy(function () { }, {
60
+ apply(_target, _thisArg, args) {
61
+ return callApi(INTERNAL_API_URL, path, ...args);
62
+ },
63
+ get(_target, prop) {
64
+ return apiTree(INTERNAL_API_URL, [...path, prop]);
65
+ }
66
+ });
67
+ }
68
+ ;
@@ -0,0 +1 @@
1
+ export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
@@ -0,0 +1,68 @@
1
+ async function callApi(INTERNAL_API_URL, method, ...args) {
2
+ function isNode() {
3
+ return typeof args[0]?.cookies?.getAll === 'function';
4
+ }
5
+ function apiUrl() {
6
+ if (isNode()) {
7
+ return INTERNAL_API_URL;
8
+ }
9
+ else {
10
+ return "/api";
11
+ }
12
+ }
13
+ let cookieHeader = {};
14
+ if (isNode()) {
15
+ const context = args[0];
16
+ const cookies = context.cookies.getAll();
17
+ cookieHeader = typeof cookies === 'object'
18
+ ? {
19
+ Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
20
+ }
21
+ : {};
22
+ }
23
+ const response = await fetch(apiUrl(), {
24
+ method: 'POST',
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ ...cookieHeader
28
+ },
29
+ body: JSON.stringify([{ method, args: [...args] }]),
30
+ });
31
+ const body = await response.json();
32
+ if (isNode()) {
33
+ const context = args[0];
34
+ for (const c of response.headers.getSetCookie()) {
35
+ const parts = c.split(';').map(p => p.trim());
36
+ const flags = parts.slice(1);
37
+ const [name, value] = parts[0].split('=').map(decodeURIComponent);
38
+ const httpOnly = flags.includes('HttpOnly');
39
+ const secure = flags.includes('Secure');
40
+ const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
41
+ context.cookies.set({
42
+ name,
43
+ value,
44
+ httpOnly,
45
+ secure,
46
+ maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
47
+ });
48
+ }
49
+ }
50
+ const error = body[0].error;
51
+ if (error) {
52
+ throw new Error(error);
53
+ }
54
+ const value = body[0].data;
55
+ return value;
56
+ }
57
+ ;
58
+ export function apiTree(INTERNAL_API_URL, path = []) {
59
+ return new Proxy(function () { }, {
60
+ apply(_target, _thisArg, args) {
61
+ return callApi(INTERNAL_API_URL, path, ...args);
62
+ },
63
+ get(_target, prop) {
64
+ return apiTree(INTERNAL_API_URL, [...path, prop]);
65
+ }
66
+ });
67
+ }
68
+ ;
@@ -8,7 +8,7 @@ export type MessageStream<T = any> = {
8
8
  export type MessageStreamSubscriber<T = any> = {
9
9
  onopen?: () => void;
10
10
  onmessage: (data: T) => void;
11
- onclose?: () => void;
11
+ onclose?: (reason: 'unsubscribed' | 'closed' | 'error') => void;
12
12
  };
13
13
  export declare class RealtimeService<T = any> extends Resource {
14
14
  #private;
@@ -0,0 +1 @@
1
+ export declare function prebuildApi(): Promise<void>;
@@ -0,0 +1,27 @@
1
+ import process from 'process';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ export async function prebuildApi() {
5
+ const CWD = process.cwd();
6
+ let API_URL = '/api';
7
+ const indexModule = await import(path.join(CWD, 'index.js'));
8
+ try {
9
+ const backendConfigModule = await import(path.join(CWD, 'config.js'));
10
+ const backendConfig = backendConfigModule.default;
11
+ console.log("backend config found", backendConfig);
12
+ if (backendConfig.apiUrl) {
13
+ API_URL = backendConfig.apiUrl;
14
+ }
15
+ }
16
+ catch {
17
+ console.log("No backend API config found.");
18
+ }
19
+ const apiCode = Object.keys(indexModule)
20
+ .map(k => `export const ${k} = apiTree(INTERNAL_API_URL, ${JSON.stringify([k])});`)
21
+ .join('\n');
22
+ const baseClient = [
23
+ `import { apiTree } from "wirejs-resources/hosting/client.js";`,
24
+ `const INTERNAL_API_URL = ${JSON.stringify(API_URL)};`,
25
+ ].join('\n');
26
+ await fs.promises.writeFile(path.join(CWD, 'index.client.js'), [baseClient, apiCode].join('\n\n'));
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.85-realtime",
3
+ "version": "0.1.87-realtime",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",