@avleon/core 0.0.26 → 0.0.28

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.
Files changed (41) hide show
  1. package/README.md +601 -561
  2. package/package.json +38 -6
  3. package/src/application.ts +104 -125
  4. package/src/authentication.ts +16 -16
  5. package/src/cache.ts +91 -91
  6. package/src/collection.test.ts +71 -0
  7. package/src/collection.ts +344 -254
  8. package/src/config.test.ts +35 -0
  9. package/src/config.ts +85 -42
  10. package/src/constants.ts +1 -1
  11. package/src/container.ts +54 -54
  12. package/src/controller.ts +125 -127
  13. package/src/decorators.ts +27 -27
  14. package/src/environment-variables.ts +53 -46
  15. package/src/exceptions/http-exceptions.ts +86 -86
  16. package/src/exceptions/index.ts +1 -1
  17. package/src/exceptions/system-exception.ts +35 -34
  18. package/src/file-storage.ts +206 -206
  19. package/src/helpers.ts +324 -328
  20. package/src/icore.ts +66 -90
  21. package/src/index.ts +30 -30
  22. package/src/interfaces/avleon-application.ts +32 -40
  23. package/src/logger.ts +72 -72
  24. package/src/map-types.ts +159 -159
  25. package/src/middleware.ts +119 -98
  26. package/src/multipart.ts +116 -116
  27. package/src/openapi.ts +372 -372
  28. package/src/params.ts +111 -111
  29. package/src/queue.ts +126 -126
  30. package/src/response.ts +74 -74
  31. package/src/results.ts +30 -30
  32. package/src/route-methods.ts +186 -186
  33. package/src/swagger-schema.ts +213 -213
  34. package/src/testing.ts +220 -220
  35. package/src/types/app-builder.interface.ts +18 -19
  36. package/src/types/application.interface.ts +7 -9
  37. package/src/utils/hash.ts +8 -5
  38. package/src/utils/index.ts +2 -2
  39. package/src/utils/optional-require.ts +50 -50
  40. package/src/validation.ts +160 -156
  41. package/src/validator-extend.ts +25 -25
