cocobase 0.1.1 → 0.1.2
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 +213 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ The official JavaScript/TypeScript SDK for Cocobase, a powerful Backend-as-a-Ser
|
|
|
8
8
|
npm install cocobase
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { Cocobase } from "cocobase";
|
|
@@ -18,6 +18,9 @@ const db = new Cocobase({
|
|
|
18
18
|
apiKey: "your-api-key",
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
+
// Initialize authentication (checks for existing session)
|
|
22
|
+
await db.initAuth();
|
|
23
|
+
|
|
21
24
|
// Create a document
|
|
22
25
|
const newUser = await db.createDocument("users", {
|
|
23
26
|
name: "Alice",
|
|
@@ -26,28 +29,156 @@ const newUser = await db.createDocument("users", {
|
|
|
26
29
|
|
|
27
30
|
// Get a document
|
|
28
31
|
const user = await db.getDocument("users", newUser.id);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Authentication
|
|
35
|
+
|
|
36
|
+
Cocobase SDK provides comprehensive authentication features including user registration, login, and session management.
|
|
37
|
+
|
|
38
|
+
### User Registration
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
// Register a new user
|
|
42
|
+
await db.register("user@example.com", "password123", {
|
|
43
|
+
name: "John Doe",
|
|
44
|
+
role: "user",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Check if user is authenticated
|
|
48
|
+
if (db.isAuthenticated()) {
|
|
49
|
+
console.log("User is logged in:", db.user);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### User Login
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// Login existing user
|
|
57
|
+
await db.login("user@example.com", "password123");
|
|
58
|
+
|
|
59
|
+
// Get current user info
|
|
60
|
+
const currentUser = await db.getCurrentUser();
|
|
61
|
+
console.log(currentUser);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Session Management
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// Initialize auth on app start (restores session from localStorage)
|
|
68
|
+
await db.initAuth();
|
|
69
|
+
|
|
70
|
+
// Logout user
|
|
71
|
+
db.logout();
|
|
72
|
+
|
|
73
|
+
// Check authentication status
|
|
74
|
+
const isLoggedIn = db.isAuthenticated();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### User Profile Management
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// Update user data
|
|
81
|
+
await db.updateUser(
|
|
82
|
+
{ name: "Jane Doe", preferences: { theme: "dark" } }, // data
|
|
83
|
+
"newemail@example.com", // email (optional)
|
|
84
|
+
"newpassword123" // password (optional)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// Update only specific fields
|
|
88
|
+
await db.updateUser({ lastLogin: new Date().toISOString() });
|
|
89
|
+
|
|
90
|
+
// Update email only
|
|
91
|
+
await db.updateUser(null, "updated@example.com");
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Document Management
|
|
95
|
+
|
|
96
|
+
### Basic Operations
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// Create a document
|
|
100
|
+
const newPost = await db.createDocument("posts", {
|
|
101
|
+
title: "My First Post",
|
|
102
|
+
content: "Hello, world!",
|
|
103
|
+
published: true,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Get a document
|
|
107
|
+
const post = await db.getDocument("posts", newPost.id);
|
|
29
108
|
|
|
30
109
|
// Update a document
|
|
31
|
-
await db.updateDocument("
|
|
32
|
-
|
|
110
|
+
await db.updateDocument("posts", newPost.id, {
|
|
111
|
+
title: "Updated Title",
|
|
112
|
+
published: false,
|
|
33
113
|
});
|
|
34
114
|
|
|
35
|
-
// List documents
|
|
36
|
-
const
|
|
115
|
+
// List all documents in a collection
|
|
116
|
+
const allPosts = await db.listDocuments("posts");
|
|
37
117
|
|
|
38
118
|
// Delete a document
|
|
39
|
-
await db.deleteDocument("
|
|
119
|
+
await db.deleteDocument("posts", newPost.id);
|
|
40
120
|
```
|
|
41
121
|
|
|
42
122
|
## API Reference
|
|
43
123
|
|
|
44
|
-
###
|
|
124
|
+
### Constructor
|
|
125
|
+
|
|
126
|
+
#### `new Cocobase(config)`
|
|
45
127
|
|
|
46
128
|
Creates a new Cocobase client instance.
|
|
47
129
|
|
|
48
|
-
|
|
130
|
+
**Parameters:**
|
|
131
|
+
|
|
132
|
+
- `config.apiKey` - Your Cocobase API key (optional for some operations)
|
|
133
|
+
|
|
134
|
+
### Authentication Methods
|
|
135
|
+
|
|
136
|
+
#### `initAuth(): Promise<void>`
|
|
137
|
+
|
|
138
|
+
Initializes authentication by checking for existing tokens in localStorage and restoring user session.
|
|
139
|
+
|
|
140
|
+
#### `register(email: string, password: string, data?: Record<string, any>): Promise<void>`
|
|
141
|
+
|
|
142
|
+
Registers a new user account.
|
|
143
|
+
|
|
144
|
+
**Parameters:**
|
|
145
|
+
|
|
146
|
+
- `email` - User's email address
|
|
147
|
+
- `password` - User's password
|
|
148
|
+
- `data` - Optional additional user data
|
|
149
|
+
|
|
150
|
+
#### `login(email: string, password: string): Promise<void>`
|
|
151
|
+
|
|
152
|
+
Authenticates an existing user.
|
|
49
153
|
|
|
50
|
-
|
|
154
|
+
**Parameters:**
|
|
155
|
+
|
|
156
|
+
- `email` - User's email address
|
|
157
|
+
- `password` - User's password
|
|
158
|
+
|
|
159
|
+
#### `logout(): void`
|
|
160
|
+
|
|
161
|
+
Logs out the current user and clears the session.
|
|
162
|
+
|
|
163
|
+
#### `isAuthenticated(): boolean`
|
|
164
|
+
|
|
165
|
+
Returns whether a user is currently authenticated.
|
|
166
|
+
|
|
167
|
+
#### `getCurrentUser(): Promise<AppUser>`
|
|
168
|
+
|
|
169
|
+
Retrieves the current authenticated user's information.
|
|
170
|
+
|
|
171
|
+
#### `updateUser(data?: Record<string, any> | null, email?: string | null, password?: string | null): Promise<AppUser>`
|
|
172
|
+
|
|
173
|
+
Updates the current user's profile information.
|
|
174
|
+
|
|
175
|
+
**Parameters:**
|
|
176
|
+
|
|
177
|
+
- `data` - User data to update (merged with existing data)
|
|
178
|
+
- `email` - New email address
|
|
179
|
+
- `password` - New password
|
|
180
|
+
|
|
181
|
+
### Document Methods
|
|
51
182
|
|
|
52
183
|
#### `createDocument<T>(collection: string, data: T): Promise<Document<T>>`
|
|
53
184
|
|
|
@@ -72,19 +203,87 @@ Lists all documents in a collection.
|
|
|
72
203
|
## Types
|
|
73
204
|
|
|
74
205
|
```typescript
|
|
206
|
+
interface CocobaseConfig {
|
|
207
|
+
apiKey?: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
interface Collection {
|
|
211
|
+
name: string;
|
|
212
|
+
id: string;
|
|
213
|
+
created_at: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
75
216
|
interface Document<T> {
|
|
76
217
|
data: T;
|
|
77
218
|
id: string;
|
|
78
219
|
collection_id: string;
|
|
79
220
|
created_at: string;
|
|
80
|
-
collection:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
221
|
+
collection: Collection;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
interface AppUser {
|
|
225
|
+
id: string;
|
|
226
|
+
email: string;
|
|
227
|
+
created_at: string;
|
|
228
|
+
data: Record<string, any>;
|
|
229
|
+
client_id: string;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
interface TokenResponse {
|
|
233
|
+
access_token: string;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Error Handling
|
|
238
|
+
|
|
239
|
+
The SDK provides detailed error information for debugging:
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
try {
|
|
243
|
+
await db.createDocument("posts", { title: "Test" });
|
|
244
|
+
} catch (error) {
|
|
245
|
+
console.error("Request failed:", error.message);
|
|
246
|
+
// Error includes status code, URL, method, and suggestions
|
|
85
247
|
}
|
|
86
248
|
```
|
|
87
249
|
|
|
250
|
+
## Complete Example
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
import { Cocobase } from "cocobase";
|
|
254
|
+
|
|
255
|
+
const db = new Cocobase({
|
|
256
|
+
apiKey: "your-api-key",
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
async function main() {
|
|
260
|
+
// Initialize auth
|
|
261
|
+
await db.initAuth();
|
|
262
|
+
|
|
263
|
+
if (!db.isAuthenticated()) {
|
|
264
|
+
// Register or login user
|
|
265
|
+
await db.register("user@example.com", "password123", {
|
|
266
|
+
name: "John Doe",
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Create a document
|
|
271
|
+
const post = await db.createDocument("posts", {
|
|
272
|
+
title: "Hello Cocobase",
|
|
273
|
+
content: "This is my first post!",
|
|
274
|
+
author: db.user?.id,
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
console.log("Created post:", post);
|
|
278
|
+
|
|
279
|
+
// List all posts
|
|
280
|
+
const posts = await db.listDocuments("posts");
|
|
281
|
+
console.log("All posts:", posts);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
main().catch(console.error);
|
|
285
|
+
```
|
|
286
|
+
|
|
88
287
|
## License
|
|
89
288
|
|
|
90
289
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cocobase",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "The Official cocobase sdk",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"repository": {
|
|
32
32
|
"type": "git",
|
|
33
|
-
"url": "git+https://github.com/
|
|
33
|
+
"url": "git+https://github.com/lordace-coder/coco_base_js.git"
|
|
34
34
|
},
|
|
35
35
|
"keywords": [
|
|
36
36
|
"baas",
|