@rebasepro/server-core 0.7.0 → 0.9.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 (300) hide show
  1. package/README.md +2 -2
  2. package/dist/api/ast-schema-editor.d.ts +21 -0
  3. package/dist/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
  4. package/dist/api/errors.d.ts +53 -0
  5. package/dist/api/index.d.ts +7 -0
  6. package/dist/api/logs-routes.d.ts +37 -0
  7. package/dist/api/openapi-generator.d.ts +16 -0
  8. package/dist/api/rest/api-generator.d.ts +68 -0
  9. package/{src/api/rest/index.ts → dist/api/rest/index.d.ts} +0 -1
  10. package/dist/api/rest/query-parser.d.ts +6 -0
  11. package/dist/api/schema-editor-routes.d.ts +3 -0
  12. package/dist/api/types.d.ts +56 -0
  13. package/dist/auth/adapter-middleware.d.ts +43 -0
  14. package/dist/auth/admin-roles-route.d.ts +18 -0
  15. package/dist/auth/admin-user-ops.d.ts +79 -0
  16. package/dist/auth/admin-users-route.d.ts +28 -0
  17. package/dist/auth/api-keys/api-key-middleware.d.ts +39 -0
  18. package/{src/auth/api-keys/api-key-permission-guard.ts → dist/auth/api-keys/api-key-permission-guard.d.ts} +2 -34
  19. package/dist/auth/api-keys/api-key-routes.d.ts +20 -0
  20. package/dist/auth/api-keys/api-key-store.d.ts +35 -0
  21. package/{src/auth/api-keys/api-key-types.ts → dist/auth/api-keys/api-key-types.d.ts} +0 -6
  22. package/{src/auth/api-keys/index.ts → dist/auth/api-keys/index.d.ts} +2 -22
  23. package/dist/auth/apple-oauth.d.ts +30 -0
  24. package/{src/auth/auth-hooks.ts → dist/auth/auth-hooks.d.ts} +15 -73
  25. package/dist/auth/bitbucket-oauth.d.ts +11 -0
  26. package/dist/auth/builtin-auth-adapter.d.ts +55 -0
  27. package/dist/auth/cookie-utils.d.ts +32 -0
  28. package/{src/auth/crypto-utils.ts → dist/auth/crypto-utils.d.ts} +1 -16
  29. package/dist/auth/custom-auth-adapter.d.ts +39 -0
  30. package/dist/auth/discord-oauth.d.ts +14 -0
  31. package/dist/auth/facebook-oauth.d.ts +14 -0
  32. package/dist/auth/github-oauth.d.ts +15 -0
  33. package/dist/auth/gitlab-oauth.d.ts +13 -0
  34. package/dist/auth/google-oauth.d.ts +47 -0
  35. package/{src/auth/index.ts → dist/auth/index.d.ts} +4 -22
  36. package/{src/auth/interfaces.ts → dist/auth/interfaces.d.ts} +20 -89
  37. package/dist/auth/jwt.d.ts +67 -0
  38. package/dist/auth/linkedin-oauth.d.ts +18 -0
  39. package/dist/auth/magic-link-routes.d.ts +32 -0
  40. package/dist/auth/mfa-crypto.d.ts +23 -0
  41. package/dist/auth/mfa-routes.d.ts +7 -0
  42. package/dist/auth/mfa.d.ts +49 -0
  43. package/dist/auth/microsoft-oauth.d.ts +16 -0
  44. package/dist/auth/middleware.d.ts +158 -0
  45. package/dist/auth/password.d.ts +22 -0
  46. package/dist/auth/rate-limiter.d.ts +50 -0
  47. package/dist/auth/reset-password-admin.d.ts +29 -0
  48. package/dist/auth/rls-scope.d.ts +31 -0
  49. package/dist/auth/routes.d.ts +66 -0
  50. package/dist/auth/session-routes.d.ts +29 -0
  51. package/dist/auth/slack-oauth.d.ts +12 -0
  52. package/dist/auth/spotify-oauth.d.ts +12 -0
  53. package/dist/auth/twitter-oauth.d.ts +18 -0
  54. package/dist/collections/BackendCollectionRegistry.d.ts +13 -0
  55. package/dist/collections/loader.d.ts +5 -0
  56. package/dist/cron/cron-loader.d.ts +18 -0
  57. package/dist/cron/cron-routes.d.ts +14 -0
  58. package/dist/cron/cron-scheduler.d.ts +106 -0
  59. package/dist/cron/cron-store.d.ts +32 -0
  60. package/dist/cron/define-cron.d.ts +28 -0
  61. package/{src/cron/index.ts → dist/cron/index.d.ts} +1 -0
  62. package/dist/db/interfaces.d.ts +18 -0
  63. package/dist/dynamic-import-Dvh-K5fl.js +22 -0
  64. package/dist/dynamic-import-Dvh-K5fl.js.map +1 -0
  65. package/dist/email/index.d.ts +6 -0
  66. package/dist/email/smtp-email-service.d.ts +30 -0
  67. package/dist/email/templates.d.ts +50 -0
  68. package/{src/email/types.ts → dist/email/types.d.ts} +40 -36
  69. package/dist/env.d.ts +102 -0
  70. package/dist/functions/define-function.d.ts +55 -0
  71. package/dist/functions/function-loader.d.ts +18 -0
  72. package/dist/functions/function-routes.d.ts +10 -0
  73. package/{src/functions/index.ts → dist/functions/index.d.ts} +2 -0
  74. package/dist/history/history-routes.d.ts +23 -0
  75. package/{src/history/index.ts → dist/history/index.d.ts} +0 -1
  76. package/dist/index.d.ts +35 -0
  77. package/dist/index.es.js +18184 -0
  78. package/dist/index.es.js.map +1 -0
  79. package/dist/index.umd.js +48629 -0
  80. package/dist/index.umd.js.map +1 -0
  81. package/dist/init/docs.d.ts +4 -0
  82. package/dist/init/health.d.ts +2 -0
  83. package/dist/init/middlewares.d.ts +10 -0
  84. package/dist/init/shutdown.d.ts +65 -0
  85. package/dist/init/storage.d.ts +5 -0
  86. package/dist/init.d.ts +344 -0
  87. package/dist/jwt-BwIn8xmk.js +4200 -0
  88. package/dist/jwt-BwIn8xmk.js.map +1 -0
  89. package/dist/logger-BYU66ENZ.js +94 -0
  90. package/dist/logger-BYU66ENZ.js.map +1 -0
  91. package/dist/ms-DnYXB-Wd.js +162 -0
  92. package/dist/ms-DnYXB-Wd.js.map +1 -0
  93. package/dist/openapi-generator-Z9oYWLf_.js +586 -0
  94. package/dist/openapi-generator-Z9oYWLf_.js.map +1 -0
  95. package/dist/schema-editor-routes-D3ef8zu1.js +219 -0
  96. package/dist/schema-editor-routes-D3ef8zu1.js.map +1 -0
  97. package/dist/serve-spa.d.ts +36 -0
  98. package/dist/services/driver-registry.d.ts +78 -0
  99. package/dist/services/routed-realtime-service.d.ts +43 -0
  100. package/dist/services/webhook-service.d.ts +29 -0
  101. package/dist/singleton.d.ts +51 -0
  102. package/dist/src-B4OLmNVa.js +437 -0
  103. package/dist/src-B4OLmNVa.js.map +1 -0
  104. package/dist/src-B5WkP1Rv.js +24778 -0
  105. package/dist/src-B5WkP1Rv.js.map +1 -0
  106. package/dist/storage/GCSStorageController.d.ts +43 -0
  107. package/dist/storage/LocalStorageController.d.ts +46 -0
  108. package/dist/storage/S3StorageController.d.ts +40 -0
  109. package/dist/storage/image-transform.d.ts +50 -0
  110. package/{src/storage/index.ts → dist/storage/index.d.ts} +6 -21
  111. package/dist/storage/routes.d.ts +70 -0
  112. package/dist/storage/storage-registry.d.ts +78 -0
  113. package/dist/storage/tus-handler.d.ts +53 -0
  114. package/{src/storage/types.ts → dist/storage/types.d.ts} +32 -38
  115. package/dist/types/index.d.ts +11 -0
  116. package/dist/utils/dev-port.d.ts +45 -0
  117. package/dist/utils/dynamic-import.d.ts +25 -0
  118. package/dist/utils/logger.d.ts +31 -0
  119. package/dist/utils/logging.d.ts +9 -0
  120. package/dist/utils/request-id.d.ts +4 -0
  121. package/dist/utils/request-logger.d.ts +19 -0
  122. package/{src/utils/sql.ts → dist/utils/sql.d.ts} +5 -16
  123. package/package.json +49 -22
  124. package/coverage/clover.xml +0 -3739
  125. package/coverage/coverage-final.json +0 -31
  126. package/coverage/lcov-report/base.css +0 -224
  127. package/coverage/lcov-report/block-navigation.js +0 -87
  128. package/coverage/lcov-report/favicon.png +0 -0
  129. package/coverage/lcov-report/index.html +0 -266
  130. package/coverage/lcov-report/prettify.css +0 -1
  131. package/coverage/lcov-report/prettify.js +0 -2
  132. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  133. package/coverage/lcov-report/sorter.js +0 -210
  134. package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +0 -952
  135. package/coverage/lcov-report/src/api/errors.ts.html +0 -472
  136. package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +0 -1069
  137. package/coverage/lcov-report/src/api/graphql/index.html +0 -116
  138. package/coverage/lcov-report/src/api/index.html +0 -176
  139. package/coverage/lcov-report/src/api/openapi-generator.ts.html +0 -565
  140. package/coverage/lcov-report/src/api/rest/api-generator.ts.html +0 -994
  141. package/coverage/lcov-report/src/api/rest/index.html +0 -131
  142. package/coverage/lcov-report/src/api/rest/query-parser.ts.html +0 -550
  143. package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +0 -202
  144. package/coverage/lcov-report/src/api/server.ts.html +0 -823
  145. package/coverage/lcov-report/src/auth/admin-routes.ts.html +0 -973
  146. package/coverage/lcov-report/src/auth/index.html +0 -176
  147. package/coverage/lcov-report/src/auth/jwt.ts.html +0 -574
  148. package/coverage/lcov-report/src/auth/middleware.ts.html +0 -745
  149. package/coverage/lcov-report/src/auth/password.ts.html +0 -310
  150. package/coverage/lcov-report/src/auth/services.ts.html +0 -2074
  151. package/coverage/lcov-report/src/collections/index.html +0 -116
  152. package/coverage/lcov-report/src/collections/loader.ts.html +0 -232
  153. package/coverage/lcov-report/src/db/auth-schema.ts.html +0 -523
  154. package/coverage/lcov-report/src/db/data-transformer.ts.html +0 -1753
  155. package/coverage/lcov-report/src/db/entityService.ts.html +0 -700
  156. package/coverage/lcov-report/src/db/index.html +0 -146
  157. package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +0 -4048
  158. package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +0 -883
  159. package/coverage/lcov-report/src/db/services/RelationService.ts.html +0 -3121
  160. package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +0 -442
  161. package/coverage/lcov-report/src/db/services/index.html +0 -176
  162. package/coverage/lcov-report/src/db/services/index.ts.html +0 -124
  163. package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +0 -1960
  164. package/coverage/lcov-report/src/index.html +0 -116
  165. package/coverage/lcov-report/src/services/driver-registry.ts.html +0 -631
  166. package/coverage/lcov-report/src/services/index.html +0 -131
  167. package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +0 -3025
  168. package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +0 -1189
  169. package/coverage/lcov-report/src/storage/S3StorageController.ts.html +0 -970
  170. package/coverage/lcov-report/src/storage/index.html +0 -161
  171. package/coverage/lcov-report/src/storage/storage-registry.ts.html +0 -646
  172. package/coverage/lcov-report/src/storage/types.ts.html +0 -451
  173. package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +0 -3082
  174. package/coverage/lcov-report/src/utils/index.html +0 -116
  175. package/coverage/lcov.info +0 -7179
  176. package/jest.config.cjs +0 -19
  177. package/src/api/ast-schema-editor.ts +0 -289
  178. package/src/api/collections_for_test/callbacks_test_collection.ts +0 -61
  179. package/src/api/errors.ts +0 -278
  180. package/src/api/graphql/graphql-schema-generator.ts +0 -383
  181. package/src/api/graphql/index.ts +0 -2
  182. package/src/api/index.ts +0 -11
  183. package/src/api/logs-routes.ts +0 -132
  184. package/src/api/openapi-generator.ts +0 -727
  185. package/src/api/rest/api-generator-count.test.ts +0 -126
  186. package/src/api/rest/api-generator.ts +0 -747
  187. package/src/api/rest/query-parser.ts +0 -300
  188. package/src/api/schema-editor-routes.ts +0 -41
  189. package/src/api/server.ts +0 -271
  190. package/src/api/types.ts +0 -104
  191. package/src/auth/adapter-middleware.ts +0 -114
  192. package/src/auth/admin-roles-route.ts +0 -36
  193. package/src/auth/admin-user-ops.ts +0 -236
  194. package/src/auth/admin-users-route.ts +0 -302
  195. package/src/auth/api-keys/api-key-middleware.ts +0 -133
  196. package/src/auth/api-keys/api-key-routes.ts +0 -193
  197. package/src/auth/api-keys/api-key-store.ts +0 -340
  198. package/src/auth/apple-oauth.ts +0 -122
  199. package/src/auth/bitbucket-oauth.ts +0 -84
  200. package/src/auth/builtin-auth-adapter.ts +0 -380
  201. package/src/auth/custom-auth-adapter.ts +0 -86
  202. package/src/auth/discord-oauth.ts +0 -85
  203. package/src/auth/facebook-oauth.ts +0 -74
  204. package/src/auth/github-oauth.ts +0 -112
  205. package/src/auth/gitlab-oauth.ts +0 -72
  206. package/src/auth/google-oauth.ts +0 -223
  207. package/src/auth/jwt.ts +0 -208
  208. package/src/auth/linkedin-oauth.ts +0 -83
  209. package/src/auth/magic-link-routes.ts +0 -167
  210. package/src/auth/mfa-crypto.ts +0 -91
  211. package/src/auth/mfa-routes.ts +0 -323
  212. package/src/auth/mfa.ts +0 -161
  213. package/src/auth/microsoft-oauth.ts +0 -90
  214. package/src/auth/middleware.ts +0 -369
  215. package/src/auth/password.ts +0 -77
  216. package/src/auth/rate-limiter.ts +0 -225
  217. package/src/auth/reset-password-admin.ts +0 -160
  218. package/src/auth/rls-scope.ts +0 -58
  219. package/src/auth/routes.ts +0 -802
  220. package/src/auth/session-routes.ts +0 -353
  221. package/src/auth/slack-oauth.ts +0 -73
  222. package/src/auth/spotify-oauth.ts +0 -69
  223. package/src/auth/twitter-oauth.ts +0 -128
  224. package/src/collections/BackendCollectionRegistry.ts +0 -20
  225. package/src/collections/loader.ts +0 -50
  226. package/src/cron/cron-loader.ts +0 -84
  227. package/src/cron/cron-routes.test.ts +0 -265
  228. package/src/cron/cron-routes.ts +0 -85
  229. package/src/cron/cron-scheduler.test.ts +0 -559
  230. package/src/cron/cron-scheduler.ts +0 -581
  231. package/src/cron/cron-store.ts +0 -166
  232. package/src/db/interfaces.ts +0 -60
  233. package/src/email/index.ts +0 -19
  234. package/src/email/smtp-email-service.ts +0 -123
  235. package/src/email/templates.ts +0 -470
  236. package/src/env.ts +0 -227
  237. package/src/functions/function-loader.ts +0 -116
  238. package/src/functions/function-routes.ts +0 -31
  239. package/src/history/history-routes.ts +0 -129
  240. package/src/index.ts +0 -70
  241. package/src/init/docs.ts +0 -47
  242. package/src/init/health.ts +0 -37
  243. package/src/init/middlewares.ts +0 -61
  244. package/src/init/shutdown.ts +0 -56
  245. package/src/init/storage.ts +0 -57
  246. package/src/init.ts +0 -1041
  247. package/src/serve-spa.ts +0 -89
  248. package/src/services/driver-registry.ts +0 -183
  249. package/src/services/routed-realtime-service.ts +0 -113
  250. package/src/services/webhook-service.ts +0 -155
  251. package/src/singleton.ts +0 -70
  252. package/src/storage/LocalStorageController.ts +0 -389
  253. package/src/storage/S3StorageController.ts +0 -300
  254. package/src/storage/image-transform.ts +0 -218
  255. package/src/storage/routes.ts +0 -368
  256. package/src/storage/storage-registry.ts +0 -188
  257. package/src/storage/tus-handler.ts +0 -315
  258. package/src/types/index.ts +0 -27
  259. package/src/utils/dev-port.ts +0 -196
  260. package/src/utils/logger.ts +0 -143
  261. package/src/utils/logging.ts +0 -40
  262. package/src/utils/request-id.ts +0 -40
  263. package/src/utils/request-logger.ts +0 -72
  264. package/test/api-generator.test.ts +0 -588
  265. package/test/api-key-permission-guard.test.ts +0 -144
  266. package/test/ast-schema-editor.test.ts +0 -89
  267. package/test/auth-middleware-hono.test.ts +0 -556
  268. package/test/auth-routes.test.ts +0 -1098
  269. package/test/backend-hooks-data.test.ts +0 -477
  270. package/test/custom-auth-adapter.test.ts +0 -189
  271. package/test/driver-registry.test.ts +0 -282
  272. package/test/email-templates.test.ts +0 -174
  273. package/test/env.test.ts +0 -128
  274. package/test/error-propagation.test.ts +0 -226
  275. package/test/errors-hono.test.ts +0 -133
  276. package/test/errors.test.ts +0 -155
  277. package/test/function-loader.test.ts +0 -127
  278. package/test/graphql-schema-generator.test.ts +0 -554
  279. package/test/jwt-security.test.ts +0 -182
  280. package/test/jwt.test.ts +0 -326
  281. package/test/mfa.test.ts +0 -197
  282. package/test/middleware.test.ts +0 -305
  283. package/test/multi-datasource-routing.test.ts +0 -113
  284. package/test/password.test.ts +0 -165
  285. package/test/query-parser.test.ts +0 -233
  286. package/test/rate-limiter.test.ts +0 -102
  287. package/test/reset-password-admin.test.ts +0 -113
  288. package/test/routed-realtime-service.test.ts +0 -86
  289. package/test/safe-compare.test.ts +0 -66
  290. package/test/singleton.test.ts +0 -59
  291. package/test/smtp-email-service.test.ts +0 -169
  292. package/test/storage-local.test.ts +0 -271
  293. package/test/storage-registry.test.ts +0 -282
  294. package/test/storage-routes.test.ts +0 -222
  295. package/test/storage-s3.test.ts +0 -304
  296. package/test/transform-auth-response.test.ts +0 -305
  297. package/test/webhook-service.test.ts +0 -260
  298. package/tsconfig.json +0 -54
  299. package/tsconfig.prod.json +0 -23
  300. package/vite.config.ts +0 -86