package/src/middleware.ts CHANGED
@@ -1,98 +1,119 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- import { Service } from "typedi";
8
- import { IRequest, IResponse } from "./icore";
9
- import { HttpExceptionTypes as HttpException, UnauthorizedException } from "./exceptions";
10
- import Container, { AUTHORIZATION_META_KEY } from "./container";
11
-
12
- export abstract class AppMiddleware {
13
- abstract invoke(req: IRequest, res?: IResponse): Promise<IRequest|HttpException>;
14
- }
15
- export type AuthHandler = (req: IRequest, roles?: string[]) => Promise<IRequest | HttpException>;
16
-
17
-
18
-
19
- export type Constructor<T> = { new(...args: any[]): T };
20
-
21
- export abstract class AuthorizeMiddleware {
22
- abstract authorize(roles: string[]): (req: IRequest, res?: IResponse) => IRequest | Promise<IRequest>;
23
-
24
- }
25
-
26
-
27
- export type AuthReturnTypes = IRequest | Promise<IRequest>
28
-
29
- interface AuthorizeClass {
30
- authorize(req: IRequest, options?:any): AuthReturnTypes;
31
- }
32
-
33
- export function Authorize(target: { new (...args: any[]): AuthorizeClass }) {
34
- if (typeof target.prototype.authorize !== "function") {
35
- throw new Error(
36
- `Class "${target.name}" must implement an "authorize" method.`,
37
- );
38
- }
39
- Service()(target);
40
- }
41
-
42
-
43
- // export function Authorized(target: Function): void;
44
- export function Authorized(): ClassDecorator & MethodDecorator;
45
- export function Authorized(options?: any): ClassDecorator & MethodDecorator;
46
- export function Authorized(options: any = {}): MethodDecorator | ClassDecorator {
47
- return function (target: any, propertyKey?: string | symbol, descriptor?: PropertyDescriptor) {
48
- if (propertyKey && descriptor) {
49
- Reflect.defineMetadata(AUTHORIZATION_META_KEY, { authorize: true, options}, target.constructor, propertyKey);
50
- } else {
51
- Reflect.defineMetadata(AUTHORIZATION_META_KEY, { authorize: true, options}, target);
52
- }
53
- };
54
- }
55
-
56
-
57
- export function Middleware(target: Constructor<AppMiddleware>) {
58
- if (typeof target.prototype.invoke !== "function") {
59
- throw new Error(
60
- `Class "${target.name}" must implement an "invoke" method.`,
61
- );
62
- }
63
-
64
- Service()(target);
65
- }
66
-
67
-
68
- export function UseMiddleware<T extends AppMiddleware | (new (...args: any[]) => AppMiddleware)>(
69
- options: T | T[],
70
- ): MethodDecorator & ClassDecorator {
71
- return function (
72
- target: Object | Function,
73
- propertyKey?: string | symbol,
74
- descriptor?: PropertyDescriptor,
75
- ) {
76
- const normalizeMiddleware = (middleware: any) =>
77
- typeof middleware === "function" ? new middleware() : middleware;
78
- const middlewareList = (Array.isArray(options) ? options : [options]).map(normalizeMiddleware);
79
- if (typeof target === "function" && !propertyKey) {
80
- const existingMiddlewares =
81
- Reflect.getMetadata("controller:middleware", target) || [];
82
- Reflect.defineMetadata(
83
- "controller:middleware",
84
- [...existingMiddlewares, ...middlewareList],
85
- target
86
- );
87
- } else if (descriptor) {
88
- const existingMiddlewares =
89
- Reflect.getMetadata("route:middleware", target, propertyKey!) || [];
90
- Reflect.defineMetadata(
91
- "route:middleware",
92
- [...existingMiddlewares, ...middlewareList],
93
- target,
94
- propertyKey!
95
- );
96
- }
97
- };
98
- }
1
+ /**
2
+ * @copyright 2024
3
+ * @author Tareq Hossain
4
+ * @email xtrinsic96@gmail.com
5
+ * @url https://github.com/xtareq
6
+ */
7
+ import { Service } from "typedi";
8
+ import { IRequest, IResponse } from "./icore";
9
+ import {
10
+ HttpExceptionTypes as HttpException,
11
+ UnauthorizedException,
12
+ } from "./exceptions";
13
+ import Container, { AUTHORIZATION_META_KEY } from "./container";
14
+
15
+ export abstract class AppMiddleware {
16
+ abstract invoke(
17
+ req: IRequest,
18
+ res?: IResponse,
19
+ ): Promise<IRequest | HttpException>;
20
+ }
21
+ export type AuthHandler = (
22
+ req: IRequest,
23
+ roles?: string[],
24
+ ) => Promise<IRequest | HttpException>;
25
+
26
+ export type Constructor<T> = { new (...args: any[]): T };
27
+
28
+ export abstract class AuthorizeMiddleware {
29
+ abstract authorize(
30
+ roles: string[],
31
+ ): (req: IRequest, res?: IResponse) => IRequest | Promise<IRequest>;
32
+ }
33
+
34
+ export type AuthReturnTypes = IRequest | Promise<IRequest>;
35
+
36
+ interface AuthorizeClass {
37
+ authorize(req: IRequest, options?: any): AuthReturnTypes;
38
+ }
39
+
40
+ export function Authorize(target: { new (...args: any[]): AuthorizeClass }) {
41
+ if (typeof target.prototype.authorize !== "function") {
42
+ throw new Error(
43
+ `Class "${target.name}" must implement an "authorize" method.`,
44
+ );
45
+ }
46
+ Service()(target);
47
+ }
48
+
49
+ // export function Authorized(target: Function): void;
50
+ export function Authorized(): ClassDecorator & MethodDecorator;
51
+ export function Authorized(options?: any): ClassDecorator & MethodDecorator;
52
+ export function Authorized(
53
+ options: any = {},
54
+ ): MethodDecorator | ClassDecorator {
55
+ return function (
56
+ target: any,
57
+ propertyKey?: string | symbol,
58
+ descriptor?: PropertyDescriptor,
59
+ ) {
60
+ if (propertyKey && descriptor) {
61
+ Reflect.defineMetadata(
62
+ AUTHORIZATION_META_KEY,
63
+ { authorize: true, options },
64
+ target.constructor,
65
+ propertyKey,
66
+ );
67
+ } else {
68
+ Reflect.defineMetadata(
69
+ AUTHORIZATION_META_KEY,
70
+ { authorize: true, options },
71
+ target,
72
+ );
73
+ }
74
+ };
75
+ }
76
+
77
+ export function Middleware(target: Constructor<AppMiddleware>) {
78
+ if (typeof target.prototype.invoke !== "function") {
79
+ throw new Error(
80
+ `Class "${target.name}" must implement an "invoke" method.`,
81
+ );
82
+ }
83
+
84
+ Service()(target);
85
+ }
86
+
87
+ export function UseMiddleware<
88
+ T extends AppMiddleware | (new (...args: any[]) => AppMiddleware),
89
+ >(options: T | T[]): MethodDecorator & ClassDecorator {
90
+ return function (
91
+ target: Object | Function,
92
+ propertyKey?: string | symbol,
93
+ descriptor?: PropertyDescriptor,
94
+ ) {
95
+ const normalizeMiddleware = (middleware: any) =>
96
+ typeof middleware === "function" ? new middleware() : middleware;
97
+ const middlewareList = (Array.isArray(options) ? options : [options]).map(
98
+ normalizeMiddleware,
99
+ );
100
+ if (typeof target === "function" && !propertyKey) {
101
+ const existingMiddlewares =
102
+ Reflect.getMetadata("controller:middleware", target) || [];
103
+ Reflect.defineMetadata(
104
+ "controller:middleware",
105
+ [...existingMiddlewares, ...middlewareList],
106
+ target,
107
+ );
108
+ } else if (descriptor) {
109
+ const existingMiddlewares =
110
+ Reflect.getMetadata("route:middleware", target, propertyKey!) || [];
111
+ Reflect.defineMetadata(
112
+ "route:middleware",
113
+ [...existingMiddlewares, ...middlewareList],
114
+ target,
115
+ propertyKey!,
116
+ );
117
+ }
118
+ };
119
+ }
package/src/multipart.ts CHANGED
@@ -1,116 +1,116 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
-
8
- import {
9
- MultipartFile as FsM,
10
- MultipartValue,
11
- SavedMultipartFile,
12
- } from "@fastify/multipart";
13
- import { IRequest } from "./icore";
14
- import fs from "fs";
15
- import path from "path";
16
- import { pipeline } from "stream/promises";
17
- import { InternalErrorException } from "./exceptions";
18
- import { REQUEST_BODY_FILE_KEY, REQUEST_BODY_FILES_KEY } from "./container";
19
-
20
- export function UploadFile(fieldName: string) {
21
- return function (
22
- target: any,
23
- propertyKey: string | symbol,
24
- parameterIndex: number
25
- ) {
26
- if (!Reflect.hasMetadata(REQUEST_BODY_FILE_KEY, target, propertyKey)) {
27
- Reflect.defineMetadata(REQUEST_BODY_FILE_KEY, [], target, propertyKey);
28
- }
29
- const existingMetadata = Reflect.getMetadata(
30
- REQUEST_BODY_FILE_KEY,
31
- target,
32
- propertyKey
33
- ) as {
34
- fieldName: string;
35
- index: number;
36
- }[];
37
- existingMetadata.push({ fieldName, index: parameterIndex });
38
- Reflect.defineMetadata(
39
- REQUEST_BODY_FILE_KEY,
40
- existingMetadata,
41
- target,
42
- propertyKey
43
- );
44
- };
45
- }
46
-
47
- export function UploadFiles(fieldName?: string) {
48
- return function (
49
- target: any,
50
- propertyKey: string | symbol,
51
- parameterIndex: number
52
- ) {
53
- if (!Reflect.hasMetadata(REQUEST_BODY_FILES_KEY, target, propertyKey)) {
54
- Reflect.defineMetadata(REQUEST_BODY_FILES_KEY, [], target, propertyKey);
55
- }
56
- const existingMetadata = Reflect.getMetadata(
57
- REQUEST_BODY_FILES_KEY,
58
- target,
59
- propertyKey
60
- ) as {
61
- fieldName: string;
62
- index: number;
63
- }[];
64
- existingMetadata.push({
65
- fieldName: fieldName ? fieldName : "all",
66
- index: parameterIndex,
67
- });
68
- Reflect.defineMetadata(
69
- REQUEST_BODY_FILES_KEY,
70
- existingMetadata,
71
- target,
72
- propertyKey
73
- );
74
- };
75
- }
76
-
77
- type Foptions = {
78
- saveAs?: string;
79
- dest?: true;
80
- };
81
-
82
- export type MultipartFile = FsM | SavedMultipartFile;
83
- export function UploadFileFromRequest(req: IRequest, options?: Foptions) {
84
- return Promise.resolve(
85
- req.file().then(async (f) => {
86
- if (f && f.file) {
87
- let fname = f.filename;
88
- if (options) {
89
- if (options.dest) {
90
- fname = options.saveAs
91
- ? options.dest + "/" + options.saveAs
92
- : options.dest + "/" + f.filename;
93
- } else {
94
- fname = path.join(
95
- process.cwd(),
96
- `public/${options.saveAs ? options.saveAs : f.filename}`
97
- );
98
- }
99
- } else {
100
- fname = path.join(process.cwd(), `public/${f.filename}`);
101
- }
102
-
103
- if (fs.existsSync(fname)) {
104
- throw new InternalErrorException("File already exists.");
105
- }
106
-
107
- await pipeline(f.file!, fs.createWriteStream(fname));
108
-
109
- return {
110
- ...f,
111
- filename: options?.saveAs ? options.saveAs : f.filename,
112
- } as MultipartFile;
113
- }
114
- })
115
- );
116
- }
1
+ /**
2
+ * @copyright 2024
3
+ * @author Tareq Hossain
4
+ * @email xtrinsic96@gmail.com
5
+ * @url https://github.com/xtareq
6
+ */
7
+
8
+ import {
9
+ MultipartFile as FsM,
10
+ MultipartValue,
11
+ SavedMultipartFile,
12
+ } from "@fastify/multipart";
13
+ import { IRequest } from "./icore";
14
+ import fs from "fs";
15
+ import path from "path";
16
+ import { pipeline } from "stream/promises";
17
+ import { InternalErrorException } from "./exceptions";
18
+ import { REQUEST_BODY_FILE_KEY, REQUEST_BODY_FILES_KEY } from "./container";
19
+
20
+ export function UploadFile(fieldName: string) {
21
+ return function (
22
+ target: any,
23
+ propertyKey: string | symbol,
24
+ parameterIndex: number,
25
+ ) {
26
+ if (!Reflect.hasMetadata(REQUEST_BODY_FILE_KEY, target, propertyKey)) {
27
+ Reflect.defineMetadata(REQUEST_BODY_FILE_KEY, [], target, propertyKey);
28
+ }
29
+ const existingMetadata = Reflect.getMetadata(
30
+ REQUEST_BODY_FILE_KEY,
31
+ target,
32
+ propertyKey,
33
+ ) as {
34
+ fieldName: string;
35
+ index: number;
36
+ }[];
37
+ existingMetadata.push({ fieldName, index: parameterIndex });
38
+ Reflect.defineMetadata(
39
+ REQUEST_BODY_FILE_KEY,
40
+ existingMetadata,
41
+ target,
42
+ propertyKey,
43
+ );
44
+ };
45
+ }
46
+
47
+ export function UploadFiles(fieldName?: string) {
48
+ return function (
49
+ target: any,
50
+ propertyKey: string | symbol,
51
+ parameterIndex: number,
52
+ ) {
53
+ if (!Reflect.hasMetadata(REQUEST_BODY_FILES_KEY, target, propertyKey)) {
54
+ Reflect.defineMetadata(REQUEST_BODY_FILES_KEY, [], target, propertyKey);
55
+ }
56
+ const existingMetadata = Reflect.getMetadata(
57
+ REQUEST_BODY_FILES_KEY,
58
+ target,
59
+ propertyKey,
60
+ ) as {
61
+ fieldName: string;
62
+ index: number;
63
+ }[];
64
+ existingMetadata.push({
65
+ fieldName: fieldName ? fieldName : "all",
66
+ index: parameterIndex,
67
+ });
68
+ Reflect.defineMetadata(
69
+ REQUEST_BODY_FILES_KEY,
70
+ existingMetadata,
71
+ target,
72
+ propertyKey,
73
+ );
74
+ };
75
+ }
76
+
77
+ type Foptions = {
78
+ saveAs?: string;
79
+ dest?: true;
80
+ };
81
+
82
+ export type MultipartFile = FsM | SavedMultipartFile;
83
+ export function UploadFileFromRequest(req: IRequest, options?: Foptions) {
84
+ return Promise.resolve(
85
+ req.file().then(async (f) => {
86
+ if (f && f.file) {
87
+ let fname = f.filename;
88
+ if (options) {
89
+ if (options.dest) {
90
+ fname = options.saveAs
91
+ ? options.dest + "/" + options.saveAs
92
+ : options.dest + "/" + f.filename;
93
+ } else {
94
+ fname = path.join(
95
+ process.cwd(),
96
+ `public/${options.saveAs ? options.saveAs : f.filename}`,
97
+ );
98
+ }
99
+ } else {
100
+ fname = path.join(process.cwd(), `public/${f.filename}`);
101
+ }
102
+
103
+ if (fs.existsSync(fname)) {
104
+ throw new InternalErrorException("File already exists.");
105
+ }
106
+
107
+ await pipeline(f.file!, fs.createWriteStream(fname));
108
+
109
+ return {
110
+ ...f,
111
+ filename: options?.saveAs ? options.saveAs : f.filename,
112
+ } as MultipartFile;
113
+ }
114
+ }),
115
+ );
116
+ }