@toapi/react 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 ADDED
@@ -0,0 +1,36 @@
1
+ # @toapi/react
2
+
3
+ React-Binding for [Toapi](https://www.npmjs.com/package/@toapi/client)
4
+
5
+ ## Installation
6
+ ```bash
7
+ npm install @toapi/react
8
+ yarn add @toapi/react
9
+ pnpm add @toapi/react
10
+ bun add @toapi/react
11
+ ```
12
+
13
+ ## Usage
14
+ ```tsx
15
+ import { useQuery } from '@toapi/react';
16
+ import { client } from "/path/to/tapi-client";
17
+ import { use } from 'react';
18
+
19
+ function Page() {
20
+ const booksPromise = useQuery(client.books.get())
21
+ const books = use(booksPromise)
22
+
23
+ return (
24
+ <div>
25
+ {books.map(item => (
26
+ <div key={item.id}>{item.name}</div>
27
+ ))}
28
+ </div>
29
+ );
30
+ }
31
+ ```
32
+
33
+ ## What does this do?
34
+ It subscribes Toapi's internal PubSub system to get updates on the data when it is invalidated through TApi's tag-based cache invalication mechanism or manually using the revalidate methods of the TApi client, e.G. `client.books.revalidate()`.
35
+
36
+ Note that `useQuery` actaully returns a `Promise` which can be passed to client components for granular control of loading states using `Suspense`-Boundaries.
@@ -0,0 +1,2 @@
1
+ export { useQuery } from "./use-query";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { useQuery } from "./use-query";
@@ -0,0 +1,11 @@
1
+ import type { Observable } from "@farbenmeer/tapi/client";
2
+ import * as React from "react";
3
+ type ObservablePromise<T> = Promise<T> & Observable<T>;
4
+ interface Options {
5
+ startTransition?: typeof React.startTransition;
6
+ onError?: (error: Error) => void;
7
+ swr?: boolean;
8
+ }
9
+ export declare function useLacy<T>(query: ObservablePromise<T> | (() => ObservablePromise<T>), options?: Options): import("@farbenmeer/lacy").LacyPromise<T, never>;
10
+ export {};
11
+ //# sourceMappingURL=use-lacy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-lacy.d.ts","sourceRoot":"","sources":["../src/use-lacy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,KAAK,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAEvD,UAAU,OAAO;IACf,eAAe,CAAC,EAAE,OAAO,KAAK,CAAC,eAAe,CAAC;IAC/C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,wBAAgB,OAAO,CAAC,CAAC,EACvB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC1D,OAAO,GAAE,OAAY,oDAuCtB"}
@@ -0,0 +1,35 @@
1
+ import * as React from "react";
2
+ import { lacy } from "@farbenmeer/lacy";
3
+ export function useLacy(query, options = {}) {
4
+ const optionsRef = React.useRef(options);
5
+ optionsRef.current = options;
6
+ const observable = React.useMemo(typeof query === "function" ? query : () => query, [query]);
7
+ const [data, setData] = React.useState(observable);
8
+ React.useEffect(() => {
9
+ const unsubscribe = observable.subscribe((next) => {
10
+ const { startTransition = React.startTransition, onError, swr, } = optionsRef.current;
11
+ startTransition(async () => {
12
+ if (swr) {
13
+ try {
14
+ await next;
15
+ setData(next);
16
+ }
17
+ catch (error) {
18
+ onError?.(error);
19
+ }
20
+ }
21
+ else {
22
+ setData(next);
23
+ try {
24
+ await next;
25
+ }
26
+ catch (error) {
27
+ onError?.(error);
28
+ }
29
+ }
30
+ });
31
+ });
32
+ return unsubscribe;
33
+ }, [observable]);
34
+ return React.useMemo(() => lacy(data), [data]);
35
+ }
@@ -0,0 +1,9 @@
1
+ import type { Observable } from "@toapi/client";
2
+ import * as React from "react";
3
+ type ObservablePromise<T> = Promise<T> & Observable<T>;
4
+ interface Options {
5
+ startTransition?: typeof React.startTransition;
6
+ }
7
+ export declare function useQuery<T>(query: ObservablePromise<T> | (() => ObservablePromise<T>), { startTransition }?: Options): T;
8
+ export {};
9
+ //# sourceMappingURL=use-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-query.d.ts","sourceRoot":"","sources":["../src/use-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,KAAK,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAEvD,UAAU,OAAO;IACf,eAAe,CAAC,EAAE,OAAO,KAAK,CAAC,eAAe,CAAC;CAChD;AAID,wBAAgB,QAAQ,CAAC,CAAC,EACxB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAC1D,EAAE,eAAuC,EAAE,GAAE,OAAY,KAkB1D"}
@@ -0,0 +1,15 @@
1
+ import * as React from "react";
2
+ const noValue = Symbol("ReactTApi:noValue");
3
+ export function useQuery(query, { startTransition = React.startTransition } = {}) {
4
+ const observable = React.useMemo(typeof query === "function" ? query : () => query, [query]);
5
+ const [data, setData] = React.useState(noValue);
6
+ React.useEffect(() => {
7
+ const unsubscribe = observable.subscribe((next) => {
8
+ startTransition(async () => {
9
+ setData(await next);
10
+ });
11
+ });
12
+ return unsubscribe;
13
+ }, [observable]);
14
+ return data === noValue ? React.use(observable) : data;
15
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@toapi/react",
3
+ "version": "0.1.0",
4
+ "author": {
5
+ "name": "Michel Smola",
6
+ "email": "michel.smola@farbenmeer.de"
7
+ },
8
+ "type": "module",
9
+ "module": "dist/index.js",
10
+ "main": "dist/index.js",
11
+ "private": false,
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/farbenmeer/tapi.git"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "devDependencies": {
21
+ "@happy-dom/global-registrator": "^20.0.0",
22
+ "@testing-library/dom": "^10.4.1",
23
+ "@testing-library/jest-dom": "^6.8.0",
24
+ "@testing-library/react": "^16.3.0",
25
+ "@types/node": "^25.0.3",
26
+ "@types/react": "^19.2.7",
27
+ "react": "^19.1.1",
28
+ "vitest": "^4.0.16",
29
+ "zod": "^4.1.5",
30
+ "@toapi/client": "^0.12.1",
31
+ "@toapi/server": "^0.12.1"
32
+ },
33
+ "peerDependencies": {
34
+ "react": "^19.1.1",
35
+ "typescript": "^5 || ^6.0.0",
36
+ "@toapi/client": "^0.12.1"
37
+ },
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.build.json",
40
+ "release": "pnpm run build && pnpm publish --no-git-checks",
41
+ "test": "vitest"
42
+ }
43
+ }