@africode/core 5.0.10 → 5.0.11
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/core/bun-runtime.js +2 -1
- package/core/cli/commands/dev.js +12 -10
- package/core/file-router.js +5 -3
- package/core/server/router.js +90 -120
- package/dist/build-info.json +3 -3
- package/package.json +1 -1
- package/templates/starter/package.json +1 -1
- package/templates/starter-3d/package.json +1 -1
- package/templates/starter-dashboard/package.json +1 -1
- package/templates/starter-fintech/package.json +1 -1
- package/templates/starter-fintech/src/pages/api/dashboard.js +19 -0
- package/templates/starter-react/package.json +1 -1
- package/templates/starter-tailwind/package.json +1 -1
- package/templates/starter-website/package.json +1 -1
package/core/bun-runtime.js
CHANGED
|
@@ -203,7 +203,8 @@ export class BunHTTPServer {
|
|
|
203
203
|
return this.attachSessionCookie(response, sessionId);
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
const
|
|
206
|
+
const moduleUrl = pathToFileURL(route.filePath).href + '?t=' + Date.now();
|
|
207
|
+
const pageModule = await import(moduleUrl);
|
|
207
208
|
|
|
208
209
|
if (route.isApi) {
|
|
209
210
|
const method = req.method.toUpperCase();
|
package/core/cli/commands/dev.js
CHANGED
|
@@ -61,11 +61,13 @@ export async function dev() {
|
|
|
61
61
|
let pathname = url.pathname;
|
|
62
62
|
|
|
63
63
|
// 1. API Route Delegation
|
|
64
|
-
if (pathname.startsWith('/api
|
|
65
|
-
const { handleApiRequest } = await import('../../server/router.js');
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
if (pathname.startsWith('/api')) {
|
|
65
|
+
const { handleApiRequest } = await import('../../server/router.js');
|
|
66
|
+
const { existsSync } = await import('node:fs');
|
|
67
|
+
const pagesDir = existsSync(nodePath.join(process.cwd(), 'src/pages')) ? 'src/pages' : 'pages';
|
|
68
|
+
const apiRes = await handleApiRequest(req, { pagesDir });
|
|
69
|
+
if (apiRes) {return apiRes;}
|
|
70
|
+
}
|
|
69
71
|
|
|
70
72
|
// 2. Root Redirect
|
|
71
73
|
if (pathname === '/') {pathname = '/index';}
|
|
@@ -105,7 +107,7 @@ export async function dev() {
|
|
|
105
107
|
// 4. Fallback to Static HTML
|
|
106
108
|
if (await Bun.file(`./${pagesDir}${pathname}.html`).exists()) {
|
|
107
109
|
const { renderPage } = await import('../../server/render.js');
|
|
108
|
-
let response = await renderPage(`./${pagesDir}${pathname}.html`);
|
|
110
|
+
let response = await renderPage(`./${pagesDir}${pathname}.html`);
|
|
109
111
|
let htmlContent = await response.text();
|
|
110
112
|
// Inject HMR client
|
|
111
113
|
htmlContent = injectHMRClient(htmlContent);
|
|
@@ -117,7 +119,7 @@ export async function dev() {
|
|
|
117
119
|
if (await file.exists()) {
|
|
118
120
|
if (pathname.endsWith('.html')) {
|
|
119
121
|
const { renderPage } = await import('../../server/render.js');
|
|
120
|
-
let response = await renderPage('.' + pathname);
|
|
122
|
+
let response = await renderPage('.' + pathname);
|
|
121
123
|
let htmlContent = await response.text();
|
|
122
124
|
// Inject HMR client
|
|
123
125
|
htmlContent = injectHMRClient(htmlContent);
|
|
@@ -245,13 +247,13 @@ export async function dev() {
|
|
|
245
247
|
function watchDir(dir) {
|
|
246
248
|
try {
|
|
247
249
|
const watcher = watch(dir, { recursive: true }, (eventType, filename) => {
|
|
248
|
-
if (!filename) return;
|
|
250
|
+
if (!filename) return;
|
|
249
251
|
|
|
250
252
|
const dirName = filename.split(nodePath.sep)[0];
|
|
251
|
-
if (ignoreDirs.has(dirName) || filename.includes('node_modules')) return;
|
|
253
|
+
if (ignoreDirs.has(dirName) || filename.includes('node_modules')) return;
|
|
252
254
|
|
|
253
255
|
const validExts = ['.js', '.jsx', '.ts', '.tsx', '.css', '.html'];
|
|
254
|
-
if (!validExts.some(ext => filename.endsWith(ext))) return;
|
|
256
|
+
if (!validExts.some(ext => filename.endsWith(ext))) return;
|
|
255
257
|
|
|
256
258
|
console.log(`${colors.blue}[HMR] File changed: ${filename}${colors.reset}`);
|
|
257
259
|
|
package/core/file-router.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { FileSystemRouter } from './router.js';
|
|
7
|
-
import { join } from 'path';
|
|
7
|
+
import { join } from 'path';
|
|
8
|
+
import { pathToFileURL } from 'node:url';
|
|
8
9
|
|
|
9
10
|
export class FileBasedRouter {
|
|
10
11
|
constructor(options = {}) {
|
|
@@ -28,7 +29,8 @@ export class FileBasedRouter {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
async loadRoute(routeMatch) {
|
|
31
|
-
const
|
|
32
|
+
const moduleUrl = pathToFileURL(routeMatch.filePath).href;
|
|
33
|
+
const module = await import(moduleUrl);
|
|
32
34
|
if (routeMatch.type === "api") {
|
|
33
35
|
routeMatch.handler = module.default || module;
|
|
34
36
|
return routeMatch.handler;
|
|
@@ -47,4 +49,4 @@ export class FileBasedRouter {
|
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
export default FileBasedRouter;
|
|
52
|
+
export default FileBasedRouter;
|
package/core/server/router.js
CHANGED
|
@@ -1,120 +1,90 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AfriCode
|
|
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
|
-
async function executeHandler(module, req) {
|
|
43
|
-
const method = req.method.toUpperCase();
|
|
44
|
-
|
|
45
|
-
if (module[method]) {
|
|
46
|
-
return await module[method](req);
|
|
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
|
-
await
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
for (const [route, path] of routeCache.entries()) {
|
|
92
|
-
if (route.includes('[') && matchDynamicRoute(route, pathname)) {
|
|
93
|
-
modulePath = path;
|
|
94
|
-
// We would inject params into request here
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (modulePath) {
|
|
101
|
-
try {
|
|
102
|
-
// Import the module
|
|
103
|
-
const module = await import(modulePath);
|
|
104
|
-
return await executeHandler(module, req);
|
|
105
|
-
} catch (err) {
|
|
106
|
-
console.error(`Status 500 at ${pathname}:`, err);
|
|
107
|
-
return new Response(JSON.stringify({ error: err.message }), {
|
|
108
|
-
status: 500,
|
|
109
|
-
headers: { 'Content-Type': 'application/json' }
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return null; // Not captured -> 404 handled by server.js
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function matchDynamicRoute(routePattern, actualPath) {
|
|
118
|
-
// Placeholder logic for future expansion
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* AfriCode development API router.
|
|
3
|
+
*
|
|
4
|
+
* Uses the same recursive FileSystemRouter as the production runtime so
|
|
5
|
+
* `src/pages/api` and nested API routes work in `africode dev` too.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { FileSystemRouter } from '../router.js';
|
|
9
|
+
import { existsSync } from 'node:fs';
|
|
10
|
+
import { resolve } from 'node:path';
|
|
11
|
+
import { pathToFileURL } from 'node:url';
|
|
12
|
+
|
|
13
|
+
const routerCache = new Map();
|
|
14
|
+
|
|
15
|
+
function getRouter(pagesDir) {
|
|
16
|
+
const absolutePagesDir = resolve(process.cwd(), pagesDir);
|
|
17
|
+
let router = routerCache.get(absolutePagesDir);
|
|
18
|
+
|
|
19
|
+
if (!router) {
|
|
20
|
+
router = new FileSystemRouter(absolutePagesDir);
|
|
21
|
+
routerCache.set(absolutePagesDir, router);
|
|
22
|
+
for (const route of router.getRoutes().apis) {
|
|
23
|
+
console.log(`[AfriCode] Registered API route: ${route.pattern}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return router;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function getConfiguredPagesDir() {
|
|
31
|
+
const configPath = resolve(process.cwd(), 'africode.config.js');
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const config = (await import(configPath + '?t=' + Date.now())).default || {};
|
|
35
|
+
return config.directories?.pages || config.pagesDir ||
|
|
36
|
+
(existsSync(resolve(process.cwd(), 'src/pages')) ? 'src/pages' : 'pages');
|
|
37
|
+
} catch {
|
|
38
|
+
return existsSync(resolve(process.cwd(), 'src/pages')) ? 'src/pages' : 'pages';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function executeHandler(module, req) {
|
|
43
|
+
const method = req.method.toUpperCase();
|
|
44
|
+
|
|
45
|
+
if (module[method]) {
|
|
46
|
+
return await module[method](req);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (module.default) {
|
|
50
|
+
return await module.default(req);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return new Response('Method Not Allowed', { status: 405 });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function handleApiRequest(req, options = {}) {
|
|
57
|
+
const url = new URL(req.url);
|
|
58
|
+
const pathname = url.pathname;
|
|
59
|
+
|
|
60
|
+
if (pathname.startsWith('/api/auth/')) {
|
|
61
|
+
try {
|
|
62
|
+
const { handleAuthRequest } = await import('./auth-endpoints.js');
|
|
63
|
+
const authResponse = await handleAuthRequest(req, pathname);
|
|
64
|
+
if (authResponse) return authResponse;
|
|
65
|
+
} catch (err) {
|
|
66
|
+
console.error(`Auth endpoint error at ${pathname}:`, err);
|
|
67
|
+
return new Response(JSON.stringify({ error: err.message }), {
|
|
68
|
+
status: 500,
|
|
69
|
+
headers: { 'Content-Type': 'application/json' }
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const pagesDir = options.pagesDir || await getConfiguredPagesDir();
|
|
75
|
+
const route = getRouter(pagesDir).resolve(pathname);
|
|
76
|
+
|
|
77
|
+
if (!route?.isApi) return null;
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const moduleUrl = pathToFileURL(route.filePath).href + '?t=' + Date.now();
|
|
81
|
+
const module = await import(moduleUrl);
|
|
82
|
+
return await executeHandler(module, req);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.error(`Status 500 at ${pathname}:`, err);
|
|
85
|
+
return new Response(JSON.stringify({ error: err.message }), {
|
|
86
|
+
status: 500,
|
|
87
|
+
headers: { 'Content-Type': 'application/json' }
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/build-info.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"timestamp": "2026-07-
|
|
3
|
-
"version": "5.0.
|
|
2
|
+
"timestamp": "2026-07-24T08:35:45.511Z",
|
|
3
|
+
"version": "5.0.11",
|
|
4
4
|
"bundles": {
|
|
5
5
|
"core": {
|
|
6
6
|
"file": "africode.js",
|
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
]
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
|
-
"buildTime": "
|
|
22
|
+
"buildTime": "221.02ms"
|
|
23
23
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@africode/core",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.11",
|
|
4
4
|
"description": "Bun-native full-stack framework with generative AI, fintech compliance, and real-time performance - built for Tanzanian digital economy",
|
|
5
5
|
"module": "core/sdk.js",
|
|
6
6
|
"main": "core/sdk.js",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function json(data, status = 200) {
|
|
2
|
+
return new Response(JSON.stringify(data), {
|
|
3
|
+
status,
|
|
4
|
+
headers: { 'Content-Type': 'application/json' }
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function GET() {
|
|
9
|
+
return json({
|
|
10
|
+
ok: true,
|
|
11
|
+
dashboard: {
|
|
12
|
+
status: 'sandbox',
|
|
13
|
+
currency: 'TZS',
|
|
14
|
+
availableBalance: 1250000,
|
|
15
|
+
pendingTransfers: 1,
|
|
16
|
+
compliance: 'guarded'
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|