@appweaver/core 1.0.0

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 (271) hide show
  1. package/LICENSE +1 -0
  2. package/README.md +7 -0
  3. package/app/application.d.ts +43 -0
  4. package/app/application.js +112 -0
  5. package/app/create-app.d.ts +32 -0
  6. package/app/create-app.js +38 -0
  7. package/app/index.d.ts +2 -0
  8. package/app/index.js +18 -0
  9. package/app/lifecycle-manager.d.ts +20 -0
  10. package/app/lifecycle-manager.js +42 -0
  11. package/app/load-modules.d.ts +10 -0
  12. package/app/load-modules.js +50 -0
  13. package/app/load-providers.d.ts +8 -0
  14. package/app/load-providers.js +33 -0
  15. package/cache/cache-service.d.ts +72 -0
  16. package/cache/cache-service.js +202 -0
  17. package/cache/cache.d.ts +12 -0
  18. package/cache/cache.js +184 -0
  19. package/cache/caching.d.ts +8 -0
  20. package/cache/caching.js +72 -0
  21. package/cache/eviction/eviction-index.d.ts +35 -0
  22. package/cache/eviction/eviction-index.js +2 -0
  23. package/cache/eviction/fifo-eviction-index.d.ts +7 -0
  24. package/cache/eviction/fifo-eviction-index.js +28 -0
  25. package/cache/eviction/index.d.ts +4 -0
  26. package/cache/eviction/index.js +20 -0
  27. package/cache/eviction/lfu-eviction-index.d.ts +8 -0
  28. package/cache/eviction/lfu-eviction-index.js +82 -0
  29. package/cache/eviction/lru-eviction-index.d.ts +8 -0
  30. package/cache/eviction/lru-eviction-index.js +81 -0
  31. package/cache/index.d.ts +2 -0
  32. package/cache/index.js +18 -0
  33. package/cache/memory-cache.d.ts +4 -0
  34. package/cache/memory-cache.js +12 -0
  35. package/cache/redis-cache.d.ts +4 -0
  36. package/cache/redis-cache.js +12 -0
  37. package/context/context.d.ts +2 -0
  38. package/context/context.js +13 -0
  39. package/context/dependency-injection.d.ts +98 -0
  40. package/context/dependency-injection.js +276 -0
  41. package/context/index.d.ts +2 -0
  42. package/context/index.js +18 -0
  43. package/database/create-client.d.ts +11 -0
  44. package/database/create-client.js +142 -0
  45. package/database/index.d.ts +1 -0
  46. package/database/index.js +17 -0
  47. package/database/prisma-database.d.ts +15 -0
  48. package/database/prisma-database.js +53 -0
  49. package/errors/error-handler.d.ts +3 -0
  50. package/errors/error-handler.js +24 -0
  51. package/errors/error-schema.d.ts +32 -0
  52. package/errors/error-schema.js +31 -0
  53. package/errors/http-error.d.ts +21 -0
  54. package/errors/http-error.js +33 -0
  55. package/errors/index.d.ts +3 -0
  56. package/errors/index.js +19 -0
  57. package/events/node-events.d.ts +7 -0
  58. package/events/node-events.js +39 -0
  59. package/export/export-service.d.ts +19 -0
  60. package/export/export-service.js +154 -0
  61. package/export/index.d.ts +1 -0
  62. package/export/index.js +17 -0
  63. package/factory/create-model.d.ts +2 -0
  64. package/factory/create-model.js +366 -0
  65. package/factory/create-policy.d.ts +2 -0
  66. package/factory/create-policy.js +16 -0
  67. package/factory/create-routes.d.ts +2 -0
  68. package/factory/create-routes.js +21 -0
  69. package/factory/create-service.d.ts +3 -0
  70. package/factory/create-service.js +97 -0
  71. package/factory/index.d.ts +4 -0
  72. package/factory/index.js +20 -0
  73. package/health/health-routes.d.ts +2 -0
  74. package/health/health-routes.js +36 -0
  75. package/health/health-schema.d.ts +45 -0
  76. package/health/health-schema.js +52 -0
  77. package/health/health-service.d.ts +38 -0
  78. package/health/health-service.js +82 -0
  79. package/health/index.d.ts +2 -0
  80. package/health/index.js +18 -0
  81. package/index.d.ts +15 -0
  82. package/index.js +31 -0
  83. package/mailer/email-service.d.ts +36 -0
  84. package/mailer/email-service.js +89 -0
  85. package/mailer/index.d.ts +1 -0
  86. package/mailer/index.js +17 -0
  87. package/mailer/json-mailer.d.ts +7 -0
  88. package/mailer/json-mailer.js +32 -0
  89. package/mailer/smtp-mailer.d.ts +7 -0
  90. package/mailer/smtp-mailer.js +43 -0
  91. package/memory/in-memory.d.ts +24 -0
  92. package/memory/in-memory.js +164 -0
  93. package/memory/redis.d.ts +26 -0
  94. package/memory/redis.js +158 -0
  95. package/package.json +68 -0
  96. package/prisma/client/browser.d.ts +34 -0
  97. package/prisma/client/browser.js +56 -0
  98. package/prisma/client/client.d.ts +53 -0
  99. package/prisma/client/client.js +70 -0
  100. package/prisma/client/commonInputTypes.d.ts +395 -0
  101. package/prisma/client/commonInputTypes.js +11 -0
  102. package/prisma/client/enums.d.ts +1 -0
  103. package/prisma/client/enums.js +11 -0
  104. package/prisma/client/internal/class.d.ts +187 -0
  105. package/prisma/client/internal/class.js +85 -0
  106. package/prisma/client/internal/prismaNamespace.d.ts +987 -0
  107. package/prisma/client/internal/prismaNamespace.js +193 -0
  108. package/prisma/client/internal/prismaNamespaceBrowser.d.ts +125 -0
  109. package/prisma/client/internal/prismaNamespaceBrowser.js +164 -0
  110. package/prisma/client/models/ApiKey.d.ts +1167 -0
  111. package/prisma/client/models/ApiKey.js +2 -0
  112. package/prisma/client/models/File.d.ts +1248 -0
  113. package/prisma/client/models/File.js +2 -0
  114. package/prisma/client/models/OneTimeToken.d.ts +1083 -0
  115. package/prisma/client/models/OneTimeToken.js +2 -0
  116. package/prisma/client/models/Permission.d.ts +1209 -0
  117. package/prisma/client/models/Permission.js +2 -0
  118. package/prisma/client/models/Role.d.ts +1209 -0
  119. package/prisma/client/models/Role.js +2 -0
  120. package/prisma/client/models/Seeder.d.ts +1023 -0
  121. package/prisma/client/models/Seeder.js +2 -0
  122. package/prisma/client/models.d.ts +7 -0
  123. package/prisma/client/models.js +2 -0
  124. package/queue/bull-queue.d.ts +37 -0
  125. package/queue/bull-queue.js +207 -0
  126. package/queue/memory-queue.d.ts +54 -0
  127. package/queue/memory-queue.js +283 -0
  128. package/resource/index.d.ts +4 -0
  129. package/resource/index.js +20 -0
  130. package/resource/resource-loader.d.ts +26 -0
  131. package/resource/resource-loader.js +161 -0
  132. package/resource/resource-routes.d.ts +3 -0
  133. package/resource/resource-routes.js +174 -0
  134. package/resource/resource-schema.d.ts +33 -0
  135. package/resource/resource-schema.js +186 -0
  136. package/resource/resource-service.d.ts +89 -0
  137. package/resource/resource-service.js +772 -0
  138. package/resources.d.ts +9 -0
  139. package/resources.js +24 -0
  140. package/scheduler/cron-scheduler.d.ts +12 -0
  141. package/scheduler/cron-scheduler.js +73 -0
  142. package/security/account/account-routes.d.ts +2 -0
  143. package/security/account/account-routes.js +104 -0
  144. package/security/account/account-schema.d.ts +290 -0
  145. package/security/account/account-schema.js +166 -0
  146. package/security/account/account-service.d.ts +88 -0
  147. package/security/account/account-service.js +218 -0
  148. package/security/account/index.d.ts +3 -0
  149. package/security/account/index.js +19 -0
  150. package/security/api-key/api-key-auth.d.ts +4 -0
  151. package/security/api-key/api-key-auth.js +66 -0
  152. package/security/api-key/index.d.ts +1 -0
  153. package/security/api-key/index.js +17 -0
  154. package/security/auth-routes.d.ts +2 -0
  155. package/security/auth-routes.js +80 -0
  156. package/security/auth-schema.d.ts +215 -0
  157. package/security/auth-schema.js +129 -0
  158. package/security/auth-service.d.ts +117 -0
  159. package/security/auth-service.js +272 -0
  160. package/security/auth.d.ts +3 -0
  161. package/security/auth.js +127 -0
  162. package/security/basic/basic-auth.d.ts +4 -0
  163. package/security/basic/basic-auth.js +32 -0
  164. package/security/basic/index.d.ts +1 -0
  165. package/security/basic/index.js +17 -0
  166. package/security/create-auth-resources.d.ts +6 -0
  167. package/security/create-auth-resources.js +129 -0
  168. package/security/helper.d.ts +127 -0
  169. package/security/helper.js +329 -0
  170. package/security/index.d.ts +4 -0
  171. package/security/index.js +20 -0
  172. package/security/jwt/index.d.ts +2 -0
  173. package/security/jwt/index.js +18 -0
  174. package/security/jwt/jwt-auth.d.ts +4 -0
  175. package/security/jwt/jwt-auth.js +57 -0
  176. package/security/jwt/jwt-keys.d.ts +7 -0
  177. package/security/jwt/jwt-keys.js +43 -0
  178. package/security/oauth2/create-oauth2-plugin.d.ts +17 -0
  179. package/security/oauth2/create-oauth2-plugin.js +85 -0
  180. package/security/oauth2/index.d.ts +3 -0
  181. package/security/oauth2/index.js +19 -0
  182. package/security/oauth2/oauth2-custom.d.ts +7 -0
  183. package/security/oauth2/oauth2-custom.js +40 -0
  184. package/security/oauth2/oauth2-facebook.d.ts +1 -0
  185. package/security/oauth2/oauth2-facebook.js +30 -0
  186. package/security/oauth2/oauth2-google.d.ts +1 -0
  187. package/security/oauth2/oauth2-google.js +28 -0
  188. package/security/oauth2/oauth2-schema.d.ts +10 -0
  189. package/security/oauth2/oauth2-schema.js +53 -0
  190. package/security/recaptcha/index.d.ts +2 -0
  191. package/security/recaptcha/index.js +18 -0
  192. package/security/recaptcha/recaptcha-verify.d.ts +1 -0
  193. package/security/recaptcha/recaptcha-verify.js +33 -0
  194. package/security/recaptcha/recaptcha.d.ts +2 -0
  195. package/security/recaptcha/recaptcha.js +22 -0
  196. package/security/resources/api-key/model.d.ts +2 -0
  197. package/security/resources/api-key/model.js +63 -0
  198. package/security/resources/api-key/policy.d.ts +2 -0
  199. package/security/resources/api-key/policy.js +26 -0
  200. package/security/resources/api-key/routes.d.ts +2 -0
  201. package/security/resources/api-key/routes.js +15 -0
  202. package/security/resources/api-key/service.d.ts +3 -0
  203. package/security/resources/api-key/service.js +33 -0
  204. package/security/resources/one-time-token/model.d.ts +2 -0
  205. package/security/resources/one-time-token/model.js +32 -0
  206. package/security/resources/permission/model.d.ts +2 -0
  207. package/security/resources/permission/model.js +16 -0
  208. package/security/resources/role/model.d.ts +2 -0
  209. package/security/resources/role/model.js +22 -0
  210. package/security/store/database-security-store.d.ts +5 -0
  211. package/security/store/database-security-store.js +51 -0
  212. package/security/store/redis-security-store.d.ts +5 -0
  213. package/security/store/redis-security-store.js +32 -0
  214. package/seeder/create-seeder.d.ts +21 -0
  215. package/seeder/create-seeder.js +17 -0
  216. package/seeder/index.d.ts +2 -0
  217. package/seeder/index.js +18 -0
  218. package/seeder/resources/seeder/model.d.ts +2 -0
  219. package/seeder/resources/seeder/model.js +38 -0
  220. package/seeder/seeder.d.ts +20 -0
  221. package/seeder/seeder.js +175 -0
  222. package/server/create-server.d.ts +20 -0
  223. package/server/create-server.js +160 -0
  224. package/server/index.d.ts +4 -0
  225. package/server/index.js +20 -0
  226. package/server/info-route.d.ts +2 -0
  227. package/server/info-route.js +18 -0
  228. package/server/info-schema.d.ts +37 -0
  229. package/server/info-schema.js +19 -0
  230. package/server/register-model.d.ts +2 -0
  231. package/server/register-model.js +12 -0
  232. package/server/register-plugin.d.ts +2 -0
  233. package/server/register-plugin.js +13 -0
  234. package/server/register-route.d.ts +3 -0
  235. package/server/register-route.js +74 -0
  236. package/server/schemas.d.ts +3 -0
  237. package/server/schemas.js +30 -0
  238. package/server/swagger.d.ts +3 -0
  239. package/server/swagger.js +122 -0
  240. package/storage/file-route.d.ts +2 -0
  241. package/storage/file-route.js +39 -0
  242. package/storage/file-schema.d.ts +9 -0
  243. package/storage/file-schema.js +42 -0
  244. package/storage/file-service.d.ts +81 -0
  245. package/storage/file-service.js +384 -0
  246. package/storage/filesystem-storage.d.ts +10 -0
  247. package/storage/filesystem-storage.js +128 -0
  248. package/storage/index.d.ts +2 -0
  249. package/storage/index.js +18 -0
  250. package/storage/resources/file/model.d.ts +2 -0
  251. package/storage/resources/file/model.js +75 -0
  252. package/types/application-context.d.ts +20 -0
  253. package/types/application-context.js +2 -0
  254. package/types/auth.d.ts +31 -0
  255. package/types/auth.js +2 -0
  256. package/types/fastify-extensions.d.ts +24 -0
  257. package/types/fastify-extensions.js +4 -0
  258. package/types/generated.d.ts +193 -0
  259. package/types/generated.js +3 -0
  260. package/types/index.d.ts +4 -0
  261. package/types/index.js +20 -0
  262. package/types/server.d.ts +4 -0
  263. package/types/server.js +2 -0
  264. package/utils/export-util.d.ts +8 -0
  265. package/utils/export-util.js +25 -0
  266. package/utils/file-util.d.ts +64 -0
  267. package/utils/file-util.js +141 -0
  268. package/utils/index.d.ts +3 -0
  269. package/utils/index.js +19 -0
  270. package/utils/schema-util.d.ts +20 -0
  271. package/utils/schema-util.js +37 -0
