azurajs 2.1.2 → 2.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/README.md +244 -7
- package/dist/LoggingMiddleware-BQKJUBqT.d.ts +6 -0
- package/dist/LoggingMiddleware-CYNvypha.d.cts +6 -0
- package/dist/Server-Ba-EFdi2.d.ts +53 -0
- package/dist/Server-CY3k1FIL.d.cts +53 -0
- package/dist/common.type-BoV71o_C.d.ts +11 -0
- package/dist/common.type-Ct06XeYQ.d.cts +11 -0
- package/dist/config.cjs +89 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +51 -0
- package/dist/config.d.ts +51 -0
- package/dist/config.js +83 -0
- package/dist/config.js.map +1 -0
- package/dist/cookies.cjs +32 -0
- package/dist/cookies.cjs.map +1 -0
- package/dist/cookies.d.cts +8 -0
- package/dist/cookies.d.ts +8 -0
- package/dist/cookies.js +29 -0
- package/dist/cookies.js.map +1 -0
- package/dist/cors.cjs +29 -0
- package/dist/cors.cjs.map +1 -0
- package/dist/cors.d.cts +14 -0
- package/dist/cors.d.ts +14 -0
- package/dist/cors.js +27 -0
- package/dist/cors.js.map +1 -0
- package/dist/decorators.cjs +141 -0
- package/dist/decorators.cjs.map +1 -0
- package/dist/decorators.d.cts +29 -0
- package/dist/decorators.d.ts +29 -0
- package/dist/decorators.js +122 -0
- package/dist/decorators.js.map +1 -0
- package/dist/http-error.cjs +14 -0
- package/dist/http-error.cjs.map +1 -0
- package/dist/http-error.d.cts +7 -0
- package/dist/http-error.d.ts +7 -0
- package/dist/http-error.js +12 -0
- package/dist/http-error.js.map +1 -0
- package/dist/index.cjs +910 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +899 -0
- package/dist/index.js.map +1 -0
- package/dist/infra.cjs +811 -0
- package/dist/infra.cjs.map +1 -0
- package/dist/infra.d.cts +8 -0
- package/dist/infra.d.ts +8 -0
- package/dist/infra.js +800 -0
- package/dist/infra.js.map +1 -0
- package/dist/logger.cjs +26 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +8 -0
- package/dist/logger.d.ts +8 -0
- package/dist/logger.js +24 -0
- package/dist/logger.js.map +1 -0
- package/dist/middleware.cjs +117 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.cts +10 -0
- package/dist/middleware.d.ts +10 -0
- package/dist/middleware.js +114 -0
- package/dist/middleware.js.map +1 -0
- package/dist/plugins.cjs +52 -0
- package/dist/plugins.cjs.map +1 -0
- package/dist/plugins.d.cts +6 -0
- package/dist/plugins.d.ts +6 -0
- package/dist/plugins.js +49 -0
- package/dist/plugins.js.map +1 -0
- package/dist/rate-limit.cjs +27 -0
- package/dist/rate-limit.cjs.map +1 -0
- package/dist/rate-limit.d.cts +8 -0
- package/dist/rate-limit.d.ts +8 -0
- package/dist/rate-limit.js +25 -0
- package/dist/rate-limit.js.map +1 -0
- package/dist/request.type-CJ-EGGcM.d.cts +22 -0
- package/dist/request.type-CJ-EGGcM.d.ts +22 -0
- package/dist/response.type-d6e6eU9D.d.cts +33 -0
- package/dist/response.type-d6e6eU9D.d.ts +33 -0
- package/dist/router.cjs +111 -0
- package/dist/router.cjs.map +1 -0
- package/dist/router.d.cts +27 -0
- package/dist/router.d.ts +27 -0
- package/dist/router.js +109 -0
- package/dist/router.js.map +1 -0
- package/dist/routes.type-VPROfhnL.d.cts +14 -0
- package/dist/routes.type-VPROfhnL.d.ts +14 -0
- package/dist/types.cjs +4 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +6 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.cjs +149 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +10 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +141 -0
- package/dist/utils.js.map +1 -0
- package/dist/validations.type-D4ZhF5g6.d.cts +6 -0
- package/dist/validations.type-D4ZhF5g6.d.ts +6 -0
- package/dist/validators.cjs +69 -0
- package/dist/validators.cjs.map +1 -0
- package/dist/validators.d.cts +11 -0
- package/dist/validators.d.ts +11 -0
- package/dist/validators.js +65 -0
- package/dist/validators.js.map +1 -0
- package/package.json +85 -66
- package/src/index.ts +1 -3
- package/src/infra/Router.ts +53 -3
- package/src/infra/Server.ts +37 -10
- package/src/infra/index.ts +1 -1
- package/src/shared/config/ConfigModule.ts +1 -0
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
🚀 **High performance** - Built for speed with minimal overhead
|
|
13
13
|
📦 **Zero dependencies** - Lightweight and efficient
|
|
14
14
|
🔧 **TypeScript first** - Full type safety out of the box
|
|
15
|
+
📜 **JavaScript support** - Works seamlessly with plain JavaScript too
|
|
15
16
|
🎯 **Parameter injection** - `@Body`, `@Query`, `@Param`, `@Req`, `@Res`, etc.
|
|
16
17
|
🔌 **Middleware support** - Express-compatible middleware system
|
|
17
18
|
⚙️ **Configurable** - File-based configuration (TS, JSON, YAML)
|
|
@@ -144,6 +145,242 @@ await app.listen();
|
|
|
144
145
|
bun run index.ts
|
|
145
146
|
```
|
|
146
147
|
|
|
148
|
+
## JavaScript Support
|
|
149
|
+
|
|
150
|
+
AzuraJS works great with plain JavaScript! Here are the same examples in JavaScript:
|
|
151
|
+
|
|
152
|
+
### JavaScript Quick Start
|
|
153
|
+
|
|
154
|
+
#### 1. Create `azura.config.js`
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
const config = {
|
|
158
|
+
environment: "development",
|
|
159
|
+
server: {
|
|
160
|
+
port: 3000,
|
|
161
|
+
cluster: false,
|
|
162
|
+
ipHost: true,
|
|
163
|
+
https: false,
|
|
164
|
+
},
|
|
165
|
+
logging: {
|
|
166
|
+
enabled: true,
|
|
167
|
+
showDetails: true,
|
|
168
|
+
},
|
|
169
|
+
plugins: {
|
|
170
|
+
cors: {
|
|
171
|
+
enabled: true,
|
|
172
|
+
origins: ["*"],
|
|
173
|
+
},
|
|
174
|
+
rateLimit: {
|
|
175
|
+
enabled: false,
|
|
176
|
+
limit: 100,
|
|
177
|
+
timeframe: 60000,
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export default config;
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
#### 2. Simple Server (Functional Style)
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
import { AzuraClient } from "azurajs";
|
|
189
|
+
import { createLoggingMiddleware } from "azurajs/middleware";
|
|
190
|
+
|
|
191
|
+
const app = new AzuraClient();
|
|
192
|
+
const logger = createLoggingMiddleware(app.getConfig());
|
|
193
|
+
app.use(logger);
|
|
194
|
+
|
|
195
|
+
// Define routes
|
|
196
|
+
app.get("/", (req, res) => {
|
|
197
|
+
res.json({ message: "Hello from AzuraJS with JavaScript!" });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
app.get("/users", (req, res) => {
|
|
201
|
+
res.json({
|
|
202
|
+
users: [
|
|
203
|
+
{ id: 1, name: "John" },
|
|
204
|
+
{ id: 2, name: "Jane" }
|
|
205
|
+
]
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
app.get("/users/:id", (req, res) => {
|
|
210
|
+
const { id } = req.params;
|
|
211
|
+
res.json({ id: Number(id), name: `User ${id}` });
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
app.post("/users", (req, res) => {
|
|
215
|
+
const body = req.body;
|
|
216
|
+
res.status(201).json({
|
|
217
|
+
id: Date.now(),
|
|
218
|
+
...body
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
await app.listen();
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### 3. CRUD API in JavaScript
|
|
226
|
+
|
|
227
|
+
```javascript
|
|
228
|
+
import { AzuraClient } from "azurajs";
|
|
229
|
+
import { createLoggingMiddleware } from "azurajs/middleware";
|
|
230
|
+
|
|
231
|
+
const app = new AzuraClient();
|
|
232
|
+
const logger = createLoggingMiddleware(app.getConfig());
|
|
233
|
+
app.use(logger);
|
|
234
|
+
|
|
235
|
+
// In-memory data store
|
|
236
|
+
const users = [];
|
|
237
|
+
|
|
238
|
+
// List all users
|
|
239
|
+
app.get("/api/users", (req, res) => {
|
|
240
|
+
res.json(users);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Get single user
|
|
244
|
+
app.get("/api/users/:id", (req, res) => {
|
|
245
|
+
const user = users.find(u => u.id === Number(req.params.id));
|
|
246
|
+
if (!user) {
|
|
247
|
+
return res.status(404).json({ error: "User not found" });
|
|
248
|
+
}
|
|
249
|
+
res.json(user);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// Create user
|
|
253
|
+
app.post("/api/users", (req, res) => {
|
|
254
|
+
const user = {
|
|
255
|
+
id: Date.now(),
|
|
256
|
+
...req.body
|
|
257
|
+
};
|
|
258
|
+
users.push(user);
|
|
259
|
+
res.status(201).json(user);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Update user
|
|
263
|
+
app.put("/api/users/:id", (req, res) => {
|
|
264
|
+
const index = users.findIndex(u => u.id === Number(req.params.id));
|
|
265
|
+
if (index === -1) {
|
|
266
|
+
return res.status(404).json({ error: "User not found" });
|
|
267
|
+
}
|
|
268
|
+
users[index] = { ...users[index], ...req.body };
|
|
269
|
+
res.json(users[index]);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// Delete user
|
|
273
|
+
app.delete("/api/users/:id", (req, res) => {
|
|
274
|
+
const index = users.findIndex(u => u.id === Number(req.params.id));
|
|
275
|
+
if (index === -1) {
|
|
276
|
+
return res.status(404).json({ error: "User not found" });
|
|
277
|
+
}
|
|
278
|
+
users.splice(index, 1);
|
|
279
|
+
res.status(204).send();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
await app.listen();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
#### 4. Middleware Example in JavaScript
|
|
286
|
+
|
|
287
|
+
```javascript
|
|
288
|
+
import { AzuraClient } from "azurajs";
|
|
289
|
+
|
|
290
|
+
const app = new AzuraClient();
|
|
291
|
+
|
|
292
|
+
// Custom authentication middleware
|
|
293
|
+
app.use((req, res, next) => {
|
|
294
|
+
const token = req.headers["authorization"];
|
|
295
|
+
|
|
296
|
+
if (!token) {
|
|
297
|
+
return res.status(401).json({ error: "No token provided" });
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Verify token logic here
|
|
301
|
+
req.user = { id: 1, name: "John" };
|
|
302
|
+
next();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// Custom logging middleware
|
|
306
|
+
app.use((req, res, next) => {
|
|
307
|
+
const start = Date.now();
|
|
308
|
+
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
|
|
309
|
+
|
|
310
|
+
next();
|
|
311
|
+
|
|
312
|
+
const duration = Date.now() - start;
|
|
313
|
+
console.log(`Request completed in ${duration}ms`);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
app.get("/protected", (req, res) => {
|
|
317
|
+
res.json({ message: `Hello ${req.user.name}!` });
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
await app.listen();
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
#### 5. Cookie Handling in JavaScript
|
|
324
|
+
|
|
325
|
+
```javascript
|
|
326
|
+
import { AzuraClient } from "azurajs";
|
|
327
|
+
|
|
328
|
+
const app = new AzuraClient();
|
|
329
|
+
|
|
330
|
+
// Set cookie
|
|
331
|
+
app.get("/set-cookie", (req, res) => {
|
|
332
|
+
res.cookie("user_session", "abc123", {
|
|
333
|
+
httpOnly: true,
|
|
334
|
+
maxAge: 3600000, // 1 hour
|
|
335
|
+
secure: true,
|
|
336
|
+
sameSite: "strict"
|
|
337
|
+
});
|
|
338
|
+
res.json({ message: "Cookie set!" });
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
// Read cookie
|
|
342
|
+
app.get("/read-cookie", (req, res) => {
|
|
343
|
+
const session = req.cookies.user_session;
|
|
344
|
+
res.json({ session });
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Clear cookie
|
|
348
|
+
app.get("/clear-cookie", (req, res) => {
|
|
349
|
+
res.clearCookie("user_session");
|
|
350
|
+
res.json({ message: "Cookie cleared!" });
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
await app.listen();
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
#### 6. Query Parameters in JavaScript
|
|
357
|
+
|
|
358
|
+
```javascript
|
|
359
|
+
import { AzuraClient } from "azurajs";
|
|
360
|
+
|
|
361
|
+
const app = new AzuraClient();
|
|
362
|
+
|
|
363
|
+
// Handle query parameters
|
|
364
|
+
app.get("/search", (req, res) => {
|
|
365
|
+
const { q, page = 1, limit = 10 } = req.query;
|
|
366
|
+
|
|
367
|
+
res.json({
|
|
368
|
+
query: q,
|
|
369
|
+
page: Number(page),
|
|
370
|
+
limit: Number(limit),
|
|
371
|
+
results: []
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// Multiple query params
|
|
376
|
+
app.get("/filter", (req, res) => {
|
|
377
|
+
const filters = req.query;
|
|
378
|
+
res.json({ appliedFilters: filters });
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
await app.listen();
|
|
382
|
+
```
|
|
383
|
+
|
|
147
384
|
## Alternative: Use with Custom Servers
|
|
148
385
|
|
|
149
386
|
AzuraJS can be used with **any server** that supports the Web Fetch API, just like Hono! This includes Bun, Deno, Cloudflare Workers, and more.
|
|
@@ -560,15 +797,15 @@ import type { ConfigTypes } from "azurajs/config";
|
|
|
560
797
|
import type { RequestHandler } from "azurajs/types";
|
|
561
798
|
```
|
|
562
799
|
|
|
563
|
-
> ⚠️
|
|
800
|
+
> ⚠️ Note about TypeScript
|
|
564
801
|
|
|
565
802
|
> This package ships uncompiled TypeScript source code.
|
|
566
|
-
> You
|
|
567
|
-
>
|
|
568
|
-
> -
|
|
569
|
-
> - tsx
|
|
570
|
-
> - Deno
|
|
571
|
-
> -
|
|
803
|
+
> You can use it with TypeScript or JavaScript (via transpilation).
|
|
804
|
+
> Recommended runtimes:
|
|
805
|
+
> - **Bun** (supports TypeScript and JavaScript natively)
|
|
806
|
+
> - **Node.js** with tsx, ts-node, or build tools like Vite/Webpack
|
|
807
|
+
> - **Deno** (supports TypeScript natively)
|
|
808
|
+
> - For production JavaScript: Use a bundler like esbuild, swc, or tsc
|
|
572
809
|
|
|
573
810
|
## Contributing
|
|
574
811
|
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ConfigTypes } from './config.js';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import { Router } from './router.js';
|
|
4
|
+
import { R as RequestHandler } from './common.type-BoV71o_C.js';
|
|
5
|
+
|
|
6
|
+
declare class AzuraClient {
|
|
7
|
+
private opts;
|
|
8
|
+
private server?;
|
|
9
|
+
private port;
|
|
10
|
+
private initPromise;
|
|
11
|
+
router: Router;
|
|
12
|
+
private middlewares;
|
|
13
|
+
constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Configura rotas padrão para evitar erros 404 comuns
|
|
16
|
+
*/
|
|
17
|
+
private setupDefaultRoutes;
|
|
18
|
+
getConfig(): ConfigTypes;
|
|
19
|
+
private init;
|
|
20
|
+
use(mw: RequestHandler): void;
|
|
21
|
+
addRoute(method: string, path: string, ...handlers: RequestHandler[]): void;
|
|
22
|
+
get: (p: string, ...h: RequestHandler[]) => void;
|
|
23
|
+
post: (p: string, ...h: RequestHandler[]) => void;
|
|
24
|
+
put: (p: string, ...h: RequestHandler[]) => void;
|
|
25
|
+
delete: (p: string, ...h: RequestHandler[]) => void;
|
|
26
|
+
patch: (p: string, ...h: RequestHandler[]) => void;
|
|
27
|
+
getRoutes(): {
|
|
28
|
+
method: string;
|
|
29
|
+
path: string;
|
|
30
|
+
}[];
|
|
31
|
+
listen(port?: number): Promise<http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Fetch handler compatible with Web API (Bun, Deno, Cloudflare Workers, etc.)
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const app = new AzuraClient();
|
|
37
|
+
* app.get('/', (req, res) => res.text('Hello World!'));
|
|
38
|
+
*
|
|
39
|
+
* // Use with Bun
|
|
40
|
+
* Bun.serve({
|
|
41
|
+
* port: 3000,
|
|
42
|
+
* fetch: app.fetch.bind(app),
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Use with Deno
|
|
46
|
+
* Deno.serve({ port: 3000 }, app.fetch.bind(app));
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
fetch(request: Request): Promise<Response>;
|
|
50
|
+
private handle;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { AzuraClient as A };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ConfigTypes } from './config.cjs';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import { Router } from './router.cjs';
|
|
4
|
+
import { R as RequestHandler } from './common.type-Ct06XeYQ.cjs';
|
|
5
|
+
|
|
6
|
+
declare class AzuraClient {
|
|
7
|
+
private opts;
|
|
8
|
+
private server?;
|
|
9
|
+
private port;
|
|
10
|
+
private initPromise;
|
|
11
|
+
router: Router;
|
|
12
|
+
private middlewares;
|
|
13
|
+
constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Configura rotas padrão para evitar erros 404 comuns
|
|
16
|
+
*/
|
|
17
|
+
private setupDefaultRoutes;
|
|
18
|
+
getConfig(): ConfigTypes;
|
|
19
|
+
private init;
|
|
20
|
+
use(mw: RequestHandler): void;
|
|
21
|
+
addRoute(method: string, path: string, ...handlers: RequestHandler[]): void;
|
|
22
|
+
get: (p: string, ...h: RequestHandler[]) => void;
|
|
23
|
+
post: (p: string, ...h: RequestHandler[]) => void;
|
|
24
|
+
put: (p: string, ...h: RequestHandler[]) => void;
|
|
25
|
+
delete: (p: string, ...h: RequestHandler[]) => void;
|
|
26
|
+
patch: (p: string, ...h: RequestHandler[]) => void;
|
|
27
|
+
getRoutes(): {
|
|
28
|
+
method: string;
|
|
29
|
+
path: string;
|
|
30
|
+
}[];
|
|
31
|
+
listen(port?: number): Promise<http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Fetch handler compatible with Web API (Bun, Deno, Cloudflare Workers, etc.)
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const app = new AzuraClient();
|
|
37
|
+
* app.get('/', (req, res) => res.text('Hello World!'));
|
|
38
|
+
*
|
|
39
|
+
* // Use with Bun
|
|
40
|
+
* Bun.serve({
|
|
41
|
+
* port: 3000,
|
|
42
|
+
* fetch: app.fetch.bind(app),
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Use with Deno
|
|
46
|
+
* Deno.serve({ port: 3000 }, app.fetch.bind(app));
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
fetch(request: Request): Promise<Response>;
|
|
50
|
+
private handle;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { AzuraClient as A };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { R as RequestServer } from './request.type-CJ-EGGcM.js';
|
|
2
|
+
import { R as ResponseServer } from './response.type-d6e6eU9D.js';
|
|
3
|
+
|
|
4
|
+
type RequestHandler = (req: RequestServer, res: ResponseServer, next: (err?: Error | any) => void) => void;
|
|
5
|
+
interface HttpContext {
|
|
6
|
+
request: RequestServer;
|
|
7
|
+
response: ResponseServer;
|
|
8
|
+
body?: Buffer | string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type { HttpContext as H, RequestHandler as R };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { R as RequestServer } from './request.type-CJ-EGGcM.cjs';
|
|
2
|
+
import { R as ResponseServer } from './response.type-d6e6eU9D.cjs';
|
|
3
|
+
|
|
4
|
+
type RequestHandler = (req: RequestServer, res: ResponseServer, next: (err?: Error | any) => void) => void;
|
|
5
|
+
interface HttpContext {
|
|
6
|
+
request: RequestServer;
|
|
7
|
+
response: ResponseServer;
|
|
8
|
+
body?: Buffer | string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type { HttpContext as H, RequestHandler as R };
|
package/dist/config.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
9
|
+
|
|
10
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
11
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
12
|
+
}) : x)(function(x) {
|
|
13
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
14
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
15
|
+
});
|
|
16
|
+
var ConfigModule = class {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.config = {};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Load config files first (azura.config.*)
|
|
22
|
+
* Recivied error if config file not found or invalid format
|
|
23
|
+
* @param configFiles
|
|
24
|
+
*/
|
|
25
|
+
initSync() {
|
|
26
|
+
const cdw = process.cwd();
|
|
27
|
+
const configFiles = [
|
|
28
|
+
"azura.config.ts",
|
|
29
|
+
"azura.config.json",
|
|
30
|
+
"azura.config.yaml",
|
|
31
|
+
"azura.config.yml"
|
|
32
|
+
];
|
|
33
|
+
let loaded = false;
|
|
34
|
+
for (const fileName of configFiles) {
|
|
35
|
+
const filePath = path__default.default.join(cdw, fileName);
|
|
36
|
+
if (!fs.existsSync(filePath)) continue;
|
|
37
|
+
const extension = path__default.default.extname(fileName);
|
|
38
|
+
const raw = fs.readFileSync(filePath, "utf8");
|
|
39
|
+
try {
|
|
40
|
+
let parsed;
|
|
41
|
+
switch (extension) {
|
|
42
|
+
case ".ts":
|
|
43
|
+
const mod = __require(filePath);
|
|
44
|
+
parsed = mod.default || mod;
|
|
45
|
+
break;
|
|
46
|
+
case ".json":
|
|
47
|
+
parsed = JSON.parse(raw);
|
|
48
|
+
break;
|
|
49
|
+
case ".yaml":
|
|
50
|
+
case ".yml":
|
|
51
|
+
parsed = __require("js-yaml").load(raw);
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Invalid config file extension: ${extension}`);
|
|
55
|
+
}
|
|
56
|
+
this.config = { ...this.config, ...parsed };
|
|
57
|
+
loaded = true;
|
|
58
|
+
break;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new Error(`Error loading config file: ${filePath}
|
|
61
|
+
${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!loaded) {
|
|
65
|
+
throw new Error("Nothing config file found in the current directory.");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get all configs from loaded config file
|
|
70
|
+
* @returns ConfigTypes
|
|
71
|
+
*/
|
|
72
|
+
getAll() {
|
|
73
|
+
return this.config;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Return a specific config from loaded config file
|
|
77
|
+
*
|
|
78
|
+
* @template T
|
|
79
|
+
* @param {T} key - key of the config to retrieve
|
|
80
|
+
* @returns {ConfigTypes[T]}
|
|
81
|
+
*/
|
|
82
|
+
get(key) {
|
|
83
|
+
return this.config[key];
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
exports.ConfigModule = ConfigModule;
|
|
88
|
+
//# sourceMappingURL=config.cjs.map
|
|
89
|
+
//# sourceMappingURL=config.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/shared/config/ConfigModule.ts"],"names":["path","existsSync","readFileSync"],"mappings":";;;;;;;;;;;;;;;AAsCO,IAAM,eAAN,MAAmB;AAAA,EAAnB,WAAA,GAAA;AACL,IAAA,IAAA,CAAQ,SAAsB,EAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ/B,QAAA,GAAiB;AACf,IAAA,MAAM,GAAA,GAAM,QAAQ,GAAA,EAAI;AACxB,IAAA,MAAM,WAAA,GAAc;AAAA,MAClB,iBAAA;AAAA,MACA,mBAAA;AAAA,MACA,mBAAA;AAAA,MACA;AAAA,KACF;AAEA,IAAA,IAAI,MAAA,GAAS,KAAA;AACb,IAAA,KAAA,MAAW,YAAY,WAAA,EAAa;AAClC,MAAA,MAAM,QAAA,GAAWA,qBAAA,CAAK,IAAA,CAAK,GAAA,EAAK,QAAQ,CAAA;AACxC,MAAA,IAAI,CAACC,aAAA,CAAW,QAAQ,CAAA,EAAG;AAE3B,MAAA,MAAM,SAAA,GAAYD,qBAAA,CAAK,OAAA,CAAQ,QAAQ,CAAA;AACvC,MAAA,MAAM,GAAA,GAAME,eAAA,CAAa,QAAA,EAAU,MAAM,CAAA;AAEzC,MAAA,IAAI;AACF,QAAA,IAAI,MAAA;AACJ,QAAA,QAAQ,SAAA;AAAW,UACjB,KAAK,KAAA;AACH,YAAA,MAAM,GAAA,GAAM,UAAQ,QAAQ,CAAA;AAC5B,YAAA,MAAA,GAAS,IAAI,OAAA,IAAW,GAAA;AACxB,YAAA;AAAA,UACF,KAAK,OAAA;AACH,YAAA,MAAA,GAAS,IAAA,CAAK,MAAM,GAAG,CAAA;AACvB,YAAA;AAAA,UACF,KAAK,OAAA;AAAA,UACL,KAAK,MAAA;AACH,YAAA,MAAA,GAAS,SAAA,CAAQ,SAAS,CAAA,CAAE,IAAA,CAAK,GAAG,CAAA;AACpC,YAAA;AAAA,UACF;AACE,YAAA,MAAM,IAAI,KAAA,CAAM,CAAA,+BAAA,EAAkC,SAAS,CAAA,CAAE,CAAA;AAAA;AAGjE,QAAA,IAAA,CAAK,SAAS,EAAE,GAAG,IAAA,CAAK,MAAA,EAAQ,GAAG,MAAA,EAAO;AAC1C,QAAA,MAAA,GAAS,IAAA;AACT,QAAA;AAAA,MACF,SAAS,KAAA,EAAoB;AAC3B,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,2BAAA,EAA8B,QAAQ;AAAA,EAAK,KAAA,CAAM,OAAO,CAAA,CAAE,CAAA;AAAA,MAC5E;AAAA,IACF;AAEA,IAAA,IAAI,CAAC,MAAA,EAAQ;AACX,MAAA,MAAM,IAAI,MAAM,qDAAqD,CAAA;AAAA,IACvE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAA,GAAsB;AACpB,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,IAAiC,GAAA,EAAwB;AACvD,IAAA,OAAO,IAAA,CAAK,OAAO,GAAG,CAAA;AAAA,EACxB;AACF","file":"config.cjs","sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Config Files Extensions Supported\n * ex: azura.config.*extension\n */\n\ntype SupportedConfigFile = \".ts\" | \".json\" | \".yaml\" | \".yml\";\n\nexport type ConfigTypes = {\n environment?: \"development\" | \"production\";\n debug?: boolean;\n server?: {\n port?: number;\n cluster?: boolean;\n ipHost?: boolean;\n https?: boolean;\n };\n plugins?: {\n rateLimit?: {\n enabled: boolean;\n limit: number;\n timeframe: number;\n };\n cors?: {\n enabled: boolean;\n origins: string | string[];\n methods: string | string[];\n allowedHeaders: string | string[];\n };\n };\n logging?: {\n enabled?: boolean;\n showDetails?: boolean;\n };\n};\n\nexport class ConfigModule {\n private config: ConfigTypes = {};\n\n /**\n * Load config files first (azura.config.*)\n * Recivied error if config file not found or invalid format\n * @param configFiles\n */\n\n initSync(): void {\n const cdw = process.cwd();\n const configFiles = [\n \"azura.config.ts\",\n \"azura.config.json\",\n \"azura.config.yaml\",\n \"azura.config.yml\",\n ];\n\n let loaded = false;\n for (const fileName of configFiles) {\n const filePath = path.join(cdw, fileName);\n if (!existsSync(filePath)) continue;\n\n const extension = path.extname(fileName) as SupportedConfigFile;\n const raw = readFileSync(filePath, \"utf8\");\n\n try {\n let parsed: ConfigTypes;\n switch (extension) {\n case \".ts\":\n const mod = require(filePath);\n parsed = mod.default || mod;\n break;\n case \".json\":\n parsed = JSON.parse(raw);\n break;\n case \".yaml\":\n case \".yml\":\n parsed = require(\"js-yaml\").load(raw);\n break;\n default:\n throw new Error(`Invalid config file extension: ${extension}`);\n }\n\n this.config = { ...this.config, ...parsed };\n loaded = true;\n break;\n } catch (error: Error | any) {\n throw new Error(`Error loading config file: ${filePath}\\n${error.message}`);\n }\n }\n\n if (!loaded) {\n throw new Error(\"Nothing config file found in the current directory.\");\n }\n }\n\n /**\n * Get all configs from loaded config file\n * @returns ConfigTypes\n */\n\n getAll(): ConfigTypes {\n return this.config;\n }\n\n /**\n * Return a specific config from loaded config file\n *\n * @template T\n * @param {T} key - key of the config to retrieve\n * @returns {ConfigTypes[T]}\n */\n\n get<T extends keyof ConfigTypes>(key: T): ConfigTypes[T] {\n return this.config[key];\n }\n}\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
type ConfigTypes = {
|
|
2
|
+
environment?: "development" | "production";
|
|
3
|
+
debug?: boolean;
|
|
4
|
+
server?: {
|
|
5
|
+
port?: number;
|
|
6
|
+
cluster?: boolean;
|
|
7
|
+
ipHost?: boolean;
|
|
8
|
+
https?: boolean;
|
|
9
|
+
};
|
|
10
|
+
plugins?: {
|
|
11
|
+
rateLimit?: {
|
|
12
|
+
enabled: boolean;
|
|
13
|
+
limit: number;
|
|
14
|
+
timeframe: number;
|
|
15
|
+
};
|
|
16
|
+
cors?: {
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
origins: string | string[];
|
|
19
|
+
methods: string | string[];
|
|
20
|
+
allowedHeaders: string | string[];
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
logging?: {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
showDetails?: boolean;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare class ConfigModule {
|
|
29
|
+
private config;
|
|
30
|
+
/**
|
|
31
|
+
* Load config files first (azura.config.*)
|
|
32
|
+
* Recivied error if config file not found or invalid format
|
|
33
|
+
* @param configFiles
|
|
34
|
+
*/
|
|
35
|
+
initSync(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Get all configs from loaded config file
|
|
38
|
+
* @returns ConfigTypes
|
|
39
|
+
*/
|
|
40
|
+
getAll(): ConfigTypes;
|
|
41
|
+
/**
|
|
42
|
+
* Return a specific config from loaded config file
|
|
43
|
+
*
|
|
44
|
+
* @template T
|
|
45
|
+
* @param {T} key - key of the config to retrieve
|
|
46
|
+
* @returns {ConfigTypes[T]}
|
|
47
|
+
*/
|
|
48
|
+
get<T extends keyof ConfigTypes>(key: T): ConfigTypes[T];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { ConfigModule, type ConfigTypes };
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
type ConfigTypes = {
|
|
2
|
+
environment?: "development" | "production";
|
|
3
|
+
debug?: boolean;
|
|
4
|
+
server?: {
|
|
5
|
+
port?: number;
|
|
6
|
+
cluster?: boolean;
|
|
7
|
+
ipHost?: boolean;
|
|
8
|
+
https?: boolean;
|
|
9
|
+
};
|
|
10
|
+
plugins?: {
|
|
11
|
+
rateLimit?: {
|
|
12
|
+
enabled: boolean;
|
|
13
|
+
limit: number;
|
|
14
|
+
timeframe: number;
|
|
15
|
+
};
|
|
16
|
+
cors?: {
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
origins: string | string[];
|
|
19
|
+
methods: string | string[];
|
|
20
|
+
allowedHeaders: string | string[];
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
logging?: {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
showDetails?: boolean;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
declare class ConfigModule {
|
|
29
|
+
private config;
|
|
30
|
+
/**
|
|
31
|
+
* Load config files first (azura.config.*)
|
|
32
|
+
* Recivied error if config file not found or invalid format
|
|
33
|
+
* @param configFiles
|
|
34
|
+
*/
|
|
35
|
+
initSync(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Get all configs from loaded config file
|
|
38
|
+
* @returns ConfigTypes
|
|
39
|
+
*/
|
|
40
|
+
getAll(): ConfigTypes;
|
|
41
|
+
/**
|
|
42
|
+
* Return a specific config from loaded config file
|
|
43
|
+
*
|
|
44
|
+
* @template T
|
|
45
|
+
* @param {T} key - key of the config to retrieve
|
|
46
|
+
* @returns {ConfigTypes[T]}
|
|
47
|
+
*/
|
|
48
|
+
get<T extends keyof ConfigTypes>(key: T): ConfigTypes[T];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { ConfigModule, type ConfigTypes };
|