@kividb/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/LICENSE +21 -0
- package/README.md +136 -0
- package/dist/index.cjs +830 -0
- package/dist/index.d.cts +493 -0
- package/dist/index.d.ts +493 -0
- package/dist/index.js +792 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anton Kivi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @kividb/client
|
|
2
|
+
|
|
3
|
+
The JavaScript/TypeScript client SDK for kividb — a thin, typed wrapper over the
|
|
4
|
+
`data-api`, `auth`, and `realtime` services. Works in browsers, React Native,
|
|
5
|
+
and Node 22+ (uses global `fetch` and `WebSocket`, no runtime dependencies).
|
|
6
|
+
|
|
7
|
+
> Status: initial scaffold. Covers the full data, auth, and realtime surface the
|
|
8
|
+
> services expose today. Storage and edge-function helpers are not built yet.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
pnpm add @kividb/client
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createClient } from "@kividb/client";
|
|
20
|
+
|
|
21
|
+
// One gateway URL + the project's anon key. The project slug is read from the
|
|
22
|
+
// key's `ref` claim, and /auth, /rest, /realtime are derived from the URL.
|
|
23
|
+
const kdb = createClient("https://database.akivi.se", ANON_KEY);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The single URL points at the gateway (see the repo's `Caddyfile` /
|
|
27
|
+
`docker-compose.prod.yml`) that fronts all services. For local dev without a
|
|
28
|
+
gateway, override the per-service base URLs to hit the ports directly:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const kdb = createClient("http://localhost", ANON_KEY, {
|
|
32
|
+
auth: "http://localhost:4000",
|
|
33
|
+
rest: "http://localhost:4001",
|
|
34
|
+
realtime: "ws://localhost:4002",
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Data
|
|
39
|
+
|
|
40
|
+
Every call resolves to `{ data, error }` — nothing throws for an HTTP failure.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// select with PostgREST-style filters
|
|
44
|
+
const { data, error } = await kdb
|
|
45
|
+
.from("posts")
|
|
46
|
+
.select("id,title,created_at")
|
|
47
|
+
.eq("published", true)
|
|
48
|
+
.ilike("title", "%launch%")
|
|
49
|
+
.order("created_at", { ascending: false })
|
|
50
|
+
.limit(20);
|
|
51
|
+
|
|
52
|
+
// insert (returns the inserted rows)
|
|
53
|
+
await kdb.from("posts").insert({ title: "Hello", body: "..." });
|
|
54
|
+
|
|
55
|
+
// update with a filter
|
|
56
|
+
await kdb.from("posts").update({ published: true }).eq("id", 42);
|
|
57
|
+
|
|
58
|
+
// delete
|
|
59
|
+
await kdb.from("posts").delete().eq("id", 42);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Filter methods: `eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `like`, `ilike`, `is`
|
|
63
|
+
(`is` accepts only `null`, `true`, `false`). Pagination: `limit`, `offset`, or
|
|
64
|
+
`range(from, to)`.
|
|
65
|
+
|
|
66
|
+
Requests use the anon key until a user signs in, then automatically switch to
|
|
67
|
+
that user's access token. Row-level security is enforced server-side per role.
|
|
68
|
+
|
|
69
|
+
## Auth
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
await kdb.auth.signUp({ email, password });
|
|
73
|
+
await kdb.auth.signInWithPassword({ email, password });
|
|
74
|
+
await kdb.auth.signInWithOtp({ email }); // magic link
|
|
75
|
+
await kdb.auth.verifyOtp({ type: "magiclink", token });
|
|
76
|
+
await kdb.auth.refreshSession();
|
|
77
|
+
await kdb.auth.getUser();
|
|
78
|
+
await kdb.auth.updateUser({ data: { displayName: "Ada" } });
|
|
79
|
+
await kdb.auth.resetPasswordForEmail(email);
|
|
80
|
+
await kdb.auth.signOut();
|
|
81
|
+
|
|
82
|
+
const unsubscribe = kdb.auth.onAuthStateChange((event, session) => {
|
|
83
|
+
// "SIGNED_IN" | "SIGNED_OUT" | "TOKEN_REFRESHED"
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### OAuth (GitHub / Google)
|
|
88
|
+
|
|
89
|
+
OAuth is a browser redirect. Get the URL, navigate to it, then exchange the
|
|
90
|
+
`?code=` the provider bounces back:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
window.location.href = kdb.auth.getOAuthUrl({
|
|
94
|
+
provider: "github",
|
|
95
|
+
redirectTo: "https://app.example.com/callback",
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// on the callback page:
|
|
99
|
+
const code = new URLSearchParams(location.search).get("code")!;
|
|
100
|
+
await kdb.auth.exchangeCodeForSession(code);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Persisting the session
|
|
104
|
+
|
|
105
|
+
By default the session lives in memory. Provide a `store` to survive reloads:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const kdb = createClient("blog", ANON_KEY, {
|
|
109
|
+
store: {
|
|
110
|
+
get: () => JSON.parse(localStorage.getItem("kdb.session") ?? "null"),
|
|
111
|
+
set: (s) => localStorage.setItem("kdb.session", JSON.stringify(s)),
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Realtime
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const channel = kdb
|
|
120
|
+
.channel("room:1")
|
|
121
|
+
.on("broadcast", { event: "message" }, ({ payload }) => console.log(payload))
|
|
122
|
+
.on("presence", { event: "sync" }, (state) => console.log(state))
|
|
123
|
+
.on("postgres_changes", { table: "messages", event: "INSERT" }, (change) =>
|
|
124
|
+
console.log(change.new),
|
|
125
|
+
)
|
|
126
|
+
.subscribe();
|
|
127
|
+
|
|
128
|
+
channel.send({ event: "message", payload: { text: "hi" } });
|
|
129
|
+
channel.track({ user: "ada" });
|
|
130
|
+
channel.untrack();
|
|
131
|
+
channel.unsubscribe();
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
One WebSocket is shared per project across all channels. It reconnects with
|
|
135
|
+
backoff and re-subscribes automatically; `DELETE` postgres events are delivered
|
|
136
|
+
only to `service_role` tokens (a documented v1 server limitation).
|