@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.
Files changed (284) hide show
  1. package/CONTRIBUTING.md +45 -0
  2. package/LICENSE +21 -0
  3. package/README.md +60 -0
  4. package/dist/auth/auth.d.ts +18 -15
  5. package/dist/auth/auth.js +20 -203
  6. package/dist/auth/auth.js.map +1 -1
  7. package/dist/auth/create-access-token.d.ts +4 -0
  8. package/dist/auth/create-access-token.js +26 -0
  9. package/dist/auth/create-access-token.js.map +1 -0
  10. package/dist/auth/create-user-mail-token.d.ts +4 -0
  11. package/dist/auth/create-user-mail-token.js +19 -0
  12. package/dist/auth/create-user-mail-token.js.map +1 -0
  13. package/dist/auth/helpers/generate-agent-id.d.ts +1 -0
  14. package/dist/auth/helpers/generate-agent-id.js +7 -0
  15. package/dist/auth/helpers/generate-agent-id.js.map +1 -0
  16. package/dist/auth/helpers/get-request-ip.d.ts +1 -0
  17. package/dist/auth/helpers/get-request-ip.js +4 -0
  18. package/dist/auth/helpers/get-request-ip.js.map +1 -0
  19. package/dist/auth/helpers/get-user-permissions.d.ts +1 -0
  20. package/dist/auth/helpers/get-user-permissions.js +9 -0
  21. package/dist/auth/helpers/get-user-permissions.js.map +1 -0
  22. package/dist/auth/helpers/index.d.ts +3 -0
  23. package/dist/auth/helpers/index.js +4 -0
  24. package/dist/auth/helpers/index.js.map +1 -0
  25. package/dist/auth/list-user-sessions.d.ts +1 -0
  26. package/dist/auth/list-user-sessions.js +10 -0
  27. package/dist/auth/list-user-sessions.js.map +1 -0
  28. package/dist/auth/revalidate-user-permissions-by-role.d.ts +1 -0
  29. package/dist/auth/revalidate-user-permissions-by-role.js +12 -0
  30. package/dist/auth/revalidate-user-permissions-by-role.js.map +1 -0
  31. package/dist/auth/revalidate-user-permissions.d.ts +1 -0
  32. package/dist/auth/revalidate-user-permissions.js +21 -0
  33. package/dist/auth/revalidate-user-permissions.js.map +1 -0
  34. package/dist/auth/revoke-access-token.d.ts +1 -0
  35. package/dist/auth/revoke-access-token.js +5 -0
  36. package/dist/auth/revoke-access-token.js.map +1 -0
  37. package/dist/auth/verify-access-token.d.ts +1 -0
  38. package/dist/auth/verify-access-token.js +47 -0
  39. package/dist/auth/verify-access-token.js.map +1 -0
  40. package/dist/auth/verify-user-mail-token.d.ts +1 -0
  41. package/dist/auth/verify-user-mail-token.js +21 -0
  42. package/dist/auth/verify-user-mail-token.js.map +1 -0
  43. package/dist/commands/cli.js +18 -27
  44. package/dist/commands/cli.js.map +1 -1
  45. package/dist/commands/make/basic-controller.js +1 -1
  46. package/dist/commands/make/basic-controller.js.map +1 -1
  47. package/dist/commands/make/basic-migration.d.ts +1 -1
  48. package/dist/commands/make/basic-migration.js +2 -2
  49. package/dist/commands/make/basic-migration.js.map +1 -1
  50. package/dist/commands/make/basic-model.js +1 -1
  51. package/dist/commands/make/basic-model.js.map +1 -1
  52. package/dist/commands/make/basic-seeder.js +1 -1
  53. package/dist/commands/make/basic-seeder.js.map +1 -1
  54. package/dist/commands/make/blueprint.js +1 -1
  55. package/dist/commands/make/blueprint.js.map +1 -1
  56. package/dist/commands/make/da-migration.js +3 -3
  57. package/dist/commands/make/da-migration.js.map +1 -1
  58. package/dist/commands/make/mail.js +2 -2
  59. package/dist/commands/make/mail.js.map +1 -1
  60. package/dist/commands/make/notification.js +1 -1
  61. package/dist/commands/make/notification.js.map +1 -1
  62. package/dist/commands/make/queue.js +1 -1
  63. package/dist/commands/make/queue.js.map +1 -1
  64. package/dist/commands/make/resource.d.ts +2 -0
  65. package/dist/commands/make/resource.js +19 -0
  66. package/dist/commands/make/resource.js.map +1 -0
  67. package/dist/commands/make/skalfa-controller.d.ts +3 -0
  68. package/dist/commands/make/{light-controller.js → skalfa-controller.js} +9 -9
  69. package/dist/commands/make/skalfa-controller.js.map +1 -0
  70. package/dist/commands/make/skalfa-model.d.ts +3 -0
  71. package/dist/commands/make/{light-model.js → skalfa-model.js} +11 -11
  72. package/dist/commands/make/skalfa-model.js.map +1 -0
  73. package/dist/commands/runner/barrels.js.map +1 -1
  74. package/dist/commands/runner/blueprint/controller-generation.js +2 -2
  75. package/dist/commands/runner/blueprint/controller-generation.js.map +1 -1
  76. package/dist/commands/runner/blueprint/documentation-generation.js.map +1 -1
  77. package/dist/commands/runner/blueprint/migration-generation.js +3 -3
  78. package/dist/commands/runner/blueprint/migration-generation.js.map +1 -1
  79. package/dist/commands/runner/blueprint/model-generation.js +2 -2
  80. package/dist/commands/runner/blueprint/model-generation.js.map +1 -1
  81. package/dist/commands/runner/blueprint/runner.js +7 -8
  82. package/dist/commands/runner/blueprint/runner.js.map +1 -1
  83. package/dist/commands/runner/blueprint/seeder-generation.js +3 -3
  84. package/dist/commands/runner/blueprint/seeder-generation.js.map +1 -1
  85. package/dist/commands/runner/da-migration.js +1 -2
  86. package/dist/commands/runner/da-migration.js.map +1 -1
  87. package/dist/commands/runner/generate-docs.d.ts +2 -0
  88. package/dist/commands/runner/generate-docs.js +400 -0
  89. package/dist/commands/runner/generate-docs.js.map +1 -0
  90. package/dist/commands/runner/migration.js +1 -1
  91. package/dist/commands/runner/migration.js.map +1 -1
  92. package/dist/commands/runner/seeder.js +1 -1
  93. package/dist/commands/runner/seeder.js.map +1 -1
  94. package/dist/commands/stubs/index.d.ts +4 -4
  95. package/dist/commands/stubs/index.js +4 -4
  96. package/dist/commands/stubs/index.js.map +1 -1
  97. package/dist/context/context.js +6 -0
  98. package/dist/context/context.js.map +1 -1
  99. package/dist/controller/controller.d.ts +17 -30
  100. package/dist/controller/controller.js +39 -121
  101. package/dist/controller/controller.js.map +1 -1
  102. package/dist/controller/response.d.ts +6 -0
  103. package/dist/controller/response.js +63 -0
  104. package/dist/controller/response.js.map +1 -0
  105. package/dist/controller/storage.d.ts +9 -0
  106. package/dist/controller/storage.js +56 -0
  107. package/dist/controller/storage.js.map +1 -0
  108. package/dist/conversion/conversion.d.ts +3 -0
  109. package/dist/conversion/conversion.js +28 -4
  110. package/dist/conversion/conversion.js.map +1 -1
  111. package/dist/conversion/date.d.ts +1 -0
  112. package/dist/conversion/date.js +77 -0
  113. package/dist/conversion/date.js.map +1 -0
  114. package/dist/index.d.ts +2 -0
  115. package/dist/index.js +2 -0
  116. package/dist/index.js.map +1 -1
  117. package/dist/logger/logger.js +33 -0
  118. package/dist/logger/logger.js.map +1 -1
  119. package/dist/mail/mail.js +6 -6
  120. package/dist/mail/mail.js.map +1 -1
  121. package/dist/middleware/access-log.d.ts +31 -0
  122. package/dist/middleware/access-log.js +13 -0
  123. package/dist/middleware/access-log.js.map +1 -0
  124. package/dist/middleware/auth.d.ts +37 -0
  125. package/dist/middleware/auth.js +16 -0
  126. package/dist/middleware/auth.js.map +1 -0
  127. package/dist/middleware/body-parse.d.ts +35 -0
  128. package/dist/middleware/body-parse.js +87 -0
  129. package/dist/middleware/body-parse.js.map +1 -0
  130. package/dist/middleware/context.d.ts +29 -0
  131. package/dist/middleware/context.js +8 -0
  132. package/dist/middleware/context.js.map +1 -0
  133. package/dist/middleware/cors.d.ts +31 -0
  134. package/dist/middleware/cors.js +27 -0
  135. package/dist/middleware/cors.js.map +1 -0
  136. package/dist/middleware/error-handler.d.ts +33 -0
  137. package/dist/middleware/error-handler.js +17 -0
  138. package/dist/middleware/error-handler.js.map +1 -0
  139. package/dist/middleware/middleware.d.ts +31 -10
  140. package/dist/middleware/middleware.js +41 -209
  141. package/dist/middleware/middleware.js.map +1 -1
  142. package/dist/middleware/private.d.ts +29 -0
  143. package/dist/middleware/private.js +8 -0
  144. package/dist/middleware/private.js.map +1 -0
  145. package/dist/middleware/rate-limiter.d.ts +32 -0
  146. package/dist/middleware/rate-limiter.js +30 -0
  147. package/dist/middleware/rate-limiter.js.map +1 -0
  148. package/dist/notification/index.d.ts +1 -0
  149. package/dist/notification/index.js +2 -0
  150. package/dist/notification/index.js.map +1 -0
  151. package/dist/notification/notification.d.ts +16 -0
  152. package/dist/notification/notification.js +64 -0
  153. package/dist/notification/notification.js.map +1 -0
  154. package/dist/permission/permission.js +9 -0
  155. package/dist/permission/permission.js.map +1 -1
  156. package/dist/registry/registry.d.ts +0 -6
  157. package/dist/registry/registry.js +6 -6
  158. package/dist/registry/registry.js.map +1 -1
  159. package/dist/storage/storage.d.ts +3 -3
  160. package/dist/storage/storage.js.map +1 -1
  161. package/dist/validation/validation.js +43 -51
  162. package/dist/validation/validation.js.map +1 -1
  163. package/package.json +4 -4
  164. package/src/auth/auth.ts +21 -252
  165. package/src/auth/create-access-token.ts +29 -0
  166. package/src/auth/create-user-mail-token.ts +24 -0
  167. package/src/auth/helpers/generate-agent-id.ts +8 -0
  168. package/src/auth/helpers/get-request-ip.ts +3 -0
  169. package/src/auth/helpers/get-user-permissions.ts +15 -0
  170. package/src/auth/helpers/index.ts +3 -0
  171. package/src/auth/list-user-sessions.ts +11 -0
  172. package/src/auth/revalidate-user-permissions-by-role.ts +13 -0
  173. package/src/auth/revalidate-user-permissions.ts +26 -0
  174. package/src/auth/revoke-access-token.ts +5 -0
  175. package/src/auth/verify-access-token.ts +56 -0
  176. package/src/auth/verify-user-mail-token.ts +24 -0
  177. package/src/commands/cli.ts +19 -29
  178. package/src/commands/make/basic-controller.ts +4 -2
  179. package/src/commands/make/basic-migration.ts +5 -3
  180. package/src/commands/make/basic-model.ts +3 -1
  181. package/src/commands/make/basic-seeder.ts +3 -1
  182. package/src/commands/make/blueprint.ts +3 -1
  183. package/src/commands/make/da-migration.ts +6 -5
  184. package/src/commands/make/mail.ts +4 -2
  185. package/src/commands/make/notification.ts +3 -1
  186. package/src/commands/make/queue.ts +3 -1
  187. package/src/commands/make/resource.ts +21 -0
  188. package/src/commands/make/{light-controller.ts → skalfa-controller.ts} +10 -8
  189. package/src/commands/make/{light-model.ts → skalfa-model.ts} +12 -10
  190. package/src/commands/runner/barrels.ts +4 -0
  191. package/src/commands/runner/blueprint/controller-generation.ts +4 -2
  192. package/src/commands/runner/blueprint/documentation-generation.ts +2 -0
  193. package/src/commands/runner/blueprint/migration-generation.ts +5 -3
  194. package/src/commands/runner/blueprint/model-generation.ts +4 -2
  195. package/src/commands/runner/blueprint/runner.ts +15 -8
  196. package/src/commands/runner/blueprint/seeder-generation.ts +5 -3
  197. package/src/commands/runner/da-migration.ts +3 -2
  198. package/src/commands/runner/generate-docs.ts +495 -0
  199. package/src/commands/runner/migration.ts +1 -1
  200. package/src/commands/runner/seeder.ts +1 -1
  201. package/src/commands/stubs/index.ts +4 -4
  202. package/src/context/context.ts +23 -17
  203. package/src/controller/controller.ts +124 -239
  204. package/src/controller/response.ts +78 -0
  205. package/src/controller/storage.ts +78 -0
  206. package/src/conversion/conversion.ts +90 -64
  207. package/src/conversion/date.ts +74 -0
  208. package/src/index.ts +2 -0
  209. package/src/logger/logger.ts +217 -176
  210. package/src/mail/mail.ts +85 -85
  211. package/src/middleware/access-log.ts +15 -0
  212. package/src/middleware/auth.ts +19 -0
  213. package/src/middleware/body-parse.ts +83 -0
  214. package/src/middleware/context.ts +11 -0
  215. package/src/middleware/cors.ts +31 -0
  216. package/src/middleware/error-handler.ts +20 -0
  217. package/src/middleware/middleware.ts +91 -288
  218. package/src/middleware/private.ts +8 -0
  219. package/src/middleware/rate-limiter.ts +41 -0
  220. package/src/notification/index.ts +1 -0
  221. package/src/notification/notification.ts +86 -0
  222. package/src/permission/permission.ts +140 -136
  223. package/src/registry/registry.ts +17 -15
  224. package/src/route/route.ts +11 -11
  225. package/src/storage/storage.ts +104 -106
  226. package/src/validation/validation.ts +322 -346
  227. package/dist/auth.util.d.ts +0 -19
  228. package/dist/auth.util.js +0 -183
  229. package/dist/auth.util.js.map +0 -1
  230. package/dist/commands/make/light-controller.d.ts +0 -3
  231. package/dist/commands/make/light-controller.js.map +0 -1
  232. package/dist/commands/make/light-model.d.ts +0 -3
  233. package/dist/commands/make/light-model.js.map +0 -1
  234. package/dist/context.util.d.ts +0 -7
  235. package/dist/context.util.js +0 -11
  236. package/dist/context.util.js.map +0 -1
  237. package/dist/controller.util.d.ts +0 -118
  238. package/dist/controller.util.js +0 -144
  239. package/dist/controller.util.js.map +0 -1
  240. package/dist/conversion.util.d.ts +0 -8
  241. package/dist/conversion.util.js +0 -52
  242. package/dist/conversion.util.js.map +0 -1
  243. package/dist/db/db.d.ts +0 -84
  244. package/dist/db/db.js +0 -177
  245. package/dist/db/db.js.map +0 -1
  246. package/dist/db/index.d.ts +0 -1
  247. package/dist/db/index.js +0 -2
  248. package/dist/db/index.js.map +0 -1
  249. package/dist/db.util.d.ts +0 -84
  250. package/dist/db.util.js +0 -177
  251. package/dist/db.util.js.map +0 -1
  252. package/dist/logger.util.d.ts +0 -30
  253. package/dist/logger.util.js +0 -126
  254. package/dist/logger.util.js.map +0 -1
  255. package/dist/mail.util.d.ts +0 -21
  256. package/dist/mail.util.js +0 -53
  257. package/dist/mail.util.js.map +0 -1
  258. package/dist/middleware.util.d.ts +0 -263
  259. package/dist/middleware.util.js +0 -233
  260. package/dist/middleware.util.js.map +0 -1
  261. package/dist/model/index.d.ts +0 -3
  262. package/dist/model/index.js +0 -4
  263. package/dist/model/index.js.map +0 -1
  264. package/dist/model/model.d.ts +0 -204
  265. package/dist/model/model.js +0 -1495
  266. package/dist/model/model.js.map +0 -1
  267. package/dist/model.util.d.ts +0 -204
  268. package/dist/model.util.js +0 -1495
  269. package/dist/model.util.js.map +0 -1
  270. package/dist/permission.util.d.ts +0 -38
  271. package/dist/permission.util.js +0 -91
  272. package/dist/permission.util.js.map +0 -1
  273. package/dist/registry.util.d.ts +0 -28
  274. package/dist/registry.util.js +0 -19
  275. package/dist/registry.util.js.map +0 -1
  276. package/dist/route.util.d.ts +0 -1
  277. package/dist/route.util.js +0 -12
  278. package/dist/route.util.js.map +0 -1
  279. package/dist/storage.util.d.ts +0 -56
  280. package/dist/storage.util.js +0 -82
  281. package/dist/storage.util.js.map +0 -1
  282. package/dist/validation.util.d.ts +0 -7
  283. package/dist/validation.util.js +0 -237
  284. package/dist/validation.util.js.map +0 -1
@@ -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;IACb,YAAY,EAAE;QACZ,MAAM,EAAE,GAAG;QACX,OAAO,EAAE,eAAe;KACzB;IACD,WAA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ACf,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"}
@@ -1,3 +0,0 @@
1
- export declare const Model: any;
2
- export declare const softDelete: any;
3
- export declare const foreignIdFor: any;
@@ -1,4 +0,0 @@
1
- export const Model = null;
2
- export const softDelete = null;
3
- export const foreignIdFor = null;
4
- //# sourceMappingURL=index.js.map
@@ -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"}
@@ -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 {};