@vandenberghinc/volt 1.1.4 → 1.1.6

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 (290) hide show
  1. package/backend/dist/cjs/database.d.ts +41 -68
  2. package/backend/dist/cjs/database.js +136 -78
  3. package/backend/dist/cjs/endpoint.d.ts +23 -9
  4. package/backend/dist/cjs/endpoint.js +98 -21
  5. package/backend/dist/cjs/file_watcher.js +2 -2
  6. package/backend/dist/cjs/frontend.d.ts +0 -2
  7. package/backend/dist/cjs/frontend.js +9 -9
  8. package/backend/dist/cjs/image_endpoint.d.ts +3 -1
  9. package/backend/dist/cjs/image_endpoint.js +2 -1
  10. package/backend/dist/cjs/payments/paddle.js +10 -2
  11. package/backend/dist/cjs/plugins/css.d.ts +6 -5
  12. package/backend/dist/cjs/plugins/css.js +32 -7
  13. package/backend/dist/cjs/plugins/ts/compiler.d.ts +6 -1
  14. package/backend/dist/cjs/plugins/ts/compiler.js +26 -2
  15. package/backend/dist/cjs/plugins/ts/preprocessing.js +5 -3
  16. package/backend/dist/cjs/server.d.ts +7 -13
  17. package/backend/dist/cjs/server.js +184 -303
  18. package/backend/dist/cjs/status.d.ts +1 -0
  19. package/backend/dist/cjs/status.js +2 -1
  20. package/backend/dist/cjs/stream.d.ts +5 -3
  21. package/backend/dist/cjs/stream.js +13 -4
  22. package/backend/dist/cjs/users.d.ts +1 -1
  23. package/backend/dist/cjs/users.js +87 -72
  24. package/backend/dist/cjs/utils.d.ts +17 -9
  25. package/backend/dist/cjs/utils.js +22 -64
  26. package/backend/dist/cjs/view.d.ts +2 -2
  27. package/backend/dist/cjs/view.js +38 -40
  28. package/backend/dist/cjs/volt.d.ts +3 -2
  29. package/backend/dist/cjs/volt.js +2 -2
  30. package/backend/dist/css/volt.css +5 -0
  31. package/backend/dist/esm/database.d.ts +41 -68
  32. package/backend/dist/esm/database.js +137 -79
  33. package/backend/dist/esm/endpoint.d.ts +23 -9
  34. package/backend/dist/esm/endpoint.js +99 -22
  35. package/backend/dist/esm/file_watcher.js +2 -2
  36. package/backend/dist/esm/frontend.d.ts +0 -2
  37. package/backend/dist/esm/frontend.js +9 -9
  38. package/backend/dist/esm/image_endpoint.d.ts +3 -1
  39. package/backend/dist/esm/image_endpoint.js +2 -1
  40. package/backend/dist/esm/payments/paddle.js +11 -3
  41. package/backend/dist/esm/plugins/css.d.ts +6 -5
  42. package/backend/dist/esm/plugins/css.js +32 -6
  43. package/backend/dist/esm/plugins/ts/compiler.d.ts +6 -1
  44. package/backend/dist/esm/plugins/ts/compiler.js +26 -2
  45. package/backend/dist/esm/plugins/ts/preprocessing.js +5 -3
  46. package/backend/dist/esm/server.d.ts +7 -13
  47. package/backend/dist/esm/server.js +182 -301
  48. package/backend/dist/esm/status.d.ts +1 -0
  49. package/backend/dist/esm/status.js +1 -0
  50. package/backend/dist/esm/stream.d.ts +5 -3
  51. package/backend/dist/esm/stream.js +13 -4
  52. package/backend/dist/esm/users.d.ts +1 -1
  53. package/backend/dist/esm/users.js +87 -72
  54. package/backend/dist/esm/utils.d.ts +17 -9
  55. package/backend/dist/esm/utils.js +21 -62
  56. package/backend/dist/esm/view.d.ts +2 -2
  57. package/backend/dist/esm/view.js +38 -40
  58. package/backend/dist/esm/volt.d.ts +3 -2
  59. package/backend/dist/esm/volt.js +2 -1
  60. package/backend/dist/esm-dev/blacklist.js +1 -1
  61. package/backend/dist/esm-dev/cli.js +2 -2
  62. package/backend/dist/esm-dev/database.d.ts +41 -68
  63. package/backend/dist/esm-dev/database.js +138 -80
  64. package/backend/dist/esm-dev/endpoint.d.ts +23 -9
  65. package/backend/dist/esm-dev/endpoint.js +100 -23
  66. package/backend/dist/esm-dev/file_watcher.js +3 -3
  67. package/backend/dist/esm-dev/frontend.d.ts +0 -2
  68. package/backend/dist/esm-dev/frontend.js +9 -9
  69. package/backend/dist/esm-dev/image_endpoint.d.ts +3 -1
  70. package/backend/dist/esm-dev/image_endpoint.js +2 -1
  71. package/backend/dist/esm-dev/logger.js +1 -1
  72. package/backend/dist/esm-dev/payments/paddle.js +12 -4
  73. package/backend/dist/esm-dev/plugins/css.d.ts +6 -5
  74. package/backend/dist/esm-dev/plugins/css.js +33 -7
  75. package/backend/dist/esm-dev/plugins/ts/compiler.d.ts +6 -1
  76. package/backend/dist/esm-dev/plugins/ts/compiler.js +27 -3
  77. package/backend/dist/esm-dev/plugins/ts/preprocessing.js +7 -5
  78. package/backend/dist/esm-dev/rate_limit.js +1 -1
  79. package/backend/dist/esm-dev/server.d.ts +7 -13
  80. package/backend/dist/esm-dev/server.js +184 -303
  81. package/backend/dist/esm-dev/status.d.ts +1 -0
  82. package/backend/dist/esm-dev/status.js +1 -0
  83. package/backend/dist/esm-dev/stream.d.ts +5 -3
  84. package/backend/dist/esm-dev/stream.js +13 -4
  85. package/backend/dist/esm-dev/users.d.ts +1 -1
  86. package/backend/dist/esm-dev/users.js +88 -73
  87. package/backend/dist/esm-dev/utils.d.ts +17 -9
  88. package/backend/dist/esm-dev/utils.js +22 -63
  89. package/backend/dist/esm-dev/view.d.ts +2 -2
  90. package/backend/dist/esm-dev/view.js +39 -41
  91. package/backend/dist/esm-dev/volt.d.ts +3 -2
  92. package/backend/dist/esm-dev/volt.js +2 -1
  93. package/backend/src/database.ts +173 -155
  94. package/backend/src/endpoint.ts +123 -31
  95. package/backend/src/file_watcher.ts +2 -2
  96. package/backend/src/frontend.ts +9 -8
  97. package/backend/src/image_endpoint.ts +4 -0
  98. package/backend/src/payments/paddle.ts +11 -3
  99. package/backend/src/plugins/css.ts +36 -8
  100. package/backend/src/plugins/ts/compiler.ts +37 -1
  101. package/backend/src/plugins/ts/preprocessing.ts +5 -3
  102. package/backend/src/server.ts +167 -306
  103. package/backend/src/status.ts +1 -0
  104. package/backend/src/stream.ts +28 -8
  105. package/backend/src/users.ts +87 -72
  106. package/backend/src/utils.ts +58 -25
  107. package/backend/src/view.ts +30 -28
  108. package/backend/src/{volt.js → volt.ts} +2 -1
  109. package/backend/tsconfig.cjs.json +3 -3
  110. package/backend/tsconfig.esm.json +3 -3
  111. package/frontend/dist/elements/base.d.ts +414 -432
  112. package/frontend/dist/elements/base.js +566 -329
  113. package/frontend/dist/elements/module.d.ts +26 -12
  114. package/frontend/dist/elements/module.js +69 -32
  115. package/frontend/dist/elements/register_element.d.ts +3 -0
  116. package/frontend/dist/elements/register_element.js +22 -0
  117. package/frontend/dist/modules/auth.d.ts +1 -0
  118. package/frontend/dist/modules/auth.js +6 -5
  119. package/frontend/dist/modules/color.d.ts +159 -0
  120. package/frontend/dist/modules/color.js +315 -0
  121. package/frontend/dist/modules/colors.d.ts +1 -26
  122. package/frontend/dist/modules/colors.js +417 -340
  123. package/frontend/dist/modules/cookies.d.ts +1 -0
  124. package/frontend/dist/modules/cookies.js +1 -0
  125. package/frontend/dist/modules/events.d.ts +1 -0
  126. package/frontend/dist/modules/events.js +1 -0
  127. package/frontend/dist/modules/google.d.ts +1 -0
  128. package/frontend/dist/modules/google.js +1 -0
  129. package/frontend/dist/modules/meta.d.ts +1 -0
  130. package/frontend/dist/modules/meta.js +1 -0
  131. package/frontend/dist/modules/mutex.d.ts +1 -2
  132. package/frontend/dist/modules/mutex.js +3 -4
  133. package/frontend/dist/modules/paddle.d.ts +1 -0
  134. package/frontend/dist/modules/paddle.js +14 -13
  135. package/frontend/dist/modules/scheme.d.ts +1 -0
  136. package/frontend/dist/modules/scheme.js +5 -3
  137. package/frontend/dist/modules/statics.d.ts +1 -0
  138. package/frontend/dist/modules/statics.js +1 -0
  139. package/frontend/dist/modules/support.d.ts +1 -0
  140. package/frontend/dist/modules/support.js +3 -2
  141. package/frontend/dist/modules/theme.d.ts +56 -0
  142. package/frontend/dist/{ui → modules}/theme.js +186 -75
  143. package/frontend/dist/modules/themes.d.ts +1 -1
  144. package/frontend/dist/modules/themes.js +1 -0
  145. package/frontend/dist/modules/user.d.ts +1 -0
  146. package/frontend/dist/modules/user.js +11 -10
  147. package/frontend/dist/modules/utils.d.ts +23 -2
  148. package/frontend/dist/modules/utils.js +93 -1
  149. package/frontend/dist/types/gradient.js +4 -0
  150. package/frontend/dist/ui/border_button.d.ts +0 -25
  151. package/frontend/dist/ui/border_button.js +50 -51
  152. package/frontend/dist/ui/button.d.ts +0 -21
  153. package/frontend/dist/ui/button.js +41 -46
  154. package/frontend/dist/ui/canvas.js +15 -15
  155. package/frontend/dist/ui/checkbox.d.ts +3 -17
  156. package/frontend/dist/ui/checkbox.js +36 -30
  157. package/frontend/dist/ui/code.d.ts +15 -82
  158. package/frontend/dist/ui/code.js +150 -125
  159. package/frontend/dist/ui/color.d.ts +0 -1
  160. package/frontend/dist/ui/color.js +1 -1
  161. package/frontend/dist/ui/context_menu.d.ts +4 -2
  162. package/frontend/dist/ui/context_menu.js +16 -17
  163. package/frontend/dist/ui/css.js +2 -0
  164. package/frontend/dist/ui/divider.d.ts +0 -7
  165. package/frontend/dist/ui/divider.js +21 -25
  166. package/frontend/dist/ui/dropdown.d.ts +13 -7
  167. package/frontend/dist/ui/dropdown.js +65 -30
  168. package/frontend/dist/ui/for_each.d.ts +0 -5
  169. package/frontend/dist/ui/for_each.js +17 -22
  170. package/frontend/dist/ui/form.d.ts +17 -12
  171. package/frontend/dist/ui/form.js +21 -18
  172. package/frontend/dist/ui/frame_modes.d.ts +9 -12
  173. package/frontend/dist/ui/frame_modes.js +8 -10
  174. package/frontend/dist/ui/google_map.d.ts +0 -11
  175. package/frontend/dist/ui/google_map.js +23 -28
  176. package/frontend/dist/ui/gradient.d.ts +0 -5
  177. package/frontend/dist/ui/gradient.js +17 -22
  178. package/frontend/dist/ui/image.d.ts +27 -58
  179. package/frontend/dist/ui/image.js +99 -93
  180. package/frontend/dist/ui/input.d.ts +20 -97
  181. package/frontend/dist/ui/input.js +192 -170
  182. package/frontend/dist/ui/link.d.ts +0 -18
  183. package/frontend/dist/ui/link.js +42 -48
  184. package/frontend/dist/ui/list.js +36 -37
  185. package/frontend/dist/ui/loader_button.d.ts +4 -19
  186. package/frontend/dist/ui/loader_button.js +35 -37
  187. package/frontend/dist/ui/loaders.d.ts +0 -8
  188. package/frontend/dist/ui/loaders.js +20 -25
  189. package/frontend/dist/ui/popup.d.ts +11 -8
  190. package/frontend/dist/ui/popup.js +183 -24
  191. package/frontend/dist/ui/pseudo.d.ts +3 -3
  192. package/frontend/dist/ui/pseudo.js +14 -17
  193. package/frontend/dist/ui/scroller.d.ts +10 -48
  194. package/frontend/dist/ui/scroller.js +306 -300
  195. package/frontend/dist/ui/slider.d.ts +9 -3
  196. package/frontend/dist/ui/slider.js +31 -17
  197. package/frontend/dist/ui/spacer.d.ts +0 -9
  198. package/frontend/dist/ui/spacer.js +21 -26
  199. package/frontend/dist/ui/span.js +13 -15
  200. package/frontend/dist/ui/stack.d.ts +14 -75
  201. package/frontend/dist/ui/stack.js +166 -169
  202. package/frontend/dist/ui/steps.d.ts +10 -23
  203. package/frontend/dist/ui/steps.js +47 -34
  204. package/frontend/dist/ui/style.d.ts +4 -3
  205. package/frontend/dist/ui/style.js +13 -18
  206. package/frontend/dist/ui/switch.d.ts +10 -4
  207. package/frontend/dist/ui/switch.js +24 -16
  208. package/frontend/dist/ui/table.d.ts +0 -23
  209. package/frontend/dist/ui/table.js +113 -119
  210. package/frontend/dist/ui/tabs.d.ts +3 -19
  211. package/frontend/dist/ui/tabs.js +35 -29
  212. package/frontend/dist/ui/text.d.ts +0 -8
  213. package/frontend/dist/ui/text.js +20 -25
  214. package/frontend/dist/ui/title.d.ts +0 -15
  215. package/frontend/dist/ui/title.js +39 -45
  216. package/frontend/dist/ui/ui.d.ts +0 -2
  217. package/frontend/dist/ui/ui.js +0 -2
  218. package/frontend/dist/ui/view.d.ts +3 -17
  219. package/frontend/dist/ui/view.js +27 -32
  220. package/frontend/dist/volt.d.ts +2 -1
  221. package/frontend/dist/volt.js +3 -1
  222. package/frontend/examples/dashboard/dashboard.ts +774 -0
  223. package/frontend/examples/theme/theme.ts +58 -0
  224. package/frontend/src/css/volt.css +5 -0
  225. package/frontend/src/elements/base.ts +767 -545
  226. package/frontend/src/elements/module.ts +90 -29
  227. package/frontend/src/elements/register_element.ts +24 -0
  228. package/frontend/src/modules/auth.ts +7 -6
  229. package/frontend/src/modules/color.ts +348 -0
  230. package/frontend/src/modules/colors.ts +468 -449
  231. package/frontend/src/modules/cookies.ts +1 -0
  232. package/frontend/src/modules/events.ts +1 -0
  233. package/frontend/src/modules/google.ts +1 -0
  234. package/frontend/src/modules/meta.ts +2 -1
  235. package/frontend/src/modules/mutex.ts +2 -4
  236. package/frontend/src/modules/paddle.ts +21 -20
  237. package/frontend/src/modules/scheme.ts +5 -4
  238. package/frontend/src/modules/statics.ts +2 -1
  239. package/frontend/src/modules/support.ts +3 -2
  240. package/frontend/src/modules/theme.ts +413 -0
  241. package/frontend/src/modules/themes.ts +2 -1
  242. package/frontend/src/modules/user.ts +12 -11
  243. package/frontend/src/modules/utils.ts +125 -2
  244. package/frontend/src/ui/border_button.ts +41 -37
  245. package/frontend/src/ui/button.ts +33 -32
  246. package/frontend/src/ui/canvas.ts +5 -2
  247. package/frontend/src/ui/checkbox.ts +21 -22
  248. package/frontend/src/ui/code.ts +92 -86
  249. package/frontend/src/ui/context_menu.ts +7 -5
  250. package/frontend/src/ui/css.ts +1 -1
  251. package/frontend/src/ui/divider.ts +15 -10
  252. package/frontend/src/ui/dropdown.ts +38 -21
  253. package/frontend/src/ui/for_each.ts +9 -8
  254. package/frontend/src/ui/form.ts +26 -21
  255. package/frontend/src/ui/frame_modes.ts +13 -17
  256. package/frontend/src/ui/google_map.ts +15 -13
  257. package/frontend/src/ui/gradient.ts +9 -8
  258. package/frontend/src/ui/image.ts +108 -86
  259. package/frontend/src/ui/input.ts +145 -144
  260. package/frontend/src/ui/link.ts +25 -23
  261. package/frontend/src/ui/list.ts +12 -6
  262. package/frontend/src/ui/loader_button.ts +26 -25
  263. package/frontend/src/ui/loaders.ts +12 -11
  264. package/frontend/src/ui/popup.ts +168 -14
  265. package/frontend/src/ui/pseudo.ts +5 -3
  266. package/frontend/src/ui/scroller.ts +303 -294
  267. package/frontend/src/ui/slider.ts +15 -10
  268. package/frontend/src/ui/spacer.ts +14 -11
  269. package/frontend/src/ui/span.ts +6 -2
  270. package/frontend/src/ui/stack.ts +196 -183
  271. package/frontend/src/ui/steps.ts +38 -22
  272. package/frontend/src/ui/style.ts +7 -4
  273. package/frontend/src/ui/switch.ts +16 -11
  274. package/frontend/src/ui/table.ts +42 -34
  275. package/frontend/src/ui/tabs.ts +20 -19
  276. package/frontend/src/ui/text.ts +12 -11
  277. package/frontend/src/ui/title.ts +22 -20
  278. package/frontend/src/ui/ui.ts +0 -2
  279. package/frontend/src/ui/view.ts +20 -19
  280. package/frontend/src/volt.ts +3 -1
  281. package/frontend/{compile.js → tools/compile.old.js} +2 -2
  282. package/frontend/tools/embed_scripts.js +69 -0
  283. package/frontend/tsconfig.json +26 -0
  284. package/package.json +7 -6
  285. package/frontend/dist/ui/theme.d.ts +0 -25
  286. package/frontend/exports.json +0 -1340
  287. package/frontend/src/modules/date.js +0 -535
  288. package/frontend/src/ui/color.ts +0 -117
  289. package/frontend/src/ui/theme.ts +0 -279
  290. /package/backend/src/{vinc.dev.js → vinc.dev.ts} +0 -0
