forked from base44/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.js
More file actions
86 lines (74 loc) · 2.48 KB
/
Copy pathbasic-usage.js
File metadata and controls
86 lines (74 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Import the SDK
import { createClient, Base44Error } from '../src';
// Example async function
async function exampleUsage() {
try {
// Create a client instance
const base44 = createClient({
appId: 'your-app-id',
// Optional parameters:
// serverUrl: 'https://api.base44.com',
// env: 'prod',
// token: 'your-auth-token'
});
// Check if the user is authenticated
const isAuthenticated = await base44.auth.isAuthenticated();
console.log('Authenticated:', isAuthenticated);
if (!isAuthenticated) {
console.log('User is not authenticated, please set a valid token');
// In a real application, you might redirect to login
// or set a token if you have one
// base44.setToken('your-token');
return;
}
// Get current user information
const me = await base44.auth.me();
console.log('Current user:', me);
// Working with entities
// List all Todo items
const todos = await base44.entities.Todo.list();
console.log('Todo items:', todos);
// Create a new Todo
const newTodo = await base44.entities.Todo.create({
title: 'Try Base44 SDK',
completed: false,
priority: 'high'
});
console.log('New Todo created:', newTodo);
// Update the Todo
const updatedTodo = await base44.entities.Todo.update(newTodo.id, {
completed: true
});
console.log('Todo updated:', updatedTodo);
// Filter Todos
const highPriorityTodos = await base44.entities.Todo.filter({
priority: 'high',
completed: false
});
console.log('High priority incomplete Todos:', highPriorityTodos);
// Using integrations
const emailResult = await base44.integrations.Core.SendEmail({
to: 'example@example.com',
subject: 'Testing Base44 SDK',
body: 'This is a test email from the Base44 SDK example'
});
console.log('Email sent:', emailResult);
// Clean up - delete the Todo we created
await base44.entities.Todo.delete(newTodo.id);
console.log('Todo deleted');
} catch (error) {
if (error instanceof Base44Error) {
console.error('Base44 API Error:');
console.error(`Status: ${error.status}`);
console.error(`Message: ${error.message}`);
console.error(`Code: ${error.code}`);
if (error.data) {
console.error(`Additional data: ${JSON.stringify(error.data, null, 2)}`);
}
} else {
console.error('Unexpected error:', error);
}
}
}
// Run the example
exampleUsage();