dexie-cloud-sdk 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 +174 -0
- package/dist/index.d.mts +236 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.js +1065 -0
- package/dist/index.mjs +1017 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Fahlander
|
|
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,174 @@
|
|
|
1
|
+
# Dexie Cloud SDK
|
|
2
|
+
|
|
3
|
+
Official server-side JavaScript/TypeScript SDK for [Dexie Cloud](https://dexie.org/cloud) — local-first database with sync.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Transparent auth** — configure credentials once, tokens are managed automatically
|
|
8
|
+
- **Impersonation** — act as any user with `db.asUser()`
|
|
9
|
+
- **Blob handling** — automatic offloading of large strings and binaries
|
|
10
|
+
- **TSON support** — `Date`, `Map`, `Set`, `BigInt` serialized out of the box
|
|
11
|
+
- **Multi-environment** — Node.js, Deno, Cloudflare Workers
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install dexie-cloud-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { DexieCloudClient } from 'dexie-cloud-sdk';
|
|
23
|
+
import { readFileSync } from 'fs';
|
|
24
|
+
|
|
25
|
+
// 1. Read credentials from dexie-cloud.key (generated by `npx dexie-cloud connect`)
|
|
26
|
+
const keys = JSON.parse(readFileSync('dexie-cloud.key', 'utf-8'));
|
|
27
|
+
const dbUrl = Object.keys(keys)[0];
|
|
28
|
+
const { clientId, clientSecret } = keys[dbUrl];
|
|
29
|
+
|
|
30
|
+
// 2. Create client and database session
|
|
31
|
+
const client = new DexieCloudClient('https://dexie.cloud');
|
|
32
|
+
const db = client.db(dbUrl, { clientId, clientSecret });
|
|
33
|
+
|
|
34
|
+
// 3. Use it — no tokens to manage!
|
|
35
|
+
const items = await db.data.list('todoItems');
|
|
36
|
+
console.log(items);
|
|
37
|
+
|
|
38
|
+
await db.data.create('todoItems', {
|
|
39
|
+
title: 'Buy milk',
|
|
40
|
+
done: false,
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Impersonation
|
|
45
|
+
|
|
46
|
+
Act as a specific user (requires `IMPERSONATE` scope on the client):
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const userDb = db.asUser({
|
|
50
|
+
sub: 'user@example.com',
|
|
51
|
+
email: 'user@example.com',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// All operations run as that user
|
|
55
|
+
const items = await userDb.data.list('todoItems');
|
|
56
|
+
await userDb.data.create('todoItems', { title: 'User task', done: false });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
### `client.db(dbUrl, credentials)`
|
|
62
|
+
|
|
63
|
+
Creates a `DatabaseSession` with automatic token management.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const db = client.db('https://xxxxxxxx.dexie.cloud', {
|
|
67
|
+
clientId: '...',
|
|
68
|
+
clientSecret: '...',
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Tokens are cached in-memory and refreshed automatically when within 5 minutes of expiry.
|
|
73
|
+
|
|
74
|
+
### `db.data`
|
|
75
|
+
|
|
76
|
+
CRUD operations on database tables:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
await db.data.list('table'); // List all
|
|
80
|
+
await db.data.list('table', { realm: 'realmId' }); // Filter by realm
|
|
81
|
+
await db.data.get('table', 'id'); // Get one
|
|
82
|
+
await db.data.create('table', { ... }); // Create
|
|
83
|
+
await db.data.replace('table', 'id', { ... }); // Full replace (PUT)
|
|
84
|
+
await db.data.delete('table', 'id'); // Delete
|
|
85
|
+
await db.data.bulkCreate('table', [{ ... }, ...]); // Bulk create
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `db.blobs`
|
|
89
|
+
|
|
90
|
+
Binary blob operations:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
const ref = await db.blobs.upload(uint8Array, 'image/png');
|
|
94
|
+
const { data, contentType } = await db.blobs.download(ref);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### `db.asUser(claims)`
|
|
98
|
+
|
|
99
|
+
Returns a new `DatabaseSession` impersonating a user:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
const userDb = db.asUser({ sub: 'user@example.com', email: 'user@example.com' });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Client Initialization
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
// Simple
|
|
109
|
+
const client = new DexieCloudClient('https://dexie.cloud');
|
|
110
|
+
|
|
111
|
+
// Full config
|
|
112
|
+
const client = new DexieCloudClient({
|
|
113
|
+
serviceUrl: 'https://dexie.cloud',
|
|
114
|
+
dbUrl: 'https://xxxxxxxx.dexie.cloud',
|
|
115
|
+
blobHandling: 'auto', // 'auto' | 'lazy'
|
|
116
|
+
maxStringLength: 32768, // Strings longer than this are offloaded to blobs
|
|
117
|
+
timeout: 30000,
|
|
118
|
+
debug: true,
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Health Monitoring
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
await client.waitForReady(60000);
|
|
126
|
+
const status = await client.getStatus();
|
|
127
|
+
const isReady = await client.isReady();
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Error Handling
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import {
|
|
134
|
+
DexieCloudError,
|
|
135
|
+
DexieCloudAuthError,
|
|
136
|
+
DexieCloudNetworkError,
|
|
137
|
+
} from 'dexie-cloud-sdk';
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
await db.data.list('todoItems');
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error instanceof DexieCloudAuthError) {
|
|
143
|
+
console.log('Auth failed:', error.message);
|
|
144
|
+
} else if (error instanceof DexieCloudError) {
|
|
145
|
+
console.log('API error:', error.status, error.message);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## E2E Testing
|
|
151
|
+
|
|
152
|
+
The repo includes a Docker-based test stack:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
# Start stack
|
|
156
|
+
docker compose -f docker-compose.test.yml up -d
|
|
157
|
+
|
|
158
|
+
# Run E2E tests
|
|
159
|
+
pnpm run test:e2e
|
|
160
|
+
|
|
161
|
+
# Stop
|
|
162
|
+
docker compose -f docker-compose.test.yml down -v
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
MIT — see [LICENSE](LICENSE) for details.
|
|
168
|
+
|
|
169
|
+
## Links
|
|
170
|
+
|
|
171
|
+
- [Dexie.js](https://dexie.org) — IndexedDB wrapper
|
|
172
|
+
- [Dexie Cloud](https://dexie.org/cloud) — Sync service
|
|
173
|
+
- [SDK Documentation](https://dexie.org/docs/cloud/sdk) — Full docs
|
|
174
|
+
- [GitHub](https://github.com/dexie/dexie-cloud-sdk) — Source code
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
type BlobHandling = 'auto' | 'lazy';
|
|
2
|
+
interface DexieCloudConfig {
|
|
3
|
+
serviceUrl: string;
|
|
4
|
+
dbUrl?: string;
|
|
5
|
+
blobHandling?: BlobHandling;
|
|
6
|
+
maxStringLength?: number;
|
|
7
|
+
fetch?: typeof globalThis.fetch;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface OTPRequest {
|
|
12
|
+
email: string;
|
|
13
|
+
scopes?: string[];
|
|
14
|
+
}
|
|
15
|
+
interface OTPVerification {
|
|
16
|
+
email: string;
|
|
17
|
+
otpId: string;
|
|
18
|
+
otp: string;
|
|
19
|
+
scopes?: string[];
|
|
20
|
+
}
|
|
21
|
+
interface TokenResponse {
|
|
22
|
+
type: 'otp-sent' | 'tokens';
|
|
23
|
+
otp_id?: string;
|
|
24
|
+
accessToken?: string;
|
|
25
|
+
refreshToken?: string;
|
|
26
|
+
userId?: string;
|
|
27
|
+
claims?: Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
interface DatabaseInfo {
|
|
30
|
+
url: string;
|
|
31
|
+
clientId: string;
|
|
32
|
+
clientSecret: string;
|
|
33
|
+
}
|
|
34
|
+
interface CreateDatabaseOptions {
|
|
35
|
+
timeZone?: string;
|
|
36
|
+
hackathon?: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface AuthTokens {
|
|
39
|
+
accessToken: string;
|
|
40
|
+
refreshToken?: string;
|
|
41
|
+
userId?: string;
|
|
42
|
+
}
|
|
43
|
+
interface DatabaseTokens {
|
|
44
|
+
accessToken: string;
|
|
45
|
+
refreshToken: string;
|
|
46
|
+
userId: string;
|
|
47
|
+
}
|
|
48
|
+
interface HealthStatus {
|
|
49
|
+
healthy: boolean;
|
|
50
|
+
ready: boolean;
|
|
51
|
+
}
|
|
52
|
+
interface BlobRef {
|
|
53
|
+
_bt: string;
|
|
54
|
+
ref: string;
|
|
55
|
+
size: number;
|
|
56
|
+
ct?: string;
|
|
57
|
+
}
|
|
58
|
+
interface InlineBlob {
|
|
59
|
+
_bt: string;
|
|
60
|
+
v: string;
|
|
61
|
+
ct?: string;
|
|
62
|
+
}
|
|
63
|
+
declare class DexieCloudError extends Error {
|
|
64
|
+
readonly status?: number | undefined;
|
|
65
|
+
readonly response?: string | undefined;
|
|
66
|
+
constructor(message: string, status?: number | undefined, response?: string | undefined);
|
|
67
|
+
}
|
|
68
|
+
declare class DexieCloudAuthError extends DexieCloudError {
|
|
69
|
+
constructor(message: string, status?: number);
|
|
70
|
+
}
|
|
71
|
+
declare class DexieCloudNetworkError extends DexieCloudError {
|
|
72
|
+
constructor(message: string);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface HttpAdapter {
|
|
76
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
77
|
+
}
|
|
78
|
+
declare class FetchAdapter implements HttpAdapter {
|
|
79
|
+
private config;
|
|
80
|
+
private fetchImpl;
|
|
81
|
+
constructor(config: DexieCloudConfig, fetchImpl?: typeof globalThis.fetch);
|
|
82
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
83
|
+
}
|
|
84
|
+
declare class NodeAdapter implements HttpAdapter {
|
|
85
|
+
private config;
|
|
86
|
+
constructor(config: DexieCloudConfig);
|
|
87
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
88
|
+
}
|
|
89
|
+
declare function createAdapter(config: DexieCloudConfig): HttpAdapter;
|
|
90
|
+
|
|
91
|
+
declare class AuthManager {
|
|
92
|
+
private config;
|
|
93
|
+
private http;
|
|
94
|
+
constructor(config: DexieCloudConfig, http: HttpAdapter);
|
|
95
|
+
private get serviceUrl();
|
|
96
|
+
requestOTP(email: string, scopes?: string[]): Promise<string>;
|
|
97
|
+
verifyOTP(email: string, otpId: string, otp: string, scopes?: string[]): Promise<AuthTokens>;
|
|
98
|
+
authenticateWithOTP(email: string, getOTP: () => Promise<string>, scopes?: string[]): Promise<AuthTokens>;
|
|
99
|
+
requestDatabaseOTP(dbUrl: string, email: string): Promise<void>;
|
|
100
|
+
verifyDatabaseOTP(dbUrl: string, email: string, otp: string): Promise<DatabaseTokens>;
|
|
101
|
+
authenticateDatabase(dbUrl: string, email: string, getOTP: () => Promise<string>): Promise<DatabaseTokens>;
|
|
102
|
+
authenticateWithClientCredentials(dbUrl: string, clientId: string, clientSecret: string, scopes?: string[]): Promise<AuthTokens>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
declare class DatabaseManager {
|
|
106
|
+
private config;
|
|
107
|
+
private http;
|
|
108
|
+
constructor(config: DexieCloudConfig, http: HttpAdapter);
|
|
109
|
+
private get serviceUrl();
|
|
110
|
+
create(accessToken: string, options?: CreateDatabaseOptions): Promise<DatabaseInfo>;
|
|
111
|
+
list(accessToken: string): Promise<DatabaseInfo[]>;
|
|
112
|
+
getInfo(dbUrl: string, accessToken: string): Promise<DatabaseInfo>;
|
|
113
|
+
delete(dbUrl: string, accessToken: string): Promise<void>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
declare class HealthManager {
|
|
117
|
+
private config;
|
|
118
|
+
private http;
|
|
119
|
+
constructor(config: DexieCloudConfig, http: HttpAdapter);
|
|
120
|
+
health(): Promise<boolean>;
|
|
121
|
+
ready(): Promise<boolean>;
|
|
122
|
+
status(): Promise<HealthStatus>;
|
|
123
|
+
waitForReady(timeout?: number, interval?: number): Promise<void>;
|
|
124
|
+
waitForHealth(timeout?: number, interval?: number): Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare class BlobManager {
|
|
128
|
+
private dbUrl;
|
|
129
|
+
private http;
|
|
130
|
+
private mode;
|
|
131
|
+
private maxStringLength;
|
|
132
|
+
constructor(dbUrl: string, http: HttpAdapter, mode?: BlobHandling, maxStringLength?: number);
|
|
133
|
+
upload(data: Uint8Array | Blob | ArrayBuffer | ArrayBufferView, contentType: string | undefined, token: string): Promise<string>;
|
|
134
|
+
download(ref: string, token: string): Promise<{
|
|
135
|
+
data: Uint8Array;
|
|
136
|
+
contentType: string;
|
|
137
|
+
}>;
|
|
138
|
+
processForUpload(obj: any, token: string): Promise<any>;
|
|
139
|
+
processForRead(obj: any, token: string): Promise<any>;
|
|
140
|
+
private _walkForUpload;
|
|
141
|
+
private _walkForRead;
|
|
142
|
+
private _parallelMap;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type DataAccess = 'my' | 'all' | 'public';
|
|
146
|
+
declare class DataManager {
|
|
147
|
+
private dbUrl;
|
|
148
|
+
private http;
|
|
149
|
+
private blobManager?;
|
|
150
|
+
private access;
|
|
151
|
+
constructor(dbUrl: string, http: HttpAdapter, blobManager?: BlobManager | undefined, access?: DataAccess);
|
|
152
|
+
private tableUrl;
|
|
153
|
+
private itemUrl;
|
|
154
|
+
list(table: string, token: string, options?: {
|
|
155
|
+
realm?: string;
|
|
156
|
+
}): Promise<any[]>;
|
|
157
|
+
get(table: string, id: string, token: string): Promise<any>;
|
|
158
|
+
create(table: string, obj: any, token: string): Promise<{
|
|
159
|
+
id: string;
|
|
160
|
+
}>;
|
|
161
|
+
replace(table: string, id: string, obj: any, token: string): Promise<{
|
|
162
|
+
id: string;
|
|
163
|
+
}>;
|
|
164
|
+
update(table: string, id: string, obj: any, token: string): Promise<any>;
|
|
165
|
+
delete(table: string, id: string, token: string): Promise<void>;
|
|
166
|
+
bulkCreate(table: string, objects: any[], token: string): Promise<any[]>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface ImpersonateClaims {
|
|
170
|
+
sub?: string;
|
|
171
|
+
email?: string;
|
|
172
|
+
[key: string]: any;
|
|
173
|
+
}
|
|
174
|
+
interface DatabaseCredentials {
|
|
175
|
+
clientId: string;
|
|
176
|
+
clientSecret: string;
|
|
177
|
+
impersonate?: ImpersonateClaims;
|
|
178
|
+
}
|
|
179
|
+
interface DataSessionProxy {
|
|
180
|
+
list(table: string, options?: {
|
|
181
|
+
realm?: string;
|
|
182
|
+
}): Promise<any[]>;
|
|
183
|
+
get(table: string, id: string): Promise<any>;
|
|
184
|
+
create(table: string, obj: any): Promise<any>;
|
|
185
|
+
replace(table: string, id: string, obj: any): Promise<any>;
|
|
186
|
+
update(table: string, id: string, obj: any): Promise<any>;
|
|
187
|
+
delete(table: string, id: string): Promise<void>;
|
|
188
|
+
bulkCreate(table: string, objects: any[]): Promise<any[]>;
|
|
189
|
+
}
|
|
190
|
+
interface BlobSessionProxy {
|
|
191
|
+
upload(data: Uint8Array | Blob | ArrayBuffer | ArrayBufferView, contentType?: string): Promise<string>;
|
|
192
|
+
download(ref: string): Promise<{
|
|
193
|
+
data: Uint8Array;
|
|
194
|
+
contentType: string;
|
|
195
|
+
}>;
|
|
196
|
+
processForUpload(obj: any): Promise<any>;
|
|
197
|
+
processForRead(obj: any): Promise<any>;
|
|
198
|
+
}
|
|
199
|
+
declare class DatabaseSession {
|
|
200
|
+
private dbUrl;
|
|
201
|
+
private credentials;
|
|
202
|
+
private http;
|
|
203
|
+
readonly data: DataSessionProxy;
|
|
204
|
+
readonly blobs: BlobSessionProxy;
|
|
205
|
+
private inflightToken;
|
|
206
|
+
constructor(dbUrl: string, credentials: DatabaseCredentials, http: HttpAdapter, blobManager: BlobManager, dataManager: DataManager);
|
|
207
|
+
asUser(claims: ImpersonateClaims): DatabaseSession;
|
|
208
|
+
private getToken;
|
|
209
|
+
private fetchToken;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
declare class DexieCloudClient {
|
|
213
|
+
readonly auth: AuthManager;
|
|
214
|
+
readonly databases: DatabaseManager;
|
|
215
|
+
readonly health: HealthManager;
|
|
216
|
+
readonly data: DataManager;
|
|
217
|
+
readonly blobs: BlobManager;
|
|
218
|
+
private readonly http;
|
|
219
|
+
constructor(config: DexieCloudConfig | string);
|
|
220
|
+
createDatabase(email: string, getOTP: () => Promise<string>, options?: CreateDatabaseOptions): Promise<DatabaseInfo & {
|
|
221
|
+
accessToken: string;
|
|
222
|
+
}>;
|
|
223
|
+
isReady(): Promise<boolean>;
|
|
224
|
+
getStatus(): Promise<HealthStatus>;
|
|
225
|
+
waitForReady(timeout?: number): Promise<void>;
|
|
226
|
+
db(dbUrl: string, credentials: DatabaseCredentials): DatabaseSession;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
declare const TSON: {
|
|
230
|
+
stringify(value: any, alternateChannel?: any, space?: number): string;
|
|
231
|
+
parse(tson: string, alternateChannel?: any): any;
|
|
232
|
+
};
|
|
233
|
+
declare function stringify(value: any, space?: number): string;
|
|
234
|
+
declare function parse<T = any>(text: string): T;
|
|
235
|
+
|
|
236
|
+
export { AuthManager, type AuthTokens, type BlobHandling, BlobManager, type BlobRef, type BlobSessionProxy, type CreateDatabaseOptions, DataManager, type DataSessionProxy, type DatabaseCredentials, type DatabaseInfo, DatabaseManager, DatabaseSession, type DatabaseTokens, DexieCloudAuthError, DexieCloudClient, type DexieCloudConfig, DexieCloudError, DexieCloudNetworkError, FetchAdapter, HealthManager, type HealthStatus, type HttpAdapter, type ImpersonateClaims, type InlineBlob, NodeAdapter, type OTPRequest, type OTPVerification, TSON, type TokenResponse, createAdapter, DexieCloudClient as default, parse, stringify };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
type BlobHandling = 'auto' | 'lazy';
|
|
2
|
+
interface DexieCloudConfig {
|
|
3
|
+
serviceUrl: string;
|
|
4
|
+
dbUrl?: string;
|
|
5
|
+
blobHandling?: BlobHandling;
|
|
6
|
+
maxStringLength?: number;
|
|
7
|
+
fetch?: typeof globalThis.fetch;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface OTPRequest {
|
|
12
|
+
email: string;
|
|
13
|
+
scopes?: string[];
|
|
14
|
+
}
|
|
15
|
+
interface OTPVerification {
|
|
16
|
+
email: string;
|
|
17
|
+
otpId: string;
|
|
18
|
+
otp: string;
|
|
19
|
+
scopes?: string[];
|
|
20
|
+
}
|
|
21
|
+
interface TokenResponse {
|
|
22
|
+
type: 'otp-sent' | 'tokens';
|
|
23
|
+
otp_id?: string;
|
|
24
|
+
accessToken?: string;
|
|
25
|
+
refreshToken?: string;
|
|
26
|
+
userId?: string;
|
|
27
|
+
claims?: Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
interface DatabaseInfo {
|
|
30
|
+
url: string;
|
|
31
|
+
clientId: string;
|
|
32
|
+
clientSecret: string;
|
|
33
|
+
}
|
|
34
|
+
interface CreateDatabaseOptions {
|
|
35
|
+
timeZone?: string;
|
|
36
|
+
hackathon?: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface AuthTokens {
|
|
39
|
+
accessToken: string;
|
|
40
|
+
refreshToken?: string;
|
|
41
|
+
userId?: string;
|
|
42
|
+
}
|
|
43
|
+
interface DatabaseTokens {
|
|
44
|
+
accessToken: string;
|
|
45
|
+
refreshToken: string;
|
|
46
|
+
userId: string;
|
|
47
|
+
}
|
|
48
|
+
interface HealthStatus {
|
|
49
|
+
healthy: boolean;
|
|
50
|
+
ready: boolean;
|
|
51
|
+
}
|
|
52
|
+
interface BlobRef {
|
|
53
|
+
_bt: string;
|
|
54
|
+
ref: string;
|
|
55
|
+
size: number;
|
|
56
|
+
ct?: string;
|
|
57
|
+
}
|
|
58
|
+
interface InlineBlob {
|
|
59
|
+
_bt: string;
|
|
60
|
+
v: string;
|
|
61
|
+
ct?: string;
|
|
62
|
+
}
|
|
63
|
+
declare class DexieCloudError extends Error {
|
|
64
|
+
readonly status?: number | undefined;
|
|
65
|
+
readonly response?: string | undefined;
|
|
66
|
+
constructor(message: string, status?: number | undefined, response?: string | undefined);
|
|
67
|
+
}
|
|
68
|
+
declare class DexieCloudAuthError extends DexieCloudError {
|
|
69
|
+
constructor(message: string, status?: number);
|
|
70
|
+
}
|
|
71
|
+
declare class DexieCloudNetworkError extends DexieCloudError {
|
|
72
|
+
constructor(message: string);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface HttpAdapter {
|
|
76
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
77
|
+
}
|
|
78
|
+
declare class FetchAdapter implements HttpAdapter {
|
|
79
|
+
private config;
|
|
80
|
+
private fetchImpl;
|
|
81
|
+
constructor(config: DexieCloudConfig, fetchImpl?: typeof globalThis.fetch);
|
|
82
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
83
|
+
}
|
|
84
|
+
declare class NodeAdapter implements HttpAdapter {
|
|
85
|
+
private config;
|
|
86
|
+
constructor(config: DexieCloudConfig);
|
|
87
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
88
|
+
}
|
|
89
|
+
declare function createAdapter(config: DexieCloudConfig): HttpAdapter;
|
|
90
|
+
|
|
91
|
+
declare class AuthManager {
|
|
92
|
+
private config;
|
|
93
|
+
private http;
|
|
94
|
+
constructor(config: DexieCloudConfig, http: HttpAdapter);
|
|
95
|
+
private get serviceUrl();
|
|
96
|
+
requestOTP(email: string, scopes?: string[]): Promise<string>;
|
|
97
|
+
verifyOTP(email: string, otpId: string, otp: string, scopes?: string[]): Promise<AuthTokens>;
|
|
98
|
+
authenticateWithOTP(email: string, getOTP: () => Promise<string>, scopes?: string[]): Promise<AuthTokens>;
|
|
99
|
+
requestDatabaseOTP(dbUrl: string, email: string): Promise<void>;
|
|
100
|
+
verifyDatabaseOTP(dbUrl: string, email: string, otp: string): Promise<DatabaseTokens>;
|
|
101
|
+
authenticateDatabase(dbUrl: string, email: string, getOTP: () => Promise<string>): Promise<DatabaseTokens>;
|
|
102
|
+
authenticateWithClientCredentials(dbUrl: string, clientId: string, clientSecret: string, scopes?: string[]): Promise<AuthTokens>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
declare class DatabaseManager {
|
|
106
|
+
private config;
|
|
107
|
+
private http;
|
|
108
|
+
constructor(config: DexieCloudConfig, http: HttpAdapter);
|
|
109
|
+
private get serviceUrl();
|
|
110
|
+
create(accessToken: string, options?: CreateDatabaseOptions): Promise<DatabaseInfo>;
|
|
111
|
+
list(accessToken: string): Promise<DatabaseInfo[]>;
|
|
112
|
+
getInfo(dbUrl: string, accessToken: string): Promise<DatabaseInfo>;
|
|
113
|
+
delete(dbUrl: string, accessToken: string): Promise<void>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
declare class HealthManager {
|
|
117
|
+
private config;
|
|
118
|
+
private http;
|
|
119
|
+
constructor(config: DexieCloudConfig, http: HttpAdapter);
|
|
120
|
+
health(): Promise<boolean>;
|
|
121
|
+
ready(): Promise<boolean>;
|
|
122
|
+
status(): Promise<HealthStatus>;
|
|
123
|
+
waitForReady(timeout?: number, interval?: number): Promise<void>;
|
|
124
|
+
waitForHealth(timeout?: number, interval?: number): Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare class BlobManager {
|
|
128
|
+
private dbUrl;
|
|
129
|
+
private http;
|
|
130
|
+
private mode;
|
|
131
|
+
private maxStringLength;
|
|
132
|
+
constructor(dbUrl: string, http: HttpAdapter, mode?: BlobHandling, maxStringLength?: number);
|
|
133
|
+
upload(data: Uint8Array | Blob | ArrayBuffer | ArrayBufferView, contentType: string | undefined, token: string): Promise<string>;
|
|
134
|
+
download(ref: string, token: string): Promise<{
|
|
135
|
+
data: Uint8Array;
|
|
136
|
+
contentType: string;
|
|
137
|
+
}>;
|
|
138
|
+
processForUpload(obj: any, token: string): Promise<any>;
|
|
139
|
+
processForRead(obj: any, token: string): Promise<any>;
|
|
140
|
+
private _walkForUpload;
|
|
141
|
+
private _walkForRead;
|
|
142
|
+
private _parallelMap;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type DataAccess = 'my' | 'all' | 'public';
|
|
146
|
+
declare class DataManager {
|
|
147
|
+
private dbUrl;
|
|
148
|
+
private http;
|
|
149
|
+
private blobManager?;
|
|
150
|
+
private access;
|
|
151
|
+
constructor(dbUrl: string, http: HttpAdapter, blobManager?: BlobManager | undefined, access?: DataAccess);
|
|
152
|
+
private tableUrl;
|
|
153
|
+
private itemUrl;
|
|
154
|
+
list(table: string, token: string, options?: {
|
|
155
|
+
realm?: string;
|
|
156
|
+
}): Promise<any[]>;
|
|
157
|
+
get(table: string, id: string, token: string): Promise<any>;
|
|
158
|
+
create(table: string, obj: any, token: string): Promise<{
|
|
159
|
+
id: string;
|
|
160
|
+
}>;
|
|
161
|
+
replace(table: string, id: string, obj: any, token: string): Promise<{
|
|
162
|
+
id: string;
|
|
163
|
+
}>;
|
|
164
|
+
update(table: string, id: string, obj: any, token: string): Promise<any>;
|
|
165
|
+
delete(table: string, id: string, token: string): Promise<void>;
|
|
166
|
+
bulkCreate(table: string, objects: any[], token: string): Promise<any[]>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface ImpersonateClaims {
|
|
170
|
+
sub?: string;
|
|
171
|
+
email?: string;
|
|
172
|
+
[key: string]: any;
|
|
173
|
+
}
|
|
174
|
+
interface DatabaseCredentials {
|
|
175
|
+
clientId: string;
|
|
176
|
+
clientSecret: string;
|
|
177
|
+
impersonate?: ImpersonateClaims;
|
|
178
|
+
}
|
|
179
|
+
interface DataSessionProxy {
|
|
180
|
+
list(table: string, options?: {
|
|
181
|
+
realm?: string;
|
|
182
|
+
}): Promise<any[]>;
|
|
183
|
+
get(table: string, id: string): Promise<any>;
|
|
184
|
+
create(table: string, obj: any): Promise<any>;
|
|
185
|
+
replace(table: string, id: string, obj: any): Promise<any>;
|
|
186
|
+
update(table: string, id: string, obj: any): Promise<any>;
|
|
187
|
+
delete(table: string, id: string): Promise<void>;
|
|
188
|
+
bulkCreate(table: string, objects: any[]): Promise<any[]>;
|
|
189
|
+
}
|
|
190
|
+
interface BlobSessionProxy {
|
|
191
|
+
upload(data: Uint8Array | Blob | ArrayBuffer | ArrayBufferView, contentType?: string): Promise<string>;
|
|
192
|
+
download(ref: string): Promise<{
|
|
193
|
+
data: Uint8Array;
|
|
194
|
+
contentType: string;
|
|
195
|
+
}>;
|
|
196
|
+
processForUpload(obj: any): Promise<any>;
|
|
197
|
+
processForRead(obj: any): Promise<any>;
|
|
198
|
+
}
|
|
199
|
+
declare class DatabaseSession {
|
|
200
|
+
private dbUrl;
|
|
201
|
+
private credentials;
|
|
202
|
+
private http;
|
|
203
|
+
readonly data: DataSessionProxy;
|
|
204
|
+
readonly blobs: BlobSessionProxy;
|
|
205
|
+
private inflightToken;
|
|
206
|
+
constructor(dbUrl: string, credentials: DatabaseCredentials, http: HttpAdapter, blobManager: BlobManager, dataManager: DataManager);
|
|
207
|
+
asUser(claims: ImpersonateClaims): DatabaseSession;
|
|
208
|
+
private getToken;
|
|
209
|
+
private fetchToken;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
declare class DexieCloudClient {
|
|
213
|
+
readonly auth: AuthManager;
|
|
214
|
+
readonly databases: DatabaseManager;
|
|
215
|
+
readonly health: HealthManager;
|
|
216
|
+
readonly data: DataManager;
|
|
217
|
+
readonly blobs: BlobManager;
|
|
218
|
+
private readonly http;
|
|
219
|
+
constructor(config: DexieCloudConfig | string);
|
|
220
|
+
createDatabase(email: string, getOTP: () => Promise<string>, options?: CreateDatabaseOptions): Promise<DatabaseInfo & {
|
|
221
|
+
accessToken: string;
|
|
222
|
+
}>;
|
|
223
|
+
isReady(): Promise<boolean>;
|
|
224
|
+
getStatus(): Promise<HealthStatus>;
|
|
225
|
+
waitForReady(timeout?: number): Promise<void>;
|
|
226
|
+
db(dbUrl: string, credentials: DatabaseCredentials): DatabaseSession;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
declare const TSON: {
|
|
230
|
+
stringify(value: any, alternateChannel?: any, space?: number): string;
|
|
231
|
+
parse(tson: string, alternateChannel?: any): any;
|
|
232
|
+
};
|
|
233
|
+
declare function stringify(value: any, space?: number): string;
|
|
234
|
+
declare function parse<T = any>(text: string): T;
|
|
235
|
+
|
|
236
|
+
export { AuthManager, type AuthTokens, type BlobHandling, BlobManager, type BlobRef, type BlobSessionProxy, type CreateDatabaseOptions, DataManager, type DataSessionProxy, type DatabaseCredentials, type DatabaseInfo, DatabaseManager, DatabaseSession, type DatabaseTokens, DexieCloudAuthError, DexieCloudClient, type DexieCloudConfig, DexieCloudError, DexieCloudNetworkError, FetchAdapter, HealthManager, type HealthStatus, type HttpAdapter, type ImpersonateClaims, type InlineBlob, NodeAdapter, type OTPRequest, type OTPVerification, TSON, type TokenResponse, createAdapter, DexieCloudClient as default, parse, stringify };
|