@skalfa/skalfa-api-core 1.0.3 → 1.0.8
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/CONTRIBUTING.md +45 -0
- package/LICENSE +21 -0
- package/README.md +60 -0
- package/dist/auth/auth.d.ts +18 -15
- package/dist/auth/auth.js +20 -203
- package/dist/auth/auth.js.map +1 -1
- package/dist/auth/create-access-token.d.ts +4 -0
- package/dist/auth/create-access-token.js +26 -0
- package/dist/auth/create-access-token.js.map +1 -0
- package/dist/auth/create-user-mail-token.d.ts +4 -0
- package/dist/auth/create-user-mail-token.js +19 -0
- package/dist/auth/create-user-mail-token.js.map +1 -0
- package/dist/auth/helpers/generate-agent-id.d.ts +1 -0
- package/dist/auth/helpers/generate-agent-id.js +7 -0
- package/dist/auth/helpers/generate-agent-id.js.map +1 -0
- package/dist/auth/helpers/get-request-ip.d.ts +1 -0
- package/dist/auth/helpers/get-request-ip.js +4 -0
- package/dist/auth/helpers/get-request-ip.js.map +1 -0
- package/dist/auth/helpers/get-user-permissions.d.ts +1 -0
- package/dist/auth/helpers/get-user-permissions.js +9 -0
- package/dist/auth/helpers/get-user-permissions.js.map +1 -0
- package/dist/auth/helpers/index.d.ts +3 -0
- package/dist/auth/helpers/index.js +4 -0
- package/dist/auth/helpers/index.js.map +1 -0
- package/dist/auth/list-user-sessions.d.ts +1 -0
- package/dist/auth/list-user-sessions.js +10 -0
- package/dist/auth/list-user-sessions.js.map +1 -0
- package/dist/auth/revalidate-user-permissions-by-role.d.ts +1 -0
- package/dist/auth/revalidate-user-permissions-by-role.js +12 -0
- package/dist/auth/revalidate-user-permissions-by-role.js.map +1 -0
- package/dist/auth/revalidate-user-permissions.d.ts +1 -0
- package/dist/auth/revalidate-user-permissions.js +21 -0
- package/dist/auth/revalidate-user-permissions.js.map +1 -0
- package/dist/auth/revoke-access-token.d.ts +1 -0
- package/dist/auth/revoke-access-token.js +5 -0
- package/dist/auth/revoke-access-token.js.map +1 -0
- package/dist/auth/verify-access-token.d.ts +1 -0
- package/dist/auth/verify-access-token.js +47 -0
- package/dist/auth/verify-access-token.js.map +1 -0
- package/dist/auth/verify-user-mail-token.d.ts +1 -0
- package/dist/auth/verify-user-mail-token.js +21 -0
- package/dist/auth/verify-user-mail-token.js.map +1 -0
- package/dist/commands/cli.js +18 -27
- package/dist/commands/cli.js.map +1 -1
- package/dist/commands/make/basic-controller.js +1 -1
- package/dist/commands/make/basic-controller.js.map +1 -1
- package/dist/commands/make/basic-migration.d.ts +1 -1
- package/dist/commands/make/basic-migration.js +2 -2
- package/dist/commands/make/basic-migration.js.map +1 -1
- package/dist/commands/make/basic-model.js +1 -1
- package/dist/commands/make/basic-model.js.map +1 -1
- package/dist/commands/make/basic-seeder.js +1 -1
- package/dist/commands/make/basic-seeder.js.map +1 -1
- package/dist/commands/make/blueprint.js +1 -1
- package/dist/commands/make/blueprint.js.map +1 -1
- package/dist/commands/make/da-migration.js +3 -3
- package/dist/commands/make/da-migration.js.map +1 -1
- package/dist/commands/make/mail.js +2 -2
- package/dist/commands/make/mail.js.map +1 -1
- package/dist/commands/make/notification.js +1 -1
- package/dist/commands/make/notification.js.map +1 -1
- package/dist/commands/make/queue.js +1 -1
- package/dist/commands/make/queue.js.map +1 -1
- package/dist/commands/make/resource.d.ts +2 -0
- package/dist/commands/make/resource.js +19 -0
- package/dist/commands/make/resource.js.map +1 -0
- package/dist/commands/make/skalfa-controller.d.ts +3 -0
- package/dist/commands/make/{light-controller.js → skalfa-controller.js} +9 -9
- package/dist/commands/make/skalfa-controller.js.map +1 -0
- package/dist/commands/make/skalfa-model.d.ts +3 -0
- package/dist/commands/make/{light-model.js → skalfa-model.js} +11 -11
- package/dist/commands/make/skalfa-model.js.map +1 -0
- package/dist/commands/runner/barrels.js.map +1 -1
- package/dist/commands/runner/blueprint/controller-generation.js +2 -2
- package/dist/commands/runner/blueprint/controller-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/documentation-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/migration-generation.js +3 -3
- package/dist/commands/runner/blueprint/migration-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/model-generation.js +2 -2
- package/dist/commands/runner/blueprint/model-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/runner.js +7 -8
- package/dist/commands/runner/blueprint/runner.js.map +1 -1
- package/dist/commands/runner/blueprint/seeder-generation.js +3 -3
- package/dist/commands/runner/blueprint/seeder-generation.js.map +1 -1
- package/dist/commands/runner/da-migration.js +1 -2
- package/dist/commands/runner/da-migration.js.map +1 -1
- package/dist/commands/runner/generate-docs.d.ts +2 -0
- package/dist/commands/runner/generate-docs.js +400 -0
- package/dist/commands/runner/generate-docs.js.map +1 -0
- package/dist/commands/runner/migration.js +1 -1
- package/dist/commands/runner/migration.js.map +1 -1
- package/dist/commands/runner/seeder.js +1 -1
- package/dist/commands/runner/seeder.js.map +1 -1
- package/dist/commands/stubs/index.d.ts +4 -4
- package/dist/commands/stubs/index.js +4 -4
- package/dist/commands/stubs/index.js.map +1 -1
- package/dist/context/context.js +6 -0
- package/dist/context/context.js.map +1 -1
- package/dist/controller/controller.d.ts +17 -30
- package/dist/controller/controller.js +39 -121
- package/dist/controller/controller.js.map +1 -1
- package/dist/controller/response.d.ts +6 -0
- package/dist/controller/response.js +63 -0
- package/dist/controller/response.js.map +1 -0
- package/dist/controller/storage.d.ts +9 -0
- package/dist/controller/storage.js +56 -0
- package/dist/controller/storage.js.map +1 -0
- package/dist/conversion/conversion.d.ts +3 -0
- package/dist/conversion/conversion.js +28 -4
- package/dist/conversion/conversion.js.map +1 -1
- package/dist/conversion/date.d.ts +1 -0
- package/dist/conversion/date.js +77 -0
- package/dist/conversion/date.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/logger/logger.js +33 -0
- package/dist/logger/logger.js.map +1 -1
- package/dist/mail/mail.js +6 -6
- package/dist/mail/mail.js.map +1 -1
- package/dist/middleware/access-log.d.ts +31 -0
- package/dist/middleware/access-log.js +13 -0
- package/dist/middleware/access-log.js.map +1 -0
- package/dist/middleware/auth.d.ts +37 -0
- package/dist/middleware/auth.js +16 -0
- package/dist/middleware/auth.js.map +1 -0
- package/dist/middleware/body-parse.d.ts +35 -0
- package/dist/middleware/body-parse.js +87 -0
- package/dist/middleware/body-parse.js.map +1 -0
- package/dist/middleware/context.d.ts +29 -0
- package/dist/middleware/context.js +8 -0
- package/dist/middleware/context.js.map +1 -0
- package/dist/middleware/cors.d.ts +31 -0
- package/dist/middleware/cors.js +27 -0
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/error-handler.d.ts +33 -0
- package/dist/middleware/error-handler.js +17 -0
- package/dist/middleware/error-handler.js.map +1 -0
- package/dist/middleware/middleware.d.ts +31 -10
- package/dist/middleware/middleware.js +41 -209
- package/dist/middleware/middleware.js.map +1 -1
- package/dist/middleware/private.d.ts +29 -0
- package/dist/middleware/private.js +8 -0
- package/dist/middleware/private.js.map +1 -0
- package/dist/middleware/rate-limiter.d.ts +32 -0
- package/dist/middleware/rate-limiter.js +30 -0
- package/dist/middleware/rate-limiter.js.map +1 -0
- package/dist/notification/index.d.ts +1 -0
- package/dist/notification/index.js +2 -0
- package/dist/notification/index.js.map +1 -0
- package/dist/notification/notification.d.ts +16 -0
- package/dist/notification/notification.js +64 -0
- package/dist/notification/notification.js.map +1 -0
- package/dist/permission/permission.js +9 -0
- package/dist/permission/permission.js.map +1 -1
- package/dist/registry/registry.d.ts +0 -6
- package/dist/registry/registry.js +6 -6
- package/dist/registry/registry.js.map +1 -1
- package/dist/storage/storage.d.ts +3 -3
- package/dist/storage/storage.js.map +1 -1
- package/dist/validation/validation.js +43 -51
- package/dist/validation/validation.js.map +1 -1
- package/package.json +4 -4
- package/src/auth/auth.ts +21 -252
- package/src/auth/create-access-token.ts +29 -0
- package/src/auth/create-user-mail-token.ts +24 -0
- package/src/auth/helpers/generate-agent-id.ts +8 -0
- package/src/auth/helpers/get-request-ip.ts +3 -0
- package/src/auth/helpers/get-user-permissions.ts +15 -0
- package/src/auth/helpers/index.ts +3 -0
- package/src/auth/list-user-sessions.ts +11 -0
- package/src/auth/revalidate-user-permissions-by-role.ts +13 -0
- package/src/auth/revalidate-user-permissions.ts +26 -0
- package/src/auth/revoke-access-token.ts +5 -0
- package/src/auth/verify-access-token.ts +56 -0
- package/src/auth/verify-user-mail-token.ts +24 -0
- package/src/commands/cli.ts +19 -29
- package/src/commands/make/basic-controller.ts +4 -2
- package/src/commands/make/basic-migration.ts +5 -3
- package/src/commands/make/basic-model.ts +3 -1
- package/src/commands/make/basic-seeder.ts +3 -1
- package/src/commands/make/blueprint.ts +3 -1
- package/src/commands/make/da-migration.ts +6 -5
- package/src/commands/make/mail.ts +4 -2
- package/src/commands/make/notification.ts +3 -1
- package/src/commands/make/queue.ts +3 -1
- package/src/commands/make/resource.ts +21 -0
- package/src/commands/make/{light-controller.ts → skalfa-controller.ts} +10 -8
- package/src/commands/make/{light-model.ts → skalfa-model.ts} +12 -10
- package/src/commands/runner/barrels.ts +4 -0
- package/src/commands/runner/blueprint/controller-generation.ts +4 -2
- package/src/commands/runner/blueprint/documentation-generation.ts +2 -0
- package/src/commands/runner/blueprint/migration-generation.ts +5 -3
- package/src/commands/runner/blueprint/model-generation.ts +4 -2
- package/src/commands/runner/blueprint/runner.ts +15 -8
- package/src/commands/runner/blueprint/seeder-generation.ts +5 -3
- package/src/commands/runner/da-migration.ts +3 -2
- package/src/commands/runner/generate-docs.ts +495 -0
- package/src/commands/runner/migration.ts +1 -1
- package/src/commands/runner/seeder.ts +1 -1
- package/src/commands/stubs/index.ts +4 -4
- package/src/context/context.ts +23 -17
- package/src/controller/controller.ts +124 -239
- package/src/controller/response.ts +78 -0
- package/src/controller/storage.ts +78 -0
- package/src/conversion/conversion.ts +90 -64
- package/src/conversion/date.ts +74 -0
- package/src/index.ts +2 -0
- package/src/logger/logger.ts +217 -176
- package/src/mail/mail.ts +85 -85
- package/src/middleware/access-log.ts +15 -0
- package/src/middleware/auth.ts +19 -0
- package/src/middleware/body-parse.ts +83 -0
- package/src/middleware/context.ts +11 -0
- package/src/middleware/cors.ts +31 -0
- package/src/middleware/error-handler.ts +20 -0
- package/src/middleware/middleware.ts +91 -288
- package/src/middleware/private.ts +8 -0
- package/src/middleware/rate-limiter.ts +41 -0
- package/src/notification/index.ts +1 -0
- package/src/notification/notification.ts +86 -0
- package/src/permission/permission.ts +140 -136
- package/src/registry/registry.ts +17 -15
- package/src/route/route.ts +11 -11
- package/src/storage/storage.ts +104 -106
- package/src/validation/validation.ts +322 -346
- package/dist/auth.util.d.ts +0 -19
- package/dist/auth.util.js +0 -183
- package/dist/auth.util.js.map +0 -1
- package/dist/commands/make/light-controller.d.ts +0 -3
- package/dist/commands/make/light-controller.js.map +0 -1
- package/dist/commands/make/light-model.d.ts +0 -3
- package/dist/commands/make/light-model.js.map +0 -1
- package/dist/context.util.d.ts +0 -7
- package/dist/context.util.js +0 -11
- package/dist/context.util.js.map +0 -1
- package/dist/controller.util.d.ts +0 -118
- package/dist/controller.util.js +0 -144
- package/dist/controller.util.js.map +0 -1
- package/dist/conversion.util.d.ts +0 -8
- package/dist/conversion.util.js +0 -52
- package/dist/conversion.util.js.map +0 -1
- package/dist/db/db.d.ts +0 -84
- package/dist/db/db.js +0 -177
- package/dist/db/db.js.map +0 -1
- package/dist/db/index.d.ts +0 -1
- package/dist/db/index.js +0 -2
- package/dist/db/index.js.map +0 -1
- package/dist/db.util.d.ts +0 -84
- package/dist/db.util.js +0 -177
- package/dist/db.util.js.map +0 -1
- package/dist/logger.util.d.ts +0 -30
- package/dist/logger.util.js +0 -126
- package/dist/logger.util.js.map +0 -1
- package/dist/mail.util.d.ts +0 -21
- package/dist/mail.util.js +0 -53
- package/dist/mail.util.js.map +0 -1
- package/dist/middleware.util.d.ts +0 -263
- package/dist/middleware.util.js +0 -233
- package/dist/middleware.util.js.map +0 -1
- package/dist/model/index.d.ts +0 -3
- package/dist/model/index.js +0 -4
- package/dist/model/index.js.map +0 -1
- package/dist/model/model.d.ts +0 -204
- package/dist/model/model.js +0 -1495
- package/dist/model/model.js.map +0 -1
- package/dist/model.util.d.ts +0 -204
- package/dist/model.util.js +0 -1495
- package/dist/model.util.js.map +0 -1
- package/dist/permission.util.d.ts +0 -38
- package/dist/permission.util.js +0 -91
- package/dist/permission.util.js.map +0 -1
- package/dist/registry.util.d.ts +0 -28
- package/dist/registry.util.js +0 -19
- package/dist/registry.util.js.map +0 -1
- package/dist/route.util.d.ts +0 -1
- package/dist/route.util.js +0 -12
- package/dist/route.util.js.map +0 -1
- package/dist/storage.util.d.ts +0 -56
- package/dist/storage.util.js +0 -82
- package/dist/storage.util.js.map +0 -1
- package/dist/validation.util.d.ts +0 -7
- package/dist/validation.util.js +0 -237
- package/dist/validation.util.js.map +0 -1
package/src/mail/mail.ts
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import { readFileSync } from "fs";
|
|
2
|
-
import { join } from "path";
|
|
3
|
-
import nodemailer, { SentMessageInfo } from "nodemailer";
|
|
4
|
-
import { logger } from "@utils";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export interface SendMailOptions {
|
|
9
|
-
to : string;
|
|
10
|
-
subject : string;
|
|
11
|
-
content ?: string;
|
|
12
|
-
text ?: string;
|
|
13
|
-
attachments ?: {
|
|
14
|
-
filename : string;
|
|
15
|
-
path : string;
|
|
16
|
-
}[];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
// ## Mail: Send mail
|
|
23
|
-
//
|
|
24
|
-
export async function sendMail(options: {
|
|
25
|
-
to : string;
|
|
26
|
-
subject : string;
|
|
27
|
-
text ?: string;
|
|
28
|
-
content ?: string;
|
|
29
|
-
attachments ?: { filename: string; path: string }[];
|
|
30
|
-
}) {
|
|
31
|
-
const transporter = nodemailer.createTransport({
|
|
32
|
-
host : process.env.MAIL_HOST,
|
|
33
|
-
port : Number(process.env.MAIL_PORT),
|
|
34
|
-
secure : Number(process.env.MAIL_PORT) === 465,
|
|
35
|
-
auth : {
|
|
36
|
-
user : process.env.MAIL_USERNAME,
|
|
37
|
-
pass : process.env.MAIL_PASSWORD,
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const info = (await transporter.sendMail({
|
|
42
|
-
from : `${process.env.MAIL_FROM_NAME || process.env.APP_NAME} <${process.env.MAIL_FROM_ADDRESS || process.env.MAIL_USERNAME}>`,
|
|
43
|
-
to : options.to,
|
|
44
|
-
subject : options.subject,
|
|
45
|
-
text : options.text,
|
|
46
|
-
html : options.content,
|
|
47
|
-
attachments : options.attachments,
|
|
48
|
-
})) as SentMessageInfo;
|
|
49
|
-
|
|
50
|
-
logger.info(`Email sent successfully: ${info.messageId}`)
|
|
51
|
-
return info;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
//
|
|
57
|
-
// ## Mail: Render mail template
|
|
58
|
-
//
|
|
59
|
-
export function renderMailTemplate(template: string, options: Record<string, string>) {
|
|
60
|
-
const templateDir = join(import.meta.dir, "./../outputs/mails/templates");
|
|
61
|
-
|
|
62
|
-
const contentPath = join(templateDir, `${template}.mail.stub`);
|
|
63
|
-
let content = readFileSync(contentPath, "utf-8");
|
|
64
|
-
|
|
65
|
-
for (const [key, value] of Object.entries(options)) {
|
|
66
|
-
const regex = new RegExp(`{{\\s*${key}\\s*}}`, "g");
|
|
67
|
-
content = content.replace(regex, value);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
let layout = readFileSync(join(templateDir, "layout.mail.stub"), "utf-8");
|
|
71
|
-
|
|
72
|
-
const globalVars = {
|
|
73
|
-
...options,
|
|
74
|
-
date : "20-10-2025",
|
|
75
|
-
app_name : process.env.APP_NAME || "",
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
for (const [key, value] of Object.entries(globalVars)) {
|
|
79
|
-
const regex = new RegExp(`{{\\s*${key}\\s*}}`, "g");
|
|
80
|
-
layout = layout.replace(regex, value);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
layout = layout.replace("{{content}}", content);
|
|
84
|
-
|
|
85
|
-
return layout;
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import nodemailer, { SentMessageInfo } from "nodemailer";
|
|
4
|
+
import { logger } from "@utils";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export interface SendMailOptions {
|
|
9
|
+
to : string;
|
|
10
|
+
subject : string;
|
|
11
|
+
content ?: string;
|
|
12
|
+
text ?: string;
|
|
13
|
+
attachments ?: {
|
|
14
|
+
filename : string;
|
|
15
|
+
path : string;
|
|
16
|
+
}[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// =====================================>
|
|
22
|
+
// ## Mail: Send mail
|
|
23
|
+
// =====================================>
|
|
24
|
+
export async function sendMail(options: {
|
|
25
|
+
to : string;
|
|
26
|
+
subject : string;
|
|
27
|
+
text ?: string;
|
|
28
|
+
content ?: string;
|
|
29
|
+
attachments ?: { filename: string; path: string }[];
|
|
30
|
+
}) {
|
|
31
|
+
const transporter = nodemailer.createTransport({
|
|
32
|
+
host : process.env.MAIL_HOST,
|
|
33
|
+
port : Number(process.env.MAIL_PORT),
|
|
34
|
+
secure : Number(process.env.MAIL_PORT) === 465,
|
|
35
|
+
auth : {
|
|
36
|
+
user : process.env.MAIL_USERNAME,
|
|
37
|
+
pass : process.env.MAIL_PASSWORD,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const info = (await transporter.sendMail({
|
|
42
|
+
from : `${process.env.MAIL_FROM_NAME || process.env.APP_NAME} <${process.env.MAIL_FROM_ADDRESS || process.env.MAIL_USERNAME}>`,
|
|
43
|
+
to : options.to,
|
|
44
|
+
subject : options.subject,
|
|
45
|
+
text : options.text,
|
|
46
|
+
html : options.content,
|
|
47
|
+
attachments : options.attachments,
|
|
48
|
+
})) as SentMessageInfo;
|
|
49
|
+
|
|
50
|
+
logger.info(`Email sent successfully: ${info.messageId}`)
|
|
51
|
+
return info;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// =====================================>
|
|
57
|
+
// ## Mail: Render mail template
|
|
58
|
+
// =====================================>
|
|
59
|
+
export function renderMailTemplate(template: string, options: Record<string, string>) {
|
|
60
|
+
const templateDir = join(import.meta.dir, "./../outputs/mails/templates");
|
|
61
|
+
|
|
62
|
+
const contentPath = join(templateDir, `${template}.mail.stub`);
|
|
63
|
+
let content = readFileSync(contentPath, "utf-8");
|
|
64
|
+
|
|
65
|
+
for (const [key, value] of Object.entries(options)) {
|
|
66
|
+
const regex = new RegExp(`{{\\s*${key}\\s*}}`, "g");
|
|
67
|
+
content = content.replace(regex, value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let layout = readFileSync(join(templateDir, "layout.mail.stub"), "utf-8");
|
|
71
|
+
|
|
72
|
+
const globalVars = {
|
|
73
|
+
...options,
|
|
74
|
+
date : "20-10-2025",
|
|
75
|
+
app_name : process.env.APP_NAME || "",
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
for (const [key, value] of Object.entries(globalVars)) {
|
|
79
|
+
const regex = new RegExp(`{{\\s*${key}\\s*}}`, "g");
|
|
80
|
+
layout = layout.replace(regex, value);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
layout = layout.replace("{{content}}", content);
|
|
84
|
+
|
|
85
|
+
return layout;
|
|
86
86
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Elysia } from 'elysia'
|
|
2
|
+
import { logger } from '@utils'
|
|
3
|
+
|
|
4
|
+
export const AccessLog = (app: Elysia) => app.state<{ startedAt?: number }>({}).onRequest(({ store }) => { store.startedAt = Date.now() }).onAfterResponse(({ request, set, store }) => {
|
|
5
|
+
const method = request.method
|
|
6
|
+
const url = new URL(request.url)
|
|
7
|
+
const path = url.pathname
|
|
8
|
+
const status = Number(set.status) ?? 200
|
|
9
|
+
const latency = Date.now() - (store.startedAt ?? Date.now())
|
|
10
|
+
const agent = request.headers.get("user-agent") || 'unknown'
|
|
11
|
+
const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() || request.headers.get('cf-connecting-ip') || 'unknown'
|
|
12
|
+
|
|
13
|
+
logger.info(`${method} : ${path} - ${status} - ${latency}ms - ${ip}]`)
|
|
14
|
+
logger.access({ method, path, status, latency, ip, agent })
|
|
15
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Elysia } from 'elysia'
|
|
2
|
+
import { auth } from '@utils'
|
|
3
|
+
|
|
4
|
+
export const Auth = (app: Elysia) => app.derive(async ({ request }) => {
|
|
5
|
+
const authHeader = request.headers.get('authorization')
|
|
6
|
+
|
|
7
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) return { user: null, permissions: [], token: null }
|
|
8
|
+
|
|
9
|
+
const bearer = authHeader.substring(7).trim()
|
|
10
|
+
const result = await auth.verifyAccessToken(bearer, request)
|
|
11
|
+
|
|
12
|
+
if (!result) return { user: null, permissions: [], token: null };
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
user: result.user,
|
|
16
|
+
permissions: result.permissions,
|
|
17
|
+
token: result.token,
|
|
18
|
+
}
|
|
19
|
+
})
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Elysia, status } from 'elysia'
|
|
2
|
+
import { logger } from '@utils'
|
|
3
|
+
import { errors } from './middleware'
|
|
4
|
+
|
|
5
|
+
export const BodyParse = (app: Elysia) => app.state<{ rawBody?: any }>({}).onRequest(async ({ request, store }) => {
|
|
6
|
+
const text = await request.clone().text();
|
|
7
|
+
|
|
8
|
+
const contentType = request.headers.get("content-type") || "";
|
|
9
|
+
let rawBody: any = {};
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
if (contentType.includes("application/json")) {
|
|
13
|
+
rawBody = text ? JSON.parse(text) : {};
|
|
14
|
+
} else if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
15
|
+
const params = new URLSearchParams(text);
|
|
16
|
+
for (const [key, value] of params.entries()) bodyParseNestedSet(rawBody, key, value);
|
|
17
|
+
} else if (contentType.includes("multipart/form-data")) {
|
|
18
|
+
const formData = await request.clone().formData();
|
|
19
|
+
for (const [key, value] of formData.entries()) bodyParseNestedSet(rawBody, key, value);
|
|
20
|
+
} else {
|
|
21
|
+
rawBody = {};
|
|
22
|
+
}
|
|
23
|
+
} catch (e) {
|
|
24
|
+
const em = e instanceof Error ? e.message : String(e)
|
|
25
|
+
logger.error(`Body parse error: ${em}`, { error: em })
|
|
26
|
+
rawBody = {};
|
|
27
|
+
throw status(errors.request.status, { message: errors.request.message })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
store.rawBody = rawBody;
|
|
31
|
+
}).derive(({ store }) => {
|
|
32
|
+
const payload = bodyParseKeyFormat(store.rawBody || {});
|
|
33
|
+
return { payload };
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
function bodyParseKeyFormat(input: any): any {
|
|
37
|
+
if ( typeof input !== "object" || input === null || input instanceof File ) return input;
|
|
38
|
+
|
|
39
|
+
if (Array.isArray(input)) return input.map(bodyParseKeyFormat)
|
|
40
|
+
|
|
41
|
+
const result: any = {}
|
|
42
|
+
for (const [key, value] of Object.entries(input)) {
|
|
43
|
+
if (key.includes(".") || key.includes("[")) {
|
|
44
|
+
bodyParseNestedSet(result, key, bodyParseKeyFormat(value))
|
|
45
|
+
} else {
|
|
46
|
+
result[key] = bodyParseKeyFormat(value)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return result
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function bodyParseNestedSet(obj: any, path: string, value: any) {
|
|
53
|
+
const parts = bodyParsePathFormat(path);
|
|
54
|
+
let current = obj;
|
|
55
|
+
|
|
56
|
+
for (let i = 0; i < parts.length; i++) {
|
|
57
|
+
const key = parts[i];
|
|
58
|
+
const isLast = i === parts.length - 1;
|
|
59
|
+
|
|
60
|
+
if (isLast) {
|
|
61
|
+
current[key] = bodyParseValueFormat(value);
|
|
62
|
+
} else {
|
|
63
|
+
if (!(key in current)) {
|
|
64
|
+
const nextKey = parts[i + 1];
|
|
65
|
+
current[key] = isNaN(Number(nextKey)) ? {} : [];
|
|
66
|
+
}
|
|
67
|
+
current = current[key];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function bodyParsePathFormat(path: string): string[] {
|
|
73
|
+
return path.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function bodyParseValueFormat(value: any) {
|
|
77
|
+
if (value == "" || value == null || value == "null") return null;
|
|
78
|
+
if (typeof value !== "string") return value;
|
|
79
|
+
if (value === "true") return true;
|
|
80
|
+
if (value === "false") return false;
|
|
81
|
+
if (!isNaN(Number(value))) return Number(value);
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Elysia } from 'elysia'
|
|
2
|
+
import { context } from '@utils'
|
|
3
|
+
|
|
4
|
+
export const ContextMiddleware = (app: Elysia) => app.derive(async ({ store }) => {
|
|
5
|
+
const userId = (store as any)?.user?.id
|
|
6
|
+
|
|
7
|
+
return context.run({
|
|
8
|
+
user_id: userId,
|
|
9
|
+
},() => ({})
|
|
10
|
+
)
|
|
11
|
+
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Elysia } from 'elysia'
|
|
2
|
+
import { logger } from '@utils'
|
|
3
|
+
|
|
4
|
+
export const Cors = (app: Elysia) => app.onRequest(({ request, set }) => {
|
|
5
|
+
const origin = request.headers.get('origin') ?? ''
|
|
6
|
+
let allowedOrigin: string = '*'
|
|
7
|
+
|
|
8
|
+
const originsConf = process.env.APP_CORS_ORIGINS || '*'
|
|
9
|
+
|
|
10
|
+
if (originsConf !== '*') {
|
|
11
|
+
try {
|
|
12
|
+
const allowedOrigins = JSON.parse(originsConf)
|
|
13
|
+
if (Array.isArray(allowedOrigins) && allowedOrigins.includes(origin)) {
|
|
14
|
+
allowedOrigin = origin || ""
|
|
15
|
+
}
|
|
16
|
+
} catch (e) {
|
|
17
|
+
const em = 'Cors Error: Failed to parse APP_CORS_ORIGINS, fallback to "*"'
|
|
18
|
+
logger.error(em, { error: em })
|
|
19
|
+
allowedOrigin = ''
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
set.headers['Access-Control-Allow-Origin'] = allowedOrigin
|
|
24
|
+
set.headers['Access-Control-Allow-Methods'] = process.env.APP_CORS_METHODS || 'GET, POST, PUT, DELETE, OPTIONS'
|
|
25
|
+
set.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Option, x-App'
|
|
26
|
+
set.headers['Access-Control-Allow-Credentials'] = 'true'
|
|
27
|
+
|
|
28
|
+
if (request.method === 'OPTIONS') {
|
|
29
|
+
return new Response(null, { status: 204, })
|
|
30
|
+
}
|
|
31
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Elysia } from 'elysia'
|
|
2
|
+
import { logger } from '@utils'
|
|
3
|
+
import { errors } from './middleware'
|
|
4
|
+
|
|
5
|
+
export const ErrorHandler = (app: Elysia) => app.onError(({ code, set, error, request }) => {
|
|
6
|
+
if (code === 'NOT_FOUND') {
|
|
7
|
+
set.status = errors.notfound.status
|
|
8
|
+
return { message: errors.notfound.message }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (code === 'INTERNAL_SERVER_ERROR') {
|
|
12
|
+
set.status = errors.error.status
|
|
13
|
+
const em = error.message
|
|
14
|
+
const url = new URL(request.url)
|
|
15
|
+
const path = url.pathname
|
|
16
|
+
|
|
17
|
+
logger.error(`error: ${em}`, { error: em, reference: path })
|
|
18
|
+
return { message: em }
|
|
19
|
+
}
|
|
20
|
+
})
|