@@ -1,226 +0,0 @@
1
- import { Hono } from "hono";
2
- import { ApiError, errorHandler } from "../src/api/errors";
3
- import { HonoEnv } from "../src/api/types";
4
-
5
- /**
6
- * End-to-end tests that verify ApiError thrown inside Hono sub-routers
7
- * produces the correct HTTP status code and JSON body WHEN the sub-router
8
- * is mounted on a parent app — exactly simulating the init.ts mounting
9
- * pattern.
10
- *
11
- * This is critical because Hono's `onError` does NOT propagate from parent
12
- * to child routers. Each sub-router must have its own `router.onError(errorHandler)`.
13
- */
14
- describe("Error Propagation through Sub-Routers", () => {
15
-
16
- // ── Without onError on sub-router (broken behavior) ─────────────
17
-
18
- describe("Sub-router WITHOUT onError (demonstrates the bug)", () => {
19
- function createBrokenApp() {
20
- // This simulates the REAL production scenario: the consumer's Hono app
21
- // (e.g. SustenTalent's index.ts) does NOT set onError on the parent app.
22
- // Without onError on the sub-router either, Hono's DEFAULT error handler
23
- // catches ApiErrors and returns a generic 500 with a plain text body.
24
- const parentApp = new Hono<HonoEnv>();
25
- // NOTE: no parentApp.onError(errorHandler) — this is the real scenario
26
-
27
- const subRouter = new Hono<HonoEnv>();
28
- // NOTE: no subRouter.onError(errorHandler) — this is the bug
29
- subRouter.get("/login", () => {
30
- throw ApiError.unauthorized("Invalid credentials", "INVALID_CREDENTIALS");
31
- });
32
-
33
- parentApp.route("/api/auth", subRouter);
34
- return parentApp;
35
- }
36
-
37
- it("ApiError.unauthorized returns 500 plain text without ANY onError handler", async () => {
38
- const app = createBrokenApp();
39
- const res = await app.request("/api/auth/login");
40
- // Without any onError handler, Hono's default handler returns 500
41
- expect(res.status).toBe(500);
42
- // And the body is NOT our structured JSON — it's plain text
43
- const text = await res.text();
44
- expect(text).toBe("Internal Server Error");
45
- });
46
- });
47
-
48
- // ── With onError on sub-router (fixed behavior) ─────────────────
49
-
50
- describe("Sub-router WITH onError (correct behavior)", () => {
51
- function createFixedApp() {
52
- const parentApp = new Hono<HonoEnv>();
53
- parentApp.onError(errorHandler);
54
-
55
- const subRouter = new Hono<HonoEnv>();
56
- subRouter.onError(errorHandler); // ← THE FIX
57
-
58
- subRouter.get("/login", () => {
59
- throw ApiError.unauthorized("Invalid credentials", "INVALID_CREDENTIALS");
60
- });
61
- subRouter.get("/register", () => {
62
- throw ApiError.conflict("Email already exists", "EMAIL_EXISTS");
63
- });
64
- subRouter.get("/refresh", () => {
65
- throw ApiError.unauthorized("Refresh token expired", "TOKEN_EXPIRED");
66
- });
67
- subRouter.get("/forbidden", () => {
68
- throw ApiError.forbidden("Admin only");
69
- });
70
- subRouter.get("/not-found", () => {
71
- throw ApiError.notFound("User not found");
72
- });
73
- subRouter.get("/bad-request", () => {
74
- throw ApiError.badRequest("Missing email", "INVALID_INPUT", { field: "email" });
75
- });
76
- subRouter.get("/service-unavailable", () => {
77
- throw ApiError.serviceUnavailable("Google login not configured", "NOT_CONFIGURED");
78
- });
79
- subRouter.get("/generic-error", () => {
80
- throw new Error("Unexpected failure");
81
- });
82
-
83
- parentApp.route("/api/auth", subRouter);
84
- return parentApp;
85
- }
86
-
87
- it("returns 401 for unauthorized errors", async () => {
88
- const app = createFixedApp();
89
- const res = await app.request("/api/auth/login");
90
- expect(res.status).toBe(401);
91
- const body = await res.json() as { error: { message: string; code: string } };
92
- expect(body.error.message).toBe("Invalid credentials");
93
- expect(body.error.code).toBe("INVALID_CREDENTIALS");
94
- });
95
-
96
- it("returns 409 for conflict errors", async () => {
97
- const app = createFixedApp();
98
- const res = await app.request("/api/auth/register");
99
- expect(res.status).toBe(409);
100
- const body = await res.json() as { error: { message: string; code: string } };
101
- expect(body.error.message).toBe("Email already exists");
102
- expect(body.error.code).toBe("EMAIL_EXISTS");
103
- });
104
-
105
- it("returns 401 for expired refresh token", async () => {
106
- const app = createFixedApp();
107
- const res = await app.request("/api/auth/refresh");
108
- expect(res.status).toBe(401);
109
- const body = await res.json() as { error: { message: string; code: string } };
110
- expect(body.error.code).toBe("TOKEN_EXPIRED");
111
- });
112
-
113
- it("returns 403 for forbidden errors", async () => {
114
- const app = createFixedApp();
115
- const res = await app.request("/api/auth/forbidden");
116
- expect(res.status).toBe(403);
117
- });
118
-
119
- it("returns 404 for not found errors", async () => {
120
- const app = createFixedApp();
121
- const res = await app.request("/api/auth/not-found");
122
- expect(res.status).toBe(404);
123
- });
124
-
125
- it("returns 400 with details for bad request errors", async () => {
126
- const app = createFixedApp();
127
- const res = await app.request("/api/auth/bad-request");
128
- expect(res.status).toBe(400);
129
- const body = await res.json() as { error: { message: string; code: string; details: unknown } };
130
- expect(body.error.code).toBe("INVALID_INPUT");
131
- expect(body.error.details).toEqual({ field: "email" });
132
- });
133
-
134
- it("returns 503 for service unavailable errors", async () => {
135
- const app = createFixedApp();
136
- const res = await app.request("/api/auth/service-unavailable");
137
- expect(res.status).toBe(503);
138
- const body = await res.json() as { error: { message: string; code: string } };
139
- expect(body.error.code).toBe("NOT_CONFIGURED");
140
- });
141
-
142
- it("returns 500 with sanitized message for generic errors", async () => {
143
- const app = createFixedApp();
144
- const res = await app.request("/api/auth/generic-error");
145
- expect(res.status).toBe(500);
146
- const body = await res.json() as { error: { message: string; code: string } };
147
- expect(body.error.code).toBe("INTERNAL_ERROR");
148
- // Should NOT leak the raw error message
149
- expect(body.error.message).toBe("Internal Server Error");
150
- });
151
-
152
- it("returns consistent { error: { message, code } } shape for all error types", async () => {
153
- const app = createFixedApp();
154
- const paths = ["/api/auth/login", "/api/auth/register", "/api/auth/forbidden", "/api/auth/not-found", "/api/auth/bad-request"];
155
-
156
- for (const path of paths) {
157
- const res = await app.request(path);
158
- const body = await res.json() as Record<string, unknown>;
159
- expect(body).toHaveProperty("error");
160
- expect(body.error).toHaveProperty("message");
161
- expect(body.error).toHaveProperty("code");
162
- }
163
- });
164
- });
165
-
166
- // ── Multi-level nesting ─────────────────────────────────────────
167
-
168
- describe("Deeply nested sub-routers", () => {
169
- it("error handler works through two levels of nesting", async () => {
170
- const root = new Hono<HonoEnv>();
171
- root.onError(errorHandler);
172
-
173
- const level1 = new Hono<HonoEnv>();
174
- level1.onError(errorHandler);
175
-
176
- const level2 = new Hono<HonoEnv>();
177
- level2.onError(errorHandler);
178
-
179
- level2.get("/deep", () => {
180
- throw ApiError.unauthorized("Deep error");
181
- });
182
-
183
- level1.route("/nested", level2);
184
- root.route("/api", level1);
185
-
186
- const res = await root.request("/api/nested/deep");
187
- expect(res.status).toBe(401);
188
- const body = await res.json() as { error: { message: string } };
189
- expect(body.error.message).toBe("Deep error");
190
- });
191
- });
192
-
193
- // ── Data router simulation ──────────────────────────────────────
194
-
195
- describe("Data router error propagation (simulates init.ts pattern)", () => {
196
- function createDataApp() {
197
- const app = new Hono<HonoEnv>();
198
- app.onError(errorHandler);
199
-
200
- const dataRouter = new Hono<HonoEnv>();
201
- dataRouter.onError(errorHandler);
202
-
203
- dataRouter.get("/posts/:id", () => {
204
- throw ApiError.notFound("Entity not found");
205
- });
206
- dataRouter.post("/posts", () => {
207
- throw ApiError.badRequest("Validation failed", "INVALID_INPUT");
208
- });
209
-
210
- app.route("/api/data", dataRouter);
211
- return app;
212
- }
213
-
214
- it("returns 404 for entity not found", async () => {
215
- const app = createDataApp();
216
- const res = await app.request("/api/data/posts/999");
217
- expect(res.status).toBe(404);
218
- });
219
-
220
- it("returns 400 for validation errors", async () => {
221
- const app = createDataApp();
222
- const res = await app.request("/api/data/posts", { method: "POST" });
223
- expect(res.status).toBe(400);
224
- });
225
- });
226
- });
@@ -1,133 +0,0 @@
1
- import { Hono } from "hono";
2
- import { ApiError, errorHandler } from "../src/api/errors";
3
- import { HonoEnv } from "../src/api/types";
4
-
5
- describe("Error Handler (Hono)", () => {
6
- function createApp() {
7
- const app = new Hono<HonoEnv>();
8
- app.onError(errorHandler);
9
-
10
- // Test routes that throw different errors
11
- app.get("/bad-request", () => {
12
- throw ApiError.badRequest("Missing required field", "MISSING_FIELD", { field: "email" });
13
- });
14
- app.get("/unauthorized", () => {
15
- throw ApiError.unauthorized("Token expired", "TOKEN_EXPIRED");
16
- });
17
- app.get("/forbidden", () => {
18
- throw ApiError.forbidden("Admin only", "FORBIDDEN");
19
- });
20
- app.get("/not-found", () => {
21
- throw ApiError.notFound("Entity not found");
22
- });
23
- app.get("/conflict", () => {
24
- throw ApiError.conflict("Email already exists", "EMAIL_EXISTS");
25
- });
26
- app.get("/internal", () => {
27
- throw ApiError.internal("Database connection failed");
28
- });
29
- app.get("/service-unavailable", () => {
30
- throw ApiError.serviceUnavailable("Feature not configured");
31
- });
32
- app.get("/generic-error", () => {
33
- throw new Error("Something went wrong");
34
- });
35
- app.get("/error-with-code", () => {
36
- const err = new Error("Rate limited") as Error & { code: string };
37
- err.code = "RATE_LIMITED";
38
- throw err;
39
- });
40
-
41
- return app;
42
- }
43
-
44
- it("formats ApiError with correct status and body structure", async () => {
45
- const app = createApp();
46
- const res = await app.request("/bad-request");
47
- expect(res.status).toBe(400);
48
- const body = await res.json() as any;
49
- expect(body.error.message).toBe("Missing required field");
50
- expect(body.error.code).toBe("MISSING_FIELD");
51
- expect(body.error.details).toEqual({ field: "email" });
52
- });
53
-
54
- it("handles 401 Unauthorized", async () => {
55
- const app = createApp();
56
- const res = await app.request("/unauthorized");
57
- expect(res.status).toBe(401);
58
- const body = await res.json() as any;
59
- expect(body.error.code).toBe("TOKEN_EXPIRED");
60
- });
61
-
62
- it("handles 403 Forbidden", async () => {
63
- const app = createApp();
64
- const res = await app.request("/forbidden");
65
- expect(res.status).toBe(403);
66
- const body = await res.json() as any;
67
- expect(body.error.code).toBe("FORBIDDEN");
68
- });
69
-
70
- it("handles 404 Not Found", async () => {
71
- const app = createApp();
72
- const res = await app.request("/not-found");
73
- expect(res.status).toBe(404);
74
- const body = await res.json() as any;
75
- expect(body.error.code).toBe("NOT_FOUND");
76
- });
77
-
78
- it("handles 409 Conflict", async () => {
79
- const app = createApp();
80
- const res = await app.request("/conflict");
81
- expect(res.status).toBe(409);
82
- const body = await res.json() as any;
83
- expect(body.error.code).toBe("EMAIL_EXISTS");
84
- });
85
-
86
- it("handles 500 Internal", async () => {
87
- const app = createApp();
88
- const res = await app.request("/internal");
89
- expect(res.status).toBe(500);
90
- });
91
-
92
- it("handles 503 Service Unavailable", async () => {
93
- const app = createApp();
94
- const res = await app.request("/service-unavailable");
95
- expect(res.status).toBe(503);
96
- });
97
-
98
- it("converts generic Error to 500 with INTERNAL_ERROR code", async () => {
99
- const app = createApp();
100
- const res = await app.request("/generic-error");
101
- expect(res.status).toBe(500);
102
- const body = await res.json() as any;
103
- expect(body.error.code).toBe("INTERNAL_ERROR");
104
- expect(body.error.message).toBe("Internal Server Error");
105
- });
106
-
107
- it("maps known error codes to HTTP status codes", async () => {
108
- const app = createApp();
109
- const res = await app.request("/error-with-code");
110
- // RATE_LIMITED is not in the code-to-status map, so it should default to 500
111
- expect(res.status).toBe(500);
112
- });
113
-
114
- it("omits details when not provided", async () => {
115
- const app = createApp();
116
- const res = await app.request("/unauthorized");
117
- const body = await res.json() as any;
118
- expect(body.error.details).toBeUndefined();
119
- });
120
-
121
- it("returns consistent error shape for all error types", async () => {
122
- const app = createApp();
123
- const paths = ["/bad-request", "/unauthorized", "/forbidden", "/not-found", "/internal", "/generic-error"];
124
-
125
- for (const path of paths) {
126
- const res = await app.request(path);
127
- const body = await res.json() as any;
128
- expect(body).toHaveProperty("error");
129
- expect(body.error).toHaveProperty("message");
130
- expect(body.error).toHaveProperty("code");
131
- }
132
- });
133
- });
@@ -1,155 +0,0 @@
1
- import { ApiError, errorHandler } from "../src/api/errors";
2
-
3
- // ── Minimal Hono-context mock ────────────────────────────────────────────
4
- function createMockContext(method = "GET", path = "/test") {
5
- let capturedStatus: number | undefined;
6
- let capturedBody: any;
7
-
8
- const c = {
9
- req: { method,
10
- path },
11
- json: (body: any, status?: number) => {
12
- capturedBody = body;
13
- capturedStatus = status ?? 200;
14
- return new Response(JSON.stringify(body), { status: capturedStatus });
15
- }
16
- } as any;
17
-
18
- return {
19
- c,
20
- getStatus: () => capturedStatus,
21
- getBody: () => capturedBody
22
- };
23
- }
24
-
25
- // ── ApiError class ────────────────────────────────────────────────────────
26
- describe("ApiError", () => {
27
- describe("constructor", () => {
28
- it("should create an error with statusCode, code, message, and details", () => {
29
- const err = new ApiError(422, "VALIDATION_ERROR", "Invalid field", { field: "email" });
30
- expect(err).toBeInstanceOf(Error);
31
- expect(err).toBeInstanceOf(ApiError);
32
- expect(err.statusCode).toBe(422);
33
- expect(err.code).toBe("VALIDATION_ERROR");
34
- expect(err.message).toBe("Invalid field");
35
- expect(err.details).toEqual({ field: "email" });
36
- expect(err.name).toBe("ApiError");
37
- });
38
-
39
- it("should default details to undefined", () => {
40
- const err = new ApiError(400, "BAD_REQUEST", "Bad");
41
- expect(err.details).toBeUndefined();
42
- });
43
- });
44
-
45
- describe("factory methods", () => {
46
- it("badRequest → 400", () => {
47
- const err = ApiError.badRequest("Missing field", "MISSING_FIELD");
48
- expect(err.statusCode).toBe(400);
49
- expect(err.code).toBe("MISSING_FIELD");
50
- });
51
-
52
- it("badRequest uses default code", () => {
53
- const err = ApiError.badRequest("Oops");
54
- expect(err.code).toBe("BAD_REQUEST");
55
- });
56
-
57
- it("unauthorized → 401", () => {
58
- const err = ApiError.unauthorized("Bad token");
59
- expect(err.statusCode).toBe(401);
60
- expect(err.code).toBe("UNAUTHORIZED");
61
- });
62
-
63
- it("forbidden → 403", () => {
64
- const err = ApiError.forbidden("No access");
65
- expect(err.statusCode).toBe(403);
66
- expect(err.code).toBe("FORBIDDEN");
67
- });
68
-
69
- it("notFound → 404", () => {
70
- const err = ApiError.notFound("Entity not found");
71
- expect(err.statusCode).toBe(404);
72
- expect(err.code).toBe("NOT_FOUND");
73
- });
74
-
75
- it("conflict → 409", () => {
76
- const err = ApiError.conflict("Already exists", "EMAIL_EXISTS");
77
- expect(err.statusCode).toBe(409);
78
- expect(err.code).toBe("EMAIL_EXISTS");
79
- });
80
-
81
- it("internal → 500", () => {
82
- const err = ApiError.internal("Boom");
83
- expect(err.statusCode).toBe(500);
84
- expect(err.code).toBe("INTERNAL_ERROR");
85
- });
86
-
87
- it("serviceUnavailable → 503", () => {
88
- const err = ApiError.serviceUnavailable("Down");
89
- expect(err.statusCode).toBe(503);
90
- expect(err.code).toBe("SERVICE_UNAVAILABLE");
91
- });
92
- });
93
- });
94
-
95
- // ── errorHandler (Hono ErrorHandler) ──────────────────────────────────────
96
- describe("errorHandler", () => {
97
- it("should format ApiError with statusCode, code, message", () => {
98
- const { c, getStatus, getBody } = createMockContext();
99
- const err = ApiError.notFound("User not found");
100
- errorHandler(err, c);
101
-
102
- expect(getStatus()).toBe(404);
103
- expect(getBody()).toEqual({
104
- error: { message: "User not found",
105
- code: "NOT_FOUND" }
106
- });
107
- });
108
-
109
- it("should include details when present", () => {
110
- const { c, getBody } = createMockContext();
111
- const err = ApiError.badRequest("Validation failed", "VALIDATION", { fields: ["email"] });
112
- errorHandler(err, c);
113
-
114
- expect(getBody()).toEqual({
115
- error: {
116
- message: "Validation failed",
117
- code: "VALIDATION",
118
- details: { fields: ["email"] }
119
- }
120
- });
121
- });
122
-
123
- it("should handle plain Error with code property", () => {
124
- const { c, getStatus, getBody } = createMockContext();
125
- const err = Object.assign(new Error("Not found"), { code: "NOT_FOUND" });
126
- errorHandler(err, c);
127
-
128
- expect(getStatus()).toBe(404);
129
- expect(getBody()).toEqual({
130
- error: { message: "Not found",
131
- code: "NOT_FOUND" }
132
- });
133
- });
134
-
135
- it("should default to 500 for unknown errors", () => {
136
- const { c, getStatus, getBody } = createMockContext();
137
- const err = new Error("Something broke");
138
- errorHandler(err, c);
139
-
140
- expect(getStatus()).toBe(500);
141
- expect(getBody()).toEqual({
142
- error: { message: "Internal Server Error",
143
- code: "INTERNAL_ERROR" }
144
- });
145
- });
146
-
147
- it("should use statusCode from error if present", () => {
148
- const { c, getStatus } = createMockContext();
149
- const err = Object.assign(new Error("Rate limited"), { statusCode: 429,
150
- code: "RATE_LIMITED" });
151
- errorHandler(err, c);
152
-
153
- expect(getStatus()).toBe(429);
154
- });
155
- });
@@ -1,127 +0,0 @@
1
- import * as fs from "fs";
2
- import * as path from "path";
3
- import * as os from "os";
4
- import { Hono } from "hono";
5
- import { loadFunctionsFromDirectory } from "../src/functions/function-loader";
6
- import { createFunctionRoutes } from "../src/functions/function-routes";
7
-
8
- describe("Function Loader & Routes", () => {
9
- let tempDir: string;
10
-
11
- beforeAll(() => {
12
- // Create temporary directory in OS temp folder
13
- tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "rebase-functions-test-"));
14
-
15
- // 1. Write a valid Hono app default export using CommonJS
16
- fs.writeFileSync(
17
- path.join(tempDir, "valid-app.js"),
18
- `
19
- const { Hono } = require("hono");
20
- const app = new Hono();
21
- app.get("/hello", (c) => c.text("hello from valid-app"));
22
- module.exports = app;
23
- `
24
- );
25
-
26
- // 2. Write a valid Hono app factory default export using CommonJS
27
- fs.writeFileSync(
28
- path.join(tempDir, "valid-factory.js"),
29
- `
30
- const { Hono } = require("hono");
31
- module.exports = () => {
32
- const app = new Hono();
33
- app.post("/hello", (c) => c.text("hello from valid-factory"));
34
- return app;
35
- };
36
- `
37
- );
38
-
39
- // 3. Write an invalid default export
40
- fs.writeFileSync(
41
- path.join(tempDir, "invalid-export.js"),
42
- `
43
- module.exports = { foo: "bar" };
44
- `
45
- );
46
-
47
- // 4. Write a file with no default export (exports is an object but no module.exports default value)
48
- fs.writeFileSync(
49
- path.join(tempDir, "no-default.js"),
50
- `
51
- exports.other = "value";
52
- `
53
- );
54
-
55
- // 5. Write an ignored non-js/ts file
56
- fs.writeFileSync(
57
- path.join(tempDir, "ignored.txt"),
58
- "some random text"
59
- );
60
- });
61
-
62
- afterAll(() => {
63
- // Clean up temp directory
64
- if (tempDir && fs.existsSync(tempDir)) {
65
- fs.rmSync(tempDir, { recursive: true,
66
- force: true });
67
- }
68
- });
69
-
70
- describe("loadFunctionsFromDirectory", () => {
71
- it("should return empty array if directory does not exist", async () => {
72
- const result = await loadFunctionsFromDirectory(path.join(tempDir, "non-existent-folder"));
73
- expect(result).toEqual([]);
74
- });
75
-
76
- it("should load valid apps and factories while ignoring invalid files", async () => {
77
- const loaded = await loadFunctionsFromDirectory(tempDir);
78
-
79
- // We expect exactly 2 functions to be loaded: "valid-app" and "valid-factory"
80
- expect(loaded).toHaveLength(2);
81
-
82
- const names = loaded.map(f => f.name);
83
- expect(names).toContain("valid-app");
84
- expect(names).toContain("valid-factory");
85
-
86
- // Verify they both look like Hono instances
87
- const validApp = loaded.find(f => f.name === "valid-app")!;
88
- expect(typeof validApp.app.fetch).toBe("function");
89
- expect(Array.isArray(validApp.app.routes)).toBe(true);
90
-
91
- const validFactory = loaded.find(f => f.name === "valid-factory")!;
92
- expect(typeof validFactory.app.fetch).toBe("function");
93
- expect(Array.isArray(validFactory.app.routes)).toBe(true);
94
- });
95
- });
96
-
97
- describe("createFunctionRoutes", () => {
98
- it("should mount and route correctly", async () => {
99
- const loaded = await loadFunctionsFromDirectory(tempDir);
100
- const routes = createFunctionRoutes(loaded);
101
-
102
- // 1. Verify GET / lists loaded functions
103
- const listRes = await routes.request("/");
104
- expect(listRes.status).toBe(200);
105
-
106
- const listData = await listRes.json();
107
- expect(listData).toEqual({
108
- functions: [
109
- { name: "valid-app",
110
- endpoint: "/functions/valid-app" },
111
- { name: "valid-factory",
112
- endpoint: "/functions/valid-factory" }
113
- ]
114
- });
115
-
116
- // 2. Verify routing to valid-app
117
- const appRes = await routes.request("/valid-app/hello");
118
- expect(appRes.status).toBe(200);
119
- expect(await appRes.text()).toBe("hello from valid-app");
120
-
121
- // 3. Verify routing to valid-factory
122
- const factoryRes = await routes.request("/valid-factory/hello", { method: "POST" });
123
- expect(factoryRes.status).toBe(200);
124
- expect(await factoryRes.text()).toBe("hello from valid-factory");
125
- });
126
- });
127
- });