create-astratra-app 1.1.0 → 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 +14 -44
package/package.json
CHANGED
package/src/createProject.js
CHANGED
|
@@ -29,9 +29,8 @@ 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
|
|
34
|
-
express: '^4.18.3'
|
|
32
|
+
'@astratra/saas-kit': '^1.3.0',
|
|
33
|
+
'@astratra/security': '^1.3.0'
|
|
35
34
|
};
|
|
36
35
|
// Scaffolded (api/stores/mongo.js, api/db/mongo.js) but not wired into
|
|
37
36
|
// api/server.js by default — the project starts on the in-memory store.
|
|
@@ -70,12 +69,10 @@ function packageJson(projectName, template) {
|
|
|
70
69
|
}
|
|
71
70
|
|
|
72
71
|
function apiServer() {
|
|
73
|
-
return `import
|
|
74
|
-
import fs from 'node:fs';
|
|
72
|
+
return `import fs from 'node:fs';
|
|
75
73
|
import path from 'node:path';
|
|
76
74
|
import saasKit from '@astratra/saas-kit';
|
|
77
75
|
import { env } from './config/env.js';
|
|
78
|
-
import { createCorsMiddleware } from './config/cors.js';
|
|
79
76
|
import { createAuthSecurity } from './security/auth.js';
|
|
80
77
|
import { createRateLimitSecurity } from './security/rateLimit.js';
|
|
81
78
|
import { createWafSecurity } from './security/waf.js';
|
|
@@ -91,10 +88,12 @@ const stores = createMemoryStores();
|
|
|
91
88
|
const notifications = createNotificationModule();
|
|
92
89
|
const aiTools = createAiTools();
|
|
93
90
|
|
|
94
|
-
const app =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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 },
|
|
98
97
|
...createAuthSecurity(env),
|
|
99
98
|
...createRateLimitSecurity(env),
|
|
100
99
|
...createWafSecurity(env),
|
|
@@ -119,7 +118,7 @@ app.use(createSaasApp({
|
|
|
119
118
|
}
|
|
120
119
|
});
|
|
121
120
|
}
|
|
122
|
-
})
|
|
121
|
+
});
|
|
123
122
|
|
|
124
123
|
function writeApiRuntimeConfig(port) {
|
|
125
124
|
const configDir = path.resolve('.astratra');
|
|
@@ -186,37 +185,6 @@ export const env = {
|
|
|
186
185
|
`;
|
|
187
186
|
}
|
|
188
187
|
|
|
189
|
-
function corsConfig() {
|
|
190
|
-
return `function isAllowedDevOrigin(origin) {
|
|
191
|
-
if (process.env.NODE_ENV === 'production') return false;
|
|
192
|
-
try {
|
|
193
|
-
const url = new URL(origin);
|
|
194
|
-
return ['127.0.0.1', 'localhost'].includes(url.hostname);
|
|
195
|
-
} catch {
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export function createCorsMiddleware({ allowedOrigins = [] } = {}) {
|
|
201
|
-
const allowed = new Set(allowedOrigins);
|
|
202
|
-
return (req, res, next) => {
|
|
203
|
-
const origin = req.headers.origin;
|
|
204
|
-
if (origin && (allowed.has(origin) || isAllowedDevOrigin(origin))) {
|
|
205
|
-
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
206
|
-
res.setHeader('Vary', 'Origin');
|
|
207
|
-
}
|
|
208
|
-
res.setHeader('Access-Control-Allow-Headers', 'content-type, authorization');
|
|
209
|
-
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PATCH,OPTIONS');
|
|
210
|
-
if (req.method === 'OPTIONS') {
|
|
211
|
-
res.status(204).end();
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
next();
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
`;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
188
|
function securityAuth() {
|
|
221
189
|
return `export function createAuthSecurity(env) {
|
|
222
190
|
return {
|
|
@@ -592,10 +560,13 @@ générique avant de te rendre la main, donc toute route ajoutée ensuite est
|
|
|
592
560
|
inatteignable. \`extendRoutes\` s'exécute avant ce 404, avec les mêmes
|
|
593
561
|
instances \`authMiddleware\`/\`csrfMiddleware\` que les routes intégrées.
|
|
594
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
|
+
|
|
595
567
|
## Fichiers Utiles
|
|
596
568
|
|
|
597
569
|
- \`api/config/env.js\` centralise la configuration.
|
|
598
|
-
- \`api/config/cors.js\` gère CORS sans port de développement fixe.
|
|
599
570
|
- \`api/security/auth.js\`, \`api/security/rateLimit.js\` et
|
|
600
571
|
\`api/security/waf.js\` branchent les primitives de sécurité Astratra.
|
|
601
572
|
- \`api/stores/memory.js\` lance vite avec des stores en mémoire.
|
|
@@ -663,7 +634,6 @@ export function createProject({ targetDir, template = 'fullstack', force = false
|
|
|
663
634
|
writeFile(absoluteTarget, 'package.json', `${packageJson(projectName, template)}\n`);
|
|
664
635
|
writeFile(absoluteTarget, 'api/server.js', apiServer());
|
|
665
636
|
writeFile(absoluteTarget, 'api/config/env.js', envConfig());
|
|
666
|
-
writeFile(absoluteTarget, 'api/config/cors.js', corsConfig());
|
|
667
637
|
writeFile(absoluteTarget, 'api/security/auth.js', securityAuth());
|
|
668
638
|
writeFile(absoluteTarget, 'api/security/rateLimit.js', securityRateLimit());
|
|
669
639
|
writeFile(absoluteTarget, 'api/security/waf.js', securityWaf());
|