@pramen/react 0.0.1
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/index.d.ts +11 -0
- package/dist/index.js +36 -0
- package/package.json +39 -0
- package/src/index.ts +56 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Input, PramenClient, Output } from "@pramen/client";
|
|
2
|
+
export interface LiveQueryState<T> {
|
|
3
|
+
data: T | undefined;
|
|
4
|
+
error: {
|
|
5
|
+
error: string;
|
|
6
|
+
code: string;
|
|
7
|
+
} | null;
|
|
8
|
+
loading: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function useLiveQuery<Api, K extends keyof Api & string>(client: PramenClient<Api>, name: K, input?: Input<Api, K>): LiveQueryState<Output<Api, K>>;
|
|
11
|
+
export declare function useMutation<Api, K extends keyof Api & string>(client: PramenClient<Api>, name: K): (input?: Input<Api, K>) => Promise<Output<Api, K>>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// @pramen/react — React hooks over @pramen/client.
|
|
2
|
+
//
|
|
3
|
+
// const client = createClient<typeof app.handlers>({ url, token, tenant });
|
|
4
|
+
// function Notes() {
|
|
5
|
+
// const { data, loading } = useLiveQuery(client, "listNotes");
|
|
6
|
+
// const createNote = useMutation(client, "createNote");
|
|
7
|
+
// ...
|
|
8
|
+
// }
|
|
9
|
+
//
|
|
10
|
+
// useLiveQuery opens a live subscription and re-renders on every server push
|
|
11
|
+
// (row-level — only when this query's result actually changed).
|
|
12
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
13
|
+
export function useLiveQuery(client, name, input) {
|
|
14
|
+
const [state, setState] = useState({
|
|
15
|
+
data: undefined,
|
|
16
|
+
error: null,
|
|
17
|
+
loading: true,
|
|
18
|
+
});
|
|
19
|
+
// Re-subscribe only when the query identity changes.
|
|
20
|
+
const key = JSON.stringify([name, input ?? null]);
|
|
21
|
+
const inputRef = useRef(input);
|
|
22
|
+
inputRef.current = input;
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
setState((s) => ({ ...s, loading: true }));
|
|
25
|
+
const stop = client.subscribe(name, inputRef.current, {
|
|
26
|
+
onData: (result) => setState({ data: result, error: null, loading: false }),
|
|
27
|
+
onError: (err) => setState((s) => ({ ...s, error: err, loading: false })),
|
|
28
|
+
});
|
|
29
|
+
return stop;
|
|
30
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
31
|
+
}, [client, name, key]);
|
|
32
|
+
return state;
|
|
33
|
+
}
|
|
34
|
+
export function useMutation(client, name) {
|
|
35
|
+
return useCallback((input) => client.call(name, input), [client, name]);
|
|
36
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pramen/react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "React hooks for a pramen backend — useLiveQuery (reactive) + useMutation.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/netvarec/pramen.git",
|
|
9
|
+
"directory": "packages/react"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/netvarec/pramen#readme",
|
|
12
|
+
"bugs": "https://github.com/netvarec/pramen/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"development": "./src/index.ts",
|
|
18
|
+
"bun": "./src/index.ts",
|
|
19
|
+
"workerd": "./src/index.ts",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": ["dist", "src"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@pramen/client": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react": ">=18"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @pramen/react — React hooks over @pramen/client.
|
|
2
|
+
//
|
|
3
|
+
// const client = createClient<typeof app.handlers>({ url, token, tenant });
|
|
4
|
+
// function Notes() {
|
|
5
|
+
// const { data, loading } = useLiveQuery(client, "listNotes");
|
|
6
|
+
// const createNote = useMutation(client, "createNote");
|
|
7
|
+
// ...
|
|
8
|
+
// }
|
|
9
|
+
//
|
|
10
|
+
// useLiveQuery opens a live subscription and re-renders on every server push
|
|
11
|
+
// (row-level — only when this query's result actually changed).
|
|
12
|
+
|
|
13
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
14
|
+
import type { Input, PramenClient, Output } from "@pramen/client";
|
|
15
|
+
|
|
16
|
+
export interface LiveQueryState<T> {
|
|
17
|
+
data: T | undefined;
|
|
18
|
+
error: { error: string; code: string } | null;
|
|
19
|
+
loading: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useLiveQuery<Api, K extends keyof Api & string>(
|
|
23
|
+
client: PramenClient<Api>,
|
|
24
|
+
name: K,
|
|
25
|
+
input?: Input<Api, K>,
|
|
26
|
+
): LiveQueryState<Output<Api, K>> {
|
|
27
|
+
const [state, setState] = useState<LiveQueryState<Output<Api, K>>>({
|
|
28
|
+
data: undefined,
|
|
29
|
+
error: null,
|
|
30
|
+
loading: true,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Re-subscribe only when the query identity changes.
|
|
34
|
+
const key = JSON.stringify([name, input ?? null]);
|
|
35
|
+
const inputRef = useRef(input);
|
|
36
|
+
inputRef.current = input;
|
|
37
|
+
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
setState((s) => ({ ...s, loading: true }));
|
|
40
|
+
const stop = client.subscribe(name, inputRef.current, {
|
|
41
|
+
onData: (result) => setState({ data: result, error: null, loading: false }),
|
|
42
|
+
onError: (err) => setState((s) => ({ ...s, error: err, loading: false })),
|
|
43
|
+
});
|
|
44
|
+
return stop;
|
|
45
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
+
}, [client, name, key]);
|
|
47
|
+
|
|
48
|
+
return state;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function useMutation<Api, K extends keyof Api & string>(
|
|
52
|
+
client: PramenClient<Api>,
|
|
53
|
+
name: K,
|
|
54
|
+
): (input?: Input<Api, K>) => Promise<Output<Api, K>> {
|
|
55
|
+
return useCallback((input?: Input<Api, K>) => client.call(name, input), [client, name]);
|
|
56
|
+
}
|