@africode/core 5.0.9 → 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.
@@ -203,7 +203,8 @@ export class BunHTTPServer {
203
203
  return this.attachSessionCookie(response, sessionId);
204
204
  }
205
205
 
206
- const pageModule = await import(route.filePath);
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();
@@ -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 apiRes = await handleApiRequest(req);
67
- if (apiRes) {return apiRes;}
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
 
@@ -820,11 +820,4 @@ export class AfricanPaymentCompliance {
820
820
  return validation;
821
821
  }
822
822
  }
823
- export {
824
- NIDAClient,
825
- TIPSClient,
826
- AMLComplianceEngine,
827
- ComplianceMiddleware,
828
- AfricanPaymentCompliance
829
- };
830
-
823
+
@@ -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 module = await import(routeMatch.filePath);
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;
@@ -1,120 +1,90 @@
1
- /**
2
- * AfriCode Server-Side Router
3
- *
4
- * Handles dynamic API routing by mapping URLs to the file system.
5
- * Edge-Ready: Uses strictly standard Request/Response objects.
6
- */
7
-
8
- const fs = require('fs');
9
- const path = require('path');
10
-
11
- // Cache routes in memory for performance
12
- const routeCache = new Map();
13
-
14
- /**
15
- * Discovers API routes in the /pages/api directory
16
- * Note: In a pure Edge environment (Cloudflare), this would be replaced
17
- * by a build-time manifest injection.
18
- */
19
- async function loadRoutes() {
20
- const apiDir = path.join(process.cwd(), 'pages', 'api');
21
-
22
- if (!fs.existsSync(apiDir)) {return;}
23
-
24
- const files = fs.readdirSync(apiDir);
25
-
26
- for (const file of files) {
27
- if (file.endsWith('.js')) {
28
- const routeName = '/api/' + file.replace('.js', '');
29
- const modulePath = path.join(apiDir, file);
30
- routeCache.set(routeName, modulePath);
31
- console.log(`✓ Registered Route: ${routeName}`);
32
- }
33
- }
34
- }
35
-
36
- /**
37
- * HTTP Method Handler
38
- * @param {object} module - The imported API module
39
- * @param {Request} req - The incoming request
40
- * @returns {Promise<Response>}
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
- } else if (module.default) {
48
- return await module.default(req);
49
- }
50
-
51
- return new Response('Method Not Allowed', { status: 405 });
52
- }
53
-
54
- /**
55
- * Router Handler
56
- * @param {Request} req
57
- * @returns {Promise<Response|null>}
58
- */
59
- export async function handleApiRequest(req) {
60
- const url = new URL(req.url);
61
- const pathname = url.pathname;
62
-
63
- // 0. Handle built-in auth endpoints
64
- if (pathname.startsWith('/api/auth/')) {
65
- try {
66
- const { handleAuthRequest } = await import('./auth-endpoints.js');
67
- const authResponse = await handleAuthRequest(req, pathname);
68
- if (authResponse) {
69
- return authResponse;
70
- }
71
- } catch (err) {
72
- console.error(`Auth endpoint error at ${pathname}:`, err);
73
- return new Response(JSON.stringify({ error: err.message }), {
74
- status: 500,
75
- headers: { 'Content-Type': 'application/json' }
76
- });
77
- }
78
- }
79
-
80
- // Load routes lazily
81
- if (routeCache.size === 0) {
82
- await loadRoutes();
83
- }
84
-
85
- // 1. Exact Match
86
- let modulePath = routeCache.get(pathname);
87
-
88
- // 2. Dynamic Match (Basic implementation)
89
- // If no exact match, look for dynamic routes [id].js?
90
- if (!modulePath) {
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
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
- "timestamp": "2026-07-22T02:25:53.751Z",
3
- "version": "5.0.9",
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": "277.75ms"
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.9",
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",
@@ -15,8 +15,8 @@
15
15
  "./components/*": "./components/*"
16
16
  },
17
17
  "bin": {
18
- "africode": "./bin/africode.js",
19
- "create-africode": "./bin/create-africode.js"
18
+ "africode": "bin/africode.js",
19
+ "create-africode": "bin/create-africode.js"
20
20
  },
21
21
  "scripts": {
22
22
  "dev": "bun run bin/africode.js dev",
@@ -9,6 +9,6 @@
9
9
  "migrate": "bun run ./bin/africode.js migrate"
10
10
  },
11
11
  "dependencies": {
12
- "@africode/core": "^5.0.9"
12
+ "@africode/core": "^5.0.11"
13
13
  }
14
14
  }
@@ -9,7 +9,7 @@
9
9
  "migrate": "bun run ./bin/africode.js migrate"
10
10
  },
11
11
  "dependencies": {
12
- "@africode/core": "^5.0.9",
12
+ "@africode/core": "^5.0.11",
13
13
  "three": "^0.160.0"
14
14
  }
15
15
  }
@@ -9,6 +9,6 @@
9
9
  "migrate": "bun run ./bin/africode.js migrate"
10
10
  },
11
11
  "dependencies": {
12
- "@africode/core": "^5.0.9"
12
+ "@africode/core": "^5.0.11"
13
13
  }
14
14
  }
@@ -9,7 +9,7 @@
9
9
  "migrate": "bun run ./bin/africode.js migrate"
10
10
  },
11
11
  "dependencies": {
12
- "@africode/core": "^5.0.9",
12
+ "@africode/core": "^5.0.11",
13
13
  "chart.js": "^4.0.0"
14
14
  }
15
15
  }
@@ -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
+ }
@@ -9,7 +9,7 @@
9
9
  "migrate": "bun run ./bin/africode.js migrate"
10
10
  },
11
11
  "dependencies": {
12
- "@africode/core": "^5.0.9",
12
+ "@africode/core": "^5.0.11",
13
13
  "react": "^19.0.0",
14
14
  "react-dom": "^19.0.0"
15
15
  }
@@ -10,7 +10,7 @@
10
10
  "migrate": "bun run ./bin/africode.js migrate"
11
11
  },
12
12
  "dependencies": {
13
- "@africode/core": "^5.0.9"
13
+ "@africode/core": "^5.0.11"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@tailwindcss/cli": "^4.0.0",
@@ -9,6 +9,6 @@
9
9
  "migrate": "bun run ./bin/africode.js migrate"
10
10
  },
11
11
  "dependencies": {
12
- "@africode/core": "^5.0.9"
12
+ "@africode/core": "^5.0.11"
13
13
  }
14
14
  }