@velajs/better-auth 0.2.2 → 0.3.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.
package/README.md CHANGED
@@ -145,15 +145,15 @@ class AppModule {}
145
145
 
146
146
  ## Calling better-auth from services & controllers
147
147
 
148
- Inject the auth instance via the `BETTER_AUTH` token. The full `auth.api.*` surface is available list sessions, revoke, impersonate, anything better-auth exposes server-side.
148
+ Inject `BetterAuthService` — a normal `@Injectable()` exposing the underlying better-auth instance plus convenience getters for `.api` and `.handler`. The full `auth.api.*` surface is available: list sessions, revoke, impersonate, anything better-auth exposes server-side.
149
149
 
150
150
  ```ts
151
151
  import { Inject, Injectable } from '@velajs/vela';
152
- import { BETTER_AUTH, type BetterAuthInstance } from '@velajs/better-auth';
152
+ import { BetterAuthService } from '@velajs/better-auth';
153
153
 
154
154
  @Injectable()
155
155
  class AdminUserService {
156
- constructor(@Inject(BETTER_AUTH) private readonly auth: BetterAuthInstance) {}
156
+ constructor(@Inject(BetterAuthService) private readonly auth: BetterAuthService) {}
157
157
 
158
158
  listSessions(userId: string) {
159
159
  return this.auth.api.listUserSessions({ userId });
@@ -164,6 +164,8 @@ class AdminUserService {
164
164
  }
165
165
  ```
166
166
 
167
+ Under `forRootAsync`, the underlying `betterAuth({...})` instance is constructed **lazily on first `.auth` / `.api` / `.handler` access**. That's what makes Cloudflare bindings (D1, KV, R2) work: the user factory only runs after request-time middleware has populated env. No proxies, no lifecycle hooks — just a service with a cached field.
168
+
167
169
  ## Decorators
168
170
 
169
171
  | Decorator | Purpose |
@@ -210,13 +212,13 @@ Set `mountHandler: false` and mount the catch-all yourself if you need a base pa
210
212
 
211
213
  ```ts
212
214
  import { Controller, All, Req, Inject, Injectable } from '@velajs/vela';
213
- import { BETTER_AUTH, Public } from '@velajs/better-auth';
215
+ import { BetterAuthService, Public } from '@velajs/better-auth';
214
216
 
215
217
  @Public(true)
216
218
  @Controller('/auth')
217
219
  @Injectable()
218
220
  class CustomCatchallController {
219
- constructor(@Inject(BETTER_AUTH) private auth: BetterAuthInstance) {}
221
+ constructor(@Inject(BetterAuthService) private auth: BetterAuthService) {}
220
222
  @All('/*') handle(@Req() c: Context) { return this.auth.handler(c.req.raw); }
221
223
  }
222
224
  ```
