appflare 0.2.18 → 0.2.20

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/cli/generate.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { mkdir } from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
2
3
  import { relative, resolve } from "node:path";
3
4
  import { generateAuthConfigSource } from "./templates/auth/config";
4
5
  import { generateClientArtifacts } from "./templates/core/client.artifacts";
@@ -154,4 +155,21 @@ export async function generateArtifacts(
154
155
  if (exitCode !== 0) {
155
156
  throw new Error(`better-auth generation failed with exit code ${exitCode}`);
156
157
  }
158
+
159
+ const tsconfigPath = resolve(configDir, "tsconfig.json");
160
+ if (existsSync(tsconfigPath)) {
161
+ const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
162
+ const tsBuild = Bun.spawn([npxCmd, "tsc", "--build"], {
163
+ cwd: configDir,
164
+ stdout: "inherit",
165
+ stderr: "inherit",
166
+ });
167
+
168
+ const tsBuildExitCode = await tsBuild.exited;
169
+ if (tsBuildExitCode !== 0) {
170
+ throw new Error(
171
+ `TypeScript build failed with exit code ${tsBuildExitCode}`,
172
+ );
173
+ }
174
+ }
157
175
  }
@@ -2,7 +2,7 @@ export function buildStoragePageRuntime(): string {
2
2
  return `
3
3
  const buildStorageListingContent = (listed: any, prefix: string) => {
4
4
  const parts = prefix.split('/').filter(Boolean);
5
- const breadcrumbs = [];
5
+ const breadcrumbs: any[] = [];
6
6
  let currentPath = '';
7
7
  const visibleObjects = listed.objects.filter((obj: any) => !obj.key.endsWith('/.keep'));
8
8
 
@@ -170,7 +170,7 @@ export function buildTableGetRoute(
170
170
  \t\tlet countQuery = db.select({ count: sql\`count(*)\` }).from(tableSchema);
171
171
 
172
172
  \t\tif (search) {
173
- \t\t\tconst searchConditions = [];
173
+ const searchConditions: any[] = [];
174
174
  \t\t\t${searchConditions}
175
175
  \t\t\tif (searchConditions.length > 0) {
176
176
  \t\t\t\tquery = query.where(or(...searchConditions)) as any;
@@ -103,7 +103,7 @@ export function buildTablePostRoutes(
103
103
  \t\tif (mode === 'all-matching') {
104
104
  \t\t\tlet deleteQuery = db.delete(tableSchema);
105
105
  \t\t\tif (search) {
106
- \t\t\t\tconst searchConditions = [];
106
+ const searchConditions: any[] = [];
107
107
  \t\t\t\t${searchConditions}
108
108
  \t\t\t\tif (searchConditions.length > 0) {
109
109
  \t\t\t\t\tdeleteQuery = deleteQuery.where(or(...searchConditions)) as any;
@@ -25,7 +25,7 @@ export function handleOperationError(
25
25
  validationMessage: string,
26
26
  ): Response {
27
27
  if (error instanceof AppflareHandledError) {
28
- return c.json(error.payload, error.status);
28
+ return c.json(error.payload, error.status as any);
29
29
  }
30
30
 
31
31
  if (error instanceof ZodError) {
@@ -1,9 +1,9 @@
1
1
  export function generateContextCreation(defaultR2Binding?: string): string {
2
2
  return `
3
- export function createSchedulerExecutionContext(
3
+ export async function createSchedulerExecutionContext(
4
4
  env: Record<string, unknown>,
5
5
  options: RegisterHandlersOptions,
6
- ): AppflareContext {
6
+ ): Promise<AppflareContext> {
7
7
  const database = env[options.databaseBinding] as D1Database;
8
8
  const r2Binding = options.r2Binding ?? ${JSON.stringify(defaultR2Binding ?? "")};
9
9
  const storageBucket = r2Binding
@@ -11,9 +11,17 @@ export function createSchedulerExecutionContext(
11
11
  : undefined;
12
12
  const db = createDb(database);
13
13
  const mutationEvents = [] as AppflareContext["mutationEvents"];
14
+ const kvNamespace = options.kvBinding
15
+ ? (env[options.kvBinding] as KVNamespace)
16
+ : undefined;
14
17
  const schedulerBinding = options.schedulerBinding ?? "APPFLARE_SCHEDULER_QUEUE";
15
18
  const schedulerQueue = env[schedulerBinding] as SchedulerQueueBinding | undefined;
16
19
  const helpers = createContextErrorHelpers();
20
+ const auth = createAuth({
21
+ DATABASE: database,
22
+ KV: kvNamespace,
23
+ });
24
+ const authAdapter = (await auth.$context).internalAdapter;
17
25
  const schedulerContext = {
18
26
  env: env as WorkerEnv["Bindings"],
19
27
  } as unknown as Context<WorkerEnv>;
@@ -27,6 +35,7 @@ export function createSchedulerExecutionContext(
27
35
  mutationEvents,
28
36
  user: null as never,
29
37
  session: null as never,
38
+ auth: authAdapter,
30
39
  context: schedulerContext,
31
40
  scheduler: createScheduler(schedulerQueue),
32
41
  storage: null as never,
@@ -60,6 +69,14 @@ export async function createExecutionContext(
60
69
  const schedulerBinding = options.schedulerBinding ?? "APPFLARE_SCHEDULER_QUEUE";
61
70
  const schedulerQueue = c.env[schedulerBinding] as SchedulerQueueBinding | undefined;
62
71
  const helpers = createContextErrorHelpers();
72
+ const auth = createAuth(
73
+ {
74
+ DATABASE: database,
75
+ KV: kvNamespace,
76
+ },
77
+ c.req.raw.cf as IncomingRequestCfProperties | undefined,
78
+ );
79
+ const authAdapter = (await auth.$context).internalAdapter;
63
80
  const ctx = {
64
81
  $db: db,
65
82
  db: createQueryDb(db, {
@@ -70,6 +87,7 @@ export async function createExecutionContext(
70
87
  mutationEvents,
71
88
  user,
72
89
  session,
90
+ auth: authAdapter,
73
91
  context: c,
74
92
  scheduler: createScheduler(schedulerQueue),
75
93
  storage: null as never,
@@ -9,14 +9,10 @@ export async function executeCronTriggers(
9
9
  return;
10
10
  }
11
11
 
12
- if (cronHandlers.length === 0) {
13
- return;
14
- }
15
-
16
- const ctx = createSchedulerExecutionContext(env, options);
12
+ const ctx = await createSchedulerExecutionContext(env, options);
17
13
 
18
14
  for (const cronEntry of cronHandlers) {
19
- if (!cronEntry.cronTriggers.includes(cronValue)) {
15
+ if (!(cronEntry.cronTriggers as readonly string[]).includes(cronValue)) {
20
16
  continue;
21
17
  }
22
18
 
@@ -131,7 +131,7 @@ function registerRealtimeRoutes(
131
131
  } | null;
132
132
  return c.json(
133
133
  { message: payload?.message ?? "Unable to remove subscription" },
134
- response.status,
134
+ response.status as any,
135
135
  );
136
136
  }
137
137
 
@@ -17,7 +17,7 @@ export async function executeScheduledBatch(
17
17
  return;
18
18
  }
19
19
 
20
- const ctx = createSchedulerExecutionContext(env, options);
20
+ const ctx = await createSchedulerExecutionContext(env, options);
21
21
 
22
22
  for (const message of batch.messages) {
23
23
  const body = (message?.body ?? {}) as QueueMessageBody;
@@ -29,7 +29,7 @@ export async function executeScheduledBatch(
29
29
 
30
30
  const operation = (schedulerHandlers as Record<string, {
31
31
  definition: {
32
- handler: (ctx: typeof ctx, args: unknown) => Promise<void> | void;
32
+ handler: (ctx: AppflareContext, args: unknown) => Promise<void> | void;
33
33
  };
34
34
  schema: z.ZodTypeAny;
35
35
  }>)[task];
@@ -57,7 +57,7 @@ export function registerGeneratedStorageRoutes(
57
57
  }, 200);
58
58
  } catch (error) {
59
59
  if (error instanceof AppflareHandledError) {
60
- return c.json(error.payload, error.status);
60
+ return c.json(error.payload, error.status as any);
61
61
  }
62
62
 
63
63
  return c.json(
@@ -89,7 +89,7 @@ export function registerGeneratedStorageRoutes(
89
89
  }, 200);
90
90
  } catch (error) {
91
91
  if (error instanceof AppflareHandledError) {
92
- return c.json(error.payload, error.status);
92
+ return c.json(error.payload, error.status as any);
93
93
  }
94
94
 
95
95
  return c.json(
@@ -119,7 +119,7 @@ export function registerGeneratedStorageRoutes(
119
119
  }, 200);
120
120
  } catch (error) {
121
121
  if (error instanceof AppflareHandledError) {
122
- return c.json(error.payload, error.status);
122
+ return c.json(error.payload, error.status as any);
123
123
  }
124
124
 
125
125
  return c.json(
@@ -137,7 +137,7 @@ export function registerGeneratedStorageRoutes(
137
137
  return c.json({ ok: true, path }, 200);
138
138
  } catch (error) {
139
139
  if (error instanceof AppflareHandledError) {
140
- return c.json(error.payload, error.status);
140
+ return c.json(error.payload, error.status as any);
141
141
  }
142
142
 
143
143
  return c.json(
@@ -181,7 +181,7 @@ export function registerGeneratedStorageRoutes(
181
181
  return c.json(result, 200);
182
182
  } catch (error) {
183
183
  if (error instanceof AppflareHandledError) {
184
- return c.json(error.payload, error.status);
184
+ return c.json(error.payload, error.status as any);
185
185
  }
186
186
 
187
187
  return c.json(
@@ -1,5 +1,6 @@
1
1
  export function generateTypesContextSection(): string {
2
2
  return `type AuthSession = typeof auth.$Infer.Session;
3
+ type AuthAdapter = Awaited<typeof auth.$context>["internalAdapter"];
3
4
  type User = AuthSession['user']
4
5
  type Session = AuthSession['session']
5
6
 
@@ -55,6 +56,7 @@ export type AppflareContext = {
55
56
  mutationEvents: DbMutationEvent[];
56
57
  user: User;
57
58
  session: Session;
59
+ auth: AuthAdapter;
58
60
  context: Context<WorkerEnv>;
59
61
  scheduler: Scheduler;
60
62
  storage: AppflareStorage;
@@ -481,11 +481,7 @@ function hasManyToManyRelationsInWith(
481
481
  }
482
482
 
483
483
  const nestedWith = relationValue.with;
484
- if (
485
- hasManyToManyRelationsInWith(relationName, nestedWith) ||
486
- (manyToMany &&
487
- hasManyToManyRelationsInWith(manyToMany.targetTable, nestedWith))
488
- ) {
484
+ if (hasManyToManyRelationsInWith(relationName, nestedWith)) {
489
485
  return true;
490
486
  }
491
487
  }
@@ -577,7 +573,7 @@ type RelationWithAggregatePlan = Record<string, RelationWithAggregatePlanEntry>;
577
573
 
578
574
  function hasRelationAggregatePlanEntries(
579
575
  plan: RelationWithAggregatePlan | undefined,
580
- ): boolean {
576
+ ): plan is RelationWithAggregatePlan {
581
577
  return !!plan && Object.keys(plan).length > 0;
582
578
  }
583
579