@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/dist/middleware.util.js
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { status } from 'elysia';
|
|
2
|
-
import { auth, context, logger } from '@utils';
|
|
3
|
-
const errors = {
|
|
4
|
-
unauthorized: {
|
|
5
|
-
status: 401,
|
|
6
|
-
message: "Unauthorized!"
|
|
7
|
-
},
|
|
8
|
-
ratelimited: {
|
|
9
|
-
status: 429,
|
|
10
|
-
message: "Too many requests!"
|
|
11
|
-
},
|
|
12
|
-
notfound: {
|
|
13
|
-
status: 404,
|
|
14
|
-
message: "Endpoint not found!"
|
|
15
|
-
},
|
|
16
|
-
request: {
|
|
17
|
-
status: 400,
|
|
18
|
-
message: "Bad Request!"
|
|
19
|
-
},
|
|
20
|
-
error: {
|
|
21
|
-
status: 500,
|
|
22
|
-
message: "Endpoint not found!"
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
export const middleware = {
|
|
26
|
-
// =============================>
|
|
27
|
-
// ## Middleware: Auth hand;er
|
|
28
|
-
// =============================>
|
|
29
|
-
Auth: (app) => app.derive(async ({ request }) => {
|
|
30
|
-
const authHeader = request.headers.get('authorization');
|
|
31
|
-
if (!authHeader || !authHeader.startsWith('Bearer '))
|
|
32
|
-
return { user: null, permissions: [], token: null };
|
|
33
|
-
const bearer = authHeader.substring(7).trim();
|
|
34
|
-
const result = await auth.verifyAccessToken(bearer, request);
|
|
35
|
-
if (!result)
|
|
36
|
-
return { user: null, permissions: [], token: null };
|
|
37
|
-
return {
|
|
38
|
-
user: result.user,
|
|
39
|
-
permissions: result.permissions,
|
|
40
|
-
token: result.token,
|
|
41
|
-
};
|
|
42
|
-
}),
|
|
43
|
-
// =============================>
|
|
44
|
-
// ## Middleware: Private handler
|
|
45
|
-
// =============================>
|
|
46
|
-
Private: (app) => app.derive(async ({ user }) => {
|
|
47
|
-
if (!user) {
|
|
48
|
-
throw status(errors.unauthorized.status, { message: errors.unauthorized.message });
|
|
49
|
-
}
|
|
50
|
-
}),
|
|
51
|
-
// =============================>
|
|
52
|
-
// ## Middleware: Cors handler
|
|
53
|
-
// =============================>
|
|
54
|
-
Cors: (app) => app.onRequest(({ request, set }) => {
|
|
55
|
-
const origin = request.headers.get('origin') ?? '';
|
|
56
|
-
let allowedOrigin = '*';
|
|
57
|
-
const originsConf = process.env.APP_CORS_ORIGINS || '*';
|
|
58
|
-
if (originsConf !== '*') {
|
|
59
|
-
try {
|
|
60
|
-
const allowedOrigins = JSON.parse(originsConf);
|
|
61
|
-
if (Array.isArray(allowedOrigins) && allowedOrigins.includes(origin)) {
|
|
62
|
-
allowedOrigin = origin || "";
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
const em = 'Cors Error: Failed to parse APP_CORS_ORIGINS, fallback to "*"';
|
|
67
|
-
logger.error(em, { error: em });
|
|
68
|
-
allowedOrigin = '';
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
set.headers['Access-Control-Allow-Origin'] = allowedOrigin;
|
|
72
|
-
set.headers['Access-Control-Allow-Methods'] = process.env.APP_CORS_METHODS || 'GET, POST, PUT, DELETE, OPTIONS';
|
|
73
|
-
set.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Option, x-App';
|
|
74
|
-
set.headers['Access-Control-Allow-Credentials'] = 'true';
|
|
75
|
-
if (request.method === 'OPTIONS') {
|
|
76
|
-
return new Response(null, { status: 204, });
|
|
77
|
-
}
|
|
78
|
-
}),
|
|
79
|
-
// =============================>
|
|
80
|
-
// ## Middleware: Rate limiter handler
|
|
81
|
-
// =============================>
|
|
82
|
-
RateLimiter: (app, options) => app.onRequest(({ request, set, store }) => {
|
|
83
|
-
const max = options?.max || (process.env.APP_RATELIMIT_COUNTDOWN ? Number(process.env.APP_RATE_LIMIT) : 60);
|
|
84
|
-
const windowMs = options?.windowMs || (process.env.APP_RATELIMIT_COUNTDOWN ? Number(process.env.APP_RATELIMIT_COUNTDOWN) : 60_000);
|
|
85
|
-
const user = store?.user;
|
|
86
|
-
const key = getClientKey(request, user?.id);
|
|
87
|
-
const now = Date.now();
|
|
88
|
-
let record = rateLimitStore.get(key);
|
|
89
|
-
if (!record || record.expiresAt < now) {
|
|
90
|
-
record = { count: 1, expiresAt: now + windowMs };
|
|
91
|
-
rateLimitStore.set(key, record);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
record.count++;
|
|
95
|
-
}
|
|
96
|
-
set.headers['X-RateLimit-Limit'] = String(max);
|
|
97
|
-
set.headers['X-RateLimit-Remaining'] = String(Math.max(0, max - record.count));
|
|
98
|
-
set.headers['X-RateLimit-Reset'] = String(record.expiresAt);
|
|
99
|
-
if (record.count > max)
|
|
100
|
-
throw status(errors.ratelimited.status, { message: errors.ratelimited.message });
|
|
101
|
-
}),
|
|
102
|
-
// =============================>
|
|
103
|
-
// ## Middleware: Body parse handler
|
|
104
|
-
// =============================>
|
|
105
|
-
BodyParse: (app) => app.state({}).onRequest(async ({ request, store }) => {
|
|
106
|
-
const text = await request.clone().text();
|
|
107
|
-
const contentType = request.headers.get("content-type") || "";
|
|
108
|
-
let rawBody = {};
|
|
109
|
-
try {
|
|
110
|
-
if (contentType.includes("application/json")) {
|
|
111
|
-
rawBody = text ? JSON.parse(text) : {};
|
|
112
|
-
}
|
|
113
|
-
else if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
114
|
-
const params = new URLSearchParams(text);
|
|
115
|
-
for (const [key, value] of params.entries())
|
|
116
|
-
bodyParseNestedSet(rawBody, key, value);
|
|
117
|
-
}
|
|
118
|
-
else if (contentType.includes("multipart/form-data")) {
|
|
119
|
-
const formData = await request.clone().formData();
|
|
120
|
-
for (const [key, value] of formData.entries())
|
|
121
|
-
bodyParseNestedSet(rawBody, key, value);
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
rawBody = {};
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
catch (e) {
|
|
128
|
-
const em = e instanceof Error ? e.message : String(e);
|
|
129
|
-
logger.error(`Body parse error: ${em}`, { error: em });
|
|
130
|
-
rawBody = {};
|
|
131
|
-
throw status(errors.request.status, { message: errors.request.message });
|
|
132
|
-
}
|
|
133
|
-
store.rawBody = rawBody;
|
|
134
|
-
}).derive(({ store }) => {
|
|
135
|
-
const payload = bodyParseKeyFormat(store.rawBody || {});
|
|
136
|
-
return { payload };
|
|
137
|
-
}),
|
|
138
|
-
AccessLog: (app) => app.state({}).onRequest(({ store }) => { store.startedAt = Date.now(); }).onAfterResponse(({ request, set, store }) => {
|
|
139
|
-
const method = request.method;
|
|
140
|
-
const url = new URL(request.url);
|
|
141
|
-
const path = url.pathname;
|
|
142
|
-
const status = Number(set.status) ?? 200;
|
|
143
|
-
const latency = Date.now() - (store.startedAt ?? Date.now());
|
|
144
|
-
const agent = request.headers.get("user-agent") || 'unknown';
|
|
145
|
-
const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() || request.headers.get('cf-connecting-ip') || 'unknown';
|
|
146
|
-
logger.info(`${method} : ${path} - ${status} - ${latency}ms - ${ip}]`);
|
|
147
|
-
logger.access({ method, path, status, latency, ip, agent });
|
|
148
|
-
}),
|
|
149
|
-
// =============================>
|
|
150
|
-
// ## Middleware: Error handler
|
|
151
|
-
// =============================>
|
|
152
|
-
ErrorHandler: (app) => app.onError(({ code, set, error, request }) => {
|
|
153
|
-
if (code === 'NOT_FOUND') {
|
|
154
|
-
set.status = errors.notfound.status;
|
|
155
|
-
return { message: errors.notfound.message };
|
|
156
|
-
}
|
|
157
|
-
if (code === 'INTERNAL_SERVER_ERROR') {
|
|
158
|
-
set.status = errors.error.status;
|
|
159
|
-
const em = error.message;
|
|
160
|
-
const url = new URL(request.url);
|
|
161
|
-
const path = url.pathname;
|
|
162
|
-
logger.error(`error: ${em}`, { error: em, reference: path });
|
|
163
|
-
return { message: em };
|
|
164
|
-
}
|
|
165
|
-
}),
|
|
166
|
-
Context: (app) => app.derive(async ({ store }) => {
|
|
167
|
-
const userId = store?.user?.id;
|
|
168
|
-
return context.run({
|
|
169
|
-
user_id: userId,
|
|
170
|
-
}, () => ({}));
|
|
171
|
-
}),
|
|
172
|
-
};
|
|
173
|
-
// =============================>
|
|
174
|
-
// ## Middleware: Body parse helpers
|
|
175
|
-
// =============================>
|
|
176
|
-
function bodyParseKeyFormat(input) {
|
|
177
|
-
if (typeof input !== "object" || input === null || input instanceof File)
|
|
178
|
-
return input;
|
|
179
|
-
if (Array.isArray(input))
|
|
180
|
-
return input.map(bodyParseKeyFormat);
|
|
181
|
-
const result = {};
|
|
182
|
-
for (const [key, value] of Object.entries(input)) {
|
|
183
|
-
if (key.includes(".") || key.includes("[")) {
|
|
184
|
-
bodyParseNestedSet(result, key, bodyParseKeyFormat(value));
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
result[key] = bodyParseKeyFormat(value);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return result;
|
|
191
|
-
}
|
|
192
|
-
function bodyParseNestedSet(obj, path, value) {
|
|
193
|
-
const parts = bodyParsePathFormat(path);
|
|
194
|
-
let current = obj;
|
|
195
|
-
for (let i = 0; i < parts.length; i++) {
|
|
196
|
-
const key = parts[i];
|
|
197
|
-
const isLast = i === parts.length - 1;
|
|
198
|
-
if (isLast) {
|
|
199
|
-
current[key] = bodyParseValueFormat(value);
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
if (!(key in current)) {
|
|
203
|
-
const nextKey = parts[i + 1];
|
|
204
|
-
current[key] = isNaN(Number(nextKey)) ? {} : [];
|
|
205
|
-
}
|
|
206
|
-
current = current[key];
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
function bodyParsePathFormat(path) {
|
|
211
|
-
return path.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".");
|
|
212
|
-
}
|
|
213
|
-
function bodyParseValueFormat(value) {
|
|
214
|
-
if (value == "" || value == null || value == "null")
|
|
215
|
-
return null;
|
|
216
|
-
if (typeof value !== "string")
|
|
217
|
-
return value;
|
|
218
|
-
if (value === "true")
|
|
219
|
-
return true;
|
|
220
|
-
if (value === "false")
|
|
221
|
-
return false;
|
|
222
|
-
if (!isNaN(Number(value)))
|
|
223
|
-
return Number(value);
|
|
224
|
-
return value;
|
|
225
|
-
}
|
|
226
|
-
const rateLimitStore = new Map();
|
|
227
|
-
function getClientKey(request, userId) {
|
|
228
|
-
if (userId)
|
|
229
|
-
return `user:${userId}`;
|
|
230
|
-
const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() || request.headers.get('cf-connecting-ip') || 'unknown';
|
|
231
|
-
return `ip:${ip}`;
|
|
232
|
-
}
|
|
233
|
-
//# sourceMappingURL=middleware.util.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.util.js","sourceRoot":"","sources":["../src/middleware.util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAA;AACvC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAkB9C,MAAM,MAAM,GAAG;IAC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C,CAAC;QAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IAChD,OAAO,KAAK,CAAC;AACf,CAAC;AAYD,MAAM,cAAc,GAAG,IAAI,GAAG,EAA2B,CAAA;AAEzD,SAAS,YAAY,CAAC,OAAgB,EAAE,MAAwB;IAC9D,IAAI,MAAM;QAAE,OAAO,QAAQ,MAAM,EAAE,CAAA;IAEnC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,IAAI,SAAS,CAAA;IAEhI,OAAO,MAAM,EAAE,EAAE,CAAA;AACnB,CAAC"}
|
package/dist/model/index.d.ts
DELETED
package/dist/model/index.js
DELETED
package/dist/model/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/model/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,KAAK,GAAQ,IAAI,CAAC;AAC/B,MAAM,CAAC,MAAM,UAAU,GAAQ,IAAI,CAAC;AACpC,MAAM,CAAC,MAAM,YAAY,GAAQ,IAAI,CAAC"}
|
package/dist/model/model.d.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import type { Knex } from 'knex';
|
|
2
|
-
export declare const PRIMARY_KEY_META: unique symbol;
|
|
3
|
-
export declare const FIELD_META: unique symbol;
|
|
4
|
-
export declare const RELATION_META: unique symbol;
|
|
5
|
-
export declare const SOFT_DELETE_META: unique symbol;
|
|
6
|
-
export declare const ATTRIBUTE_META: unique symbol;
|
|
7
|
-
export declare const FORMATTER_META: unique symbol;
|
|
8
|
-
export declare const SCOPE_META: unique symbol;
|
|
9
|
-
export type FieldFlag = 'fillable' | 'selectable' | 'searchable' | 'hidden';
|
|
10
|
-
export type FieldMeta = {
|
|
11
|
-
cast?: ModelCastType;
|
|
12
|
-
fillable?: boolean;
|
|
13
|
-
selectable?: boolean;
|
|
14
|
-
searchable?: boolean;
|
|
15
|
-
hidden?: boolean;
|
|
16
|
-
};
|
|
17
|
-
type NonFunctionKeys<T> = {
|
|
18
|
-
[K in keyof T]: T[K] extends Function ? never : K;
|
|
19
|
-
}[keyof T];
|
|
20
|
-
type DataShape<T> = Pick<T, NonFunctionKeys<T>>;
|
|
21
|
-
type ModelPayload<T> = Partial<DataShape<T>>;
|
|
22
|
-
export type ModelCastType = 'string' | 'number' | 'boolean' | 'date' | 'json';
|
|
23
|
-
export type ModelRelationType = 'hasMany' | 'hasOne' | 'belongsTo' | 'belongsToMany';
|
|
24
|
-
export type ModelRelationDescriptor = {
|
|
25
|
-
type: ModelRelationType;
|
|
26
|
-
model: () => typeof Model;
|
|
27
|
-
foreignKey: string;
|
|
28
|
-
localKey: string;
|
|
29
|
-
pivotTable?: string;
|
|
30
|
-
pivotLocal?: string;
|
|
31
|
-
pivotForeign?: string;
|
|
32
|
-
callback?: (q: any) => void;
|
|
33
|
-
};
|
|
34
|
-
export type ModelHookEventType = "before-create" | "after-create" | "before-update" | "after-update" | "before-delete" | "after-delete";
|
|
35
|
-
export type ModelHookFn = (ctx: ModelHookContextType) => any;
|
|
36
|
-
export type ModelHookContextType<T extends Model = Model> = {
|
|
37
|
-
model: T;
|
|
38
|
-
trx?: any;
|
|
39
|
-
snapshot?: any;
|
|
40
|
-
};
|
|
41
|
-
export type ScopeType = {
|
|
42
|
-
fn: Function;
|
|
43
|
-
mode: 'global' | 'internal';
|
|
44
|
-
};
|
|
45
|
-
declare module 'knex' {
|
|
46
|
-
namespace Knex {
|
|
47
|
-
interface QueryBuilder<TRecord = any, TResult = any> {
|
|
48
|
-
$model?: any;
|
|
49
|
-
_withTree?: Record<string, any>;
|
|
50
|
-
_formatter?: ((item: any) => any) | null;
|
|
51
|
-
_softDeleteScope?: 'default' | 'with' | 'only';
|
|
52
|
-
_withAggregates?: Array<{
|
|
53
|
-
relation: string;
|
|
54
|
-
alias: string;
|
|
55
|
-
fn: 'count' | 'sum' | 'avg' | 'min' | 'max';
|
|
56
|
-
column: string;
|
|
57
|
-
callback?: (q: any) => void;
|
|
58
|
-
}>;
|
|
59
|
-
_orderByAggregates?: Array<{
|
|
60
|
-
relation: string;
|
|
61
|
-
alias?: string;
|
|
62
|
-
fn: 'count' | 'sum' | 'avg' | 'min' | 'max';
|
|
63
|
-
column: string;
|
|
64
|
-
direction: 'asc' | 'desc';
|
|
65
|
-
callback?: (q: any) => void;
|
|
66
|
-
}>;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
export interface ModelQueryBuilder<T extends Record<string, any> = Record<string, any>> extends Knex.QueryBuilder<T, T[]> {
|
|
71
|
-
_withTree?: Record<string, any>;
|
|
72
|
-
_formatter?: ((item: T) => any) | null;
|
|
73
|
-
_softDeleteScope?: 'default' | 'with' | 'only';
|
|
74
|
-
findOrNotFound(id: string | number): Promise<T>;
|
|
75
|
-
firstOrNotFound(): Promise<T>;
|
|
76
|
-
search(keyword?: string, options?: {
|
|
77
|
-
includes?: string[];
|
|
78
|
-
searchable?: string[];
|
|
79
|
-
}): this;
|
|
80
|
-
filter(filters?: Record<string, string>): this;
|
|
81
|
-
selects(options?: {
|
|
82
|
-
includes?: string[];
|
|
83
|
-
selectable?: string[];
|
|
84
|
-
}): this;
|
|
85
|
-
sorts(sorts?: string[]): this;
|
|
86
|
-
with(relation: string, callback?: any): this;
|
|
87
|
-
expand(relations?: Array<string | Record<string, (q: any) => void>>): this;
|
|
88
|
-
whereHas(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
|
|
89
|
-
orWhereHas(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
|
|
90
|
-
whereDoesntHave(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
|
|
91
|
-
orWhereDoesntHave(relation: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
|
|
92
|
-
withAggregate(expr: string, fn: 'count' | 'sum' | 'avg' | 'min' | 'max', column?: string, callback?: (q: ModelQueryBuilder<any>) => void): this;
|
|
93
|
-
orderByAggregate(expr: string, fn: 'count' | 'sum' | 'avg' | 'min' | 'max', column?: string, direction?: 'asc' | 'desc', callback?: (q: ModelQueryBuilder<any>) => void): this;
|
|
94
|
-
get(): Promise<T[]>;
|
|
95
|
-
getFirst(): Promise<T>;
|
|
96
|
-
paginate(page?: number, limit?: number): Promise<{
|
|
97
|
-
data: T[];
|
|
98
|
-
total: number;
|
|
99
|
-
}>;
|
|
100
|
-
option(selectableOption?: string[]): Promise<Array<{
|
|
101
|
-
value: any;
|
|
102
|
-
label: any;
|
|
103
|
-
}>>;
|
|
104
|
-
paginateOrOption(page?: number, limit?: number, option?: string | boolean, selectableOption?: string[]): Promise<{
|
|
105
|
-
data: any[];
|
|
106
|
-
total: number;
|
|
107
|
-
}>;
|
|
108
|
-
resolve(input?: any): Promise<{
|
|
109
|
-
data: T[];
|
|
110
|
-
total: number;
|
|
111
|
-
}>;
|
|
112
|
-
format(formatter: string | ((item: T) => any)): this;
|
|
113
|
-
withTrashed(): this;
|
|
114
|
-
onlyTrashed(): this;
|
|
115
|
-
}
|
|
116
|
-
export declare abstract class Model {
|
|
117
|
-
id: number;
|
|
118
|
-
created_at: Date;
|
|
119
|
-
updated_at: Date;
|
|
120
|
-
deleted_at: Date;
|
|
121
|
-
static table: string;
|
|
122
|
-
static primaryKey: string;
|
|
123
|
-
static softDelete: boolean;
|
|
124
|
-
static deletedAtColumn: string;
|
|
125
|
-
protected _original: Record<string, any>;
|
|
126
|
-
protected _exists: boolean;
|
|
127
|
-
protected _trx?: Knex.Transaction;
|
|
128
|
-
private static _hooks;
|
|
129
|
-
private _instanceHooks;
|
|
130
|
-
private _disabledHooks;
|
|
131
|
-
constructor(data?: Record<string, any>);
|
|
132
|
-
protected static newInstance<T extends typeof Model>(this: T): InstanceType<T> & Model;
|
|
133
|
-
static [FIELD_META]?: Record<string, any>;
|
|
134
|
-
static getDefaultFields(): Record<string, FieldMeta>;
|
|
135
|
-
static get fields(): Record<string, FieldMeta>;
|
|
136
|
-
static get fillable(): string[];
|
|
137
|
-
static get selectable(): string[];
|
|
138
|
-
static get searchable(): string[];
|
|
139
|
-
static getTable(): string;
|
|
140
|
-
static getPrimaryKey(): string;
|
|
141
|
-
static [RELATION_META]?: Record<string, any>;
|
|
142
|
-
static get relations(): Record<string, any>;
|
|
143
|
-
static get attributes(): Record<string, Function>;
|
|
144
|
-
static get formatters(): Record<string, Function>;
|
|
145
|
-
static get scopes(): Record<string, ScopeType>;
|
|
146
|
-
getOriginal(key?: string): any;
|
|
147
|
-
getChanges(): Record<string, any>;
|
|
148
|
-
getPrevious(): Record<string, any>;
|
|
149
|
-
static query<T extends Record<string, any> = Record<string, any>>(trx?: Knex | Knex.Transaction): ModelQueryBuilder<T>;
|
|
150
|
-
castFromDB(row: Record<string, any>): this;
|
|
151
|
-
castToDB(): Record<string, any>;
|
|
152
|
-
getDirty(): Record<string, any>;
|
|
153
|
-
static hydrate<T extends typeof Model>(this: T, rows: any[] | null | undefined): InstanceType<T>[];
|
|
154
|
-
fill<T extends this>(this: T, payload: ModelPayload<T>): T;
|
|
155
|
-
static create<T extends typeof Model>(this: T, payload: ModelPayload<InstanceType<T>>, trx?: Knex.Transaction): Promise<InstanceType<T>>;
|
|
156
|
-
static update<T extends typeof Model>(this: T, payload: ModelPayload<InstanceType<T>>, uniqueKeys: (keyof InstanceType<T> & string)[], trx?: Knex.Transaction): Promise<InstanceType<T>>;
|
|
157
|
-
static upsert<T extends typeof Model>(this: T, payload: ModelPayload<InstanceType<T>>, uniqueKeys: (keyof InstanceType<T> & string)[], trx?: Knex.Transaction): Promise<InstanceType<T>>;
|
|
158
|
-
save(): Promise<this>;
|
|
159
|
-
pump<T extends this>(this: T, payload: ModelPayload<T> | ModelPayload<T>[], options?: {
|
|
160
|
-
trx?: Knex.Transaction;
|
|
161
|
-
}): Promise<T | T[]>;
|
|
162
|
-
static getSoftDeleteConfig(): any;
|
|
163
|
-
static isSoftDelete(): boolean;
|
|
164
|
-
static getDeletedAtColumn(): string | null;
|
|
165
|
-
delete(): Promise<Record<string, any> | null>;
|
|
166
|
-
forceDelete(): Promise<any>;
|
|
167
|
-
restore(): Promise<this>;
|
|
168
|
-
on<T extends this>(event: ModelHookEventType, fn: (ctx: ModelHookContextType<T>) => any): this;
|
|
169
|
-
off(event: ModelHookEventType): this;
|
|
170
|
-
protected runHook(event: ModelHookEventType, ctx: ModelHookContextType): Promise<void>;
|
|
171
|
-
toJSON(): Record<string, any>;
|
|
172
|
-
useTransaction(trx: Knex.Transaction): this;
|
|
173
|
-
}
|
|
174
|
-
export declare function extendModelQuery(query: Knex.QueryBuilder, Model: any): Knex.QueryBuilder<any, any>;
|
|
175
|
-
export declare function PrimaryKey(): (target: any, key: string) => void;
|
|
176
|
-
export declare function Field(defs: string[]): (target: any, key: string) => void;
|
|
177
|
-
export declare function HasMany(model: () => typeof Model, options?: {
|
|
178
|
-
foreignKey?: string;
|
|
179
|
-
localKey?: string;
|
|
180
|
-
callback?: (q: any) => void;
|
|
181
|
-
}): (target: any, key: string) => void;
|
|
182
|
-
export declare function HasOne(model: () => typeof Model, options?: {
|
|
183
|
-
foreignKey?: string;
|
|
184
|
-
localKey?: string;
|
|
185
|
-
callback?: (q: any) => void;
|
|
186
|
-
}): (target: any, key: string) => void;
|
|
187
|
-
export declare function BelongsTo(model: () => typeof Model, options?: {
|
|
188
|
-
foreignKey?: string;
|
|
189
|
-
ownerKey?: string;
|
|
190
|
-
callback?: (q: any) => void;
|
|
191
|
-
}): (target: any, key: string) => void;
|
|
192
|
-
export declare function BelongsToMany(model: () => typeof Model, options?: {
|
|
193
|
-
pivotTable?: string;
|
|
194
|
-
pivotLocal?: string;
|
|
195
|
-
pivotForeign?: string;
|
|
196
|
-
localKey?: string;
|
|
197
|
-
callback?: (q: any) => void;
|
|
198
|
-
}): (target: any, key: string) => void;
|
|
199
|
-
export declare function SoftDelete(): (target: any, propertyKey: string) => void;
|
|
200
|
-
export declare function Attribute(): (target: any, key: string, descriptor: PropertyDescriptor) => void;
|
|
201
|
-
export declare function Formatter(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
202
|
-
export declare function Scope(mode?: 'global' | 'internal'): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
203
|
-
export declare function On(event: ModelHookEventType): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
204
|
-
export {};
|