opticore-api-gateway 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 +618 -0
- package/dist/index.cjs +1100 -0
- package/dist/index.d.cts +329 -0
- package/dist/index.d.ts +329 -0
- package/dist/index.js +1056 -0
- package/dist/utils/translations/message.translation.en.json +5 -0
- package/dist/utils/translations/message.translation.fr.json +5 -0
- package/package.json +50 -0
- package/scripts/postinstall.cjs +127 -0
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opticore-api-gateway",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "OptiCore API Gateway",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"postinstall": "node scripts/postinstall.cjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"scripts"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/guyzoum77/opticore-api-gateway.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"opticore",
|
|
24
|
+
"opticore-api-gateway"
|
|
25
|
+
],
|
|
26
|
+
"author": "Guy-serge Kouacou",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/guyzoum77/opticore-api-gateway/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/guyzoum77/opticore-api-gateway#readme",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"ansi-colors": "^4.1.3",
|
|
34
|
+
"chalk": "^5.6.2",
|
|
35
|
+
"gradient-string": "^3.0.0",
|
|
36
|
+
"opticore-http-response": "^1.0.9",
|
|
37
|
+
"opticore-logger": "^1.0.29",
|
|
38
|
+
"opticore-router": "^1.0.20",
|
|
39
|
+
"opticore-translator": "^1.0.13",
|
|
40
|
+
"opticore-validator": "^1.0.3"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/gradient-string": "^1.1.6",
|
|
44
|
+
"@types/node": "^22.19.7",
|
|
45
|
+
"reflect-metadata": "^0.2.2",
|
|
46
|
+
"tslib": "^2.8.1",
|
|
47
|
+
"tsup": "^8.4.0",
|
|
48
|
+
"typescript": "^5.4.5"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* postinstall.js
|
|
5
|
+
*
|
|
6
|
+
* Runs automatically after `npm install opticore-api-gateway` inside a project.
|
|
7
|
+
* If the project looks like an opticore-webapp template (has src/bootstrap/server/webApp.server.ts),
|
|
8
|
+
* this script:
|
|
9
|
+
* 1. Creates src/bootstrap/apiGateway.ts (gateway configuration)
|
|
10
|
+
* 2. Patches src/bootstrap/server/webApp.server.ts (replaces registerRouter() with gateway.getOpticoreRoutes())
|
|
11
|
+
*
|
|
12
|
+
* Safe to re-run: already-patched files are left untouched.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
|
|
18
|
+
// When installed as a dependency, process.cwd() is the consumer project root.
|
|
19
|
+
// When running in the package's own dev install, skip everything.
|
|
20
|
+
const projectRoot = process.cwd();
|
|
21
|
+
const packageDir = path.resolve(__dirname, '..');
|
|
22
|
+
|
|
23
|
+
if (projectRoot === packageDir) {
|
|
24
|
+
// Running inside the gateway package itself — nothing to do
|
|
25
|
+
process.exit(0);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const webAppServerPath = path.join(projectRoot, 'src', 'bootstrap', 'server', 'webApp.server.ts');
|
|
29
|
+
const apiGatewayPath = path.join(projectRoot, 'src', 'bootstrap', 'apiGateway.ts');
|
|
30
|
+
const bootstrapDir = path.join(projectRoot, 'src', 'bootstrap');
|
|
31
|
+
|
|
32
|
+
// Only proceed if this looks like an opticore-webapp template project
|
|
33
|
+
if (!fs.existsSync(webAppServerPath)) {
|
|
34
|
+
process.exit(0);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (fs.existsSync(apiGatewayPath)) {
|
|
38
|
+
console.log('[opticore-api-gateway] src/bootstrap/apiGateway.ts already exists — skipping.');
|
|
39
|
+
} else {
|
|
40
|
+
const apiGatewayContent = `import {
|
|
41
|
+
APIGateway,
|
|
42
|
+
AuthMiddleware,
|
|
43
|
+
RateLimitMiddleware,
|
|
44
|
+
LoggingMiddleware
|
|
45
|
+
} from "opticore-api-gateway";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* API Gateway configuration.
|
|
49
|
+
*
|
|
50
|
+
* - Add your microservices to \`services\`.
|
|
51
|
+
* - Define how incoming routes map to those services in \`routes\`.
|
|
52
|
+
* - \`globalMiddlewares\` are applied to every matched request before proxying.
|
|
53
|
+
*
|
|
54
|
+
* In standalone mode: call \`gateway.start()\` to spin up a dedicated HTTP server.
|
|
55
|
+
* In opticore-webapp mode: pass \`gateway.getOpticoreRoutes()\` to \`onStartServer()\`.
|
|
56
|
+
*/
|
|
57
|
+
export const gateway = new APIGateway({
|
|
58
|
+
port: 4200, // used only by gateway.start() — WebServerCore controls the Express port
|
|
59
|
+
services: [
|
|
60
|
+
{ name: "user-service", url: "http://user-service:4001" },
|
|
61
|
+
{ name: "product-service", url: "http://product-service:4002" },
|
|
62
|
+
],
|
|
63
|
+
routes: [
|
|
64
|
+
{
|
|
65
|
+
path: "/api/users",
|
|
66
|
+
method: "get",
|
|
67
|
+
target: "http://user-service:4001",
|
|
68
|
+
serviceName: "user-service",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
path: "/api/products",
|
|
72
|
+
method: "get",
|
|
73
|
+
// Multiple targets activate load balancing
|
|
74
|
+
target: ["http://product-service-1:4002", "http://product-service-2:4002"],
|
|
75
|
+
serviceName: "product-service",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
globalMiddlewares: [
|
|
79
|
+
new LoggingMiddleware("info"),
|
|
80
|
+
new RateLimitMiddleware(100, 60000),
|
|
81
|
+
new AuthMiddleware(["my-api-key"]),
|
|
82
|
+
],
|
|
83
|
+
loadBalancer: "round-robin",
|
|
84
|
+
enableLogging: true,
|
|
85
|
+
});
|
|
86
|
+
`;
|
|
87
|
+
|
|
88
|
+
fs.mkdirSync(bootstrapDir, { recursive: true });
|
|
89
|
+
fs.writeFileSync(apiGatewayPath, apiGatewayContent, 'utf8');
|
|
90
|
+
console.log('[opticore-api-gateway] Created src/bootstrap/apiGateway.ts');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let serverContent = fs.readFileSync(webAppServerPath, 'utf8');
|
|
94
|
+
|
|
95
|
+
if (serverContent.includes('opticore-api-gateway') || serverContent.includes('apiGateway')) {
|
|
96
|
+
console.log('[opticore-api-gateway] src/bootstrap/server/webApp.server.ts already patched — skipping.');
|
|
97
|
+
process.exit(0);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Add the gateway import after the last existing import line
|
|
101
|
+
const gatewayImport = `import { gateway } from "../apiGateway";`;
|
|
102
|
+
|
|
103
|
+
// Insert import after the last import block
|
|
104
|
+
const lastImportIndex = serverContent.lastIndexOf('\nimport ');
|
|
105
|
+
if (lastImportIndex !== -1) {
|
|
106
|
+
const insertAfter = serverContent.indexOf('\n', lastImportIndex + 1);
|
|
107
|
+
serverContent =
|
|
108
|
+
serverContent.slice(0, insertAfter) +
|
|
109
|
+
'\n' + gatewayImport +
|
|
110
|
+
serverContent.slice(insertAfter);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Replace registerRouter() with gateway.getOpticoreRoutes()
|
|
114
|
+
serverContent = serverContent.replace(
|
|
115
|
+
/registerRouter\(\)/g,
|
|
116
|
+
'gateway.getOpticoreRoutes()'
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Remove the registerRouter import if present
|
|
120
|
+
serverContent = serverContent.replace(
|
|
121
|
+
/^import\s*\{[^}]*registerRouter[^}]*\}\s*from\s*["'][^"']+["'];\s*\n?/m,
|
|
122
|
+
''
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
fs.writeFileSync(webAppServerPath, serverContent, 'utf8');
|
|
126
|
+
console.log('[opticore-api-gateway] Patched src/bootstrap/server/webApp.server.ts');
|
|
127
|
+
console.log('[opticore-api-gateway] Setup complete. Edit src/bootstrap/apiGateway.ts to configure your services and routes.');
|