@protobase/client 0.4.1 → 0.5.0
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/dist/assistant.d.ts +6 -0
- package/dist/assistant.js +20 -0
- package/package.json +3 -3
package/dist/assistant.d.ts
CHANGED
|
@@ -25,6 +25,12 @@ export type AssistantClient = {
|
|
|
25
25
|
send: (text: string, page?: string) => Promise<void>;
|
|
26
26
|
/** Sends a click on a card's button; the backend answers with events. Rejects with `ApiError`. */
|
|
27
27
|
act: (partId: string, actionId: string) => Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Calls the backend beyond the protocol, such as a widget reading what it shows: `path` resolves against the
|
|
30
|
+
* backend's URL and is refused, before any request, when it lands on another origin, so the user's token goes only
|
|
31
|
+
* to the backend. Sends the token like every request; rejects with `ApiError` on an answer that is not 2xx.
|
|
32
|
+
*/
|
|
33
|
+
fetch: (path: string, init?: RequestInit) => Promise<Response>;
|
|
28
34
|
};
|
|
29
35
|
/** A client of an assistant backend: the protocol's event stream and its two requests, with the user's token. */
|
|
30
36
|
export declare const createAssistantClient: (options: AssistantClientOptions) => AssistantClient;
|
package/dist/assistant.js
CHANGED
|
@@ -9,6 +9,14 @@ const pause = (ms, signal) => new Promise((resolve) => {
|
|
|
9
9
|
resolve();
|
|
10
10
|
}, { once: true });
|
|
11
11
|
});
|
|
12
|
+
const sameOrigin = "http://same-origin.invalid";
|
|
13
|
+
/** The URL `path` names on the backend at `base`, or an error when it is on another origin. */
|
|
14
|
+
const backendUrl = (base, path) => {
|
|
15
|
+
const backend = new URL(base, sameOrigin);
|
|
16
|
+
const url = new URL(path, `${backend.href}/`);
|
|
17
|
+
if (url.origin !== backend.origin) throw new Error(`${path} is not on the assistant backend's origin`);
|
|
18
|
+
return backend.origin === sameOrigin ? `${url.pathname}${url.search}` : url.href;
|
|
19
|
+
};
|
|
12
20
|
/** A client of an assistant backend: the protocol's event stream and its two requests, with the user's token. */
|
|
13
21
|
const createAssistantClient = (options) => {
|
|
14
22
|
const base = options.url.replace(/\/+$/, "");
|
|
@@ -76,6 +84,18 @@ const createAssistantClient = (options) => {
|
|
|
76
84
|
actionId
|
|
77
85
|
}
|
|
78
86
|
});
|
|
87
|
+
},
|
|
88
|
+
fetch: async (path, init = {}) => {
|
|
89
|
+
const url = backendUrl(base, path);
|
|
90
|
+
const token = await options.token();
|
|
91
|
+
const headers = new Headers(init.headers);
|
|
92
|
+
if (token) headers.set("authorization", `Bearer ${token}`);
|
|
93
|
+
const response = await doFetch(url, {
|
|
94
|
+
...init,
|
|
95
|
+
headers
|
|
96
|
+
});
|
|
97
|
+
if (!response.ok) throw await readProblem(response);
|
|
98
|
+
return response;
|
|
79
99
|
}
|
|
80
100
|
};
|
|
81
101
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Typed fetch client for the Protobase REST API.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@better-auth/passkey": "^1.7.7",
|
|
33
|
-
"@protobase/layout": "0.
|
|
34
|
-
"@protobase/schema": "0.
|
|
33
|
+
"@protobase/layout": "0.5.0",
|
|
34
|
+
"@protobase/schema": "0.5.0",
|
|
35
35
|
"better-auth": "^1.7.7"
|
|
36
36
|
}
|
|
37
37
|
}
|