@@ -1,7 +1,21 @@
1
- import type { Context } from 'hono';
2
- import { BetterAuthService } from './better-auth.service';
3
- export declare class BetterAuthCatchallController {
4
- private readonly auth;
5
- constructor(auth: BetterAuthService);
6
- handle(c: Context): Promise<Response>;
7
- }
1
+ import { type Type } from '@velajs/vela';
2
+ /**
3
+ * Build a catch-all controller that mounts better-auth's handler at `basePath`
4
+ * (default `/api/auth`). This is a factory because vela reads a controller's
5
+ * route off the class at decoration time, so a custom base path needs its own
6
+ * decorated class — the path can't be parametrized on a single shared class.
7
+ *
8
+ * Two base paths to keep consistent:
9
+ * - this `basePath` is RELATIVE to vela's `globalPrefix` (always prepended);
10
+ * - better-auth routes against its OWN absolute `basePath` (the one you pass to
11
+ * `betterAuth({ basePath })`), which must equal `globalPrefix + basePath`.
12
+ *
13
+ * Both default to `/api/auth`, so the no-prefix / no-config case just works.
14
+ */
15
+ export declare function createBetterAuthCatchallController(basePath?: string): Type;
16
+ /**
17
+ * Default-path (`/api/auth`) catch-all controller. Retained for back-compat;
18
+ * `BetterAuthModule` now mounts {@link createBetterAuthCatchallController} with
19
+ * the configured `basePath`. Prefer the factory for a custom base path.
20
+ */
21
+ export declare const BetterAuthCatchallController: Type;
@@ -15,34 +15,54 @@ function _ts_param(paramIndex, decorator) {
15
15
  import { All, Controller, Inject, Injectable, Req } from "@velajs/vela";
16
16
  import { BetterAuthService } from "./better-auth.service.js";
17
17
  import { Public } from "./decorators/public.decorator.js";
18
- export class BetterAuthCatchallController {
19
- auth;
20
- // Inject the service its `.handler` getter triggers lazy construction
21
- // of the underlying betterAuth() instance on first access, AFTER any
22
- // runtime adapter middleware (Cloudflare env capture) has run.
23
- constructor(auth){
24
- this.auth = auth;
25
- }
26
- async handle(c) {
27
- return this.auth.handler(c.req.raw);
28
- }
18
+ /**
19
+ * Build a catch-all controller that mounts better-auth's handler at `basePath`
20
+ * (default `/api/auth`). This is a factory because vela reads a controller's
21
+ * route off the class at decoration time, so a custom base path needs its own
22
+ * decorated class the path can't be parametrized on a single shared class.
23
+ *
24
+ * Two base paths to keep consistent:
25
+ * - this `basePath` is RELATIVE to vela's `globalPrefix` (always prepended);
26
+ * - better-auth routes against its OWN absolute `basePath` (the one you pass to
27
+ * `betterAuth({ basePath })`), which must equal `globalPrefix + basePath`.
28
+ *
29
+ * Both default to `/api/auth`, so the no-prefix / no-config case just works.
30
+ */ export function createBetterAuthCatchallController(basePath = '/api/auth') {
31
+ let BetterAuthCatchallController = class BetterAuthCatchallController {
32
+ auth;
33
+ // Inject the service — its `.handler` getter triggers lazy construction
34
+ // of the underlying betterAuth() instance on first access, AFTER any
35
+ // runtime adapter middleware (Cloudflare env capture) has run.
36
+ constructor(auth){
37
+ this.auth = auth;
38
+ }
39
+ async handle(c) {
40
+ return this.auth.handler(c.req.raw);
41
+ }
42
+ };
43
+ _ts_decorate([
44
+ All('/*'),
45
+ _ts_param(0, Req()),
46
+ _ts_metadata("design:type", Function),
47
+ _ts_metadata("design:paramtypes", [
48
+ typeof Context === "undefined" ? Object : Context
49
+ ]),
50
+ _ts_metadata("design:returntype", Promise)
51
+ ], BetterAuthCatchallController.prototype, "handle", null);
52
+ BetterAuthCatchallController = _ts_decorate([
53
+ Public(true),
54
+ Controller(basePath),
55
+ Injectable(),
56
+ _ts_param(0, Inject(BetterAuthService)),
57
+ _ts_metadata("design:type", Function),
58
+ _ts_metadata("design:paramtypes", [
59
+ typeof BetterAuthService === "undefined" ? Object : BetterAuthService
60
+ ])
61
+ ], BetterAuthCatchallController);
62
+ return BetterAuthCatchallController;
29
63
  }
30
- _ts_decorate([
31
- All('/*'),
32
- _ts_param(0, Req()),
33
- _ts_metadata("design:type", Function),
34
- _ts_metadata("design:paramtypes", [
35
- typeof Context === "undefined" ? Object : Context
36
- ]),
37
- _ts_metadata("design:returntype", Promise)
38
- ], BetterAuthCatchallController.prototype, "handle", null);
39
- BetterAuthCatchallController = _ts_decorate([
40
- Public(true),
41
- Controller('/api/auth'),
42
- Injectable(),
43
- _ts_param(0, Inject(BetterAuthService)),
44
- _ts_metadata("design:type", Function),
45
- _ts_metadata("design:paramtypes", [
46
- typeof BetterAuthService === "undefined" ? Object : BetterAuthService
47
- ])
48
- ], BetterAuthCatchallController);
64
+ /**
65
+ * Default-path (`/api/auth`) catch-all controller. Retained for back-compat;
66
+ * `BetterAuthModule` now mounts {@link createBetterAuthCatchallController} with
67
+ * the configured `basePath`. Prefer the factory for a custom base path.
68
+ */ export const BetterAuthCatchallController = createBetterAuthCatchallController();
@@ -1,5 +1,5 @@
1
1
  import { APP_GUARD, stableHash } from "@velajs/vela";
2
- import { BetterAuthCatchallController } from "./better-auth.controller.js";
2
+ import { createBetterAuthCatchallController } from "./better-auth.controller.js";
3
3
  import { BetterAuthService, BETTER_AUTH_BUILDER } from "./better-auth.service.js";
4
4
  import { BETTER_AUTH_OPTIONS } from "./better-auth.tokens.js";
5
5
  import { AuthGuard } from "./guards/auth.guard.js";
@@ -24,7 +24,7 @@ export class BetterAuthModule {
24
24
  }),
25
25
  providers,
26
26
  controllers: normalized.mountHandler ? [
27
- BetterAuthCatchallController
27
+ createBetterAuthCatchallController(normalized.basePath)
28
28
  ] : [],
29
29
  exports: [
30
30
  BetterAuthService,
@@ -100,7 +100,7 @@ export class BetterAuthModule {
100
100
  imports: options.imports ?? [],
101
101
  providers,
102
102
  controllers: mountHandler ? [
103
- BetterAuthCatchallController
103
+ createBetterAuthCatchallController(basePath)
104
104
  ] : [],
105
105
  exports: [
106
106
  BetterAuthService,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { BetterAuthModule } from './better-auth.module';
2
2
  export { BetterAuthService } from './better-auth.service';
3
- export { BetterAuthCatchallController } from './better-auth.controller';
3
+ export { BetterAuthCatchallController, createBetterAuthCatchallController, } from './better-auth.controller';
4
4
  export { BETTER_AUTH_OPTIONS, AUTH_USER_KEY, AUTH_SESSION_KEY, } from './better-auth.tokens';
5
5
  export { AuthGuard } from './guards/auth.guard';
6
6
  export { RolesGuard } from './guards/roles.guard';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Module
2
2
  export { BetterAuthModule } from "./better-auth.module.js";
3
3
  export { BetterAuthService } from "./better-auth.service.js";
4
- export { BetterAuthCatchallController } from "./better-auth.controller.js";
4
+ export { BetterAuthCatchallController, createBetterAuthCatchallController } from "./better-auth.controller.js";
5
5
  // Tokens & symbols
6
6
  export { BETTER_AUTH_OPTIONS, AUTH_USER_KEY, AUTH_SESSION_KEY } from "./better-auth.tokens.js";
7
7
  // Guards
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velajs/better-auth",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "better-auth integration for the Vela framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -17,6 +17,13 @@
17
17
  "LICENSE",
18
18
  "CHANGELOG.md"
19
19
  ],
20
+ "scripts": {
21
+ "build": "rm -rf dist && swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
22
+ "test": "vitest run",
23
+ "typecheck": "tsc --noEmit",
24
+ "prepare": "swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
25
+ "prepublishOnly": "pnpm run typecheck && pnpm test"
26
+ },
20
27
  "sideEffects": false,
21
28
  "keywords": [
22
29
  "vela",
@@ -47,19 +54,21 @@
47
54
  "hono": ">=4"
48
55
  },
49
56
  "devDependencies": {
50
- "@swc/cli": "^0.8.0",
51
- "@swc/core": "^1.15.11",
57
+ "@swc/cli": "^0.8.1",
58
+ "@swc/core": "^1.15.41",
52
59
  "@velajs/testing": "^0.2.1",
53
- "@velajs/vela": "^1.8.2",
54
- "better-auth": "^1.2.0",
55
- "hono": "^4",
56
- "typescript": "^5",
60
+ "@velajs/vela": "^1.8.5",
61
+ "better-auth": "^1.6.20",
62
+ "hono": "^4.12.26",
63
+ "typescript": "^6.0.3",
57
64
  "unplugin-swc": "^1.5.9",
58
- "vitest": "^4.0.18"
65
+ "vitest": "^4.1.9"
59
66
  },
60
- "scripts": {
61
- "build": "rm -rf dist && swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
62
- "test": "vitest run",
63
- "typecheck": "tsc --noEmit"
67
+ "packageManager": "pnpm@10.20.0",
68
+ "pnpm": {
69
+ "onlyBuiltDependencies": [
70
+ "@swc/core",
71
+ "esbuild"
72
+ ]
64
73
  }
65
- }
74
+ }