crypt-express-app 1.0.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/README.md +1 -0
- package/dist/index.js +20 -0
- package/dist/scripts/generate-app.js +96 -0
- package/dist/scripts/generate-locales.js +29 -0
- package/dist/scripts/generate-middleware.js +165 -0
- package/dist/scripts/generate-module.js +677 -0
- package/dist/scripts/generate-root-files.js +123 -0
- package/dist/scripts/generate-utils.js +191 -0
- package/dist/scripts/middleware.js +165 -0
- package/dist/scripts/setup-prisma.js +21 -0
- package/index.ts +29 -0
- package/package.json +23 -0
- package/scripts/generate-app.ts +104 -0
- package/scripts/generate-locales.ts +36 -0
- package/scripts/generate-middleware.ts +176 -0
- package/scripts/generate-module.ts +715 -0
- package/scripts/generate-root-files.ts +147 -0
- package/scripts/generate-utils.ts +208 -0
- package/scripts/setup-prisma.ts +26 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// scripts/generate-root-files.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as readline from "readline";
|
|
5
|
+
const rl = readline.createInterface({
|
|
6
|
+
input: process.stdin,
|
|
7
|
+
output: process.stdout,
|
|
8
|
+
});
|
|
9
|
+
rl.question("Enter project name: ", (projectName) => {
|
|
10
|
+
/* ================= tsconfig.json ================= */
|
|
11
|
+
const tsconfig = {
|
|
12
|
+
compilerOptions: {
|
|
13
|
+
module: "nodenext",
|
|
14
|
+
moduleResolution: "nodenext",
|
|
15
|
+
resolvePackageJsonExports: true,
|
|
16
|
+
esModuleInterop: true,
|
|
17
|
+
isolatedModules: true,
|
|
18
|
+
declaration: true,
|
|
19
|
+
removeComments: true,
|
|
20
|
+
emitDecoratorMetadata: true,
|
|
21
|
+
experimentalDecorators: true,
|
|
22
|
+
allowSyntheticDefaultImports: true,
|
|
23
|
+
target: "ES2023",
|
|
24
|
+
sourceMap: true,
|
|
25
|
+
rootDir: ".",
|
|
26
|
+
outDir: "./dist",
|
|
27
|
+
types: ["node"],
|
|
28
|
+
incremental: true,
|
|
29
|
+
skipLibCheck: true,
|
|
30
|
+
strictNullChecks: true,
|
|
31
|
+
forceConsistentCasingInFileNames: true,
|
|
32
|
+
noImplicitAny: false,
|
|
33
|
+
strictBindCallApply: false,
|
|
34
|
+
noFallthroughCasesInSwitch: false,
|
|
35
|
+
},
|
|
36
|
+
include: ["src", "server.ts", "prisma.config.ts"],
|
|
37
|
+
exclude: ["node_modules"],
|
|
38
|
+
assets: ["src/locales"],
|
|
39
|
+
};
|
|
40
|
+
fs.writeFileSync(path.join(process.cwd(), "tsconfig.json"), JSON.stringify(tsconfig, null, 2));
|
|
41
|
+
/* ================= package.json ================= */
|
|
42
|
+
const packageJson = {
|
|
43
|
+
name: projectName.toLowerCase().replace(/\s+/g, "-"),
|
|
44
|
+
version: "1.0.0",
|
|
45
|
+
description: `${projectName} backend.`,
|
|
46
|
+
license: "ISC",
|
|
47
|
+
author: "abir-hosen",
|
|
48
|
+
main: "server.ts",
|
|
49
|
+
scripts: {
|
|
50
|
+
"copy-locales": 'cpx "src/locales/**/*" dist/src/locales',
|
|
51
|
+
dev: "nodemon",
|
|
52
|
+
build: "tsc && npm run copy-locales",
|
|
53
|
+
start: "node dist/server.js",
|
|
54
|
+
"generate-module": "ts-node scripts/generate-module.ts",
|
|
55
|
+
"generate-middlewares": "ts-node scripts/middleware.ts",
|
|
56
|
+
"generate-locales": "ts-node scripts/generate-locales.ts",
|
|
57
|
+
"generate-utils": "ts-node scripts/generate-utils.ts"
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
fs.writeFileSync(path.join(process.cwd(), "package.json"), JSON.stringify(packageJson, null, 2));
|
|
61
|
+
/* ================= nodemon.json ================= */
|
|
62
|
+
const nodemon = {
|
|
63
|
+
watch: ["src"],
|
|
64
|
+
ext: "ts",
|
|
65
|
+
ignore: ["dist"],
|
|
66
|
+
exec: "ts-node src/server.ts",
|
|
67
|
+
};
|
|
68
|
+
fs.writeFileSync(path.join(process.cwd(), "nodemon.json"), JSON.stringify(nodemon, null, 2));
|
|
69
|
+
/* ================= .gitignore ================= */
|
|
70
|
+
const gitignore = `
|
|
71
|
+
node_modules
|
|
72
|
+
/src/generated/prisma
|
|
73
|
+
.env
|
|
74
|
+
package-lock.json
|
|
75
|
+
dist
|
|
76
|
+
pnpm-lock.yaml
|
|
77
|
+
/generated/prisma
|
|
78
|
+
`;
|
|
79
|
+
fs.writeFileSync(path.join(process.cwd(), ".gitignore"), gitignore.trim());
|
|
80
|
+
/* ================= .prisma ================= */
|
|
81
|
+
const prisma = `
|
|
82
|
+
// This is your Prisma schema file,
|
|
83
|
+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
|
84
|
+
|
|
85
|
+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
|
|
86
|
+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
|
|
87
|
+
|
|
88
|
+
generator client {
|
|
89
|
+
provider = "prisma-client-js"
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
datasource db {
|
|
93
|
+
provider = "postgresql"
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
model Common {
|
|
97
|
+
commonId String @id @default(uuid())
|
|
98
|
+
name String @unique
|
|
99
|
+
}
|
|
100
|
+
`;
|
|
101
|
+
fs.writeFileSync(path.join(process.cwd(), "prisma", "schema.prisma"), prisma.trim());
|
|
102
|
+
/* ================= .prisma ================= */
|
|
103
|
+
const prismaConfig = `
|
|
104
|
+
// This file was generated by Prisma, and assumes you have installed the following:
|
|
105
|
+
// npm install --save-dev prisma dotenv
|
|
106
|
+
import "dotenv/config";
|
|
107
|
+
import { defineConfig } from "prisma/config";
|
|
108
|
+
|
|
109
|
+
export default defineConfig({
|
|
110
|
+
schema: "prisma/schema.prisma",
|
|
111
|
+
migrations: {
|
|
112
|
+
path: "prisma/migrations",
|
|
113
|
+
},
|
|
114
|
+
datasource: {
|
|
115
|
+
url: process.env["DATABASE_URL"],
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
`;
|
|
120
|
+
fs.writeFileSync(path.join(process.cwd(), "prisma.config.ts"), prismaConfig.trim());
|
|
121
|
+
console.log(`✅ Root config files generated for project: ${projectName}`);
|
|
122
|
+
rl.close();
|
|
123
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
const projectRoot = process.cwd();
|
|
4
|
+
const basePath = path.join(projectRoot, "src", "utils");
|
|
5
|
+
if (!fs.existsSync(basePath)) {
|
|
6
|
+
fs.mkdirSync(basePath, { recursive: true });
|
|
7
|
+
}
|
|
8
|
+
/* ================= redis.ts ================= */
|
|
9
|
+
const redisContent = `
|
|
10
|
+
import { createClient, RedisClientType } from "redis";
|
|
11
|
+
import { CONSTS } from "./consts";
|
|
12
|
+
|
|
13
|
+
let redis: RedisClientType = createClient({
|
|
14
|
+
url: CONSTS.REDIS_URL,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
redis.on("connect", () => {
|
|
18
|
+
console.log("✅ Redis Connected");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
redis.on("error", (err) => {
|
|
22
|
+
console.error("❌ Redis Error", err);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const connectRedis = async () => {
|
|
26
|
+
if (!redis.isOpen) {
|
|
27
|
+
await redis.connect();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export class CacheService {
|
|
32
|
+
async set(key: string, value: any, ttl?: number) {
|
|
33
|
+
const stringValue = JSON.stringify(value);
|
|
34
|
+
ttl ? await redis.set(key, stringValue, { EX: ttl }) : await redis.set(key, stringValue);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async get<T>(key: string): Promise<T | null> {
|
|
38
|
+
const data = await redis.get(key);
|
|
39
|
+
return data ? JSON.parse(data) : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async del(key: string) {
|
|
43
|
+
await redis.del(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const cacheService = new CacheService();
|
|
48
|
+
export default redis;
|
|
49
|
+
`;
|
|
50
|
+
fs.writeFileSync(path.join(basePath, "redis.ts"), redisContent.trim());
|
|
51
|
+
/* ================= cache.ts ================= */
|
|
52
|
+
const cacheContent = `
|
|
53
|
+
interface CachedSecret {
|
|
54
|
+
value: string;
|
|
55
|
+
expiresAt: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const realmSecretCache = new Map<string, CachedSecret>();
|
|
59
|
+
const DEFAULT_TTL_MS = 5 * 60 * 1000;
|
|
60
|
+
|
|
61
|
+
export const getCachedSecret = (realmId: string) => {
|
|
62
|
+
const cached = realmSecretCache.get(realmId);
|
|
63
|
+
if (!cached || cached.expiresAt < Date.now()) {
|
|
64
|
+
realmSecretCache.delete(realmId);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return cached.value;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const setCachedSecret = (realmId: string, secret: string, ttlMs = DEFAULT_TTL_MS) => {
|
|
71
|
+
realmSecretCache.set(realmId, {
|
|
72
|
+
value: secret,
|
|
73
|
+
expiresAt: Date.now() + ttlMs,
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const invalidateSecret = (realmId: string) => {
|
|
78
|
+
realmSecretCache.delete(realmId);
|
|
79
|
+
};
|
|
80
|
+
`;
|
|
81
|
+
fs.writeFileSync(path.join(basePath, "cache.ts"), cacheContent.trim());
|
|
82
|
+
/* ================= redis.dto.ts ================= */
|
|
83
|
+
const redisDtoContent = `
|
|
84
|
+
export interface CachedUserProfilePermissionDto {
|
|
85
|
+
userId: string;
|
|
86
|
+
username: string;
|
|
87
|
+
email: string;
|
|
88
|
+
realm: {
|
|
89
|
+
realmId: string;
|
|
90
|
+
name: string;
|
|
91
|
+
};
|
|
92
|
+
roles: string[];
|
|
93
|
+
permissions: string[];
|
|
94
|
+
}
|
|
95
|
+
`;
|
|
96
|
+
fs.writeFileSync(path.join(basePath, "redis.dto.ts"), redisDtoContent.trim());
|
|
97
|
+
/* ================= logger.ts ================= */
|
|
98
|
+
const loggerContent = `
|
|
99
|
+
import winston from "winston";
|
|
100
|
+
|
|
101
|
+
const logger = winston.createLogger({
|
|
102
|
+
level: "info",
|
|
103
|
+
format: winston.format.combine(
|
|
104
|
+
winston.format.colorize(),
|
|
105
|
+
winston.format.timestamp(),
|
|
106
|
+
winston.format.printf(({ timestamp, level, message }) => \`[\${timestamp}] \${level}: \${message}\`)
|
|
107
|
+
),
|
|
108
|
+
transports: [new winston.transports.Console()],
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
export default logger;
|
|
112
|
+
`;
|
|
113
|
+
fs.writeFileSync(path.join(basePath, "logger.ts"), loggerContent.trim());
|
|
114
|
+
/* ================= bootstrap.ts ================= */
|
|
115
|
+
const bootstrapContent = `
|
|
116
|
+
import prisma from "../prisma/client";
|
|
117
|
+
import logger from "./logger";
|
|
118
|
+
|
|
119
|
+
async function bootstrapSystem() {
|
|
120
|
+
// optional: seed data or migrations
|
|
121
|
+
console.log("🚀 System bootstrap complete");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function wipeAllTables() {
|
|
125
|
+
try {
|
|
126
|
+
await prisma.$executeRawUnsafe(\`TRUNCATE TABLE "User" CASCADE;\`);
|
|
127
|
+
logger.info("✅ All tables wiped successfully");
|
|
128
|
+
} catch (error) {
|
|
129
|
+
logger.error(error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export default bootstrapSystem;
|
|
134
|
+
`;
|
|
135
|
+
fs.writeFileSync(path.join(basePath, "bootstrap.ts"), bootstrapContent.trim());
|
|
136
|
+
/* ================= consts.ts ================= */
|
|
137
|
+
const constsContent = `
|
|
138
|
+
export const CONSTS = {
|
|
139
|
+
REDIS_URL: process.env.REDIS_URL || "redis://localhost:6379",
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export enum ActionsType {
|
|
143
|
+
CREATE = "CREATE",
|
|
144
|
+
READ = "READ",
|
|
145
|
+
READ_ALL = "READ_ALL",
|
|
146
|
+
UPDATE = "UPDATE",
|
|
147
|
+
DELETE = "DELETE",
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export enum Resource {
|
|
151
|
+
COMMON = "COMMON",
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export enum ResourceType {
|
|
155
|
+
API_ENDPOINT = "API_ENDPOINT",
|
|
156
|
+
UI_PAGE = "UI_PAGE",
|
|
157
|
+
FILE = "FILE",
|
|
158
|
+
SERVICE = "SERVICE",
|
|
159
|
+
DATASET = "DATASET",
|
|
160
|
+
}
|
|
161
|
+
`;
|
|
162
|
+
fs.writeFileSync(path.join(basePath, "consts.ts"), constsContent.trim());
|
|
163
|
+
/* ================= swagger.ts ================= */
|
|
164
|
+
const swaggerContent = `
|
|
165
|
+
import swaggerJsdoc from "swagger-jsdoc";
|
|
166
|
+
|
|
167
|
+
export const swaggerSpec = swaggerJsdoc({
|
|
168
|
+
definition: {
|
|
169
|
+
openapi: "3.0.0",
|
|
170
|
+
info: {
|
|
171
|
+
title: "Express REST API",
|
|
172
|
+
version: "1.0.0",
|
|
173
|
+
description: "RESTful API with Express + TypeScript + Swagger",
|
|
174
|
+
},
|
|
175
|
+
components: {
|
|
176
|
+
securitySchemes: {
|
|
177
|
+
bearerAuth: {
|
|
178
|
+
type: "http",
|
|
179
|
+
scheme: "bearer",
|
|
180
|
+
bearerFormat: "JWT",
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
security: [{ bearerAuth: [] }],
|
|
185
|
+
servers: [{ url: "http://localhost:3000" }],
|
|
186
|
+
},
|
|
187
|
+
apis: ["./src/**/*.routes.ts", "./src/**/*.dto.ts"],
|
|
188
|
+
});
|
|
189
|
+
`;
|
|
190
|
+
fs.writeFileSync(path.join(basePath, "swagger.ts"), swaggerContent.trim());
|
|
191
|
+
console.log("✅ Utils files generated successfully inside src/utils");
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
const projectRoot = process.cwd();
|
|
4
|
+
const basePath = path.join(projectRoot, "src", "middlewares");
|
|
5
|
+
if (!fs.existsSync(basePath)) {
|
|
6
|
+
fs.mkdirSync(basePath, { recursive: true });
|
|
7
|
+
}
|
|
8
|
+
/* ================= AUTHORIZATION ================= */
|
|
9
|
+
const authorizationContent = `
|
|
10
|
+
import { Request, Response, NextFunction } from "express";
|
|
11
|
+
import { ResourceType, ActionsType, Resource } from "../utils/consts";
|
|
12
|
+
import { cacheService } from "../utils/redis";
|
|
13
|
+
|
|
14
|
+
export function authorizationMiddleware(authorization: {
|
|
15
|
+
resource: Resource;
|
|
16
|
+
resourceType: ResourceType;
|
|
17
|
+
actions: ActionsType[];
|
|
18
|
+
}) {
|
|
19
|
+
return async (req: Request, res: Response, next: NextFunction) => {
|
|
20
|
+
try {
|
|
21
|
+
const userId = req?.user?.userId;
|
|
22
|
+
if (!userId) {
|
|
23
|
+
return res.status(401).json({ message: "Unauthorized" });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const cached = await cacheService.get(\`user:\${userId}:permissions\`);
|
|
27
|
+
if (!cached) {
|
|
28
|
+
return res.status(403).json({ message: "Permissions not loaded" });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const profile = JSON.parse(cached as string);
|
|
32
|
+
const permissions: string[] = profile.permissions || [];
|
|
33
|
+
|
|
34
|
+
const allowed = permissions.some((perm) => {
|
|
35
|
+
const [resName, action, type] = perm.split(":");
|
|
36
|
+
return (
|
|
37
|
+
resName === authorization.resource &&
|
|
38
|
+
type === authorization.resourceType &&
|
|
39
|
+
authorization.actions.includes(action as ActionsType)
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (!allowed) {
|
|
44
|
+
return res.status(403).json({ message: "Access denied" });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
next();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error(error);
|
|
50
|
+
return res.status(500).json({ message: "Authorization error" });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
`;
|
|
55
|
+
fs.writeFileSync(path.join(basePath, "authorization.middleware.ts"), authorizationContent.trim());
|
|
56
|
+
/* ================= AUTH ================= */
|
|
57
|
+
const authContent = `
|
|
58
|
+
import { Request, Response, NextFunction } from "express";
|
|
59
|
+
import jwt from "jsonwebtoken";
|
|
60
|
+
import prisma from "../../prisma/client";
|
|
61
|
+
import { cacheService } from "../utils/redis";
|
|
62
|
+
|
|
63
|
+
interface JwtPayload {
|
|
64
|
+
realmId: string;
|
|
65
|
+
sessionId: string;
|
|
66
|
+
userId: string;
|
|
67
|
+
email: string;
|
|
68
|
+
clientIdInternal: string;
|
|
69
|
+
isMasterRealmUser: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare global {
|
|
73
|
+
namespace Express {
|
|
74
|
+
interface Request {
|
|
75
|
+
user?: JwtPayload;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const authMiddleware = async (req: Request, res: Response, next: NextFunction) => {
|
|
81
|
+
const authHeader = req.headers.authorization;
|
|
82
|
+
if (!authHeader) return res.status(401).json({ message: "Authorization header missing" });
|
|
83
|
+
|
|
84
|
+
const [scheme, tokenRaw] = authHeader.split(" ");
|
|
85
|
+
if (scheme !== "Bearer" || !tokenRaw) {
|
|
86
|
+
return res.status(401).json({ message: "Invalid Authorization header format" });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const token = tokenRaw.replace(/^\"|\"$/g, "");
|
|
90
|
+
|
|
91
|
+
const decodedPayload = jwt.decode(token) as JwtPayload | null;
|
|
92
|
+
if (!decodedPayload || !decodedPayload.realmId) {
|
|
93
|
+
return res.status(401).json({ message: "Invalid token: realmId missing" });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const realmId = decodedPayload.realmId;
|
|
97
|
+
|
|
98
|
+
const secretKey = \`realm:\${realmId}:secret\`;
|
|
99
|
+
const secret = await cacheService.get(secretKey);
|
|
100
|
+
if (!secret) {
|
|
101
|
+
return res.status(500).json({ message: "JWT secret not configured in Redis" });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
const decoded = jwt.verify(token, secret as string, {
|
|
106
|
+
algorithms: ["HS256"],
|
|
107
|
+
}) as JwtPayload & { userId: string; sessionId: string };
|
|
108
|
+
|
|
109
|
+
const session = await prisma.userSession.findUnique({
|
|
110
|
+
where: { userSessionId: decoded.sessionId },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!session) {
|
|
114
|
+
return res.status(401).json({ message: "Session invalid or logged out" });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
req.user = decoded;
|
|
118
|
+
next();
|
|
119
|
+
} catch (err: any) {
|
|
120
|
+
if (err.name === "TokenExpiredError") {
|
|
121
|
+
return res.status(401).json({ message: "Token expired" });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return res.status(401).json({ message: "Invalid token" });
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
`;
|
|
128
|
+
fs.writeFileSync(path.join(basePath, "auth.middleware.ts"), authContent.trim());
|
|
129
|
+
/* ================= ERROR ================= */
|
|
130
|
+
const errorContent = `
|
|
131
|
+
import { Request, Response, NextFunction } from "express";
|
|
132
|
+
|
|
133
|
+
export const errorMiddleware = (err: any, req: Request, res: Response, next: NextFunction) => {
|
|
134
|
+
console.error(err);
|
|
135
|
+
|
|
136
|
+
const status = err.status || 500;
|
|
137
|
+
const message = err.message || "Internal Server Error";
|
|
138
|
+
|
|
139
|
+
res.status(status).json({
|
|
140
|
+
success: false,
|
|
141
|
+
status,
|
|
142
|
+
message,
|
|
143
|
+
...(process.env.NODE_ENV === "development" && { stack: err.stack }),
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
`;
|
|
147
|
+
fs.writeFileSync(path.join(basePath, "error.middleware.ts"), errorContent.trim());
|
|
148
|
+
/* ================= LOGGER ================= */
|
|
149
|
+
const loggerContent = `
|
|
150
|
+
import { Request, Response, NextFunction } from "express";
|
|
151
|
+
import logger from "../utils/logger";
|
|
152
|
+
|
|
153
|
+
export const loggerMiddleware = (req: Request, res: Response, next: NextFunction) => {
|
|
154
|
+
const start = Date.now();
|
|
155
|
+
|
|
156
|
+
res.on("finish", () => {
|
|
157
|
+
const duration = Date.now() - start;
|
|
158
|
+
logger.info(\`[\${new Date().toISOString()}] \${req.method} \${req.originalUrl} \${res.statusCode} - \${duration}ms\`);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
next();
|
|
162
|
+
};
|
|
163
|
+
`;
|
|
164
|
+
fs.writeFileSync(path.join(basePath, "logger.middleware.ts"), loggerContent.trim());
|
|
165
|
+
console.log(`✅ Middleware files generated successfully at ${basePath}`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
const projectRoot = process.cwd();
|
|
4
|
+
const basePath = path.join(projectRoot, "src", "prisma");
|
|
5
|
+
if (!fs.existsSync(basePath)) {
|
|
6
|
+
fs.mkdirSync(basePath, { recursive: true });
|
|
7
|
+
}
|
|
8
|
+
const clientContent = `
|
|
9
|
+
import { PrismaClient } from "@prisma/client";
|
|
10
|
+
import { PrismaPg } from "@prisma/adapter-pg";
|
|
11
|
+
import "dotenv/config";
|
|
12
|
+
|
|
13
|
+
const connectionString = process.env.DATABASE_URL;
|
|
14
|
+
|
|
15
|
+
const adapter = new PrismaPg({ connectionString });
|
|
16
|
+
const prisma = new PrismaClient({ adapter });
|
|
17
|
+
|
|
18
|
+
export default prisma;
|
|
19
|
+
`;
|
|
20
|
+
fs.writeFileSync(path.join(basePath, "client.ts"), clientContent.trim());
|
|
21
|
+
console.log(`✅ Prisma client generated successfully at ${basePath}`);
|
package/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { execSync } from "child_process";
|
|
4
|
+
|
|
5
|
+
const projectName = process.argv[2];
|
|
6
|
+
|
|
7
|
+
if (!projectName) {
|
|
8
|
+
console.error("❌ project-name required");
|
|
9
|
+
console.log("Usage: npx crypt-express-app <project-name>");
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
process.env.PROJECT_NAME = projectName;
|
|
14
|
+
|
|
15
|
+
console.log(projectName)
|
|
16
|
+
|
|
17
|
+
function run(cmd: string) {
|
|
18
|
+
execSync(cmd, { stdio: "inherit" });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// run generators
|
|
22
|
+
// run("tsx scripts/generate-root-files.ts");
|
|
23
|
+
// run("tsx scripts/generate-utils.ts");
|
|
24
|
+
// run("tsx scripts/generate-locales.ts");
|
|
25
|
+
// run("tsx scripts/setup-prisma.ts");
|
|
26
|
+
// run("tsx scripts/generate-middleware.ts");
|
|
27
|
+
// run("tsx scripts/generate-module.ts common");
|
|
28
|
+
|
|
29
|
+
console.log("✅ project generated");
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "crypt-express-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "tsx index.ts",
|
|
9
|
+
"build": "tsc"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"crypt-express-app": "index.ts"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"packageManager": "pnpm@10.29.3",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^25.3.2",
|
|
20
|
+
"tsx": "^4.21.0",
|
|
21
|
+
"typescript": "^5.9.3"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// generate-utils.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
|
|
5
|
+
const basePath = path.join(__dirname, "..");
|
|
6
|
+
|
|
7
|
+
if (!fs.existsSync(basePath)) {
|
|
8
|
+
fs.mkdirSync(basePath, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/* ================= redis.ts ================= */
|
|
12
|
+
const appContent = `
|
|
13
|
+
import express, { type Express } from "express";
|
|
14
|
+
import cors from "cors";
|
|
15
|
+
import morgan from "morgan";
|
|
16
|
+
import { json, urlencoded } from "express";
|
|
17
|
+
import i18next from 'i18next';
|
|
18
|
+
import Backend from 'i18next-fs-backend';
|
|
19
|
+
import middleware from 'i18next-http-middleware';
|
|
20
|
+
import path from 'path';
|
|
21
|
+
import { errorMiddleware } from "./middlewares/error.middleware";
|
|
22
|
+
import { loggerMiddleware } from "./middlewares/logger.middleware";
|
|
23
|
+
import swaggerUi from "swagger-ui-express";
|
|
24
|
+
import { swaggerSpec } from "./utils/swagger";
|
|
25
|
+
|
|
26
|
+
export const app: Express = express();
|
|
27
|
+
|
|
28
|
+
// i18n
|
|
29
|
+
i18next
|
|
30
|
+
.use(Backend)
|
|
31
|
+
.use(middleware.LanguageDetector)
|
|
32
|
+
.init({
|
|
33
|
+
fallbackLng: 'en',
|
|
34
|
+
preload: ['en', 'bn'],
|
|
35
|
+
backend: {
|
|
36
|
+
loadPath: path.join(__dirname, 'locales/{{lng}}/translation.json')
|
|
37
|
+
},
|
|
38
|
+
detection: {
|
|
39
|
+
order: ['querystring', 'cookie', 'header'],
|
|
40
|
+
caches: ['cookie']
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Middlewares
|
|
45
|
+
app.use(cors());
|
|
46
|
+
app.use(json());
|
|
47
|
+
app.use(urlencoded({ extended: true }));
|
|
48
|
+
app.use(morgan("dev"));
|
|
49
|
+
app.use(middleware.handle(i18next));
|
|
50
|
+
|
|
51
|
+
// Logger middleware should come first
|
|
52
|
+
app.use(loggerMiddleware);
|
|
53
|
+
|
|
54
|
+
// Routes
|
|
55
|
+
app.get('/', (req, res) => {
|
|
56
|
+
res.send(req.t('WELCOME')); // automatically returns text in detected language
|
|
57
|
+
});
|
|
58
|
+
app.use("/api/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
|
|
59
|
+
swaggerOptions: {
|
|
60
|
+
persistAuthorization: true, // ✅ এটা রাখে token reload-এও
|
|
61
|
+
},
|
|
62
|
+
}));
|
|
63
|
+
|
|
64
|
+
// Error handler
|
|
65
|
+
app.use(errorMiddleware);
|
|
66
|
+
|
|
67
|
+
export default app;
|
|
68
|
+
`;
|
|
69
|
+
fs.writeFileSync(path.join(basePath, "src", "app.ts"), appContent);
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/* ================= redis.ts ================= */
|
|
74
|
+
const serverContent = `
|
|
75
|
+
import app from "./src/app";
|
|
76
|
+
import bootstrapSystem from "./src/utils/bootstrap";
|
|
77
|
+
import { connectRedis } from "./src/utils/redis";
|
|
78
|
+
|
|
79
|
+
async function startServer() {
|
|
80
|
+
try{
|
|
81
|
+
await bootstrapSystem(); // 🔥 important
|
|
82
|
+
await connectRedis();
|
|
83
|
+
|
|
84
|
+
app.listen(5010, () => {
|
|
85
|
+
console.log("🚀 Server running at http://localhost:3000");
|
|
86
|
+
console.log("📘 Swagger docs at http://localhost:3000/api/docs");
|
|
87
|
+
});
|
|
88
|
+
}catch(error){
|
|
89
|
+
console.log("Error.........\\n\\n")
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
process.on("uncaughtException", (err) => {
|
|
93
|
+
console.error("🔥 Uncaught Exception:", err);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
process.on("unhandledRejection", (reason) => {
|
|
97
|
+
console.error("🔥 Unhandled Rejection:", reason);
|
|
98
|
+
});
|
|
99
|
+
startServer();
|
|
100
|
+
`;
|
|
101
|
+
fs.writeFileSync(path.join(basePath, "server.ts"), serverContent);
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
console.log("✅ App files generated successfully inside src");
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// generate-locales.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
|
|
5
|
+
const basePath = path.join(__dirname, "..", "src", "locales");
|
|
6
|
+
|
|
7
|
+
const bnPath = path.join(basePath, "bn");
|
|
8
|
+
const enPath = path.join(basePath, "en");
|
|
9
|
+
|
|
10
|
+
// ফোল্ডার তৈরি
|
|
11
|
+
[basePath, bnPath, enPath].forEach((dir) => {
|
|
12
|
+
if (!fs.existsSync(dir)) {
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// বাংলা translation
|
|
18
|
+
const bnContent = `
|
|
19
|
+
{
|
|
20
|
+
"WELCOME": "আমাদের এপিতে স্বাগতম",
|
|
21
|
+
"USER_NOT_FOUND": "ব্যবহারকারী পাওয়া যায়নি"
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
|
|
25
|
+
// ইংরেজি translation
|
|
26
|
+
const enContent = `
|
|
27
|
+
{
|
|
28
|
+
"WELCOME": "Welcome to our API",
|
|
29
|
+
"USER_NOT_FOUND": "User not found"
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
fs.writeFileSync(path.join(bnPath, "translation.json"), bnContent.trim());
|
|
34
|
+
fs.writeFileSync(path.join(enPath, "translation.json"), enContent.trim());
|
|
35
|
+
|
|
36
|
+
console.log("Locales generated successfully inside src/locales");
|