@@ -0,0 +1,24 @@
1
+ import { AuthSource, AuthType, AuthUser, RouteConfig } from '@appweaver/common';
2
+ import { ApiKey } from './generated';
3
+ import '@fastify/request-context';
4
+ import '@fastify/auth';
5
+ declare module 'fastify' {
6
+ interface FastifyContextConfig extends RouteConfig {
7
+ }
8
+ interface FastifyInstance {
9
+ recaptcha: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
10
+ authenticateJWT: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
11
+ authenticateApiKey: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
12
+ authenticate: (authTypes?: AuthType[]) => any;
13
+ currentUser: () => AuthUser;
14
+ }
15
+ }
16
+ declare module '@fastify/request-context' {
17
+ interface RequestContextData {
18
+ authUser: AuthUser | null;
19
+ authType: AuthType | null;
20
+ authSource: AuthSource | null;
21
+ apiKey: ApiKey | null;
22
+ cached?: boolean;
23
+ }
24
+ }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("@fastify/request-context");
4
+ require("@fastify/auth");
@@ -0,0 +1,193 @@
1
+ export type ApiKey = {
2
+ id: number;
3
+ key: string;
4
+ keyHash: string;
5
+ name?: string | null;
6
+ description?: string | null;
7
+ enabled: boolean;
8
+ expiresAt?: Date | null;
9
+ updatedAt: Date;
10
+ createdAt: Date;
11
+ createdById?: number | null;
12
+ };
13
+ export type ApiKeySingle = {
14
+ id: number;
15
+ key: string;
16
+ name?: string | null;
17
+ description?: string | null;
18
+ enabled: boolean;
19
+ expiresAt?: Date | null;
20
+ updatedAt: Date;
21
+ createdAt: Date;
22
+ createdById?: number | null;
23
+ };
24
+ export type ApiKeyMultiple = {
25
+ id: number;
26
+ key: string;
27
+ name?: string | null;
28
+ description?: string | null;
29
+ enabled: boolean;
30
+ expiresAt?: Date | null;
31
+ updatedAt: Date;
32
+ createdAt: Date;
33
+ createdById?: number | null;
34
+ };
35
+ export type ApiKeyCreate = {
36
+ name?: string | null;
37
+ description?: string | null;
38
+ enabled?: boolean;
39
+ expiresAt?: Date | null;
40
+ };
41
+ export type ApiKeyUpdate = {
42
+ name?: string | null;
43
+ description?: string | null;
44
+ enabled?: boolean;
45
+ };
46
+ export type OneTimeToken = {
47
+ id: number;
48
+ tokenHash: string;
49
+ purpose: string;
50
+ expiresAt: Date;
51
+ data: any;
52
+ updatedAt: Date;
53
+ createdAt: Date;
54
+ };
55
+ export type OneTimeTokenSingle = {
56
+ id: number;
57
+ tokenHash: string;
58
+ purpose: string;
59
+ expiresAt: Date;
60
+ data: any;
61
+ updatedAt: Date;
62
+ createdAt: Date;
63
+ };
64
+ export type OneTimeTokenMultiple = {
65
+ id: number;
66
+ tokenHash: string;
67
+ purpose: string;
68
+ expiresAt: Date;
69
+ data: any;
70
+ updatedAt: Date;
71
+ createdAt: Date;
72
+ };
73
+ export type OneTimeTokenCreate = {
74
+ tokenHash: string;
75
+ purpose: string;
76
+ expiresAt: Date;
77
+ data: any;
78
+ };
79
+ export type OneTimeTokenUpdate = {
80
+ tokenHash?: string;
81
+ purpose?: string;
82
+ expiresAt?: Date;
83
+ data?: any;
84
+ };
85
+ export type Permission = {
86
+ id: number;
87
+ name: string;
88
+ updatedAt: Date;
89
+ createdAt: Date;
90
+ createdById?: number | null;
91
+ };
92
+ export type PermissionSingle = {
93
+ id: number;
94
+ name: string;
95
+ };
96
+ export type PermissionMultiple = {
97
+ id: number;
98
+ name: string;
99
+ };
100
+ export type PermissionCreate = {
101
+ name: string;
102
+ };
103
+ export type PermissionUpdate = {
104
+ name?: string;
105
+ };
106
+ export type Role = {
107
+ id: number;
108
+ name: string;
109
+ permissions: Array<PermissionSingle>;
110
+ updatedAt: Date;
111
+ createdAt: Date;
112
+ createdById?: number | null;
113
+ };
114
+ export type RoleSingle = {
115
+ id: number;
116
+ name: string;
117
+ permissions: Array<PermissionSingle>;
118
+ };
119
+ export type RoleMultiple = {
120
+ id: number;
121
+ name: string;
122
+ permissions: Array<PermissionSingle>;
123
+ };
124
+ export type RoleCreate = {
125
+ name: string;
126
+ permissions: Array<{
127
+ id: number;
128
+ }> | Array<number>;
129
+ };
130
+ export type RoleUpdate = {
131
+ name?: string;
132
+ permissions?: Array<{
133
+ id: number;
134
+ }> | Array<number>;
135
+ };
136
+ export type File = {
137
+ id: number;
138
+ name: string;
139
+ originalName: string;
140
+ mimeType: string;
141
+ sizeBytes: number;
142
+ title?: string | null;
143
+ description?: string | null;
144
+ resourceField?: string | null;
145
+ resourceName?: string | null;
146
+ resourceId?: number | null;
147
+ url: string;
148
+ updatedAt: Date;
149
+ createdAt: Date;
150
+ createdById?: number | null;
151
+ };
152
+ export type FileSingle = {
153
+ id: number;
154
+ name: string;
155
+ originalName: string;
156
+ mimeType: string;
157
+ sizeBytes: number;
158
+ title?: string | null;
159
+ description?: string | null;
160
+ url: string;
161
+ updatedAt: Date;
162
+ createdAt: Date;
163
+ createdById?: number | null;
164
+ };
165
+ export type FileMultiple = {
166
+ id: number;
167
+ name: string;
168
+ originalName: string;
169
+ mimeType: string;
170
+ sizeBytes: number;
171
+ title?: string | null;
172
+ description?: string | null;
173
+ url: string;
174
+ updatedAt: Date;
175
+ createdAt: Date;
176
+ createdById?: number | null;
177
+ };
178
+ export type FileCreate = {
179
+ name: string;
180
+ originalName: string;
181
+ mimeType: string;
182
+ sizeBytes: number;
183
+ title?: string | null;
184
+ description?: string | null;
185
+ };
186
+ export type FileUpdate = {
187
+ name?: string;
188
+ originalName?: string;
189
+ mimeType?: string;
190
+ sizeBytes?: number;
191
+ title?: string | null;
192
+ description?: string | null;
193
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // Generated by Appweaver. Please do not edit this file manually.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export * from './application-context';
2
+ export * from './auth';
3
+ export * from './generated';
4
+ export * from './server';
package/types/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./application-context"), exports);
18
+ __exportStar(require("./auth"), exports);
19
+ __exportStar(require("./generated"), exports);
20
+ __exportStar(require("./server"), exports);
@@ -0,0 +1,4 @@
1
+ import { FastifyBaseLogger, FastifyInstance, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault } from 'fastify';
2
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
3
+ export type Server = FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, FastifyBaseLogger, TypeBoxTypeProvider>;
4
+ export type Router = Pick<Server, 'route' | 'get' | 'post' | 'patch' | 'put' | 'delete' | 'options' | 'head'>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Converts an array of data into a CSV-formatted string.
3
+ *
4
+ * @param {Object[]} data - The array of data to be converted into CSV format.
5
+ * @param {boolean} [header=false] - Whether to include a header row in the generated CSV.
6
+ * @return {string} The CSV-formatted string representation of the input data.
7
+ */
8
+ export declare function toCsv(data: any[], header?: boolean): string;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toCsv = toCsv;
4
+ const sync_1 = require("csv-stringify/sync");
5
+ const common_1 = require("@appweaver/common");
6
+ /**
7
+ * Converts an array of data into a CSV-formatted string.
8
+ *
9
+ * @param {Object[]} data - The array of data to be converted into CSV format.
10
+ * @param {boolean} [header=false] - Whether to include a header row in the generated CSV.
11
+ * @return {string} The CSV-formatted string representation of the input data.
12
+ */
13
+ function toCsv(data, header = false) {
14
+ return (0, sync_1.stringify)(Object.values(data), {
15
+ delimiter: common_1.config.EXPORT_CSV_DELIMITER,
16
+ cast: {
17
+ date: (value) => value.toISOString(),
18
+ object: (value) => (0, common_1.isArray)(value) &&
19
+ ((0, common_1.isString)(value) || (0, common_1.isNumber)(value) || (0, common_1.isBoolean)(value))
20
+ ? value.join(common_1.config.EXPORT_CSV_JOIN_DELIMITER)
21
+ : JSON.stringify(value)
22
+ },
23
+ header
24
+ });
25
+ }
@@ -0,0 +1,64 @@
1
+ import { FilesConfig } from '@appweaver/common';
2
+ import { File } from '../types';
3
+ /**
4
+ * Parses a range string and converts it into an object with start and end values.
5
+ *
6
+ * @param {string} [range] - The range string to parse, typically in the format "bytes=start-end".
7
+ * @return {{start: number, end?: number}} An object containing the start value and an optional end value.
8
+ */
9
+ export declare function parseRange(range?: string): {
10
+ start: number;
11
+ end?: number;
12
+ };
13
+ /**
14
+ * Calculates the maximum file size allowed based on the provided file configuration.
15
+ *
16
+ * @param {FilesConfig} fileConfig - An object containing file configuration data, including maximum size for each file
17
+ * type.
18
+ * @return {number} The maximum size in bytes allowed for any file based on the given configuration.
19
+ */
20
+ export declare function maxFileSize(fileConfig: FilesConfig): number;
21
+ /**
22
+ * Converts a given size represented as a string or number into its equivalent size in bytes.
23
+ *
24
+ * @param {string | number} [size] - The size to be converted, either as a string or a number.
25
+ * @return {number} The size in bytes. Returns 0 if the input is invalid or not provided.
26
+ */
27
+ export declare function sizeInBytes(size?: string | number): number;
28
+ /**
29
+ * Validates if the provided MIME type matches a given pattern or regular expression.
30
+ *
31
+ * @param {string} mimeType - The MIME type to validate.
32
+ * @param {string|RegExp} [mimeTypeExp] - An optional pattern or regular expression to match the MIME type against.
33
+ * If not provided, the method will always return true.
34
+ * @return {boolean} True if the MIME type matches the specified pattern or regular expression, false otherwise.
35
+ */
36
+ export declare function isValidMimeType(mimeType: string, mimeTypeExp?: string | RegExp): boolean;
37
+ /**
38
+ * Generates a file name based on a given name, an optional pattern, and a set of variables.
39
+ *
40
+ * @param {string} name The original file name including its extension.
41
+ * @param {string} [pattern] An optional pattern to customize the generated file name. If not provided, a default
42
+ * pattern will be used.
43
+ * @param {Record<string, any>} [variables={}] Additional variables to replace in the pattern. These can include custom
44
+ * placeholders to dynamically generate the file name.
45
+ * @return {string} The generated, sanitized file name based on the provided or default pattern.
46
+ */
47
+ export declare function generateFileName(name: string, pattern?: string, variables?: Record<string, any>): string;
48
+ /**
49
+ * Sanitizes a given filename by removing invalid characters that are not allowed in file systems.
50
+ *
51
+ * @param fileName The original filename to sanitize.
52
+ * @return A sanitized filename with invalid characters removed.
53
+ */
54
+ export declare function sanitizeFilename(fileName: string): string;
55
+ /**
56
+ * Aggregates the provided files into a structured object based on the given configuration.
57
+ *
58
+ * @param {File[]} files - An array of file objects to be processed and aggregated.
59
+ * @param {FilesConfig} filesConfig - An object that defines the configuration for each file field, including whether
60
+ * the field supports multiple files.
61
+ * @return {Record<string, File | File[] | null>} A record where keys correspond to the configured fields, and values
62
+ * are either a single file, an array of files, or null if no files match the field.
63
+ */
64
+ export declare function aggregateFiles(files: File[], filesConfig: FilesConfig): Record<string, File | File[] | null>;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRange = parseRange;
4
+ exports.maxFileSize = maxFileSize;
5
+ exports.sizeInBytes = sizeInBytes;
6
+ exports.isValidMimeType = isValidMimeType;
7
+ exports.generateFileName = generateFileName;
8
+ exports.sanitizeFilename = sanitizeFilename;
9
+ exports.aggregateFiles = aggregateFiles;
10
+ const common_1 = require("@appweaver/common");
11
+ /**
12
+ * Parses a range string and converts it into an object with start and end values.
13
+ *
14
+ * @param {string} [range] - The range string to parse, typically in the format "bytes=start-end".
15
+ * @return {{start: number, end?: number}} An object containing the start value and an optional end value.
16
+ */
17
+ function parseRange(range) {
18
+ if (!range) {
19
+ return { start: 0 };
20
+ }
21
+ const parts = range.replace(/bytes=/, '').split('-');
22
+ const start = parseInt(parts[0], 10);
23
+ const end = parts[1] ? parseInt(parts[1], 10) : undefined;
24
+ return { start, end };
25
+ }
26
+ /**
27
+ * Calculates the maximum file size allowed based on the provided file configuration.
28
+ *
29
+ * @param {FilesConfig} fileConfig - An object containing file configuration data, including maximum size for each file
30
+ * type.
31
+ * @return {number} The maximum size in bytes allowed for any file based on the given configuration.
32
+ */
33
+ function maxFileSize(fileConfig) {
34
+ const sizes = Object.values(fileConfig)
35
+ .map((conf) => sizeInBytes(conf.maxSize))
36
+ .filter((v) => v > 0);
37
+ if (!sizes.length) {
38
+ return (0, common_1.textToBytes)(common_1.config.SERVER_BODY_MAX_SIZE);
39
+ }
40
+ return Math.max(...sizes);
41
+ }
42
+ /**
43
+ * Converts a given size represented as a string or number into its equivalent size in bytes.
44
+ *
45
+ * @param {string | number} [size] - The size to be converted, either as a string or a number.
46
+ * @return {number} The size in bytes. Returns 0 if the input is invalid or not provided.
47
+ */
48
+ function sizeInBytes(size) {
49
+ return (0, common_1.isString)(size) ? (0, common_1.textToBytes)(size) : 0;
50
+ }
51
+ /**
52
+ * Validates if the provided MIME type matches a given pattern or regular expression.
53
+ *
54
+ * @param {string} mimeType - The MIME type to validate.
55
+ * @param {string|RegExp} [mimeTypeExp] - An optional pattern or regular expression to match the MIME type against.
56
+ * If not provided, the method will always return true.
57
+ * @return {boolean} True if the MIME type matches the specified pattern or regular expression, false otherwise.
58
+ */
59
+ function isValidMimeType(mimeType, mimeTypeExp) {
60
+ if (!mimeTypeExp) {
61
+ return true;
62
+ }
63
+ else if (mimeTypeExp instanceof RegExp) {
64
+ return mimeTypeExp.test(mimeType);
65
+ }
66
+ else {
67
+ const regexPattern = mimeTypeExp
68
+ .replace(/[.+?^$]/g, '\\$&')
69
+ .replace(/\*/g, '.*');
70
+ const regex = new RegExp(`^${regexPattern}$`);
71
+ return regex.test(mimeType);
72
+ }
73
+ }
74
+ /**
75
+ * Generates a file name based on a given name, an optional pattern, and a set of variables.
76
+ *
77
+ * @param {string} name The original file name including its extension.
78
+ * @param {string} [pattern] An optional pattern to customize the generated file name. If not provided, a default
79
+ * pattern will be used.
80
+ * @param {Record<string, any>} [variables={}] Additional variables to replace in the pattern. These can include custom
81
+ * placeholders to dynamically generate the file name.
82
+ * @return {string} The generated, sanitized file name based on the provided or default pattern.
83
+ */
84
+ function generateFileName(name, pattern, variables = {}) {
85
+ const nameParts = name.split('.');
86
+ let extension = '';
87
+ let nameWithoutExtension = name;
88
+ if (nameParts.length > 1) {
89
+ extension = nameParts.pop();
90
+ nameWithoutExtension = nameParts.join('.');
91
+ }
92
+ const defaultPattern = common_1.config.STORAGE_NAME_PATTERN ?? '{name}-{hash}.{extension}';
93
+ let fileName = (0, common_1.replacePatternVariables)(pattern ?? defaultPattern, {
94
+ name: nameWithoutExtension,
95
+ extension,
96
+ ...variables
97
+ });
98
+ fileName = sanitizeFilename(fileName);
99
+ fileName = fileName.endsWith('.')
100
+ ? fileName.substring(0, fileName.length - 1)
101
+ : fileName;
102
+ return fileName || name;
103
+ }
104
+ /**
105
+ * Sanitizes a given filename by removing invalid characters that are not allowed in file systems.
106
+ *
107
+ * @param fileName The original filename to sanitize.
108
+ * @return A sanitized filename with invalid characters removed.
109
+ */
110
+ function sanitizeFilename(fileName) {
111
+ return fileName.replace(/[\\:*?"<>|]/g, '');
112
+ }
113
+ /**
114
+ * Aggregates the provided files into a structured object based on the given configuration.
115
+ *
116
+ * @param {File[]} files - An array of file objects to be processed and aggregated.
117
+ * @param {FilesConfig} filesConfig - An object that defines the configuration for each file field, including whether
118
+ * the field supports multiple files.
119
+ * @return {Record<string, File | File[] | null>} A record where keys correspond to the configured fields, and values
120
+ * are either a single file, an array of files, or null if no files match the field.
121
+ */
122
+ function aggregateFiles(files, filesConfig) {
123
+ const aggregated = Object.entries(filesConfig)
124
+ .map(([field, value]) => ({ [field]: value.array ? [] : null }))
125
+ .reduce((acc, obj) => Object.assign(acc, obj), {});
126
+ for (const file of files) {
127
+ const resourceField = file.resourceField;
128
+ if (resourceField) {
129
+ if (filesConfig[resourceField].array) {
130
+ if (!aggregated[resourceField]) {
131
+ aggregated[resourceField] = [];
132
+ }
133
+ aggregated[resourceField].push(file);
134
+ }
135
+ else {
136
+ aggregated[resourceField] = file;
137
+ }
138
+ }
139
+ }
140
+ return aggregated;
141
+ }
@@ -0,0 +1,3 @@
1
+ export * from './export-util';
2
+ export * from './file-util';
3
+ export * from './schema-util';
package/utils/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./export-util"), exports);
18
+ __exportStar(require("./file-util"), exports);
19
+ __exportStar(require("./schema-util"), exports);
@@ -0,0 +1,20 @@
1
+ import { TObject } from '@sinclair/typebox';
2
+ /**
3
+ * Creates a schema model by defining it with the given name and schema object. This function returns a TypeBox
4
+ * reference to the created schema model.
5
+ *
6
+ * @param {TObject} schema - The schema object defining the structure of the model.
7
+ * @param {Object} config - Configuration options for schema model creation.
8
+ * @param {string} [config.name] - The name of the schema model to be referenced by. If not provided, then
9
+ * `schema.$id` or `schema.title` will be used by default. If both are `undefined`, then a generic name will be used.
10
+ * @param {boolean} [config.addToServer] - Whether to add the schema to the server instance. Defaults to `true`.
11
+ * @param {boolean} [config.skipExisting] - Whether to skip creating a schema model if it is alredy created with the
12
+ * same name. Defaults to `true`.
13
+ * @return {TObject} A reference to the created schema model, but returned type will be the model it references, so
14
+ * this function can be used while building schema properties and provide type support in routes.
15
+ */
16
+ export declare function createSchemaModel<T extends TObject>(schema: T, config?: {
17
+ name?: string;
18
+ addToServer?: boolean;
19
+ skipExisting?: boolean;
20
+ }): T;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSchemaModel = createSchemaModel;
4
+ const typebox_1 = require("@sinclair/typebox");
5
+ const common_1 = require("@appweaver/common");
6
+ const context_1 = require("../context");
7
+ /**
8
+ * Creates a schema model by defining it with the given name and schema object. This function returns a TypeBox
9
+ * reference to the created schema model.
10
+ *
11
+ * @param {TObject} schema - The schema object defining the structure of the model.
12
+ * @param {Object} config - Configuration options for schema model creation.
13
+ * @param {string} [config.name] - The name of the schema model to be referenced by. If not provided, then
14
+ * `schema.$id` or `schema.title` will be used by default. If both are `undefined`, then a generic name will be used.
15
+ * @param {boolean} [config.addToServer] - Whether to add the schema to the server instance. Defaults to `true`.
16
+ * @param {boolean} [config.skipExisting] - Whether to skip creating a schema model if it is alredy created with the
17
+ * same name. Defaults to `true`.
18
+ * @return {TObject} A reference to the created schema model, but returned type will be the model it references, so
19
+ * this function can be used while building schema properties and provide type support in routes.
20
+ */
21
+ function createSchemaModel(schema, config = {}) {
22
+ const existingModels = context_1.context.definitions.filter((d) => d.name === common_1.MODEL);
23
+ const name = config.name ??
24
+ schema.$id ??
25
+ schema.title ??
26
+ `Object${existingModels.length}`;
27
+ const reference = typebox_1.Type.Ref(name);
28
+ if (existingModels.find((m) => m.name === name) &&
29
+ config.skipExisting !== false) {
30
+ return reference;
31
+ }
32
+ (0, context_1.define)({ name, schema }, common_1.MODEL, 'append');
33
+ if (context_1.context.server && config.addToServer !== false) {
34
+ context_1.context.server.addSchema({ ...schema, $id: name });
35
+ }
36
+ return reference;
37
+ }