fragment-ts 1.0.2 ā 1.0.4
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 +401 -0
- package/USAGE.md +1439 -0
- package/bin/fragment.js +2 -0
- package/dist/ai/ai.module.d.ts +27 -0
- package/dist/ai/ai.module.d.ts.map +1 -0
- package/dist/ai/ai.module.js +77 -0
- package/dist/ai/ai.module.js.map +1 -0
- package/dist/auth/auth.module.d.ts +18 -0
- package/dist/auth/auth.module.d.ts.map +1 -0
- package/dist/auth/auth.module.js +89 -0
- package/dist/auth/auth.module.js.map +1 -0
- package/dist/cli/commands/build.command.d.ts +6 -1
- package/dist/cli/commands/build.command.d.ts.map +1 -0
- package/dist/cli/commands/build.command.js +130 -10
- package/dist/cli/commands/build.command.js.map +1 -1
- package/dist/cli/commands/diagnostics.command.d.ts +16 -0
- package/dist/cli/commands/diagnostics.command.d.ts.map +1 -0
- package/dist/cli/commands/diagnostics.command.js +419 -0
- package/dist/cli/commands/diagnostics.command.js.map +1 -0
- package/dist/cli/commands/generate.command.d.ts +13 -6
- package/dist/cli/commands/generate.command.d.ts.map +1 -0
- package/dist/cli/commands/generate.command.js +200 -128
- package/dist/cli/commands/generate.command.js.map +1 -1
- package/dist/cli/commands/init.command.d.ts +11 -5
- package/dist/cli/commands/init.command.d.ts.map +1 -0
- package/dist/cli/commands/init.command.js +267 -305
- package/dist/cli/commands/init.command.js.map +1 -1
- package/dist/cli/commands/migrate.command.d.ts +12 -5
- package/dist/cli/commands/migrate.command.d.ts.map +1 -0
- package/dist/cli/commands/migrate.command.js +211 -83
- package/dist/cli/commands/migrate.command.js.map +1 -1
- package/dist/cli/commands/serve.command.d.ts +5 -4
- package/dist/cli/commands/serve.command.d.ts.map +1 -0
- package/dist/cli/commands/serve.command.js +178 -21
- package/dist/cli/commands/serve.command.js.map +1 -1
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +22 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/container/di-container.d.ts +13 -7
- package/dist/core/container/di-container.d.ts.map +1 -0
- package/dist/core/container/di-container.js +75 -25
- package/dist/core/container/di-container.js.map +1 -1
- package/dist/core/decorators/application.decorator.d.ts +8 -0
- package/dist/core/decorators/application.decorator.d.ts.map +1 -0
- package/dist/core/decorators/application.decorator.js +16 -0
- package/dist/core/decorators/application.decorator.js.map +1 -0
- package/dist/core/decorators/auto-configuration.decorator.d.ts +2 -0
- package/dist/core/decorators/auto-configuration.decorator.d.ts.map +1 -0
- package/dist/core/decorators/auto-configuration.decorator.js +19 -0
- package/dist/core/decorators/auto-configuration.decorator.js.map +1 -0
- package/dist/core/decorators/conditional.decorators.d.ts +4 -0
- package/dist/core/decorators/conditional.decorators.d.ts.map +1 -0
- package/dist/core/decorators/conditional.decorators.js +22 -0
- package/dist/core/decorators/conditional.decorators.js.map +1 -0
- package/dist/core/decorators/controller.decorator.d.ts +1 -3
- package/dist/core/decorators/controller.decorator.d.ts.map +1 -0
- package/dist/core/decorators/controller.decorator.js +12 -9
- package/dist/core/decorators/controller.decorator.js.map +1 -1
- package/dist/core/decorators/http.decorators.d.ts +12 -0
- package/dist/core/decorators/http.decorators.d.ts.map +1 -0
- package/dist/core/decorators/http.decorators.js +52 -0
- package/dist/core/decorators/http.decorators.js.map +1 -0
- package/dist/core/decorators/injectable.decorator.d.ts +3 -3
- package/dist/core/decorators/injectable.decorator.d.ts.map +1 -0
- package/dist/core/decorators/injectable.decorator.js +11 -8
- package/dist/core/decorators/injectable.decorator.js.map +1 -1
- package/dist/core/decorators/injection.decorators.d.ts +5 -0
- package/dist/core/decorators/injection.decorators.d.ts.map +1 -0
- package/dist/core/decorators/injection.decorators.js +29 -0
- package/dist/core/decorators/injection.decorators.js.map +1 -0
- package/dist/core/decorators/repository.decorator.d.ts +1 -0
- package/dist/core/decorators/repository.decorator.d.ts.map +1 -0
- package/dist/core/decorators/repository.decorator.js +12 -1
- package/dist/core/decorators/repository.decorator.js.map +1 -1
- package/dist/core/decorators/service.decorator.d.ts +1 -0
- package/dist/core/decorators/service.decorator.d.ts.map +1 -0
- package/dist/core/decorators/service.decorator.js +12 -1
- package/dist/core/decorators/service.decorator.js.map +1 -1
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +29 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/loader/file-loader.d.ts +5 -0
- package/dist/core/loader/file-loader.d.ts.map +1 -0
- package/dist/core/loader/file-loader.js +85 -0
- package/dist/core/loader/file-loader.js.map +1 -0
- package/dist/core/metadata/metadata-keys.d.ts +20 -0
- package/dist/core/metadata/metadata-keys.d.ts.map +1 -0
- package/dist/core/metadata/metadata-keys.js +23 -0
- package/dist/core/metadata/metadata-keys.js.map +1 -0
- package/dist/core/metadata/metadata-storage.d.ts +36 -0
- package/dist/core/metadata/metadata-storage.d.ts.map +1 -0
- package/dist/core/metadata/metadata-storage.js +53 -0
- package/dist/core/metadata/metadata-storage.js.map +1 -0
- package/dist/core/scanner/component-scanner.d.ts +16 -0
- package/dist/core/scanner/component-scanner.d.ts.map +1 -0
- package/dist/core/scanner/component-scanner.js +147 -0
- package/dist/core/scanner/component-scanner.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/plugin-manager.d.ts +14 -0
- package/dist/plugins/plugin-manager.d.ts.map +1 -0
- package/dist/{cli/utils/file-generator.js ā plugins/plugin-manager.js} +32 -24
- package/dist/plugins/plugin-manager.js.map +1 -0
- package/dist/shared/errors.d.ts +18 -0
- package/dist/shared/errors.d.ts.map +1 -0
- package/dist/shared/errors.js +41 -0
- package/dist/shared/errors.js.map +1 -0
- package/dist/testing/runner.d.ts +26 -0
- package/dist/testing/runner.d.ts.map +1 -0
- package/dist/testing/runner.js +143 -0
- package/dist/testing/runner.js.map +1 -0
- package/dist/typeorm/typeorm-module.d.ts +36 -0
- package/dist/typeorm/typeorm-module.d.ts.map +1 -0
- package/dist/typeorm/typeorm-module.js +150 -0
- package/dist/typeorm/typeorm-module.js.map +1 -0
- package/dist/web/application.d.ts +29 -0
- package/dist/web/application.d.ts.map +1 -0
- package/dist/web/application.js +301 -0
- package/dist/web/application.js.map +1 -0
- package/dist/web/interfaces.d.ts +14 -0
- package/dist/web/interfaces.d.ts.map +1 -0
- package/dist/{auth/dto/login.dto.js ā web/interfaces.js} +1 -1
- package/dist/web/interfaces.js.map +1 -0
- package/examples/blog-api/fragment.json +14 -0
- package/examples/blog-api/package-lock.json +3405 -0
- package/examples/blog-api/package.json +19 -0
- package/examples/blog-api/src/controllers/app.controller.ts +9 -0
- package/examples/blog-api/src/controllers/auth.controller.ts +17 -0
- package/examples/blog-api/src/controllers/category.controller.ts +29 -0
- package/examples/blog-api/src/controllers/comment.controller.ts +31 -0
- package/examples/blog-api/src/controllers/post.controller.ts +46 -0
- package/examples/blog-api/src/dto/create-category.dto.ts +6 -0
- package/examples/blog-api/src/dto/create-comment.dto.ts +6 -0
- package/examples/blog-api/src/dto/create-post.dto.ts +6 -0
- package/examples/blog-api/src/entities/category.entity.ts +16 -0
- package/examples/blog-api/src/entities/comment.entity.ts +29 -0
- package/examples/blog-api/src/entities/post.entity.ts +42 -0
- package/examples/blog-api/src/entities/user.entity.ts +25 -0
- package/examples/blog-api/src/main.ts +16 -0
- package/examples/blog-api/src/migrations/1767737463842-InitialSchema.ts +60 -0
- package/examples/blog-api/src/repositories/category.repository.ts +25 -0
- package/examples/blog-api/src/repositories/comment.repository.ts +25 -0
- package/examples/blog-api/src/repositories/post.repository.ts +29 -0
- package/examples/blog-api/src/seeds/SampleData.seed.ts +41 -0
- package/examples/blog-api/src/services/app.service.ts +8 -0
- package/examples/blog-api/src/services/auth.service.ts +15 -0
- package/examples/blog-api/src/services/category.service.ts +27 -0
- package/examples/blog-api/src/services/comment.service.ts +31 -0
- package/examples/blog-api/src/services/post.service.ts +35 -0
- package/examples/blog-api/tsconfig.json +23 -0
- package/package.json +55 -33
- package/src/ai/ai.module.ts +110 -0
- package/src/auth/auth.module.ts +77 -0
- package/src/cli/commands/build.command.ts +123 -13
- package/src/cli/commands/diagnostics.command.ts +438 -0
- package/src/cli/commands/generate.command.ts +206 -137
- package/src/cli/commands/init.command.ts +337 -349
- package/src/cli/commands/migrate.command.ts +203 -88
- package/src/cli/commands/serve.command.ts +176 -24
- package/src/cli/index.ts +23 -0
- package/src/core/container/di-container.ts +83 -26
- package/src/core/decorators/application.decorator.ts +26 -0
- package/src/core/decorators/auto-configuration.decorator.ts +17 -0
- package/src/core/decorators/conditional.decorators.ts +19 -0
- package/src/core/decorators/controller.decorator.ts +14 -11
- package/src/core/decorators/http.decorators.ts +71 -0
- package/src/core/decorators/injectable.decorator.ts +14 -9
- package/src/core/decorators/injection.decorators.ts +26 -0
- package/src/core/decorators/repository.decorator.ts +13 -2
- package/src/core/decorators/service.decorator.ts +13 -2
- package/src/core/index.ts +13 -0
- package/src/core/loader/file-loader.ts +55 -0
- package/src/core/metadata/metadata-keys.ts +19 -0
- package/src/core/metadata/metadata-storage.ts +91 -0
- package/src/core/scanner/component-scanner.ts +129 -0
- package/src/index.ts +45 -0
- package/src/plugins/plugin-manager.ts +52 -0
- package/src/shared/errors.ts +34 -0
- package/src/testing/runner.ts +143 -0
- package/src/typeorm/typeorm-module.ts +216 -0
- package/src/web/application.ts +348 -0
- package/src/web/interfaces.ts +17 -0
- package/tsconfig.json +8 -6
- package/.env.example +0 -0
- package/base.ts +0 -1810
- package/base2.ts +0 -968
- package/bin/frg.ts +0 -5
- package/config/fragment.lock.yaml +0 -0
- package/config/fragment.yaml +0 -0
- package/dist/app.d.ts +0 -15
- package/dist/app.js +0 -91
- package/dist/app.js.map +0 -1
- package/dist/auth/auth.controller.d.ts +0 -10
- package/dist/auth/auth.controller.js +0 -88
- package/dist/auth/auth.controller.js.map +0 -1
- package/dist/auth/auth.middleware.d.ts +0 -2
- package/dist/auth/auth.middleware.js +0 -25
- package/dist/auth/auth.middleware.js.map +0 -1
- package/dist/auth/auth.service.d.ts +0 -20
- package/dist/auth/auth.service.js +0 -144
- package/dist/auth/auth.service.js.map +0 -1
- package/dist/auth/dto/login.dto.d.ts +0 -9
- package/dist/auth/dto/login.dto.js.map +0 -1
- package/dist/cli/cli.d.ts +0 -12
- package/dist/cli/cli.js +0 -187
- package/dist/cli/cli.js.map +0 -1
- package/dist/cli/commands/config.command.d.ts +0 -6
- package/dist/cli/commands/config.command.js +0 -285
- package/dist/cli/commands/config.command.js.map +0 -1
- package/dist/cli/templates/controller.template.d.ts +0 -1
- package/dist/cli/templates/controller.template.js +0 -53
- package/dist/cli/templates/controller.template.js.map +0 -1
- package/dist/cli/templates/entity.template.d.ts +0 -1
- package/dist/cli/templates/entity.template.js +0 -24
- package/dist/cli/templates/entity.template.js.map +0 -1
- package/dist/cli/templates/repository.template.d.ts +0 -1
- package/dist/cli/templates/repository.template.js +0 -44
- package/dist/cli/templates/repository.template.js.map +0 -1
- package/dist/cli/templates/service.template.d.ts +0 -1
- package/dist/cli/templates/service.template.js +0 -44
- package/dist/cli/templates/service.template.js.map +0 -1
- package/dist/cli/utils/file-generator.d.ts +0 -9
- package/dist/cli/utils/file-generator.js.map +0 -1
- package/dist/cli/utils/logger.d.ts +0 -14
- package/dist/cli/utils/logger.js +0 -50
- package/dist/cli/utils/logger.js.map +0 -1
- package/dist/controllers/health.controller.d.ts +0 -13
- package/dist/controllers/health.controller.js +0 -51
- package/dist/controllers/health.controller.js.map +0 -1
- package/dist/core/config/config-loader.d.ts +0 -31
- package/dist/core/config/config-loader.js +0 -99
- package/dist/core/config/config-loader.js.map +0 -1
- package/dist/core/decorators/auth-guard.decorator.d.ts +0 -3
- package/dist/core/decorators/auth-guard.decorator.js +0 -19
- package/dist/core/decorators/auth-guard.decorator.js.map +0 -1
- package/dist/core/decorators/autowire.decorator.d.ts +0 -3
- package/dist/core/decorators/autowire.decorator.js +0 -18
- package/dist/core/decorators/autowire.decorator.js.map +0 -1
- package/dist/core/decorators/middleware.decorator.d.ts +0 -3
- package/dist/core/decorators/middleware.decorator.js +0 -21
- package/dist/core/decorators/middleware.decorator.js.map +0 -1
- package/dist/core/decorators/route.decorator.d.ts +0 -14
- package/dist/core/decorators/route.decorator.js +0 -33
- package/dist/core/decorators/route.decorator.js.map +0 -1
- package/dist/core/openai/openai-client.d.ts +0 -12
- package/dist/core/openai/openai-client.js +0 -94
- package/dist/core/openai/openai-client.js.map +0 -1
- package/dist/database/data-source.d.ts +0 -4
- package/dist/database/data-source.js +0 -27
- package/dist/database/data-source.js.map +0 -1
- package/dist/entities/session.entity.d.ts +0 -9
- package/dist/entities/session.entity.js +0 -46
- package/dist/entities/session.entity.js.map +0 -1
- package/dist/entities/user.entity.d.ts +0 -10
- package/dist/entities/user.entity.js +0 -49
- package/dist/entities/user.entity.js.map +0 -1
- package/dist/middlewares/logging.middleware.d.ts +0 -2
- package/dist/middlewares/logging.middleware.js +0 -29
- package/dist/middlewares/logging.middleware.js.map +0 -1
- package/dist/repositories/session.repository.d.ts +0 -9
- package/dist/repositories/session.repository.js +0 -51
- package/dist/repositories/session.repository.js.map +0 -1
- package/dist/repositories/user.repository.d.ts +0 -10
- package/dist/repositories/user.repository.js +0 -44
- package/dist/repositories/user.repository.js.map +0 -1
- package/dist/server.d.ts +0 -1
- package/dist/server.js +0 -31
- package/dist/server.js.map +0 -1
- package/dist/services/health.service.d.ts +0 -13
- package/dist/services/health.service.js +0 -45
- package/dist/services/health.service.js.map +0 -1
- package/readme.md +0 -120
- package/src/app.ts +0 -121
- package/src/auth/auth.controller.ts +0 -52
- package/src/auth/auth.middleware.ts +0 -27
- package/src/auth/auth.service.ts +0 -110
- package/src/auth/dto/login.dto.ts +0 -11
- package/src/cli/cli.ts +0 -212
- package/src/cli/commands/config.command.ts +0 -280
- package/src/cli/templates/controller.template.ts +0 -51
- package/src/cli/templates/entity.template.ts +0 -22
- package/src/cli/templates/repository.template.ts +0 -42
- package/src/cli/templates/service.template.ts +0 -42
- package/src/cli/utils/file-generator.ts +0 -37
- package/src/cli/utils/logger.ts +0 -52
- package/src/controllers/health.controller.ts +0 -24
- package/src/core/config/config-loader.ts +0 -98
- package/src/core/decorators/auth-guard.decorator.ts +0 -15
- package/src/core/decorators/autowire.decorator.ts +0 -18
- package/src/core/decorators/middleware.decorator.ts +0 -18
- package/src/core/decorators/route.decorator.ts +0 -33
- package/src/core/openai/openai-client.ts +0 -99
- package/src/database/data-source.ts +0 -29
- package/src/entities/session.entity.ts +0 -25
- package/src/entities/user.entity.ts +0 -27
- package/src/middlewares/logging.middleware.ts +0 -28
- package/src/repositories/session.repository.ts +0 -42
- package/src/repositories/user.repository.ts +0 -37
- package/src/server.ts +0 -32
- package/src/services/health.service.ts +0 -29
package/package.json
CHANGED
|
@@ -1,46 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fragment-ts",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "TypeScript
|
|
5
|
-
"main": "dist/
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Spring Boot-style framework for TypeScript with Express and TypeORM",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"bin": {
|
|
7
|
-
"
|
|
8
|
+
"fragment": "./bin/fragment.js",
|
|
9
|
+
"frg": "./bin/fragment.js"
|
|
8
10
|
},
|
|
9
11
|
"scripts": {
|
|
10
|
-
"dev": "ts-node-dev --respawn --transpile-only src/server.ts",
|
|
11
12
|
"build": "tsc",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
14
|
+
"test": "node dist/testing/runner.js"
|
|
14
15
|
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"framework",
|
|
18
|
+
"typescript",
|
|
19
|
+
"express",
|
|
20
|
+
"typeorm",
|
|
21
|
+
"spring-boot",
|
|
22
|
+
"dependency-injection"
|
|
23
|
+
],
|
|
24
|
+
"author": "Fragment Team",
|
|
25
|
+
"license": "MIT",
|
|
15
26
|
"dependencies": {
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"pg": "^8.11.3",
|
|
19
|
-
"mysql2": "^3.6.0",
|
|
20
|
-
"sqlite3": "^5.1.6",
|
|
21
|
-
"dotenv": "^16.3.1",
|
|
22
|
-
"pino": "^8.15.0",
|
|
23
|
-
"pino-pretty": "^10.2.0",
|
|
24
|
-
"reflect-metadata": "^0.1.13",
|
|
25
|
-
"commander": "^11.0.0",
|
|
26
|
-
"inquirer": "^9.2.11",
|
|
27
|
+
"axios": "^1.6.2",
|
|
28
|
+
"bcryptjs": "^2.4.3",
|
|
27
29
|
"chalk": "^4.1.2",
|
|
30
|
+
"chokidar": "^3.6.0",
|
|
31
|
+
"class-transformer": "^0.5.1",
|
|
32
|
+
"class-validator": "^0.14.0",
|
|
33
|
+
"commander": "^11.1.0",
|
|
34
|
+
"compression": "^1.7.4",
|
|
35
|
+
"cors": "^2.8.5",
|
|
36
|
+
"dotenv": "^16.3.1",
|
|
37
|
+
"ejs": "^3.1.9",
|
|
38
|
+
"express": "^4.18.2",
|
|
39
|
+
"express-rate-limit": "^7.1.5",
|
|
40
|
+
"figlet": "^1.9.4",
|
|
41
|
+
"fs-extra": "^11.1.1",
|
|
42
|
+
"glob": "^10.3.10",
|
|
43
|
+
"helmet": "^7.1.0",
|
|
44
|
+
"inquirer": "^9.2.12",
|
|
45
|
+
"jsonwebtoken": "^9.0.2",
|
|
46
|
+
"openai": "^4.20.1",
|
|
28
47
|
"ora": "^5.4.1",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"axios": "^1.6.0",
|
|
33
|
-
"bcrypt": "^5.1.1"
|
|
48
|
+
"reflect-metadata": "^0.1.13",
|
|
49
|
+
"ts-node": "^10.9.2",
|
|
50
|
+
"typeorm": "^0.3.17"
|
|
34
51
|
},
|
|
35
52
|
"devDependencies": {
|
|
36
|
-
"@types/
|
|
37
|
-
"@types/
|
|
38
|
-
"@types/
|
|
39
|
-
"@types/
|
|
40
|
-
"@types/
|
|
41
|
-
"@types/
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
53
|
+
"@types/bcryptjs": "^2.4.6",
|
|
54
|
+
"@types/chokidar": "^2.1.3",
|
|
55
|
+
"@types/compression": "^1.7.5",
|
|
56
|
+
"@types/cors": "^2.8.17",
|
|
57
|
+
"@types/express": "^4.17.21",
|
|
58
|
+
"@types/figlet": "^1.7.0",
|
|
59
|
+
"@types/fs-extra": "^11.0.4",
|
|
60
|
+
"@types/inquirer": "^9.0.9",
|
|
61
|
+
"@types/jsonwebtoken": "^9.0.5",
|
|
62
|
+
"@types/node": "^20.10.5",
|
|
63
|
+
"typescript": "^5.3.3"
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": ">=16.0.0"
|
|
45
67
|
}
|
|
46
|
-
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
|
|
4
|
+
export interface AIMessage {
|
|
5
|
+
role: 'system' | 'user' | 'assistant';
|
|
6
|
+
content: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface AICompletionOptions {
|
|
10
|
+
model?: string;
|
|
11
|
+
temperature?: number;
|
|
12
|
+
maxTokens?: number;
|
|
13
|
+
stream?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class AIModule {
|
|
17
|
+
private openaiClient?: OpenAI;
|
|
18
|
+
private azureClient?: any;
|
|
19
|
+
private ollamaBaseUrl?: string;
|
|
20
|
+
|
|
21
|
+
constructor(config?: {
|
|
22
|
+
openaiKey?: string;
|
|
23
|
+
azureKey?: string;
|
|
24
|
+
azureEndpoint?: string;
|
|
25
|
+
ollamaUrl?: string;
|
|
26
|
+
}) {
|
|
27
|
+
if (config?.openaiKey) {
|
|
28
|
+
this.openaiClient = new OpenAI({ apiKey: config.openaiKey });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (config?.azureKey && config?.azureEndpoint) {
|
|
32
|
+
this.azureClient = { key: config.azureKey, endpoint: config.azureEndpoint };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (config?.ollamaUrl) {
|
|
36
|
+
this.ollamaBaseUrl = config.ollamaUrl;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async complete(messages: AIMessage[], options: AICompletionOptions = {}): Promise<string> {
|
|
41
|
+
if (this.openaiClient) {
|
|
42
|
+
return this.completeWithOpenAI(messages, options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (this.ollamaBaseUrl) {
|
|
46
|
+
return this.completeWithOllama(messages, options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
throw new Error('No AI provider configured');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private async completeWithOpenAI(messages: AIMessage[], options: AICompletionOptions): Promise<string> {
|
|
53
|
+
const response = await this.openaiClient!.chat.completions.create({
|
|
54
|
+
model: options.model || 'gpt-3.5-turbo',
|
|
55
|
+
messages: messages as any,
|
|
56
|
+
temperature: options.temperature || 0.7,
|
|
57
|
+
max_tokens: options.maxTokens || 1000
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return response.choices[0]?.message?.content || '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async completeWithOllama(messages: AIMessage[], options: AICompletionOptions): Promise<string> {
|
|
64
|
+
const response = await axios.post(`${this.ollamaBaseUrl}/api/chat`, {
|
|
65
|
+
model: options.model || 'llama2',
|
|
66
|
+
messages,
|
|
67
|
+
stream: false
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return response.data.message.content;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async streamComplete(
|
|
74
|
+
messages: AIMessage[],
|
|
75
|
+
options: AICompletionOptions,
|
|
76
|
+
onChunk: (chunk: string) => void
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
if (!this.openaiClient) {
|
|
79
|
+
throw new Error('Streaming only supported with OpenAI');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const stream = await this.openaiClient.chat.completions.create({
|
|
83
|
+
model: options.model || 'gpt-3.5-turbo',
|
|
84
|
+
messages: messages as any,
|
|
85
|
+
temperature: options.temperature || 0.7,
|
|
86
|
+
max_tokens: options.maxTokens || 1000,
|
|
87
|
+
stream: true
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
for await (const chunk of stream) {
|
|
91
|
+
const content = chunk.choices[0]?.delta?.content || '';
|
|
92
|
+
if (content) {
|
|
93
|
+
onChunk(content);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async embeddings(texts: string[]): Promise<number[][]> {
|
|
99
|
+
if (!this.openaiClient) {
|
|
100
|
+
throw new Error('Embeddings only supported with OpenAI');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const response = await this.openaiClient.embeddings.create({
|
|
104
|
+
model: 'text-embedding-ada-002',
|
|
105
|
+
input: texts
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return response.data.map(item => item.embedding);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import * as jwt from "jsonwebtoken";
|
|
2
|
+
import * as bcrypt from "bcryptjs";
|
|
3
|
+
import { Request, Response, NextFunction } from "express";
|
|
4
|
+
|
|
5
|
+
export interface JWTPayload {
|
|
6
|
+
userId: string;
|
|
7
|
+
email?: string;
|
|
8
|
+
roles?: string[];
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class AuthModule {
|
|
13
|
+
private static jwtSecret: jwt.Secret =
|
|
14
|
+
process.env.JWT_SECRET || "fragment-secret-key";
|
|
15
|
+
private static jwtExpiration: string = process.env.JWT_EXPIRATION || "1d";
|
|
16
|
+
|
|
17
|
+
static async hashPassword(password: string): Promise<string> {
|
|
18
|
+
return bcrypt.hash(password, 10);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static async comparePassword(
|
|
22
|
+
password: string,
|
|
23
|
+
hash: string,
|
|
24
|
+
): Promise<boolean> {
|
|
25
|
+
return bcrypt.compare(password, hash);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static generateToken(payload: JWTPayload): string {
|
|
29
|
+
const options: jwt.SignOptions = {
|
|
30
|
+
expiresIn: this.jwtExpiration as unknown as jwt.SignOptions["expiresIn"],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return jwt.sign(payload, this.jwtSecret, options);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static verifyToken(token: string): JWTPayload {
|
|
37
|
+
return jwt.verify(token, this.jwtSecret) as JWTPayload;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static authMiddleware() {
|
|
41
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
42
|
+
const authHeader = req.headers.authorization;
|
|
43
|
+
|
|
44
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
45
|
+
return res.status(401).json({ error: "Unauthorized" });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const token = authHeader.substring(7);
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const payload = AuthModule.verifyToken(token);
|
|
52
|
+
(req as any).user = payload;
|
|
53
|
+
next();
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return res.status(401).json({ error: "Invalid token" });
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static roleGuard(...roles: string[]) {
|
|
61
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
62
|
+
const user = (req as any).user;
|
|
63
|
+
|
|
64
|
+
if (!user || !user.roles) {
|
|
65
|
+
return res.status(403).json({ error: "Forbidden" });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const hasRole = roles.some((role) => user.roles.includes(role));
|
|
69
|
+
|
|
70
|
+
if (!hasRole) {
|
|
71
|
+
return res.status(403).json({ error: "Insufficient permissions" });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
next();
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -1,24 +1,134 @@
|
|
|
1
|
-
|
|
2
|
-
import { execSync } from
|
|
3
|
-
import
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { execSync } from "child_process";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
import chalk from "chalk";
|
|
6
|
+
import ora from "ora";
|
|
4
7
|
|
|
5
8
|
export class BuildCommand {
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
static register(program: Command): void {
|
|
10
|
+
program
|
|
11
|
+
.command("build")
|
|
12
|
+
.description("Build the project for production")
|
|
13
|
+
.option("--minify", "Minify output")
|
|
14
|
+
.option("--analyze", "Analyze bundle size")
|
|
15
|
+
.action(async (options) => {
|
|
16
|
+
await BuildCommand.execute(options);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
8
19
|
|
|
9
|
-
|
|
20
|
+
static async execute(options: any): Promise<void> {
|
|
21
|
+
const spinner = ora("Building project...").start();
|
|
10
22
|
|
|
11
23
|
try {
|
|
12
|
-
|
|
13
|
-
|
|
24
|
+
// Find TypeScript compiler
|
|
25
|
+
const tscPath = this.findTsc();
|
|
26
|
+
|
|
27
|
+
if (!tscPath) {
|
|
28
|
+
spinner.fail("TypeScript compiler not found");
|
|
29
|
+
console.log(chalk.yellow("\nInstall TypeScript:"));
|
|
30
|
+
console.log(chalk.cyan(" npm install --save-dev typescript"));
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check if tsconfig.json exists
|
|
35
|
+
const tsconfigPath = path.join(process.cwd(), "tsconfig.json");
|
|
36
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
37
|
+
spinner.fail("tsconfig.json not found");
|
|
38
|
+
console.log(
|
|
39
|
+
chalk.yellow("\nCreate a tsconfig.json file in your project root"),
|
|
40
|
+
);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
14
43
|
|
|
15
|
-
|
|
16
|
-
|
|
44
|
+
// Run TypeScript compiler
|
|
45
|
+
spinner.text = "Compiling TypeScript...";
|
|
46
|
+
execSync(`"${tscPath}"`, {
|
|
47
|
+
cwd: process.cwd(),
|
|
48
|
+
stdio: "inherit",
|
|
49
|
+
});
|
|
17
50
|
|
|
51
|
+
spinner.succeed("Build completed successfully!");
|
|
52
|
+
|
|
53
|
+
if (options.analyze) {
|
|
54
|
+
console.log(chalk.blue("\nš Bundle Analysis:"));
|
|
55
|
+
this.analyzeBuild();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
console.log(chalk.green("\n⨠Build output: dist/"));
|
|
18
59
|
} catch (error: any) {
|
|
19
|
-
spinner.fail(
|
|
20
|
-
|
|
60
|
+
spinner.fail("Build failed");
|
|
61
|
+
console.error(chalk.red(error.message || error));
|
|
21
62
|
process.exit(1);
|
|
22
63
|
}
|
|
23
64
|
}
|
|
24
|
-
|
|
65
|
+
|
|
66
|
+
private static findTsc(): string | null {
|
|
67
|
+
// Try local node_modules first
|
|
68
|
+
const localTsc = path.join(process.cwd(), "node_modules", ".bin", "tsc");
|
|
69
|
+
|
|
70
|
+
if (fs.existsSync(localTsc)) {
|
|
71
|
+
return localTsc;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Try with platform-specific extension
|
|
75
|
+
const localTscCmd = path.join(
|
|
76
|
+
process.cwd(),
|
|
77
|
+
"node_modules",
|
|
78
|
+
".bin",
|
|
79
|
+
process.platform === "win32" ? "tsc.cmd" : "tsc",
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (fs.existsSync(localTscCmd)) {
|
|
83
|
+
return localTscCmd;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Try global TypeScript
|
|
87
|
+
try {
|
|
88
|
+
execSync("tsc --version", { stdio: "ignore" });
|
|
89
|
+
return "tsc"; // Global tsc is available
|
|
90
|
+
} catch {
|
|
91
|
+
// Not available globally
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private static analyzeBuild(): void {
|
|
98
|
+
const distPath = path.join(process.cwd(), "dist");
|
|
99
|
+
|
|
100
|
+
if (!fs.existsSync(distPath)) {
|
|
101
|
+
console.log(chalk.yellow(" No dist/ directory found"));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let totalSize = 0;
|
|
106
|
+
let fileCount = 0;
|
|
107
|
+
|
|
108
|
+
const calculateSize = (dir: string) => {
|
|
109
|
+
const files = fs.readdirSync(dir);
|
|
110
|
+
|
|
111
|
+
files.forEach((file) => {
|
|
112
|
+
const filePath = path.join(dir, file);
|
|
113
|
+
const stat = fs.statSync(filePath);
|
|
114
|
+
|
|
115
|
+
if (stat.isDirectory()) {
|
|
116
|
+
calculateSize(filePath);
|
|
117
|
+
} else {
|
|
118
|
+
totalSize += stat.size;
|
|
119
|
+
fileCount++;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
calculateSize(distPath);
|
|
125
|
+
|
|
126
|
+
const sizeInKB = (totalSize / 1024).toFixed(2);
|
|
127
|
+
const sizeInMB = (totalSize / 1024 / 1024).toFixed(2);
|
|
128
|
+
|
|
129
|
+
console.log(` Files: ${fileCount}`);
|
|
130
|
+
console.log(
|
|
131
|
+
` Size: ${totalSize > 1024 * 1024 ? sizeInMB + " MB" : sizeInKB + " KB"}`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|