baraqex 1.0.71 → 1.0.73

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.
@@ -1,271 +0,0 @@
1
- import { AuthProvider, useAuth } from './contexts/AuthContext';
2
- import { ApiProvider } from './contexts/ApiContext';
3
- import { Header } from './components/Header';
4
- import { Dashboard } from './components/Dashboard';
5
- import { LoginForm } from './components/LoginForm';
6
- import { TodoList } from './components/TodoList';
7
- import { UserProfile } from './components/UserProfile';
8
- import { LoadingSpinner } from './components/LoadingSpinner';
9
- import './styles/App.css';
10
- import { useState, useEffect } from 'baraqex';
11
-
12
- interface User {
13
- id: number;
14
- name: string;
15
- email: string;
16
- }
17
-
18
- type View = 'dashboard' | 'todos' | 'profile' | 'login';
19
-
20
- function AppContent() {
21
- const { user, loading: authLoading } = useAuth();
22
- const [currentView, setCurrentView] = useState<View>('dashboard');
23
- const [loading, setLoading] = useState(true);
24
- const [users, setUsers] = useState<User[]>([]);
25
- const [newUser, setNewUser] = useState({ name: '', email: '' });
26
- const [userLoading, setUserLoading] = useState(false);
27
-
28
- useEffect(() => {
29
- // Simulate app initialization
30
- const timer = setTimeout(() => {
31
- setLoading(false);
32
- }, 1000);
33
-
34
- return () => clearTimeout(timer);
35
- }, []);
36
-
37
- useEffect(() => {
38
- fetchUsers();
39
- }, []);
40
-
41
- const fetchUsers = async () => {
42
- try {
43
- const response = await fetch('/api/users');
44
- const data = await response.json();
45
- if (data.success) {
46
- setUsers(data.users);
47
- }
48
- } catch (error) {
49
- console.error('Error fetching users:', error);
50
- }
51
- };
52
-
53
- const handleSubmit = async (e: React.FormEvent) => {
54
- e.preventDefault();
55
- setUserLoading(true);
56
-
57
- try {
58
- const response = await fetch('/api/users', {
59
- method: 'POST',
60
- headers: {
61
- 'Content-Type': 'application/json',
62
- },
63
- body: JSON.stringify(newUser),
64
- });
65
-
66
- const data = await response.json();
67
-
68
- if (data.success) {
69
- setUsers([...users, data.user]);
70
- setNewUser({ name: '', email: '' });
71
- } else {
72
- alert('Error: ' + data.error);
73
- }
74
- } catch (error) {
75
- console.error('Error creating user:', error);
76
- alert('Error creating user');
77
- } finally {
78
- setUserLoading(false);
79
- }
80
- };
81
-
82
- if (loading || authLoading) {
83
- return <LoadingSpinner message="Loading application..." />;
84
- }
85
-
86
- if (!user) {
87
- return (
88
- <div className="app">
89
- <Header
90
- user={null}
91
- onNavigate={setCurrentView}
92
- currentView={currentView}
93
- />
94
- <main className="main-content">
95
- <LoginForm />
96
- </main>
97
- </div>
98
- );
99
- }
100
-
101
- const renderView = () => {
102
- switch (currentView) {
103
- case 'dashboard':
104
- return <Dashboard />;
105
- case 'todos':
106
- return <TodoList />;
107
- case 'profile':
108
- return <UserProfile />;
109
- default:
110
- return <Dashboard />;
111
- }
112
- };
113
-
114
- return (
115
- <div className="app">
116
- <Header
117
- user={user}
118
- onNavigate={setCurrentView}
119
- currentView={currentView}
120
- />
121
- <main className="main-content">
122
- {renderView()}
123
- </main>
124
- <footer className="app-footer">
125
- <p>&copy; 2024 Fullstack Baraqex App. Built with ❤️ using Baraqex Framework.</p>
126
- </footer>
127
-
128
- {/* Add User Form */}
129
- <div style={{
130
- background: '#f5f5f5',
131
- padding: '20px',
132
- borderRadius: '8px',
133
- marginBottom: '30px'
134
- }}>
135
- <h2 style={{ marginTop: '0', color: '#333' }}>Add New User</h2>
136
- <form onSubmit={handleSubmit}>
137
- <div style={{ marginBottom: '15px' }}>
138
- <input
139
- type="text"
140
- placeholder="Name"
141
- value={newUser.name}
142
- onChange={(e) => setNewUser({ ...newUser, name: e.target.value })}
143
- style={{
144
- width: '100%',
145
- padding: '10px',
146
- border: '1px solid #ddd',
147
- borderRadius: '4px',
148
- fontSize: '14px'
149
- }}
150
- required
151
- />
152
- </div>
153
- <div style={{ marginBottom: '15px' }}>
154
- <input
155
- type="email"
156
- placeholder="Email"
157
- value={newUser.email}
158
- onChange={(e) => setNewUser({ ...newUser, email: e.target.value })}
159
- style={{
160
- width: '100%',
161
- padding: '10px',
162
- border: '1px solid #ddd',
163
- borderRadius: '4px',
164
- fontSize: '14px'
165
- }}
166
- required
167
- />
168
- </div>
169
- <button
170
- type="submit"
171
- disabled={userLoading}
172
- style={{
173
- background: userLoading ? '#ccc' : '#007bff',
174
- color: 'white',
175
- border: 'none',
176
- padding: '10px 20px',
177
- borderRadius: '4px',
178
- cursor: userLoading ? 'not-allowed' : 'pointer',
179
- fontSize: '14px'
180
- }}
181
- >
182
- {userLoading ? 'Adding...' : 'Add User'}
183
- </button>
184
- </form>
185
- </div>
186
-
187
- {/* Users List */}
188
- <div>
189
- <h2 style={{ color: '#333' }}>Users ({users.length})</h2>
190
- {users.length === 0 ? (
191
- <p style={{ color: '#666', fontStyle: 'italic' }}>
192
- No users found. Add some users using the form above!
193
- </p>
194
- ) : (
195
- <div style={{ display: 'grid', gap: '10px' }}>
196
- {users.map((user) => (
197
- <div
198
- key={user.id}
199
- style={{
200
- background: 'white',
201
- border: '1px solid #ddd',
202
- borderRadius: '6px',
203
- padding: '15px',
204
- display: 'flex',
205
- justifyContent: 'space-between',
206
- alignItems: 'center'
207
- }}
208
- >
209
- <div>
210
- <strong style={{ color: '#333' }}>{user.name}</strong>
211
- <br />
212
- <span style={{ color: '#666', fontSize: '14px' }}>{user.email}</span>
213
- </div>
214
- <div style={{
215
- background: '#28a745',
216
- color: 'white',
217
- padding: '4px 8px',
218
- borderRadius: '4px',
219
- fontSize: '12px'
220
- }}>
221
- ID: {user.id}
222
- </div>
223
- </div>
224
- ))}
225
- </div>
226
- )}
227
- </div>
228
-
229
- {/* API Info */}
230
- <div style={{
231
- marginTop: '40px',
232
- padding: '20px',
233
- background: '#e7f3ff',
234
- borderRadius: '8px'
235
- }}>
236
- <h3 style={{ marginTop: '0', color: '#0066cc' }}>🔗 API Endpoints</h3>
237
- <ul style={{ color: '#333' }}>
238
- <li><code>GET /api/users</code> - Fetch all users</li>
239
- <li><code>POST /api/users</code> - Create a new user</li>
240
- <li><code>GET /api/hello</code> - Simple hello endpoint</li>
241
- </ul>
242
- <p style={{ color: '#666', fontSize: '14px', marginBottom: '0' }}>
243
- Try visiting <a href="/api/users" target="_blank">/api/users</a> or <a href="/api/hello" target="_blank">/api/hello</a> to see the API responses.
244
- </p>
245
- </div>
246
- </div>
247
- );
248
- }
249
-
250
- export default function App() {
251
- return (
252
- <AuthProvider>
253
- <ApiProvider>
254
- <AppContent />
255
- </ApiProvider>
256
- </AuthProvider>
257
- );
258
- }
259
-
260
- // SSR support
261
- App.getTitle = () => "Fullstack Baraqex App";
262
- App.getDescription = () => "A complete fullstack application with authentication, API integration, and modern UI";
263
- App.getMeta = () => ({
264
- 'og:title': 'Fullstack Baraqex App',
265
- 'og:description': 'Complete fullstack solution with Baraqex',
266
- 'og:type': 'website'
267
- });
268
-
269
- // Page metadata for SEO
270
- App.title = 'Baraqex Fullstack Demo';
271
- App.description = 'A demonstration of Baraqex fullstack capabilities with SSR and API integration';
@@ -1,75 +0,0 @@
1
- import { AuthService, sendSuccess, sendError, validateFields } from 'baraqex';
2
-
3
- // Login endpoint
4
- export async function post(req: any, res: any) {
5
- try {
6
- // Validate required fields
7
- const validation = validateFields(req, ['username', 'password']);
8
- if (!validation.valid) {
9
- return sendError(res, 'Missing required fields', 400, { missing: validation.missing });
10
- }
11
-
12
- const { username, password } = req.body;
13
-
14
- // Get auth service from server
15
- const authService = req.app.locals.authService as AuthService;
16
- if (!authService) {
17
- return sendError(res, 'Authentication service not available', 500);
18
- }
19
-
20
- // In a real app, you'd fetch user from database
21
- // This is a demo implementation
22
- const demoUsers = [
23
- {
24
- id: 1,
25
- username: 'admin',
26
- password: '$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
27
- email: 'admin@example.com',
28
- roles: ['admin', 'user'],
29
- name: 'Administrator'
30
- },
31
- {
32
- id: 2,
33
- username: 'user',
34
- password: '$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
35
- email: 'user@example.com',
36
- roles: ['user'],
37
- name: 'Regular User'
38
- }
39
- ];
40
-
41
- // Find user
42
- const user = demoUsers.find(u => u.username === username);
43
- if (!user) {
44
- return sendError(res, 'Invalid credentials', 401);
45
- }
46
-
47
- // Verify password
48
- const isValid = await authService.comparePasswords(password, user.password);
49
- if (!isValid) {
50
- return sendError(res, 'Invalid credentials', 401);
51
- }
52
-
53
- // Generate token
54
- const userForToken = {
55
- id: user.id,
56
- username: user.username,
57
- email: user.email,
58
- roles: user.roles,
59
- name: user.name
60
- };
61
-
62
- const token = authService.generateToken(userForToken);
63
-
64
- // Send response
65
- return sendSuccess(res, {
66
- token,
67
- user: userForToken,
68
- expiresIn: '7d'
69
- }, 'Login successful');
70
-
71
- } catch (error: any) {
72
- console.error('Login error:', error);
73
- return sendError(res, 'Login failed', 500);
74
- }
75
- }
@@ -1,66 +0,0 @@
1
- import { sendSuccess, sendError } from 'baraqex';
2
-
3
- // Middleware to require authentication
4
- export const middleware = [
5
- (req: any, res: any, next: any) => {
6
- if (!req.user) {
7
- return sendError(res, 'Authentication required', 401);
8
- }
9
- next();
10
- }
11
- ];
12
-
13
- // Get current user endpoint
14
- export async function get(req: any, res: any) {
15
- try {
16
- const user = req.user;
17
-
18
- // In a real app, you might fetch fresh user data from database
19
- // For demo, we'll just return the user from token
20
-
21
- return sendSuccess(res, {
22
- user: {
23
- id: user.id,
24
- username: user.username,
25
- email: user.email || `${user.username}@example.com`,
26
- name: user.name || user.username,
27
- roles: user.roles || ['user'],
28
- lastLogin: new Date().toISOString()
29
- }
30
- }, 'User profile retrieved');
31
-
32
- } catch (error: any) {
33
- console.error('Get user error:', error);
34
- return sendError(res, 'Failed to get user profile', 500);
35
- }
36
- }
37
-
38
- // Update user profile
39
- export async function put(req: any, res: any) {
40
- try {
41
- const user = req.user;
42
- const { name, email } = req.body;
43
-
44
- // Basic validation
45
- if (email && !email.includes('@')) {
46
- return sendError(res, 'Invalid email format', 400);
47
- }
48
-
49
- // In a real app, you'd update the database
50
- // For demo, we'll just return updated user data
51
- const updatedUser = {
52
- ...user,
53
- name: name || user.name,
54
- email: email || user.email,
55
- updatedAt: new Date().toISOString()
56
- };
57
-
58
- return sendSuccess(res, {
59
- user: updatedUser
60
- }, 'Profile updated successfully');
61
-
62
- } catch (error: any) {
63
- console.error('Update user error:', error);
64
- return sendError(res, 'Failed to update profile', 500);
65
- }
66
- }
@@ -1,59 +0,0 @@
1
- import { AuthService, sendSuccess, sendError, validateFields } from 'baraqex';
2
-
3
- // Registration endpoint
4
- export async function post(req: any, res: any) {
5
- try {
6
- // Validate required fields
7
- const validation = validateFields(req, ['username', 'password', 'email', 'name']);
8
- if (!validation.valid) {
9
- return sendError(res, 'Missing required fields', 400, { missing: validation.missing });
10
- }
11
-
12
- const { username, password, email, name } = req.body;
13
-
14
- // Basic validation
15
- if (password.length < 6) {
16
- return sendError(res, 'Password must be at least 6 characters long', 400);
17
- }
18
-
19
- if (!email.includes('@')) {
20
- return sendError(res, 'Invalid email format', 400);
21
- }
22
-
23
- // Get auth service
24
- const authService = req.app.locals.authService as AuthService;
25
- if (!authService) {
26
- return sendError(res, 'Authentication service not available', 500);
27
- }
28
-
29
- // In a real app, you'd check if user exists in database
30
- // For demo purposes, we'll just simulate registration
31
-
32
- // Hash password
33
- const hashedPassword = await authService.hashPassword(password);
34
-
35
- // Create user object (in real app, save to database)
36
- const newUser = {
37
- id: Date.now(), // In real app, this would be from database
38
- username,
39
- email,
40
- name,
41
- roles: ['user'],
42
- createdAt: new Date().toISOString()
43
- };
44
-
45
- // Generate token
46
- const token = authService.generateToken(newUser);
47
-
48
- // Send response
49
- return sendSuccess(res, {
50
- token,
51
- user: newUser,
52
- expiresIn: '7d'
53
- }, 'Registration successful');
54
-
55
- } catch (error: any) {
56
- console.error('Registration error:', error);
57
- return sendError(res, 'Registration failed', 500);
58
- }
59
- }
@@ -1,19 +0,0 @@
1
- // Example API route: GET /api/hello
2
- export async function get(req, res) {
3
- res.json({
4
- message: 'Hello from Baraqex API!',
5
- timestamp: new Date().toISOString(),
6
- method: 'GET'
7
- });
8
- }
9
-
10
- // POST /api/hello
11
- export async function post(req, res) {
12
- const { name } = req.body;
13
- res.json({
14
- message: `Hello ${name || 'World'}!`,
15
- timestamp: new Date().toISOString(),
16
- method: 'POST',
17
- body: req.body
18
- });
19
- }
@@ -1,108 +0,0 @@
1
- import { sendSuccess, sendError } from 'baraqex';
2
-
3
- // Import the todos array from the index file
4
- // In a real app, this would be in a database
5
- let todos = [
6
- { id: 1, text: 'Learn Baraqex Framework', completed: false, userId: 1, createdAt: new Date().toISOString() },
7
- { id: 2, text: 'Build awesome fullstack app', completed: false, userId: 1, createdAt: new Date().toISOString() },
8
- { id: 3, text: 'Deploy to production', completed: false, userId: 1, createdAt: new Date().toISOString() }
9
- ];
10
-
11
- // Require authentication
12
- export const middleware = [
13
- (req: any, res: any, next: any) => {
14
- if (!req.user) {
15
- return sendError(res, 'Authentication required', 401);
16
- }
17
- next();
18
- }
19
- ];
20
-
21
- // Get specific todo
22
- export async function get(req: any, res: any) {
23
- try {
24
- const todoId = parseInt(req.params.id);
25
- const userId = req.user.id;
26
-
27
- if (isNaN(todoId)) {
28
- return sendError(res, 'Invalid todo ID', 400);
29
- }
30
-
31
- const todo = todos.find(t => t.id === todoId && t.userId === userId);
32
- if (!todo) {
33
- return sendError(res, 'Todo not found', 404);
34
- }
35
-
36
- return sendSuccess(res, { todo }, 'Todo retrieved successfully');
37
-
38
- } catch (error: any) {
39
- console.error('Get todo error:', error);
40
- return sendError(res, 'Failed to get todo', 500);
41
- }
42
- }
43
-
44
- // Update todo
45
- export async function put(req: any, res: any) {
46
- try {
47
- const todoId = parseInt(req.params.id);
48
- const userId = req.user.id;
49
- const { text, completed } = req.body;
50
-
51
- if (isNaN(todoId)) {
52
- return sendError(res, 'Invalid todo ID', 400);
53
- }
54
-
55
- const todoIndex = todos.findIndex(t => t.id === todoId && t.userId === userId);
56
- if (todoIndex === -1) {
57
- return sendError(res, 'Todo not found', 404);
58
- }
59
-
60
- // Update todo
61
- if (text !== undefined) {
62
- if (text.trim().length === 0) {
63
- return sendError(res, 'Todo text cannot be empty', 400);
64
- }
65
- todos[todoIndex].text = text.trim();
66
- }
67
-
68
- if (completed !== undefined) {
69
- todos[todoIndex].completed = Boolean(completed);
70
- }
71
-
72
- todos[todoIndex].updatedAt = new Date().toISOString();
73
-
74
- return sendSuccess(res, { todo: todos[todoIndex] }, 'Todo updated successfully');
75
-
76
- } catch (error: any) {
77
- console.error('Update todo error:', error);
78
- return sendError(res, 'Failed to update todo', 500);
79
- }
80
- }
81
-
82
- // Delete todo
83
- export async function del(req: any, res: any) {
84
- try {
85
- const todoId = parseInt(req.params.id);
86
- const userId = req.user.id;
87
-
88
- if (isNaN(todoId)) {
89
- return sendError(res, 'Invalid todo ID', 400);
90
- }
91
-
92
- const todoIndex = todos.findIndex(t => t.id === todoId && t.userId === userId);
93
- if (todoIndex === -1) {
94
- return sendError(res, 'Todo not found', 404);
95
- }
96
-
97
- const deletedTodo = todos.splice(todoIndex, 1)[0];
98
-
99
- return sendSuccess(res, { todo: deletedTodo }, 'Todo deleted successfully');
100
-
101
- } catch (error: any) {
102
- console.error('Delete todo error:', error);
103
- return sendError(res, 'Failed to delete todo', 500);
104
- }
105
- }
106
-
107
- // Export delete function with correct name
108
- export { del as delete };
@@ -1,79 +0,0 @@
1
- import { sendSuccess, sendError, validateFields, getPagination } from 'baraqex';
2
-
3
- // In-memory storage for demo (use database in real app)
4
- let todos = [
5
- { id: 1, text: 'Learn Baraqex Framework', completed: false, userId: 1, createdAt: new Date().toISOString() },
6
- { id: 2, text: 'Build awesome fullstack app', completed: false, userId: 1, createdAt: new Date().toISOString() },
7
- { id: 3, text: 'Deploy to production', completed: false, userId: 1, createdAt: new Date().toISOString() }
8
- ];
9
-
10
- // Require authentication for all todo endpoints
11
- export const middleware = [
12
- (req: any, res: any, next: any) => {
13
- if (!req.user) {
14
- return sendError(res, 'Authentication required', 401);
15
- }
16
- next();
17
- }
18
- ];
19
-
20
- // Get todos for current user
21
- export async function get(req: any, res: any) {
22
- try {
23
- const userId = req.user.id;
24
- const { page, limit, skip } = getPagination(req);
25
-
26
- // Filter todos for current user
27
- const userTodos = todos.filter(todo => todo.userId === userId);
28
-
29
- // Apply pagination
30
- const paginatedTodos = userTodos.slice(skip, skip + limit);
31
-
32
- return sendSuccess(res, {
33
- todos: paginatedTodos,
34
- pagination: {
35
- page,
36
- limit,
37
- total: userTodos.length,
38
- pages: Math.ceil(userTodos.length / limit)
39
- }
40
- }, 'Todos retrieved successfully');
41
-
42
- } catch (error: any) {
43
- console.error('Get todos error:', error);
44
- return sendError(res, 'Failed to get todos', 500);
45
- }
46
- }
47
-
48
- // Create new todo
49
- export async function post(req: any, res: any) {
50
- try {
51
- const validation = validateFields(req, ['text']);
52
- if (!validation.valid) {
53
- return sendError(res, 'Missing required fields', 400, { missing: validation.missing });
54
- }
55
-
56
- const { text } = req.body;
57
- const userId = req.user.id;
58
-
59
- if (text.trim().length === 0) {
60
- return sendError(res, 'Todo text cannot be empty', 400);
61
- }
62
-
63
- const newTodo = {
64
- id: Date.now(),
65
- text: text.trim(),
66
- completed: false,
67
- userId,
68
- createdAt: new Date().toISOString()
69
- };
70
-
71
- todos.push(newTodo);
72
-
73
- return sendSuccess(res, { todo: newTodo }, 'Todo created successfully');
74
-
75
- } catch (error: any) {
76
- console.error('Create todo error:', error);
77
- return sendError(res, 'Failed to create todo', 500);
78
- }
79
- }