@livequery/rest 2.0.75 → 2.0.78
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 +118 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @livequery/rest
|
|
2
|
+
|
|
3
|
+
REST transporter for [livequery](https://github.com/livequery) — connects your livequery client to a REST API backend with optional real-time WebSocket support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @livequery/rest
|
|
9
|
+
# or
|
|
10
|
+
bun add @livequery/rest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Basic REST
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { RestTransporter } from '@livequery/rest'
|
|
19
|
+
|
|
20
|
+
const transporter = new RestTransporter({
|
|
21
|
+
api: 'https://api.example.com'
|
|
22
|
+
})
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### With WebSocket (real-time updates)
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
const transporter = new RestTransporter({
|
|
29
|
+
api: 'https://api.example.com',
|
|
30
|
+
ws: 'wss://api.example.com/ws'
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Request / Response Hooks
|
|
35
|
+
|
|
36
|
+
Use `onRequest` to modify or intercept outgoing requests (e.g. inject auth headers), and `onResponse` to inspect responses.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const transporter = new RestTransporter({
|
|
40
|
+
api: 'https://api.example.com',
|
|
41
|
+
ws: 'wss://api.example.com/ws',
|
|
42
|
+
|
|
43
|
+
onRequest: async ({ url, method, headers, ref }) => {
|
|
44
|
+
const token = await getAccessToken()
|
|
45
|
+
return {
|
|
46
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
onResponse: async (request, response) => {
|
|
51
|
+
if (response.error) console.error('API error', response.error)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
You can also short-circuit a request by returning a `response` from `onRequest` — useful for caching or mocking:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
onRequest: ({ ref }) => {
|
|
60
|
+
const cached = cache.get(ref)
|
|
61
|
+
if (cached) return { response: { data: cached } }
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `RestTransporter`
|
|
68
|
+
|
|
69
|
+
#### Constructor options (`RestTransporterConfig`)
|
|
70
|
+
|
|
71
|
+
| Option | Type | Description |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `api` | `string` | Base URL of your REST API |
|
|
74
|
+
| `ws` | `string` (optional) | WebSocket endpoint for real-time updates |
|
|
75
|
+
| `onRequest` | function (optional) | Interceptor called before each request. Return partial request overrides or a fake response. |
|
|
76
|
+
| `onResponse` | function (optional) | Called after each response. |
|
|
77
|
+
|
|
78
|
+
#### Methods
|
|
79
|
+
|
|
80
|
+
These follow the `LivequeryTransporter` interface from `@livequery/core`:
|
|
81
|
+
|
|
82
|
+
| Method | Description |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `query(ref, filters)` | Query a collection or document. Returns an Observable. |
|
|
85
|
+
| `add(ref, data)` | Create a new document (`POST`) |
|
|
86
|
+
| `update(ref, id, data)` | Update a document (`PATCH`) |
|
|
87
|
+
| `delete(ref, id)` | Delete a document (`DELETE`) |
|
|
88
|
+
| `trigger({ ref, action, payload })` | Trigger a custom action (`POST /ref/~action`) |
|
|
89
|
+
|
|
90
|
+
### `Socket`
|
|
91
|
+
|
|
92
|
+
Manages the WebSocket connection lifecycle automatically — reconnects on failure, sends heartbeat pings, and routes server-pushed `DataChangeEvent`s to the appropriate collection streams.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { Socket } from '@livequery/rest'
|
|
96
|
+
|
|
97
|
+
const socket = new Socket('wss://api.example.com/ws')
|
|
98
|
+
socket.listen('users/123').subscribe(change => console.log(change))
|
|
99
|
+
socket.stop() // close connection
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Real-time Flow
|
|
103
|
+
|
|
104
|
+
1. On `query()`, a `subscription_token` returned by the server is forwarded to the socket.
|
|
105
|
+
2. The socket subscribes to the token and listens for `sync` events from the server.
|
|
106
|
+
3. Incoming changes are emitted as `DataChangeEvent`s with `source: "realtime"`.
|
|
107
|
+
|
|
108
|
+
## Build
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
bun run build
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Outputs ESM + type declarations to `dist/`.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
ISC
|