@synap-core/client 0.1.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/README.md +253 -0
- package/dist/core.d.ts +1292 -0
- package/dist/facade.d.ts +188 -0
- package/dist/facades/capabilities.d.ts +27 -0
- package/dist/facades/content.d.ts +138 -0
- package/dist/index.cjs +43 -0
- package/dist/index.d.ts +1348 -0
- package/dist/index.js +18 -0
- package/dist/react/index.d.ts +9 -0
- package/dist/react/provider.cjs +94 -0
- package/dist/react/provider.d.ts +57 -0
- package/dist/react/provider.js +67 -0
- package/dist/react/useEntities.d.ts +34 -0
- package/dist/react/useEvents.d.ts +21 -0
- package/dist/react/useThreads.d.ts +27 -0
- package/dist/react.cjs +38 -0
- package/dist/react.d.ts +76 -0
- package/dist/react.js +12 -0
- package/dist/realtime.cjs +117 -0
- package/dist/realtime.d.ts +80 -0
- package/dist/realtime.js +92 -0
- package/dist/types.d.ts +8 -0
- package/package.json +77 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createTRPCClient, httpBatchLink } from "@trpc/client";
|
|
3
|
+
function createSynapClient(config) {
|
|
4
|
+
return createTRPCClient({
|
|
5
|
+
links: [
|
|
6
|
+
httpBatchLink({
|
|
7
|
+
url: `${config.url}/trpc`,
|
|
8
|
+
headers: config.headers,
|
|
9
|
+
fetch: config.fetch
|
|
10
|
+
})
|
|
11
|
+
]
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
var index_default = createSynapClient;
|
|
15
|
+
export {
|
|
16
|
+
createSynapClient,
|
|
17
|
+
index_default as default
|
|
18
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @synap/client React Hooks
|
|
3
|
+
*
|
|
4
|
+
* Reusable React hooks for any frontend using Synap Data Pod
|
|
5
|
+
*/
|
|
6
|
+
export { useEntities, useEntity, useEntitySearch, useCreateEntity, useUpdateEntity, useDeleteEntity } from './useEntities.js';
|
|
7
|
+
export { useThreads, useThread, useBranches, useSendMessage, useCreateBranch } from './useThreads.js';
|
|
8
|
+
export { useEvents, useAggregateEvents } from './useEvents.js';
|
|
9
|
+
export { SynapProvider, useSynapClient, useSynap } from './provider.js';
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/react/provider.tsx
|
|
22
|
+
var provider_exports = {};
|
|
23
|
+
__export(provider_exports, {
|
|
24
|
+
SynapProvider: () => SynapProvider,
|
|
25
|
+
trpc: () => trpc,
|
|
26
|
+
useSynap: () => useSynap,
|
|
27
|
+
useSynapClient: () => useSynapClient
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(provider_exports);
|
|
30
|
+
var import_react = require("react");
|
|
31
|
+
var import_react_query = require("@tanstack/react-query");
|
|
32
|
+
var import_react_query2 = require("@trpc/react-query");
|
|
33
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
|
+
var trpc = (0, import_react_query2.createTRPCReact)();
|
|
35
|
+
var SynapContext = (0, import_react.createContext)(null);
|
|
36
|
+
function SynapProvider({ url = "http://localhost:3000", getToken, headers, children }) {
|
|
37
|
+
const queryClient = (0, import_react.useMemo)(() => new import_react_query.QueryClient({
|
|
38
|
+
defaultOptions: {
|
|
39
|
+
queries: {
|
|
40
|
+
staleTime: 5 * 60 * 1e3,
|
|
41
|
+
// 5 minutes
|
|
42
|
+
retry: 1
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}), []);
|
|
46
|
+
const trpcClient = (0, import_react.useMemo)(() => trpc.createClient({
|
|
47
|
+
links: [
|
|
48
|
+
(0, import_react_query2.httpBatchLink)({
|
|
49
|
+
url: `${url}/trpc`,
|
|
50
|
+
async headers() {
|
|
51
|
+
const baseHeaders = {
|
|
52
|
+
...headers
|
|
53
|
+
};
|
|
54
|
+
if (getToken) {
|
|
55
|
+
const token = await getToken();
|
|
56
|
+
if (token) {
|
|
57
|
+
baseHeaders["Authorization"] = `Bearer ${token}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return baseHeaders;
|
|
61
|
+
},
|
|
62
|
+
fetch(url2, options) {
|
|
63
|
+
return fetch(url2, {
|
|
64
|
+
...options,
|
|
65
|
+
credentials: "include"
|
|
66
|
+
// Send cookies
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
]
|
|
71
|
+
}), [url, getToken, headers]);
|
|
72
|
+
const contextValue = (0, import_react.useMemo)(() => ({
|
|
73
|
+
api: trpc,
|
|
74
|
+
url
|
|
75
|
+
}), [url]);
|
|
76
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SynapContext.Provider, { value: contextValue, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(trpc.Provider, { client: trpcClient, queryClient, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_query.QueryClientProvider, { client: queryClient, children }) }) });
|
|
77
|
+
}
|
|
78
|
+
function useSynap() {
|
|
79
|
+
const context = (0, import_react.useContext)(SynapContext);
|
|
80
|
+
if (!context) {
|
|
81
|
+
throw new Error("useSynap must be used within a SynapProvider");
|
|
82
|
+
}
|
|
83
|
+
return context;
|
|
84
|
+
}
|
|
85
|
+
function useSynapClient() {
|
|
86
|
+
return useSynap().api;
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
SynapProvider,
|
|
91
|
+
trpc,
|
|
92
|
+
useSynap,
|
|
93
|
+
useSynapClient
|
|
94
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synap Provider
|
|
3
|
+
*
|
|
4
|
+
* React context provider for Synap Client SDK
|
|
5
|
+
* Wraps tRPC and QueryClient, provides hooks access
|
|
6
|
+
*/
|
|
7
|
+
import { type ReactNode } from 'react';
|
|
8
|
+
import type { AppRouter } from '../types.js';
|
|
9
|
+
import { type CreateTRPCReact } from '@trpc/react-query';
|
|
10
|
+
declare const trpc: CreateTRPCReact<AppRouter, unknown>;
|
|
11
|
+
export interface SynapProviderConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Data Pod URL
|
|
14
|
+
* @default 'http://localhost:3000'
|
|
15
|
+
*/
|
|
16
|
+
url?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Token getter for authentication
|
|
19
|
+
*/
|
|
20
|
+
getToken?: () => Promise<string | null> | string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Additional headers
|
|
23
|
+
*/
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
/**
|
|
26
|
+
* Children to wrap
|
|
27
|
+
*/
|
|
28
|
+
children: ReactNode;
|
|
29
|
+
}
|
|
30
|
+
interface SynapContextValue {
|
|
31
|
+
api: typeof trpc;
|
|
32
|
+
url: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Synap Provider Component
|
|
36
|
+
*
|
|
37
|
+
* Wraps your app with tRPC and React Query providers
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* <SynapProvider url="http://localhost:3000">
|
|
42
|
+
* <App />
|
|
43
|
+
* </SynapProvider>
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function SynapProvider({ url, getToken, headers, children }: SynapProviderConfig): import("react/jsx-runtime").JSX.Element;
|
|
47
|
+
/**
|
|
48
|
+
* Hook to access Synap context
|
|
49
|
+
* Must be used within SynapProvider
|
|
50
|
+
*/
|
|
51
|
+
export declare function useSynap(): SynapContextValue;
|
|
52
|
+
/**
|
|
53
|
+
* Hook to access the Synap tRPC client
|
|
54
|
+
* Must be used within SynapProvider
|
|
55
|
+
*/
|
|
56
|
+
export declare function useSynapClient(): any;
|
|
57
|
+
export { trpc };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/react/provider.tsx
|
|
4
|
+
import { createContext, useContext, useMemo } from "react";
|
|
5
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
6
|
+
import { createTRPCReact, httpBatchLink } from "@trpc/react-query";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
var trpc = createTRPCReact();
|
|
9
|
+
var SynapContext = createContext(null);
|
|
10
|
+
function SynapProvider({ url = "http://localhost:3000", getToken, headers, children }) {
|
|
11
|
+
const queryClient = useMemo(() => new QueryClient({
|
|
12
|
+
defaultOptions: {
|
|
13
|
+
queries: {
|
|
14
|
+
staleTime: 5 * 60 * 1e3,
|
|
15
|
+
// 5 minutes
|
|
16
|
+
retry: 1
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}), []);
|
|
20
|
+
const trpcClient = useMemo(() => trpc.createClient({
|
|
21
|
+
links: [
|
|
22
|
+
httpBatchLink({
|
|
23
|
+
url: `${url}/trpc`,
|
|
24
|
+
async headers() {
|
|
25
|
+
const baseHeaders = {
|
|
26
|
+
...headers
|
|
27
|
+
};
|
|
28
|
+
if (getToken) {
|
|
29
|
+
const token = await getToken();
|
|
30
|
+
if (token) {
|
|
31
|
+
baseHeaders["Authorization"] = `Bearer ${token}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return baseHeaders;
|
|
35
|
+
},
|
|
36
|
+
fetch(url2, options) {
|
|
37
|
+
return fetch(url2, {
|
|
38
|
+
...options,
|
|
39
|
+
credentials: "include"
|
|
40
|
+
// Send cookies
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
]
|
|
45
|
+
}), [url, getToken, headers]);
|
|
46
|
+
const contextValue = useMemo(() => ({
|
|
47
|
+
api: trpc,
|
|
48
|
+
url
|
|
49
|
+
}), [url]);
|
|
50
|
+
return /* @__PURE__ */ jsx(SynapContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(trpc.Provider, { client: trpcClient, queryClient, children: /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children }) }) });
|
|
51
|
+
}
|
|
52
|
+
function useSynap() {
|
|
53
|
+
const context = useContext(SynapContext);
|
|
54
|
+
if (!context) {
|
|
55
|
+
throw new Error("useSynap must be used within a SynapProvider");
|
|
56
|
+
}
|
|
57
|
+
return context;
|
|
58
|
+
}
|
|
59
|
+
function useSynapClient() {
|
|
60
|
+
return useSynap().api;
|
|
61
|
+
}
|
|
62
|
+
export {
|
|
63
|
+
SynapProvider,
|
|
64
|
+
trpc,
|
|
65
|
+
useSynap,
|
|
66
|
+
useSynapClient
|
|
67
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useEntities Hook
|
|
3
|
+
*
|
|
4
|
+
* Fetches entities from Data Pod via tRPC
|
|
5
|
+
*/
|
|
6
|
+
export interface UseEntitiesOptions {
|
|
7
|
+
type?: 'task' | 'contact' | 'meeting' | 'idea' | 'note' | 'project';
|
|
8
|
+
limit?: number;
|
|
9
|
+
offset?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetch user's entities
|
|
13
|
+
*/
|
|
14
|
+
export declare function useEntities(options?: UseEntitiesOptions): any;
|
|
15
|
+
/**
|
|
16
|
+
* Fetch single entity by ID
|
|
17
|
+
*/
|
|
18
|
+
export declare function useEntity(id: string): any;
|
|
19
|
+
/**
|
|
20
|
+
* Search entities by text
|
|
21
|
+
*/
|
|
22
|
+
export declare function useEntitySearch(query: string, options?: Omit<UseEntitiesOptions, 'offset'>): any;
|
|
23
|
+
/**
|
|
24
|
+
* Create entity mutation
|
|
25
|
+
*/
|
|
26
|
+
export declare function useCreateEntity(): any;
|
|
27
|
+
/**
|
|
28
|
+
* Update entity mutation
|
|
29
|
+
*/
|
|
30
|
+
export declare function useUpdateEntity(): any;
|
|
31
|
+
/**
|
|
32
|
+
* Delete entity mutation
|
|
33
|
+
*/
|
|
34
|
+
export declare function useDeleteEntity(): any;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useEvents Hook
|
|
3
|
+
*
|
|
4
|
+
* Fetches events from Data Pod via tRPC
|
|
5
|
+
*/
|
|
6
|
+
export interface UseEventsOptions {
|
|
7
|
+
days?: number;
|
|
8
|
+
limit?: number;
|
|
9
|
+
eventTypes?: string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetch user's event stream
|
|
13
|
+
*/
|
|
14
|
+
export declare function useEvents(options?: UseEventsOptions): any;
|
|
15
|
+
/**
|
|
16
|
+
* Fetch events for specific aggregate
|
|
17
|
+
*/
|
|
18
|
+
export declare function useAggregateEvents(_aggregateId: string, _options?: {
|
|
19
|
+
fromVersion?: number;
|
|
20
|
+
toVersion?: number;
|
|
21
|
+
}): any;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useThreads Hook
|
|
3
|
+
*
|
|
4
|
+
* Fetches chat threads from Data Pod via tRPC
|
|
5
|
+
*
|
|
6
|
+
* DISABLED - chat router not implemented in @synap/api yet
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Fetch user's chat threads
|
|
10
|
+
*/
|
|
11
|
+
export declare function useThreads(_limit?: number): any;
|
|
12
|
+
/**
|
|
13
|
+
* Fetch single thread with messages
|
|
14
|
+
*/
|
|
15
|
+
export declare function useThread(_threadId: string, _limit?: number): any;
|
|
16
|
+
/**
|
|
17
|
+
* Get branches from a message
|
|
18
|
+
*/
|
|
19
|
+
export declare function useBranches(_parentId: string): any;
|
|
20
|
+
/**
|
|
21
|
+
* Send chat message mutation
|
|
22
|
+
*/
|
|
23
|
+
export declare function useSendMessage(): any;
|
|
24
|
+
/**
|
|
25
|
+
* Create branch mutation
|
|
26
|
+
*/
|
|
27
|
+
export declare function useCreateBranch(): any;
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
SynapQueryClientProvider: () => import_react_query2.QueryClientProvider,
|
|
24
|
+
createSynapReact: () => createSynapReact,
|
|
25
|
+
default: () => react_default
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(react_exports);
|
|
28
|
+
var import_react_query = require("@trpc/react-query");
|
|
29
|
+
var import_react_query2 = require("@tanstack/react-query");
|
|
30
|
+
function createSynapReact() {
|
|
31
|
+
return (0, import_react_query.createTRPCReact)();
|
|
32
|
+
}
|
|
33
|
+
var react_default = createSynapReact;
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
SynapQueryClientProvider,
|
|
37
|
+
createSynapReact
|
|
38
|
+
});
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @synap/client/react - React Query hooks for Synap
|
|
3
|
+
*
|
|
4
|
+
* Provides tRPC React Query hooks with full type safety.
|
|
5
|
+
* Auto-syncs with API changes via AppRouter type.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createSynapReact, SynapProvider } from '@synap/client/react';
|
|
10
|
+
*
|
|
11
|
+
* // Create hooks
|
|
12
|
+
* export const api = createSynapReact();
|
|
13
|
+
*
|
|
14
|
+
* // In your app
|
|
15
|
+
* function App() {
|
|
16
|
+
* return (
|
|
17
|
+
* <SynapProvider client={trpcClient} queryClient={queryClient}>
|
|
18
|
+
* <MyComponent />
|
|
19
|
+
* </SynapProvider>
|
|
20
|
+
* );
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // In components
|
|
24
|
+
* function MyComponent() {
|
|
25
|
+
* const { data } = api.entities.list.useQuery({ limit: 20 });
|
|
26
|
+
* const createTag = api.tags.create.useMutation();
|
|
27
|
+
*
|
|
28
|
+
* return <div>{data?.entities.map(...)}</div>;
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
import { createTRPCReact } from '@trpc/react-query';
|
|
33
|
+
import type { AppRouter } from '@synap/api';
|
|
34
|
+
/**
|
|
35
|
+
* Create tRPC React hooks for Synap API
|
|
36
|
+
*
|
|
37
|
+
* This provides fully typed React Query hooks that auto-sync
|
|
38
|
+
* with the API's AppRouter type.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // Create once in your app
|
|
43
|
+
* export const api = createSynapReact();
|
|
44
|
+
*
|
|
45
|
+
* // Use in components
|
|
46
|
+
* function NotesScreen() {
|
|
47
|
+
* const { data, isLoading } = api.entities.list.useQuery({
|
|
48
|
+
* type: 'note',
|
|
49
|
+
* limit: 50
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* const createEntity = api.entities.create.useMutation({
|
|
53
|
+
* onSuccess: () => {
|
|
54
|
+
* // Invalidate cache
|
|
55
|
+
* api.useContext().entities.list.invalidate();
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* return (
|
|
60
|
+
* <div>
|
|
61
|
+
* {data?.entities.map(entity => (
|
|
62
|
+
* <div key={entity.id}>{entity.title}</div>
|
|
63
|
+
* ))}
|
|
64
|
+
* </div>
|
|
65
|
+
* );
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function createSynapReact(): ReturnType<typeof createTRPCReact<AppRouter>>;
|
|
70
|
+
/**
|
|
71
|
+
* Type alias for the Synap tRPC React context
|
|
72
|
+
*/
|
|
73
|
+
export type SynapReactContext = ReturnType<typeof createSynapReact>;
|
|
74
|
+
export { QueryClientProvider as SynapQueryClientProvider } from '@tanstack/react-query';
|
|
75
|
+
export type { AppRouter } from '@synap/api';
|
|
76
|
+
export default createSynapReact;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/react.ts
|
|
2
|
+
import { createTRPCReact } from "@trpc/react-query";
|
|
3
|
+
import { QueryClientProvider } from "@tanstack/react-query";
|
|
4
|
+
function createSynapReact() {
|
|
5
|
+
return createTRPCReact();
|
|
6
|
+
}
|
|
7
|
+
var react_default = createSynapReact;
|
|
8
|
+
export {
|
|
9
|
+
QueryClientProvider as SynapQueryClientProvider,
|
|
10
|
+
createSynapReact,
|
|
11
|
+
react_default as default
|
|
12
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/realtime.ts
|
|
21
|
+
var realtime_exports = {};
|
|
22
|
+
__export(realtime_exports, {
|
|
23
|
+
SynapRealtimeClient: () => SynapRealtimeClient
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(realtime_exports);
|
|
26
|
+
var SynapRealtimeClient = class {
|
|
27
|
+
ws = null;
|
|
28
|
+
config;
|
|
29
|
+
reconnectAttempts = 0;
|
|
30
|
+
maxReconnectAttempts;
|
|
31
|
+
reconnectDelay;
|
|
32
|
+
reconnectTimer = null;
|
|
33
|
+
isIntentionallyDisconnected = false;
|
|
34
|
+
constructor(config) {
|
|
35
|
+
this.config = config;
|
|
36
|
+
this.maxReconnectAttempts = config.maxReconnectAttempts ?? 5;
|
|
37
|
+
this.reconnectDelay = config.reconnectDelay ?? 1e3;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Connect to the WebSocket server
|
|
41
|
+
*/
|
|
42
|
+
connect() {
|
|
43
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.isIntentionallyDisconnected = false;
|
|
47
|
+
const wsUrl = this.config.url;
|
|
48
|
+
try {
|
|
49
|
+
this.ws = new WebSocket(wsUrl);
|
|
50
|
+
this.ws.onopen = () => {
|
|
51
|
+
this.reconnectAttempts = 0;
|
|
52
|
+
this.config.onConnect?.();
|
|
53
|
+
};
|
|
54
|
+
this.ws.onmessage = (event) => {
|
|
55
|
+
try {
|
|
56
|
+
const message = JSON.parse(event.data);
|
|
57
|
+
this.config.onMessage?.(message);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error("Failed to parse WebSocket message:", error);
|
|
60
|
+
this.config.onError?.(
|
|
61
|
+
new Error(`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
this.ws.onerror = () => {
|
|
66
|
+
this.config.onError?.(new Error("WebSocket error"));
|
|
67
|
+
};
|
|
68
|
+
this.ws.onclose = () => {
|
|
69
|
+
this.config.onDisconnect?.();
|
|
70
|
+
if (!this.isIntentionallyDisconnected) {
|
|
71
|
+
this.attemptReconnect();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
} catch (error) {
|
|
75
|
+
this.config.onError?.(
|
|
76
|
+
error instanceof Error ? error : new Error(String(error))
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Disconnect from the WebSocket server
|
|
82
|
+
*/
|
|
83
|
+
disconnect() {
|
|
84
|
+
this.isIntentionallyDisconnected = true;
|
|
85
|
+
if (this.reconnectTimer) {
|
|
86
|
+
clearTimeout(this.reconnectTimer);
|
|
87
|
+
this.reconnectTimer = null;
|
|
88
|
+
}
|
|
89
|
+
if (this.ws) {
|
|
90
|
+
this.ws.close();
|
|
91
|
+
this.ws = null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if currently connected
|
|
96
|
+
*/
|
|
97
|
+
isConnected() {
|
|
98
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
99
|
+
}
|
|
100
|
+
attemptReconnect() {
|
|
101
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
102
|
+
this.config.onError?.(
|
|
103
|
+
new Error(`Max reconnection attempts (${this.maxReconnectAttempts}) reached`)
|
|
104
|
+
);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.reconnectAttempts++;
|
|
108
|
+
const delay = this.reconnectDelay * this.reconnectAttempts;
|
|
109
|
+
this.reconnectTimer = setTimeout(() => {
|
|
110
|
+
this.connect();
|
|
111
|
+
}, delay);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
115
|
+
0 && (module.exports = {
|
|
116
|
+
SynapRealtimeClient
|
|
117
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real-time Client - WebSocket connection for live updates
|
|
3
|
+
*
|
|
4
|
+
* Connects to the Synap real-time notification system (Cloudflare Durable Objects)
|
|
5
|
+
* to receive live updates about async operations.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* NotificationMessage type definition
|
|
9
|
+
*
|
|
10
|
+
* Matches the type from @synap/realtime but defined here to avoid dependency.
|
|
11
|
+
*/
|
|
12
|
+
export interface NotificationMessage {
|
|
13
|
+
type: string;
|
|
14
|
+
data: Record<string, unknown>;
|
|
15
|
+
requestId?: string;
|
|
16
|
+
timestamp?: string;
|
|
17
|
+
status?: 'success' | 'error' | 'pending';
|
|
18
|
+
}
|
|
19
|
+
export interface RealtimeConfig {
|
|
20
|
+
/** WebSocket URL (e.g., 'wss://realtime.synap.app/rooms/user_123/subscribe') */
|
|
21
|
+
url: string;
|
|
22
|
+
/** User ID for the connection */
|
|
23
|
+
userId: string;
|
|
24
|
+
/** Callback when a message is received */
|
|
25
|
+
onMessage?: (message: NotificationMessage) => void;
|
|
26
|
+
/** Callback when an error occurs */
|
|
27
|
+
onError?: (error: Error) => void;
|
|
28
|
+
/** Callback when connection is established */
|
|
29
|
+
onConnect?: () => void;
|
|
30
|
+
/** Callback when connection is closed */
|
|
31
|
+
onDisconnect?: () => void;
|
|
32
|
+
/** Maximum number of reconnection attempts (default: 5) */
|
|
33
|
+
maxReconnectAttempts?: number;
|
|
34
|
+
/** Reconnection delay in milliseconds (default: 1000) */
|
|
35
|
+
reconnectDelay?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Real-time WebSocket Client
|
|
39
|
+
*
|
|
40
|
+
* Connects to the Synap real-time notification system to receive live updates.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const realtime = new SynapRealtimeClient({
|
|
45
|
+
* url: 'wss://realtime.synap.app/rooms/user_123/subscribe',
|
|
46
|
+
* userId: 'user-123',
|
|
47
|
+
* onMessage: (message) => {
|
|
48
|
+
* console.log('Received:', message);
|
|
49
|
+
* if (message.type === 'note.creation.completed') {
|
|
50
|
+
* // Refresh notes list
|
|
51
|
+
* }
|
|
52
|
+
* },
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* realtime.connect();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare class SynapRealtimeClient {
|
|
59
|
+
private ws;
|
|
60
|
+
private config;
|
|
61
|
+
private reconnectAttempts;
|
|
62
|
+
private maxReconnectAttempts;
|
|
63
|
+
private reconnectDelay;
|
|
64
|
+
private reconnectTimer;
|
|
65
|
+
private isIntentionallyDisconnected;
|
|
66
|
+
constructor(config: RealtimeConfig);
|
|
67
|
+
/**
|
|
68
|
+
* Connect to the WebSocket server
|
|
69
|
+
*/
|
|
70
|
+
connect(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Disconnect from the WebSocket server
|
|
73
|
+
*/
|
|
74
|
+
disconnect(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Check if currently connected
|
|
77
|
+
*/
|
|
78
|
+
isConnected(): boolean;
|
|
79
|
+
private attemptReconnect;
|
|
80
|
+
}
|