create-astratra-app 1.0.2 → 1.2.0
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/package.json +1 -1
- package/src/createProject.js +58 -48
package/package.json
CHANGED
package/src/createProject.js
CHANGED
|
@@ -29,10 +29,16 @@ function packageJson(projectName, template) {
|
|
|
29
29
|
const dependencies = {
|
|
30
30
|
'@astratra/ai': '^1.0.0',
|
|
31
31
|
'@astratra/core': '^1.0.0',
|
|
32
|
-
'@astratra/saas-kit': '^1.0
|
|
33
|
-
'@astratra/security': '^1.0
|
|
32
|
+
'@astratra/saas-kit': '^1.3.0',
|
|
33
|
+
'@astratra/security': '^1.3.0'
|
|
34
|
+
};
|
|
35
|
+
// Scaffolded (api/stores/mongo.js, api/db/mongo.js) but not wired into
|
|
36
|
+
// api/server.js by default — the project starts on the in-memory store.
|
|
37
|
+
// Kept optional so a from-scratch install doesn't pull in mongoose (a
|
|
38
|
+
// real driver dependency) before the project has ever chosen to use it.
|
|
39
|
+
// `npm install @astratra/store-mongo mongoose` to switch to MongoDB.
|
|
40
|
+
const optionalDependencies = {
|
|
34
41
|
'@astratra/store-mongo': '^1.0.1',
|
|
35
|
-
express: '^4.18.3',
|
|
36
42
|
mongoose: '^8.17.0'
|
|
37
43
|
};
|
|
38
44
|
const devDependencies = {};
|
|
@@ -57,34 +63,37 @@ function packageJson(projectName, template) {
|
|
|
57
63
|
start: 'node api/server.js'
|
|
58
64
|
},
|
|
59
65
|
dependencies,
|
|
66
|
+
optionalDependencies,
|
|
60
67
|
...(Object.keys(devDependencies).length > 0 ? { devDependencies } : {})
|
|
61
68
|
}, null, 2);
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
function apiServer() {
|
|
65
|
-
return `import
|
|
66
|
-
import fs from 'node:fs';
|
|
72
|
+
return `import fs from 'node:fs';
|
|
67
73
|
import path from 'node:path';
|
|
68
74
|
import saasKit from '@astratra/saas-kit';
|
|
69
75
|
import { env } from './config/env.js';
|
|
70
|
-
import { createCorsMiddleware } from './config/cors.js';
|
|
71
76
|
import { createAuthSecurity } from './security/auth.js';
|
|
72
77
|
import { createRateLimitSecurity } from './security/rateLimit.js';
|
|
73
78
|
import { createWafSecurity } from './security/waf.js';
|
|
74
79
|
import { createMemoryStores } from './stores/memory.js';
|
|
75
80
|
import { userPublicFields } from './modules/users.js';
|
|
76
81
|
import { createNotificationModule } from './modules/notifications.js';
|
|
82
|
+
import { createAiTools } from './ai/tools.js';
|
|
77
83
|
|
|
78
84
|
const { createSaasApp } = saasKit;
|
|
79
85
|
const preferredPort = env.port;
|
|
80
86
|
const host = env.host;
|
|
81
87
|
const stores = createMemoryStores();
|
|
82
88
|
const notifications = createNotificationModule();
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
app
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
const aiTools = createAiTools();
|
|
90
|
+
|
|
91
|
+
const app = createSaasApp({
|
|
92
|
+
// Mounted FIRST, ahead of every other middleware — required for CORS
|
|
93
|
+
// headers to reach the OPTIONS preflight response too. Astratra has no
|
|
94
|
+
// fixed opinion on which origins to allow (that's project-specific);
|
|
95
|
+
// env.corsOrigins comes from CORS_ORIGIN in .env.
|
|
96
|
+
cors: { allowedOrigins: env.corsOrigins },
|
|
88
97
|
...createAuthSecurity(env),
|
|
89
98
|
...createRateLimitSecurity(env),
|
|
90
99
|
...createWafSecurity(env),
|
|
@@ -92,8 +101,24 @@ app.use(createSaasApp({
|
|
|
92
101
|
settingsStore: stores.settingsStore,
|
|
93
102
|
publicUserFields: userPublicFields(),
|
|
94
103
|
verifyPassword: async (user, password) => user.password === password,
|
|
95
|
-
notify: notifications.notify
|
|
96
|
-
|
|
104
|
+
notify: notifications.notify,
|
|
105
|
+
// This is where YOUR routes go — not app.use()'d after createSaasApp()
|
|
106
|
+
// returns. createSaasApp() ends its own middleware stack with a
|
|
107
|
+
// catch-all 404 handler, so anything registered on the app afterward
|
|
108
|
+
// is unreachable. extendRoutes runs before that catch-all, with the
|
|
109
|
+
// same authMiddleware/csrfMiddleware instances the built-in routes use.
|
|
110
|
+
extendRoutes: (saasApp, { authMiddleware }) => {
|
|
111
|
+
saasApp.get('/api/status', authMiddleware, async (req, res, next) => {
|
|
112
|
+
try {
|
|
113
|
+
const healthCheck = aiTools.getToolByName('health_check');
|
|
114
|
+
const result = await healthCheck.handler({}, { user: req.user });
|
|
115
|
+
res.status(200).json({ success: true, message: 'Status', data: result });
|
|
116
|
+
} catch (error) {
|
|
117
|
+
next(error);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
});
|
|
97
122
|
|
|
98
123
|
function writeApiRuntimeConfig(port) {
|
|
99
124
|
const configDir = path.resolve('.astratra');
|
|
@@ -160,37 +185,6 @@ export const env = {
|
|
|
160
185
|
`;
|
|
161
186
|
}
|
|
162
187
|
|
|
163
|
-
function corsConfig() {
|
|
164
|
-
return `function isAllowedDevOrigin(origin) {
|
|
165
|
-
if (process.env.NODE_ENV === 'production') return false;
|
|
166
|
-
try {
|
|
167
|
-
const url = new URL(origin);
|
|
168
|
-
return ['127.0.0.1', 'localhost'].includes(url.hostname);
|
|
169
|
-
} catch {
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function createCorsMiddleware({ allowedOrigins = [] } = {}) {
|
|
175
|
-
const allowed = new Set(allowedOrigins);
|
|
176
|
-
return (req, res, next) => {
|
|
177
|
-
const origin = req.headers.origin;
|
|
178
|
-
if (origin && (allowed.has(origin) || isAllowedDevOrigin(origin))) {
|
|
179
|
-
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
180
|
-
res.setHeader('Vary', 'Origin');
|
|
181
|
-
}
|
|
182
|
-
res.setHeader('Access-Control-Allow-Headers', 'content-type, authorization');
|
|
183
|
-
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PATCH,OPTIONS');
|
|
184
|
-
if (req.method === 'OPTIONS') {
|
|
185
|
-
res.status(204).end();
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
next();
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
`;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
188
|
function securityAuth() {
|
|
195
189
|
return `export function createAuthSecurity(env) {
|
|
196
190
|
return {
|
|
@@ -556,16 +550,33 @@ Par défaut, l'API demande un port libre au système et écrit l'URL choisie
|
|
|
556
550
|
dans \`.astratra/api.json\`. Lance \`dev:api\` avant \`dev:web\` pour que Vite
|
|
557
551
|
lise la bonne URL. Laisse \`PORT\` vide pour garder le choix automatique.
|
|
558
552
|
|
|
553
|
+
## Ajouter tes propres routes
|
|
554
|
+
|
|
555
|
+
\`api/server.js\` montre le motif à suivre avec \`GET /api/status\` : passe une
|
|
556
|
+
fonction \`extendRoutes(app, { authMiddleware, csrfMiddleware })\` à
|
|
557
|
+
\`createSaasApp()\`, ne fais pas \`app.use()\` sur l'app retournée après coup —
|
|
558
|
+
\`createSaasApp()\` termine sa propre pile de middlewares par un 404
|
|
559
|
+
générique avant de te rendre la main, donc toute route ajoutée ensuite est
|
|
560
|
+
inatteignable. \`extendRoutes\` s'exécute avant ce 404, avec les mêmes
|
|
561
|
+
instances \`authMiddleware\`/\`csrfMiddleware\` que les routes intégrées.
|
|
562
|
+
|
|
563
|
+
CORS est géré par \`options.cors\` passé directement à \`createSaasApp()\`
|
|
564
|
+
dans \`api/server.js\` (origines autorisées via \`CORS_ORIGIN\` dans \`.env\`,
|
|
565
|
+
plus \`127.0.0.1\`/\`localhost\` autorisés automatiquement hors production).
|
|
566
|
+
|
|
559
567
|
## Fichiers Utiles
|
|
560
568
|
|
|
561
569
|
- \`api/config/env.js\` centralise la configuration.
|
|
562
|
-
- \`api/config/cors.js\` gère CORS sans port de développement fixe.
|
|
563
570
|
- \`api/security/auth.js\`, \`api/security/rateLimit.js\` et
|
|
564
571
|
\`api/security/waf.js\` branchent les primitives de sécurité Astratra.
|
|
565
572
|
- \`api/stores/memory.js\` lance vite avec des stores en mémoire.
|
|
566
|
-
- \`api/db/mongo.js\` et \`api/stores/mongo.js\` préparent MongoDB
|
|
573
|
+
- \`api/db/mongo.js\` et \`api/stores/mongo.js\` préparent MongoDB — installe
|
|
574
|
+
d'abord \`npm install @astratra/store-mongo mongoose\` (dépendances
|
|
575
|
+
optionnelles, pas installées par défaut) avant de les brancher dans
|
|
576
|
+
\`api/server.js\`.
|
|
567
577
|
- \`api/ai/providers.js\`, \`api/ai/tools.js\` et \`api/ai/agent.js\`
|
|
568
|
-
préparent la logique IA.
|
|
578
|
+
préparent la logique IA — \`api/ai/tools.js\` est déjà branché dans
|
|
579
|
+
\`api/server.js\` via \`extendRoutes\` (route \`GET /api/status\`).
|
|
569
580
|
- \`api/modules/users.js\`, \`api/modules/settings.js\` et
|
|
570
581
|
\`api/modules/notifications.js\` isolent la logique métier de départ.
|
|
571
582
|
|
|
@@ -623,7 +634,6 @@ export function createProject({ targetDir, template = 'fullstack', force = false
|
|
|
623
634
|
writeFile(absoluteTarget, 'package.json', `${packageJson(projectName, template)}\n`);
|
|
624
635
|
writeFile(absoluteTarget, 'api/server.js', apiServer());
|
|
625
636
|
writeFile(absoluteTarget, 'api/config/env.js', envConfig());
|
|
626
|
-
writeFile(absoluteTarget, 'api/config/cors.js', corsConfig());
|
|
627
637
|
writeFile(absoluteTarget, 'api/security/auth.js', securityAuth());
|
|
628
638
|
writeFile(absoluteTarget, 'api/security/rateLimit.js', securityRateLimit());
|
|
629
639
|
writeFile(absoluteTarget, 'api/security/waf.js', securityWaf());
|