clawsocial-plugin 1.0.28 → 1.0.29

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/api.ts +35 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clawsocial-plugin",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
4
4
  "description": "ClawSocial OpenClaw Plugin — social discovery for AI agents",
5
5
  "type": "module",
6
6
  "dependencies": {
package/src/api.ts CHANGED
@@ -25,14 +25,12 @@ async function ensureToken(): Promise<string | null> {
25
25
  return null;
26
26
  }
27
27
 
28
- async function request<T = unknown>(
28
+ async function doRequest<T = unknown>(
29
29
  method: string,
30
30
  path: string,
31
- body?: unknown,
32
- token?: string,
33
- ): Promise<T> {
34
- const authToken = token ?? (await ensureToken());
35
-
31
+ body: unknown,
32
+ authToken: string | null,
33
+ ): Promise<{ res: Response; data: T }> {
36
34
  const res = await fetch(`${_serverUrl}${path}`, {
37
35
  method,
38
36
  headers: {
@@ -41,8 +39,38 @@ async function request<T = unknown>(
41
39
  },
42
40
  body: body ? JSON.stringify(body) : undefined,
43
41
  });
42
+ const data = (await res.json().catch(() => ({}))) as T;
43
+ return { res, data };
44
+ }
45
+
46
+ async function request<T = unknown>(
47
+ method: string,
48
+ path: string,
49
+ body?: unknown,
50
+ token?: string,
51
+ ): Promise<T> {
52
+ let authToken = token ?? (await ensureToken());
53
+
54
+ let { res, data } = await doRequest<T>(method, path, body, authToken);
44
55
 
45
- const data = (await res.json().catch(() => ({}))) as T & { error?: string };
56
+ // On 401, clear stale token, refresh with api_key and retry once
57
+ if (res.status === 401 && !token) {
58
+ setState({ token: undefined });
59
+ const state = getState();
60
+ if (state.agent_id && state.api_key) {
61
+ const refreshRes = await fetch(`${_serverUrl}/agents/auth`, {
62
+ method: "POST",
63
+ headers: { "Content-Type": "application/json" },
64
+ body: JSON.stringify({ agent_id: state.agent_id, api_key: state.api_key }),
65
+ });
66
+ const refreshData = (await refreshRes.json().catch(() => ({}))) as { token?: string };
67
+ if (refreshRes.ok && refreshData.token) {
68
+ setState({ token: refreshData.token });
69
+ authToken = refreshData.token;
70
+ ({ res, data } = await doRequest<T>(method, path, body, authToken));
71
+ }
72
+ }
73
+ }
46
74
 
47
75
  if (!res.ok) {
48
76
  const err = new Error((data as { error?: string }).error ?? `HTTP ${res.status}`);