@@ -57,4 +57,5 @@ export declare const Status: {
57
57
  two_factor_auth_required: number;
58
58
  get_description(status: number): string;
59
59
  };
60
+ export { Status as status };
60
61
  export default Status;
@@ -4,7 +4,7 @@
4
4
  * Copyright: © 2022 - 2024 Daan van den Bergh.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.Status = void 0;
7
+ exports.status = exports.Status = void 0;
8
8
  // HTTP response status.
9
9
  // - Does not include all status codes.
10
10
  /* @docs:
@@ -196,4 +196,5 @@ exports.Status = {
196
196
  }
197
197
  }
198
198
  };
199
+ exports.status = exports.Status;
199
200
  exports.default = exports.Status;
@@ -55,12 +55,14 @@ export declare class Stream {
55
55
  data?: any;
56
56
  compress?: boolean;
57
57
  }): this;
58
- error({ status, headers, body, data, compress }?: {
58
+ error({ message, type, invalid_fields, status, headers, compress, data, }: {
59
+ message: string;
60
+ type?: string;
61
+ invalid_fields?: Record<string, string>;
59
62
  status?: number;
60
63
  headers?: Record<string, any>;
61
- body?: any;
62
- data?: any;
63
64
  compress?: boolean;
65
+ data?: any[] | Record<string, any>;
64
66
  }): this;
65
67
  set_header(name: string, value: any): this;
66
68
  set_headers(headers?: Record<string, any>): this;
@@ -670,6 +670,7 @@ class Stream {
670
670
  * stream.send({status: 200, data: "Hello World!"});
671
671
  */
