@unifiedcommerce/core 0.3.2 → 0.3.4

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.
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/auth/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA0B/C,wBAAgB,cAAc,CAC5B,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,cAAc,GACrB,iBAAiB,CAqInB"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/auth/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA0B/C,wBAAgB,cAAc,CAC5B,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,cAAc,GACrB,iBAAiB,CAsInB"}
@@ -34,6 +34,7 @@ export function authMiddleware(auth, config) {
34
34
  try {
35
35
  const org = await auth.api.getFullOrganization({
36
36
  query: { organizationId: orgId ?? DEFAULT_ORG_ID },
37
+ headers: c.req.raw.headers,
37
38
  });
38
39
  if (org?.members) {
39
40
  const membership = org.members.find((m) => m.userId === session.user.id);
@@ -1,69 +1,29 @@
1
+ import { betterAuth } from "better-auth";
2
+ import { apiKey } from "@better-auth/api-key";
3
+ import { organization, twoFactor, phoneNumber, jwt, bearer } from "better-auth/plugins";
1
4
  import type { CommerceConfig } from "../config/types.js";
2
5
  import type { DatabaseAdapter } from "../kernel/database/adapter.js";
3
- /** Member shape returned by Better Auth's organization plugin */
4
- export interface OrgMember {
5
- id: string;
6
- userId: string;
7
- organizationId: string;
8
- role: string;
9
- createdAt: string;
10
- }
11
- export interface AuthInstance {
12
- handler(request: Request): Promise<Response>;
13
- api: {
14
- getSession(input: {
15
- headers: Headers;
16
- }): Promise<unknown>;
17
- getActiveMemberRole?: (input: {
18
- headers: Headers;
19
- }) => Promise<{
20
- role: string;
21
- } | null>;
22
- getFullOrganization?: (input: {
23
- query: {
24
- organizationId: string;
25
- };
26
- }) => Promise<{
27
- id: string;
28
- name: string;
29
- members: OrgMember[];
30
- } | null>;
31
- listMembers?: (input: {
32
- query: {
33
- organizationId: string;
34
- };
35
- }) => Promise<OrgMember[]>;
36
- verifyApiKey?: (input: {
37
- body: {
38
- key: string;
39
- permissions?: Record<string, string[]>;
40
- };
41
- }) => Promise<{
42
- valid: boolean;
43
- error: {
44
- message: string;
45
- code: string;
46
- } | null;
47
- key: Record<string, unknown> | null;
48
- }>;
49
- createApiKey?: (input: {
50
- body: {
51
- configId?: string;
52
- name?: string;
53
- permissions?: Record<string, string[]>;
54
- userId?: string;
55
- organizationId?: string;
56
- };
57
- headers?: Headers;
58
- }) => Promise<{
59
- key: string;
60
- id: string;
61
- }>;
62
- /** Allow access to other Better Auth API methods added by plugins */
63
- [key: string]: unknown;
64
- };
65
- options?: Record<string, unknown>;
66
- $context?: Promise<unknown>;
67
- }
6
+ /**
7
+ * The auth type is inferred from `typeof betterAuth(...)` with all plugins enabled.
8
+ * This gives us the full API surface without manual interface maintenance.
9
+ *
10
+ * We use a type-level-only reference (never executed) to capture the complete type.
11
+ */
12
+ type FullBetterAuth = ReturnType<typeof betterAuth<{
13
+ plugins: [
14
+ ReturnType<typeof organization>,
15
+ ReturnType<typeof bearer>,
16
+ ReturnType<typeof jwt>,
17
+ ReturnType<typeof twoFactor>,
18
+ ReturnType<typeof apiKey>,
19
+ ReturnType<typeof phoneNumber>
20
+ ];
21
+ }>>;
22
+ /**
23
+ * AuthInstance — inferred from Better Auth with all plugins.
24
+ * No manual type maintenance needed.
25
+ */
26
+ export type AuthInstance = FullBetterAuth;
68
27
  export declare function createAuth(db: DatabaseAdapter, config: CommerceConfig): AuthInstance;
28
+ export {};
69
29
  //# sourceMappingURL=setup.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/auth/setup.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAgCrE,iEAAiE;AACjE,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,GAAG,EAAE;QACH,UAAU,CAAC,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,CAAA;SAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;QAG1D,mBAAmB,CAAC,EAAE,CAAC,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,CAAA;SAAE,KAAK,OAAO,CAAC;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,GAAG,IAAI,CAAC,CAAC;QACxF,mBAAmB,CAAC,EAAE,CAAC,KAAK,EAAE;YAAE,KAAK,EAAE;gBAAE,cAAc,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,KAAK,OAAO,CAAC;YAC9E,EAAE,EAAE,MAAM,CAAC;YACX,IAAI,EAAE,MAAM,CAAC;YACb,OAAO,EAAE,SAAS,EAAE,CAAC;SACtB,GAAG,IAAI,CAAC,CAAC;QACV,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE;YAAE,KAAK,EAA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Y,CAuHd"}
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/auth/setup.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAiBrE;;;;;GAKG;AACH,KAAK,cAAc,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC;IACjD,OAAO,EAAE;QACP,UAAU,CAAC,OAAO,YAAY,CAAC;QAC/B,UAAU,CAAC,OAAO,MAAM,CAAC;QACzB,UAAU,CAAC,OAAO,GAAG,CAAC;QACtB,UAAU,CAAC,OAAO,SAAS,CAAC;QAC5B,UAAU,CAAC,OAAO,MAAM,CAAC;QACzB,UAAU,CAAC,OAAO,WAAW,CAAC;KAC/B,CAAC;CACH,CAAC,CAAC,CAAC;AAEJ;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC;AAE1C,wBAAgB,UAAU,CACxB,EAAE,EAAE,eAAe,EACnB,MAAM,EAAE,cAAc,GACrB,YAAY,CA+Gd"}
@@ -4,24 +4,17 @@ import { apiKey } from "@better-auth/api-key";
4
4
  import { organization, twoFactor, phoneNumber, jwt, bearer } from "better-auth/plugins";
5
5
  import * as authSchema from "./auth-schema.js";
6
6
  function resolveAuthDbProvider(provider) {
7
- if (provider === "postgres" ||
8
- provider === "postgresql" ||
9
- provider === "pg") {
7
+ if (provider === "postgres" || provider === "postgresql" || provider === "pg")
10
8
  return "pg";
11
- }
12
- if (provider === "mysql") {
9
+ if (provider === "mysql")
13
10
  return "mysql";
14
- }
15
- if (provider === "sqlite") {
11
+ if (provider === "sqlite")
16
12
  return "sqlite";
17
- }
18
- throw new Error(`Unsupported auth database provider "${provider}". Expected one of: postgres, mysql, sqlite.`);
13
+ throw new Error(`Unsupported auth database provider "${provider}".`);
19
14
  }
20
15
  export function createAuth(db, config) {
21
16
  const plugins = [
22
17
  organization({
23
- // Better Auth's Role includes `authorize` and `statements` fields that
24
- // our RoleDefinition doesn't have. Double-cast is required — upstream type gap.
25
18
  roles: (config.auth?.roles ?? {}),
26
19
  }),
27
20
  bearer(),
@@ -30,7 +23,6 @@ export function createAuth(db, config) {
30
23
  if (config.auth?.twoFactor?.enabled) {
31
24
  plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
32
25
  }
33
- // Configure API key plugin — one config per defined scope, or a single default config.
34
26
  const scopes = config.auth?.apiKeyScopes;
35
27
  if (scopes && Object.keys(scopes).length > 0) {
36
28
  const apiKeyConfigs = Object.entries(scopes).map(([scopeId, scope]) => ({
@@ -62,11 +54,8 @@ export function createAuth(db, config) {
62
54
  },
63
55
  }));
64
56
  }
65
- // API key support can be attached via external plugin package in newer better-auth versions.
66
57
  try {
67
58
  const auth = betterAuth({
68
- // Better Auth's drizzle adapter expects a plain object, not PgDatabase.
69
- // Double-cast required — PgDatabase has no index signature.
70
59
  database: drizzleAdapter(db.db, {
71
60
  provider: resolveAuthDbProvider(db.provider),
72
61
  schema: authSchema,
@@ -100,7 +89,7 @@ export function createAuth(db, config) {
100
89
  updateAge: 60 * 60 * 24,
101
90
  cookieCache: {
102
91
  enabled: true,
103
- maxAge: 60 * 5, // 5 minute cookie cache for performance
92
+ maxAge: 60 * 5,
104
93
  },
105
94
  },
106
95
  advanced: {
@@ -115,10 +104,8 @@ export function createAuth(db, config) {
115
104
  },
116
105
  },
117
106
  });
118
- // Better Auth's plugin-extended return type is structurally incompatible with
119
- // our simplified AuthInstance interface (upstream generic union vs our narrowed shape).
120
- // Double-cast is the accepted pattern — same approach used by PayloadCMS.
121
- // @see https://github.com/better-auth/better-auth/discussions
107
+ // The runtime return matches FullBetterAuth structurally plugins may differ
108
+ // at runtime (conditional) but the API surface is a superset.
122
109
  return auth;
123
110
  }
124
111
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unifiedcommerce/core",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -50,6 +50,7 @@ export function authMiddleware(
50
50
  try {
51
51
  const org = await auth.api.getFullOrganization({
52
52
  query: { organizationId: orgId ?? DEFAULT_ORG_ID },
53
+ headers: c.req.raw.headers,
53
54
  });
54
55
  if (org?.members) {
55
56
  const membership = org.members.find(
package/src/auth/setup.ts CHANGED
@@ -10,80 +10,39 @@ import * as authSchema from "./auth-schema.js";
10
10
  type BetterAuthDbProvider = "pg" | "mysql" | "sqlite";
11
11
 
12
12
  function resolveAuthDbProvider(provider: string): BetterAuthDbProvider {
13
- if (
14
- provider === "postgres" ||
15
- provider === "postgresql" ||
16
- provider === "pg"
17
- ) {
18
- return "pg";
19
- }
20
- if (provider === "mysql") {
21
- return "mysql";
22
- }
23
- if (provider === "sqlite") {
24
- return "sqlite";
25
- }
26
- throw new Error(
27
- `Unsupported auth database provider "${provider}". Expected one of: postgres, mysql, sqlite.`,
28
- );
13
+ if (provider === "postgres" || provider === "postgresql" || provider === "pg") return "pg";
14
+ if (provider === "mysql") return "mysql";
15
+ if (provider === "sqlite") return "sqlite";
16
+ throw new Error(`Unsupported auth database provider "${provider}".`);
29
17
  }
30
18
 
31
19
  interface AuthEmailPayload {
32
- user: {
33
- email: string;
34
- name: string | null;
35
- };
20
+ user: { email: string; name: string | null };
36
21
  url: string;
37
22
  }
38
23
 
39
- /** Member shape returned by Better Auth's organization plugin */
40
- export interface OrgMember {
41
- id: string;
42
- userId: string;
43
- organizationId: string;
44
- role: string;
45
- createdAt: string;
46
- }
47
-
48
- export interface AuthInstance {
49
- handler(request: Request): Promise<Response>;
50
- api: {
51
- getSession(input: { headers: Headers }): Promise<unknown>;
52
-
53
- // Organization plugin methods
54
- getActiveMemberRole?: (input: { headers: Headers }) => Promise<{ role: string } | null>;
55
- getFullOrganization?: (input: { query: { organizationId: string } }) => Promise<{
56
- id: string;
57
- name: string;
58
- members: OrgMember[];
59
- } | null>;
60
- listMembers?: (input: { query: { organizationId: string } }) => Promise<OrgMember[]>;
61
-
62
- // API key plugin methods
63
- verifyApiKey?: (input: {
64
- body: { key: string; permissions?: Record<string, string[]> };
65
- }) => Promise<{
66
- valid: boolean;
67
- error: { message: string; code: string } | null;
68
- key: Record<string, unknown> | null;
69
- }>;
70
- createApiKey?: (input: {
71
- body: {
72
- configId?: string;
73
- name?: string;
74
- permissions?: Record<string, string[]>;
75
- userId?: string;
76
- organizationId?: string;
77
- };
78
- headers?: Headers;
79
- }) => Promise<{ key: string; id: string }>;
24
+ /**
25
+ * The auth type is inferred from `typeof betterAuth(...)` with all plugins enabled.
26
+ * This gives us the full API surface without manual interface maintenance.
27
+ *
28
+ * We use a type-level-only reference (never executed) to capture the complete type.
29
+ */
30
+ type FullBetterAuth = ReturnType<typeof betterAuth<{
31
+ plugins: [
32
+ ReturnType<typeof organization>,
33
+ ReturnType<typeof bearer>,
34
+ ReturnType<typeof jwt>,
35
+ ReturnType<typeof twoFactor>,
36
+ ReturnType<typeof apiKey>,
37
+ ReturnType<typeof phoneNumber>,
38
+ ];
39
+ }>>;
80
40
 
81
- /** Allow access to other Better Auth API methods added by plugins */
82
- [key: string]: unknown;
83
- };
84
- options?: Record<string, unknown>;
85
- $context?: Promise<unknown>;
86
- }
41
+ /**
42
+ * AuthInstance — inferred from Better Auth with all plugins.
43
+ * No manual type maintenance needed.
44
+ */
45
+ export type AuthInstance = FullBetterAuth;
87
46
 
88
47
  export function createAuth(
89
48
  db: DatabaseAdapter,
@@ -98,8 +57,6 @@ export function createAuth(
98
57
  | ReturnType<typeof bearer>
99
58
  > = [
100
59
  organization({
101
- // Better Auth's Role includes `authorize` and `statements` fields that
102
- // our RoleDefinition doesn't have. Double-cast is required — upstream type gap.
103
60
  roles: (config.auth?.roles ?? {}) as unknown as Record<string, Role | undefined>,
104
61
  }),
105
62
  bearer(),
@@ -110,7 +67,6 @@ export function createAuth(
110
67
  plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
111
68
  }
112
69
 
113
- // Configure API key plugin — one config per defined scope, or a single default config.
114
70
  const scopes = config.auth?.apiKeyScopes;
115
71
  if (scopes && Object.keys(scopes).length > 0) {
116
72
  const apiKeyConfigs = Object.entries(scopes).map(([scopeId, scope]) => ({
@@ -143,12 +99,8 @@ export function createAuth(
143
99
  }));
144
100
  }
145
101
 
146
- // API key support can be attached via external plugin package in newer better-auth versions.
147
-
148
102
  try {
149
103
  const auth = betterAuth({
150
- // Better Auth's drizzle adapter expects a plain object, not PgDatabase.
151
- // Double-cast required — PgDatabase has no index signature.
152
104
  database: drizzleAdapter(db.db as unknown as Record<string, unknown>, {
153
105
  provider: resolveAuthDbProvider(db.provider),
154
106
  schema: authSchema,
@@ -180,7 +132,7 @@ export function createAuth(
180
132
  updateAge: 60 * 60 * 24,
181
133
  cookieCache: {
182
134
  enabled: true,
183
- maxAge: 60 * 5, // 5 minute cookie cache for performance
135
+ maxAge: 60 * 5,
184
136
  },
185
137
  },
186
138
  advanced: {
@@ -195,10 +147,9 @@ export function createAuth(
195
147
  },
196
148
  },
197
149
  });
198
- // Better Auth's plugin-extended return type is structurally incompatible with
199
- // our simplified AuthInstance interface (upstream generic union vs our narrowed shape).
200
- // Double-cast is the accepted pattern same approach used by PayloadCMS.
201
- // @see https://github.com/better-auth/better-auth/discussions
150
+
151
+ // The runtime return matches FullBetterAuth structurally plugins may differ
152
+ // at runtime (conditional) but the API surface is a superset.
202
153
  return auth as unknown as AuthInstance;
203
154
  } catch (error) {
204
155
  const message =