@avleon/core 0.0.39 → 0.0.40
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 +76 -23
- package/dist/middleware.d.ts +3 -0
- package/dist/middleware.js +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Avleon
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
 
|
|
4
|
+
## ⚠️ WARNING
|
|
4
5
|
|
|
5
6
|
> **🚧 This project is in active development.**
|
|
6
|
-
>
|
|
7
|
-
> It is **not stable** and **not ready** for live environments.
|
|
8
|
-
> Use **only for testing, experimentation, or internal evaluation**.
|
|
9
|
-
>
|
|
10
|
-
> ####❗ Risks of using this in production:
|
|
11
|
-
>
|
|
12
|
-
> - 🔄 Breaking changes may be introduced at any time
|
|
13
|
-
> - 🧪 Features are experimental and may be unstable
|
|
14
|
-
> - 🔐 Security has not been audited
|
|
15
|
-
> - 💥 Potential for data loss or critical errors
|
|
16
|
-
>
|
|
17
|
-
> **Please do not deploy this in production environments.**
|
|
18
7
|
|
|
19
8
|
## Overview
|
|
20
9
|
|
|
@@ -69,11 +58,11 @@ Avleon is a powerful, TypeScript-based web framework built on top of Fastify, de
|
|
|
69
58
|
## Installation
|
|
70
59
|
|
|
71
60
|
```bash
|
|
72
|
-
|
|
61
|
+
npx @avleon/cli new myapp
|
|
73
62
|
# or
|
|
74
|
-
yarn
|
|
63
|
+
yarn dlx @avleon/cli new myapp
|
|
75
64
|
# or
|
|
76
|
-
pnpm
|
|
65
|
+
pnpm dlx @avleon/cli new myapp
|
|
77
66
|
```
|
|
78
67
|
|
|
79
68
|
## Quick Start
|
|
@@ -81,7 +70,7 @@ pnpm add @avleon/core
|
|
|
81
70
|
### Minimal
|
|
82
71
|
|
|
83
72
|
```typescript
|
|
84
|
-
import { Avleon
|
|
73
|
+
import { Avleon } from "@avleon/core";
|
|
85
74
|
|
|
86
75
|
const app = Avleon.createApplication();
|
|
87
76
|
app.mapGet("/", () => "Hello, Avleon");
|
|
@@ -123,7 +112,9 @@ const app = Avleon.createApplication();
|
|
|
123
112
|
// Configure and run the application
|
|
124
113
|
app.useCors();
|
|
125
114
|
app.useControllers([UserController]);
|
|
126
|
-
app.
|
|
115
|
+
// For auto register controller `app.useControllers({auto:true});`
|
|
116
|
+
|
|
117
|
+
app.run(); // or app.run(port)
|
|
127
118
|
```
|
|
128
119
|
|
|
129
120
|
### Controllers
|
|
@@ -253,7 +244,9 @@ class UserController {
|
|
|
253
244
|
Secure your API with authentication and authorization:
|
|
254
245
|
|
|
255
246
|
```typescript
|
|
256
|
-
@
|
|
247
|
+
import { CanAuthorize } from "@avleon/core";
|
|
248
|
+
|
|
249
|
+
@CanAuthorize
|
|
257
250
|
class JwtAuthorization extends AuthorizeMiddleware {
|
|
258
251
|
authorize(roles: string[]) {
|
|
259
252
|
return async (req: IRequest) => {
|
|
@@ -382,6 +375,66 @@ app.useOpenApi(OpenApiConfig, (config) => {
|
|
|
382
375
|
|
|
383
376
|
### Database Integration
|
|
384
377
|
|
|
378
|
+
## 1. Knex
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
const app = Avleon.createApplication();
|
|
382
|
+
app.useKnex({
|
|
383
|
+
client: 'mysql',
|
|
384
|
+
connection: {
|
|
385
|
+
host: '127.0.0.1',
|
|
386
|
+
port: 3306,
|
|
387
|
+
user: 'your_database_user',
|
|
388
|
+
password: 'your_database_password',
|
|
389
|
+
database: 'myapp_test',
|
|
390
|
+
},
|
|
391
|
+
})
|
|
392
|
+
```
|
|
393
|
+
or using config class
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
@AppConfig
|
|
397
|
+
export class KnexConfig implements IConfig {
|
|
398
|
+
// config method is mendatory
|
|
399
|
+
// config method has access to environment variables by default
|
|
400
|
+
config(env: Environment) {
|
|
401
|
+
return {
|
|
402
|
+
client: 'mysql',
|
|
403
|
+
connection: {
|
|
404
|
+
host: env.get("DB_HOST") || '127.0.0.1',
|
|
405
|
+
port: env.get("DB_PORT") || 3306,
|
|
406
|
+
user: env.get("DB_USER")|| 'your_database_user',
|
|
407
|
+
password: env.get("DB_PASS") || 'your_database_password',
|
|
408
|
+
database: env.get("DB_NAME") || 'myapp_test',
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// now we can register it with our app
|
|
415
|
+
|
|
416
|
+
app.useKenx(KnexConfig)
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### Exmaple uses
|
|
420
|
+
|
|
421
|
+
```typescript
|
|
422
|
+
import { DB, AppService } from "@avleon/core";
|
|
423
|
+
|
|
424
|
+
@AppService
|
|
425
|
+
export class UsersService{
|
|
426
|
+
constructor(
|
|
427
|
+
private readonly db: DB
|
|
428
|
+
){}
|
|
429
|
+
|
|
430
|
+
async findAll(){
|
|
431
|
+
const result = await this.db.client.select("*").from("users");
|
|
432
|
+
return result;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
## 2. Typeorm
|
|
385
438
|
Connect to databases using TypeORM:
|
|
386
439
|
|
|
387
440
|
```typescript
|
|
@@ -402,9 +455,9 @@ Or use the config class:
|
|
|
402
455
|
|
|
403
456
|
```typescript
|
|
404
457
|
// datasource.config.ts
|
|
405
|
-
import {
|
|
458
|
+
import { AppConfig, IConfig } from "@avleon/core";
|
|
406
459
|
|
|
407
|
-
@
|
|
460
|
+
@AppConfig
|
|
408
461
|
export class DataSourceConfig implements IConfig {
|
|
409
462
|
// config method is mendatory
|
|
410
463
|
// config method has access to environment variables by default
|
|
@@ -552,7 +605,7 @@ app
|
|
|
552
605
|
// Handler function
|
|
553
606
|
})
|
|
554
607
|
.useMiddleware([AuthMiddleware])
|
|
555
|
-
.
|
|
608
|
+
.useOpenApi({
|
|
556
609
|
summary: "Get all users",
|
|
557
610
|
description: "Retrieves a list of all users",
|
|
558
611
|
tags: ["users"],
|
package/dist/middleware.d.ts
CHANGED
|
@@ -14,6 +14,9 @@ export type AuthReturnTypes = IRequest | Promise<IRequest>;
|
|
|
14
14
|
interface AuthorizeClass {
|
|
15
15
|
authorize(req: IRequest, options?: any): AuthReturnTypes;
|
|
16
16
|
}
|
|
17
|
+
export declare function CanAuthorize(target: {
|
|
18
|
+
new (...args: any[]): AuthorizeClass;
|
|
19
|
+
}): void;
|
|
17
20
|
export declare function AppAuthorization(target: {
|
|
18
21
|
new (...args: any[]): AuthorizeClass;
|
|
19
22
|
}): void;
|
package/dist/middleware.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuthorizeMiddleware = exports.AvleonMiddleware = void 0;
|
|
4
|
+
exports.CanAuthorize = CanAuthorize;
|
|
4
5
|
exports.AppAuthorization = AppAuthorization;
|
|
5
6
|
exports.Authorized = Authorized;
|
|
6
7
|
exports.AppMiddleware = AppMiddleware;
|
|
@@ -19,6 +20,12 @@ exports.AvleonMiddleware = AvleonMiddleware;
|
|
|
19
20
|
class AuthorizeMiddleware {
|
|
20
21
|
}
|
|
21
22
|
exports.AuthorizeMiddleware = AuthorizeMiddleware;
|
|
23
|
+
function CanAuthorize(target) {
|
|
24
|
+
if (typeof target.prototype.authorize !== "function") {
|
|
25
|
+
throw new Error(`Class "${target.name}" must implement an "authorize" method.`);
|
|
26
|
+
}
|
|
27
|
+
(0, typedi_1.Service)()(target);
|
|
28
|
+
}
|
|
22
29
|
function AppAuthorization(target) {
|
|
23
30
|
if (typeof target.prototype.authorize !== "function") {
|
|
24
31
|
throw new Error(`Class "${target.name}" must implement an "authorize" method.`);
|