672
672
  send({ status = 200, headers = {}, body = null, data = null, compress = false } = {}) {
673
+ compress = false; // @todo @tmp
673
674
  // Copy body.
674
675
  if (data != null) {
675
676
  body = data;
@@ -694,12 +695,12 @@ class Stream {
694
695
  this.res_headers["Content-Type"] = "application/json";
695
696
  body = JSON.stringify(body);
696
697
  }
697
- // Respond.
698
- stream.respond(this.res_headers);
699
698
  // Compress.
700
699
  if (compress) {
701
700
  body = zlib_1.default.gzipSync(body, { level: zlib_1.default.constants.Z_BEST_COMPRESSION });
702
701
  }
702
+ // Respond.
703
+ stream.respond(this.res_headers);
703
704
  // End.
704
705
  if (body != null) {
705
706
  stream.end(Buffer.from(body));
@@ -811,8 +812,16 @@ class Stream {
811
812
  * ...
812
813
  * stream.error({data: "Some error occured"});
813
814
  */
814
- error({ status = 500, headers = {}, body = null, data = null, compress = false } = {}) {
815
- return this.send({ status: status, headers: headers, body: body ?? data, compress: compress });
815
+ error({ message, type = "APIError", invalid_fields = {}, status = 500, headers = {}, compress = false, data = undefined, }) {
816
+ return this.send({ status: status, headers: headers, compress: compress, body: {
817
+ error: {
818
+ type,
819
+ message,
820
+ status,
821
+ invalid_fields,
822
+ },
823
+ data,
824
+ } });
816
825
  }
817
826
  // Set headers.
818
827
  /* @docs:
@@ -55,7 +55,7 @@ export declare class Users {
55
55
  _create_user_cookie(stream: Stream, uid: string): Promise<void>;
56
56
  _create_detailed_user_cookie(stream: Stream, uid: string): Promise<void>;
57
57
  _reset_cookies(stream: Stream): void;
58
- _initialize(): void;
58
+ _initialize(): Promise<void>;
59
59
  uid_exists(uid: string): Promise<boolean>;
60
60
  username_exists(username: string): Promise<boolean>;
61
61
  email_exists(email: string): Promise<boolean>;
@@ -44,7 +44,7 @@ const _vinc_1 = require("./vinc.js");
44
44
  const utils = __importStar(require("./utils.js"));
45
45
  const Mail = __importStar(require("./plugins/mail.js"));
46
46
  const status_js_1 = require("./status.js");
47
- const { FrontendError } = utils;
47
+ const { APIError } = utils;
48
48
  const logger_js_1 = require("./logger.js");
49
49
  const log_source = logger_js_1.logger.LogSource("Users");
50
50
  // interface Server {
@@ -306,14 +306,14 @@ class Users {
306
306
  // ---------------------------------------------------------
307
307
  // Initialization (private).
308
308
  // Initialize.
309
- _initialize() {
309
+ async _initialize() {
310
310
  // Database collections.
311
- this._tokens_db = this.server.db.create_uid_collection("_tokens");
312
- this._users_db = this.server.db.create_uid_collection("_users");
311
+ this._tokens_db = await this.server.db.create_uid_collection("_tokens");
312
+ this._users_db = await this.server.db.create_uid_collection("_users");
313
313
  // Public database collections.
314
- this.public = this.server.db.create_uid_collection("_users_public");
315
- this.protected = this.server.db.create_uid_collection("_users_protected");
316
- this.private = this.server.db.create_uid_collection("_users_private");
314
+ this.public = await this.server.db.create_uid_collection("_users_public");
315
+ this.protected = await this.server.db.create_uid_collection("_users_protected");
316
+ this.private = await this.server.db.create_uid_collection("_users_private");
317
317
  // ---------------------------------------------------------
318
318
  // Default auth endpoints.
319
319
  this.server.endpoint(
@@ -367,26 +367,33 @@ class Users {
367
367
  username_err = err;
368
368
  }
369
369
  if (email_err && username_err) {
370
- return stream.error({ status: status_js_1.Status.bad_request, data: { error: email_err.message } });
370
+ return stream.error({
371
+ status: status_js_1.Status.bad_request,
372
+ type: "InvalidParams",
373
+ message: email_err.message,
374
+ });
371
375
  }
372
376
  try {
373
377
  password = stream.param("password");
374
378
  }
375
379
  catch (err) {
376
- return stream.error({ status: status_js_1.Status.bad_request, data: { error: err.message } });
380
+ return stream.error({
381
+ status: status_js_1.Status.bad_request,
382
+ type: "InvalidParams",
383
+ message: err.message,
384
+ });
377
385
  }
378
386
  // Get uid.
379
387
  if (email) {
380
388
  if ((uid = await this.get_uid_by_email(email)) == null) {
381
389
  return stream.error({
382
390
  status: status_js_1.Status.unauthorized,
383
- data: {
384
- error: "Unauthorized.",
385
- invalid_fields: {
386
- "email": "Invalid or unrecognized email",
387
- "password": "Invalid or unrecognized password",
388
- },
389
- }
391
+ type: "Unauthorized",
392
+ message: "Unauthorized.",
393
+ invalid_fields: {
394
+ "email": "Invalid or unrecognized email",
395
+ "password": "Invalid or unrecognized password",
396
+ },
390
397
  });
391
398
  }
392
399
  }
@@ -394,13 +401,12 @@ class Users {
394
401
  if ((uid = await this.get_uid(username)) == null) {
395
402
  return stream.error({
396
403
  status: status_js_1.Status.unauthorized,
397
- data: {
398
- error: "Unauthorized.",
399
- invalid_fields: {
400
- "username": "Invalid or unrecognized username",
401
- "password": "Invalid or unrecognized password",
402
- },
403
- }
404
+ type: "Unauthorized",
405
+ message: "Unauthorized.",
406
+ invalid_fields: {
407
+ "username": "Invalid or unrecognized username",
408
+ "password": "Invalid or unrecognized password",
409
+ },
404
410
  });
405
411
  }
406
412
  }
@@ -506,22 +512,27 @@ class Users {
506
512
  if (error) {
507
513
  return stream.error({
508
514
  status: status_js_1.Status.bad_request,
509
- data: {
510
- error,
511
- invalid_fields,
512
- }
515
+ type: "InvalidParams",
516
+ message: error,
517
+ invalid_fields: invalid_fields ?? undefined,
513
518
  });
514
519
  }
515
520
  // Verify username and email.
516
521
  if (await this.username_exists(params.username)) {
517
- const e = new FrontendError(`Username "${params.username}" is already registered.`);
518
- e.invalid_fields = { "username": "Username is already registered" };
519
- throw e;
522
+ throw new APIError({
523
+ type: "UsernameAlreadyExists",
524
+ message: `Username "${params.username}" is already registered.`,
525
+ status: status_js_1.Status.bad_request,
526
+ invalid_fields: { "username": "Username is already registered" },
527
+ });
520
528
  }
521
529
  if (await this.email_exists(params.email)) {
522
- const e = new FrontendError(`Email "${params.email}" is already registered.`);
523
- e.invalid_fields = { "email": "Email is already registered" };
524
- throw e;
530
+ throw new APIError({
531
+ type: "EmailAlreadyExists",
532
+ message: `Email "${params.email}" is already registered.`,
533
+ status: status_js_1.Status.bad_request,
534
+ invalid_fields: { "email": "Email is already registered" }
535
+ });
525
536
  }
526
537
  // Verify 2fa.
527
538
  if (this.server.enable_2fa) {
@@ -572,10 +583,9 @@ class Users {
572
583
  catch (err) {
573
584
  return stream.error({
574
585
  status: status_js_1.Status.bad_request,
575
- data: {
576
- error: err.message,
577
- invalid_fields: err.invalid_fields || {},
578
- }
586
+ type: "InvalidParams",
587
+ message: err.message,
588
+ invalid_fields: err.invalid_fields || {},
579
589
  });
580
590
  }
581
591
  // Sign in.
@@ -603,19 +613,17 @@ class Users {
603
613
  }
604
614
  // Check uid.
605
615
  if (uid == null) {
606
- return stream.error({ status: status_js_1.Status.forbidden, data: { error: "Permission denied." } });
616
+ return stream.error({ status: status_js_1.Status.forbidden, message: "Permission denied." });
607
617
  }
608
618
  // Verify.
609
619
  const err = await this.verify_2fa(uid, params.code);
610
620
  if (err) {
611
621
  return stream.error({
612
622
  status: status_js_1.Status.forbidden,
613
- data: {
614
- error: "Permission denied.",
615
- invalid_fields: {
616
- "code": err,
617
- },
618
- }
623
+ message: "Permission denied.",
624
+ invalid_fields: {
625
+ "code": err,
626
+ },
619
627
  });
620
628
  }
621
629
  // Set activated.
@@ -643,28 +651,24 @@ class Users {
643
651
  if (error) {
644
652
  return stream.error({
645
653
  status: status_js_1.Status.bad_request,
646
- data: {
647
- error: error,
648
- invalid_fields,
649
- }
654
+ message: error,
655
+ invalid_fields: invalid_fields ?? undefined,
650
656
  });
651
657
  }
652
658
  // Get uid.
653
659
  let uid;
654
660
  if ((uid = await this.get_uid_by_email(params.email)) == null) {
655
- return stream.error({ status: status_js_1.Status.forbidden, data: { error: "Invalid email." } });
661
+ return stream.error({ status: status_js_1.Status.forbidden, message: "Invalid email." });
656
662
  }
657
663
  // Verify 2fa.
658
664
  const err = await this.verify_2fa(uid, params.code);
659
665
  if (err) {
660
666
  return stream.error({
661
667
  status: status_js_1.Status.forbidden,
662
- data: {
663
- error: "Invalid 2FA code.",
664
- invalid_fields: {
665
- "code": "Invalid code"
666
- },
667
- }
668
+ message: "Invalid 2FA code.",
669
+ invalid_fields: {
670
+ "code": "Invalid code"
671
+ },
668
672
  });
669
673
  }
670
674
  // Set password.
@@ -710,6 +714,7 @@ class Users {
710
714
  {
711
715
  method: "POST",
712
716
  endpoint: "/volt/user",
717
+ content_type: "application/json",
713
718
  authenticated: true,
714
719
  rate_limit: "global",
715
720
  callback: async (stream) => {
@@ -722,6 +727,7 @@ class Users {
722
727
  {
723
728
  method: "POST",
724
729
  endpoint: "/volt/user/change_password",
730
+ content_type: "application/json",
725
731
  authenticated: true,
726
732
  rate_limit: "global",
727
733
  params: {
@@ -734,12 +740,10 @@ class Users {
734
740
  if (await this.verify_password(stream.uid, params.current_password) !== true) {
735
741
  return stream.error({
736
742
  status: status_js_1.Status.unauthorized,
737
- data: {
738
- error: "Incorrect password.",
739
- invalid_fields: {
740
- current_password: "Incorrect password.",
741
- }
742
- },
743
+ message: "Incorrect password.",
744
+ invalid_fields: {
745
+ current_password: "Incorrect password.",
746
+ }
743
747
  });
744
748
  }
745
749
  // Verify new password.
@@ -747,10 +751,8 @@ class Users {
747
751
  if (error) {
748
752
  return stream.error({
749
753
  status: status_js_1.Status.bad_request,
750
- data: {
751
- error: error,
752
- invalid_fields,
753
- }
754
+ message: error,
755
+ invalid_fields: invalid_fields ?? undefined,
754
756
  });
755
757
  }
756
758
  // Set password.
@@ -766,6 +768,7 @@ class Users {
766
768
  {
767
769
  method: "DELETE",
768
770
  endpoint: "/volt/user",
771
+ content_type: "application/json",
769
772
  authenticated: true,
770
773
  rate_limit: "global",
771
774
  callback: async (stream) => {
@@ -784,6 +787,7 @@ class Users {
784
787
  {
785
788
  method: "POST",
786
789
  endpoint: "/volt/user/api_key",
790
+ content_type: "application/json",
787
791
  authenticated: true,
788
792
  rate_limit: "global",
789
793
  callback: async (stream) => {
@@ -799,6 +803,7 @@ class Users {
799
803
  {
800
804
  method: "DELETE",
801
805
  endpoint: "/volt/user/api_key",
806
+ content_type: "application/json",
802
807
  authenticated: true,
803
808
  rate_limit: "global",
804
809
  callback: async (stream) => {
@@ -813,6 +818,7 @@ class Users {
813
818
  {
814
819
  method: "GET",
815
820
  endpoint: "/volt/user/data",
821
+ content_type: "application/json",
816
822
  authenticated: true,
817
823
  rate_limit: "global",
818
824
  params: {
@@ -830,6 +836,7 @@ class Users {
830
836
  {
831
837
  method: "POST",
832
838
  endpoint: "/volt/user/data",
839
+ content_type: "application/json",
833
840
  authenticated: true,
834
841
  rate_limit: "global",
835
842
  params: {
@@ -848,6 +855,7 @@ class Users {
848
855
  {
849
856
  method: "DELETE",
850
857
  endpoint: "/volt/user/data",
858
+ content_type: "application/json",
851
859
  authenticated: true,
852
860
  rate_limit: "global",
853
861
  params: {
@@ -867,6 +875,7 @@ class Users {
867
875
  {
868
876
  method: "GET",
869
877
  endpoint: "/volt/user/data/protected",
878
+ content_type: "application/json",
870
879
  authenticated: true,
871
880
  rate_limit: "global",
872
881
  params: {
@@ -920,7 +929,7 @@ class Users {
920
929
  last_name = stream.param("last_name");
921
930
  }
922
931
  catch (err) {
923
- return stream.error({ status: status_js_1.Status.bad_request, data: { error: err.message } });
932
+ return stream.error({ status: status_js_1.Status.bad_request, message: err.message });
924
933
  }
925
934
  }
926
935
  else {
@@ -1142,14 +1151,20 @@ class Users {
1142
1151
  // Check if username & email already exist.
1143
1152
  if (_check_username_email) {
1144
1153
  if (await this.username_exists(username)) {
1145
- const e = new FrontendError(`Username "${username}" is already registered.`);
1146
- e.invalid_fields = { "username": "Username is already registered" };
1147
- throw e;
1154
+ throw new APIError({
1155
+ type: "UsernameAlreadyExists",
1156
+ message: `Username "${username}" is already registered.`,
1157
+ status: status_js_1.Status.bad_request,
1158
+ invalid_fields: { "username": "Username is already registered" },
1159
+ });
1148
1160
  }
1149
1161
  if (await this.email_exists(email)) {
1150
- const e = new FrontendError(`Email "${email}" is already registered.`);
1151
- e.invalid_fields = { "email": "Email is already registered" };
1152
- throw e;
1162
+ throw new APIError({
1163
+ type: "EmailAlreadyExists",
1164
+ message: `Email "${email}" is already registered.`,
1165
+ status: status_js_1.Status.bad_request,
1166
+ invalid_fields: { "email": "Email is already registered" }
1167
+ });
1153
1168
  }
1154
1169
  }
1155
1170
  // Generate a uid.
@@ -1,15 +1,22 @@
1
- export declare class FrontendError extends Error {
2
- name: string;
3
- status?: number;
4
- data?: any;
1
+ import type { Stream } from "./stream.js";
2
+ /**
3
+ * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
4
+ */
5
+ export declare class APIError extends Error {
6
+ type: string;
7
+ status: number;
8
+ data?: any[] | Record<string, any>;
5
9
  invalid_fields: Record<string, string>;
6
- constructor(message: string, status?: number, data?: any, invalid_fields?: Record<string, string>);
7
- }
8
- export declare class APIError extends FrontendError {
9
- constructor(message: string, status?: number, data?: any, invalid_fields?: Record<string, string>);
10
+ constructor({ type, message, status, data, invalid_fields }: {
11
+ type?: string;
12
+ message: string;
13
+ status?: number;
14
+ data?: any;
15
+ invalid_fields?: Record<string, string>;
16
+ });
17
+ serve(stream: Stream): this;
10
18
  }
11
19
  interface UtilsInt {
12
- "FrontendError": typeof FrontendError;
13
20
  "APIError": typeof APIError;
14
21
  clean_endpoint(endpoint: undefined): undefined;
15
22
  clean_endpoint(endpoint: RegExp): RegExp;
@@ -24,4 +31,5 @@ interface UtilsInt {
24
31
  set_compiled_cache(path: any, data: string, hash: string): void;
25
32
  }
26
33
  export declare const Utils: UtilsInt;
34
+ export { Utils as utils };
27
35
  export default Utils;
@@ -4,84 +4,41 @@
4
4
  * Copyright: © 2022 - 2024 Daan van den Bergh.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.Utils = exports.APIError = exports.FrontendError = void 0;
7
+ exports.utils = exports.Utils = exports.APIError = void 0;
8
8
  const _vinc_1 = require("./vinc.js");
9
+ const status_js_1 = require("./status.js");
9
10
  // ---------------------------------------------------------
10
11
  // Utils.
11
- /* @docs:
12
- * @nav: Backend
13
- @parent: Utils
14
- @chapter: Exceptions
15
- @title: Frontend Error
16
- @description:
17
- The frontend error class can be used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
18
- @usage:
19
- throw new volt.FrontendError("Some error occured.");
20
- throw new volt.FrontendError("Bad request.", volt.status.bad_request);
21
- @param:
22
- @name: message
23
- @descr: The error message.
24
- @type: string
25
- @param:
26
- @name: status
27
- @descr: The http error status.
28
- @type: number
29
- @param:
30
- @name: data
31
- @descr: The error body data.
32
- @type: any
12
+ /**
13
+ * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
33
14
  */
34
- class FrontendError extends Error {
35
- name;
15
+ class APIError extends Error {
16
+ type;
36
17
  status;
37
18
  data;
38
- invalid_fields = {};
39
- constructor(message, status, data, invalid_fields) {
19
+ invalid_fields;
20
+ constructor({ type = "APIError", message, status, data, invalid_fields }) {
40
21
  super(message);
41
- this.name = "FrontendError";
42
- this.status = status;
22
+ this.name = "APIError";
23
+ this.type = type;
24
+ this.status = status ?? status_js_1.Status.internal_server_error;
43
25
  this.data = data;
44
- if (invalid_fields !== undefined) {
45
- this;
46
- invalid_fields = invalid_fields;
47
- }
26
+ this.invalid_fields = invalid_fields ?? {};
48
27
  }
49
- }
50
- exports.FrontendError = FrontendError;
51
- /* @docs:
52
- * @nav: Backend
53
- @parent: Utils
54
- @chapter: Exceptions
55
- @title: API Error
56
- @description:
57
- The api error class can be used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
58
- @usage:
59
- throw new volt.APIError("Some error occured.");
60
- throw new volt.APIError("Bad request.", volt.status.bad_request);
61
- @param:
62
- @name: message
63
- @descr: The error message.
64
- @type: string
65
- @param:
66
- @name: status
67
- @descr: The http error status.
68
- @type: number
69
- @param:
70
- @name: data
71
- @descr: The error body data.
72
- @type: any
73
- */
74
- class APIError extends FrontendError {
75
- constructor(message, status, data, invalid_fields) {
76
- super(message, status, data, invalid_fields);
77
- this.name = "APIError";
28
+ serve(stream) {
29
+ stream.error({
30
+ status: this.status ?? status_js_1.Status.internal_server_error,
31
+ headers: { "Content-Type": "application/json" },
32
+ message: this.message,
33
+ type: this.type,
34
+ invalid_fields: this.invalid_fields,
35
+ });
36
+ return this;
78
37
  }
79
38
  }
80
39
  exports.APIError = APIError;
81
40
  // Implementation
82
41
  exports.Utils = {
83
- // An error that may be shown to the frontend user.
84
- "FrontendError": FrontendError,
85
42
  // An error that may be shown to the frontend user.
86
43
  "APIError": APIError,
87
44
  // Clean an endpoint url.
@@ -326,4 +283,5 @@ exports.Utils = {
326
283
  new _vinc_1.vlib.Path(path.str() + '.hash').save_sync(hash);
327
284
  },
328
285
  };
286
+ exports.utils = exports.Utils;
329
287
  exports.default = exports.Utils;
@@ -23,7 +23,7 @@ export declare class View {
23
23
  is_js_ts_view: boolean;
24
24
  html?: string | Buffer;
25
25
  raw_html?: string | Buffer;
26
- bundle: any;
26
+ _bundle: any;
27
27
  payments?: string | undefined;
28
28
  min_device_width?: number;
29
29
  _server?: any;
@@ -45,7 +45,7 @@ export declare class View {
45
45
  _src?: string;
46
46
  });
47
47
  _initialize(server: any, endpoint: any): void;
48
- _bundle_ts(dist_path: string, bundle?: any): Promise<any>;
48
+ _dynamic_bundle(): Promise<void>;
49
49
  _build_html(): Promise<void>;
50
50
  _serve(stream: any, status_code?: number): void;
51
51
  }