appflare 0.1.12 → 0.2.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 (31) hide show
  1. package/Documentation.md +735 -0
  2. package/cli/generate.ts +1 -1
  3. package/cli/templates/auth/README.md +3 -3
  4. package/cli/templates/auth/route-config.ts +1 -18
  5. package/cli/templates/auth/route-handler.ts +1 -18
  6. package/cli/templates/auth/route-request-utils.ts +2 -52
  7. package/cli/templates/auth/route.config.ts +18 -0
  8. package/cli/templates/auth/route.handler.ts +18 -0
  9. package/cli/templates/auth/route.request-utils.ts +55 -0
  10. package/cli/templates/auth/route.ts +2 -2
  11. package/cli/templates/core/README.md +2 -2
  12. package/cli/templates/core/client/appflare.ts +78 -3
  13. package/cli/templates/core/client/handlers.ts +1 -0
  14. package/cli/templates/core/client/index.ts +1 -0
  15. package/cli/templates/core/client/storage.ts +85 -5
  16. package/cli/templates/core/client/types.ts +91 -0
  17. package/cli/templates/core/client-modules/appflare.ts +1 -112
  18. package/cli/templates/core/client-modules/handlers.ts +1 -1
  19. package/cli/templates/core/client-modules/index.ts +1 -7
  20. package/cli/templates/core/client-modules/storage.ts +1 -180
  21. package/cli/templates/core/client-modules/types.ts +1 -145
  22. package/cli/templates/core/client.artifacts.ts +39 -0
  23. package/cli/templates/core/client.ts +4 -39
  24. package/cli/templates/core/server.ts +1 -1
  25. package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +2 -5
  26. package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +4 -11
  27. package/cli/templates/handlers/generators/types/query-definitions.ts +74 -18
  28. package/cli/templates/handlers/generators/types/query-runtime.ts +20 -4
  29. package/package.json +1 -1
  30. /package/cli/templates/core/{client-modules → client}/handlers/index.ts +0 -0
  31. /package/cli/templates/core/{handlers-route.ts → handlers.route.ts} +0 -0
@@ -1,112 +1 @@
1
- export function generateClientAppflareSource(): string {
2
- return `import { createAuthClient, type BetterAuthClientOptions } from "better-auth/client";
3
- import type {
4
- AppflareAuth,
5
- AppflareAuthTokenResolver,
6
- AppflareOptions,
7
- InferredAuthOptions,
8
- StorageClient,
9
- } from "./types";
10
- import type { MutationsClient, QueriesClient } from "./handlers";
11
- import { createStorageClient } from "./storage";
12
- import { createMutationsClient, createQueriesClient } from "./handlers";
13
-
14
- export class Appflare<Options extends BetterAuthClientOptions = InferredAuthOptions> {
15
- public readonly endpoint: string;
16
- public readonly wsEndpoint: string;
17
- public readonly auth: AppflareAuth<Options>;
18
- public readonly storage: StorageClient;
19
- public readonly queries: QueriesClient;
20
- public readonly mutations: MutationsClient;
21
-
22
- public constructor(options: AppflareOptions<Options>) {
23
- this.endpoint = options.endpoint.replace(/\\/$/, "");
24
- this.wsEndpoint = (options.wsEndpoint ?? this.endpoint).replace(/\\/$/, "");
25
- const authOptions = (options.authOptions ?? {}) as Options;
26
- const request = options.fetch ?? fetch;
27
- const onSetAuthToken = options.onSetAuthToken;
28
- const onGetAuthToken: AppflareAuthTokenResolver =
29
- options.onGetAuthToken ?? (() => "");
30
-
31
- const authFetchOptions =
32
- (authOptions as { fetchOptions?: Record<string, unknown> }).fetchOptions ??
33
- {};
34
- const mergedAuthOptions = {
35
- ...(authOptions as Record<string, unknown>),
36
- fetchOptions: {
37
- ...authFetchOptions,
38
- onSuccess: async (ctx: {
39
- response: { headers: { get: (name: string) => string | null } };
40
- }) => {
41
- if (
42
- typeof (authFetchOptions as { onSuccess?: unknown }).onSuccess ===
43
- "function"
44
- ) {
45
- await (
46
- (authFetchOptions as { onSuccess: (ctx: unknown) => unknown }).onSuccess
47
- )(ctx);
48
- }
49
-
50
- const authToken = ctx.response.headers.get("set-auth-token");
51
- if (authToken && onSetAuthToken) {
52
- await onSetAuthToken(authToken);
53
- }
54
- },
55
- onRequest: async (ctx: {
56
- headers: Headers;
57
- }) => {
58
- let nextCtx = ctx;
59
- if (
60
- typeof (authFetchOptions as { onRequest?: unknown }).onRequest ===
61
- "function"
62
- ) {
63
- const maybeCtx = await (
64
- (authFetchOptions as { onRequest: (ctx: unknown) => unknown }).onRequest
65
- )(ctx);
66
- if (
67
- typeof maybeCtx === "object" &&
68
- maybeCtx !== null &&
69
- "headers" in maybeCtx
70
- ) {
71
- nextCtx = maybeCtx as typeof ctx;
72
- }
73
- }
74
-
75
- const authToken = await onGetAuthToken();
76
- if (typeof authToken === "string" && authToken.trim().length > 0) {
77
- nextCtx.headers.set("Authorization", \`Bearer \${authToken.trim()}\`);
78
- }
79
-
80
- return nextCtx;
81
- },
82
- },
83
- };
84
-
85
- this.auth = createAuthClient<Options>({
86
- ...(mergedAuthOptions as Options),
87
- baseURL: \`\${this.endpoint}\${options.authPath ?? "/api/auth"}\`,
88
- fetch: request,
89
- });
90
-
91
- this.storage = createStorageClient(this.endpoint, request, onGetAuthToken);
92
- this.queries = createQueriesClient(
93
- this.endpoint,
94
- options.requestOptions,
95
- this.wsEndpoint,
96
- onGetAuthToken,
97
- );
98
- this.mutations = createMutationsClient(
99
- this.endpoint,
100
- options.requestOptions,
101
- onGetAuthToken,
102
- );
103
- }
104
- }
105
-
106
- export function createAppflare<Options extends BetterAuthClientOptions = InferredAuthOptions>(
107
- options: AppflareOptions<Options>,
108
- ): Appflare<Options> {
109
- return new Appflare(options);
110
- }
111
- `;
112
- }
1
+ export { generateClientAppflareSource } from "../client/appflare";
@@ -1 +1 @@
1
- export { generateClientHandlersSource } from "./handlers/index";
1
+ export { generateClientHandlersSource } from "../client/handlers";
@@ -1,7 +1 @@
1
- export function generateClientIndexSource(): string {
2
- return `export * from "./types";
3
- export * from "./appflare";
4
- export * from "./storage";
5
- export * from "./handlers";
6
- `;
7
- }
1
+ export { generateClientIndexSource } from "../client/index";
@@ -1,180 +1 @@
1
- export function generateClientStorageSource(): string {
2
- return `import type { StorageClient, StorageSignedUrlResponse, StorageListResponse } from "./types";
3
-
4
- type AuthTokenResolver = (() => string | Promise<string>) | undefined;
5
-
6
- function toHeaderRecord(headers: HeadersInit | undefined): Record<string, string> {
7
- const result: Record<string, string> = {};
8
- if (!headers) {
9
- return result;
10
- }
11
-
12
- if (Array.isArray(headers)) {
13
- for (const entry of headers) {
14
- if (!Array.isArray(entry) || entry.length < 2) {
15
- continue;
16
- }
17
- result[String(entry[0])] = String(entry[1]);
18
- }
19
- return result;
20
- }
21
-
22
- if (typeof (headers as { forEach?: unknown }).forEach === "function") {
23
- (headers as Headers).forEach((value, key) => {
24
- result[key] = String(value);
25
- });
26
- return result;
27
- }
28
-
29
- for (const [key, value] of Object.entries(headers as Record<string, unknown>)) {
30
- result[key] = String(value ?? "");
31
- }
32
-
33
- return result;
34
- }
35
-
36
- function hasAuthorizationHeader(headers: Record<string, string>): boolean {
37
- for (const key of Object.keys(headers)) {
38
- if (key.toLowerCase() === "authorization") {
39
- return true;
40
- }
41
- }
42
-
43
- return false;
44
- }
45
-
46
- async function createAuthorizedHeaders(
47
- headers: HeadersInit | undefined,
48
- onGetAuthToken: AuthTokenResolver,
49
- ): Promise<HeadersInit | undefined> {
50
- const resolvedHeaders = toHeaderRecord(headers);
51
- if (onGetAuthToken) {
52
- const authToken = await onGetAuthToken();
53
- if (typeof authToken === "string" && authToken.trim().length > 0) {
54
- if (!hasAuthorizationHeader(resolvedHeaders)) {
55
- resolvedHeaders.authorization = \`Bearer \${authToken.trim()}\`;
56
- }
57
- }
58
- }
59
-
60
- return resolvedHeaders;
61
- }
62
-
63
- export function createStorageClient(
64
- endpoint: string,
65
- request: typeof fetch = fetch,
66
- onGetAuthToken?: () => string | Promise<string>,
67
- ): StorageClient {
68
- return {
69
- upload: async (args) => {
70
- const headers = await createAuthorizedHeaders(
71
- {
72
- "content-type": "application/json",
73
- },
74
- onGetAuthToken,
75
- );
76
-
77
- const response = await request(\`\${endpoint}/storage/upload\`, {
78
- method: "POST",
79
- headers,
80
- body: JSON.stringify(args),
81
- });
82
- if (!response.ok) {
83
- throw new Error(await response.text());
84
- }
85
-
86
- return (await response.json()) as StorageSignedUrlResponse;
87
- },
88
- download: async (args) => {
89
- const query = new URLSearchParams({
90
- path: args.path,
91
- ...(args.fileName ? { fileName: args.fileName } : {}),
92
- ...(typeof args.expiresIn === "number"
93
- ? { expiresIn: String(args.expiresIn) }
94
- : {}),
95
- });
96
- const response = await request(
97
- \`\${endpoint}/storage/download?\${query.toString()}\`,
98
- {
99
- headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
100
- },
101
- );
102
- if (!response.ok) {
103
- throw new Error(await response.text());
104
- }
105
-
106
- return (await response.json()) as StorageSignedUrlResponse;
107
- },
108
- preview: async (args) => {
109
- const query = new URLSearchParams({
110
- path: args.path,
111
- ...(typeof args.expiresIn === "number"
112
- ? { expiresIn: String(args.expiresIn) }
113
- : {}),
114
- });
115
- const response = await request(
116
- \`\${endpoint}/storage/preview?\${query.toString()}\`,
117
- {
118
- headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
119
- },
120
- );
121
- if (!response.ok) {
122
- throw new Error(await response.text());
123
- }
124
-
125
- return (await response.json()) as StorageSignedUrlResponse;
126
- },
127
- delete: async (args) => {
128
- const query = new URLSearchParams({
129
- path: args.path,
130
- });
131
- const response = await request(
132
- \`\${endpoint}/storage/object?\${query.toString()}\`,
133
- {
134
- method: "DELETE",
135
- headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
136
- },
137
- );
138
- if (!response.ok) {
139
- throw new Error(await response.text());
140
- }
141
-
142
- return (await response.json()) as { ok: boolean; path: string };
143
- },
144
- list: async (args = {}) => {
145
- const query = new URLSearchParams();
146
- if (args.prefix) {
147
- query.set("prefix", args.prefix);
148
- }
149
- if (args.cursor) {
150
- query.set("cursor", args.cursor);
151
- }
152
- if (typeof args.limit === "number") {
153
- query.set("limit", String(args.limit));
154
- }
155
- if (args.delimiter) {
156
- query.set("delimiter", args.delimiter);
157
- }
158
- if (args.method) {
159
- query.set("method", args.method);
160
- }
161
-
162
- const querySuffix = query.toString();
163
- const response = await request(
164
- querySuffix.length > 0
165
- ? \`\${endpoint}/storage/list?\${querySuffix}\`
166
- : \`\${endpoint}/storage/list\`,
167
- {
168
- headers: await createAuthorizedHeaders(undefined, onGetAuthToken),
169
- },
170
- );
171
- if (!response.ok) {
172
- throw new Error(await response.text());
173
- }
174
-
175
- return (await response.json()) as StorageListResponse;
176
- },
177
- };
178
- }
179
- `;
180
- }
1
+ export { generateClientStorageSource } from "../client/storage";
@@ -1,145 +1 @@
1
- export function generateClientTypesSource(configPathImport: string): string {
2
- return `import { createAuthClient, type BetterAuthClientOptions } from "better-auth/client";
3
- import type appflareConfig from "${configPathImport}";
4
-
5
- export type AppflareConfig = typeof appflareConfig;
6
- export type InferredAuthOptions = AppflareConfig["auth"]["clientOptions"];
7
-
8
- export type AppflareAuth<Options extends BetterAuthClientOptions = InferredAuthOptions> = ReturnType<
9
- typeof createAuthClient<Options>
10
- >;
11
-
12
- export type AppflareAuthTokenResolver = () => string | Promise<string>;
13
-
14
- export type AppflareOptions<Options extends BetterAuthClientOptions = InferredAuthOptions> = {
15
- endpoint: string;
16
- wsEndpoint?: string;
17
- authPath?: string;
18
- authOptions?: Options;
19
- fetch?: typeof fetch;
20
- requestOptions?: AppflareResultRouteCallOptions;
21
- onSetAuthToken?: (token: string) => void | Promise<void>;
22
- onGetAuthToken?: AppflareAuthTokenResolver;
23
- };
24
-
25
- export type AppflareErrorMode = "throw" | "return";
26
-
27
- export type AppflareRequestError = {
28
- route: string;
29
- method: "GET" | "POST";
30
- status: number;
31
- message: string;
32
- body?: unknown;
33
- responseText?: string;
34
- };
35
-
36
- export type AppflareRequestResult<TData> = {
37
- data: TData | null;
38
- error: AppflareRequestError | null;
39
- };
40
-
41
- export type AppflareRouteCallOptions<
42
- Mode extends AppflareErrorMode = "throw",
43
- > = {
44
- errorMode?: Mode;
45
- headers?: HeadersInit;
46
- signal?: AbortSignal;
47
- onError?: (error: AppflareRequestError) => void;
48
- };
49
-
50
- export type AppflareResultRouteCallOptions = Omit<
51
- AppflareRouteCallOptions<"return">,
52
- "errorMode"
53
- >;
54
-
55
- export type AppflareRouteClient<
56
- TSchema extends import("zod").ZodObject<import("zod").ZodRawShape>,
57
- TOutput,
58
- > = {
59
- schema: TSchema;
60
- run(
61
- args: import("zod").input<TSchema>,
62
- options?: AppflareResultRouteCallOptions,
63
- ): Promise<AppflareRequestResult<TOutput>>;
64
- };
65
-
66
- export type AppflareRealtimeSubscription = {
67
- remove: () => void;
68
- };
69
-
70
- export type AppflareRealtimeQueryUpdate<TOutput> = {
71
- event: "query:update";
72
- payload: {
73
- queryName: string;
74
- signature: string;
75
- data: TOutput;
76
- };
77
- };
78
-
79
- export type AppflareQuerySubscribeOptions<TInput, TOutput> = {
80
- args?: TInput;
81
- authToken?: string;
82
- onChange: (data: TOutput, update: AppflareRealtimeQueryUpdate<TOutput>) => void;
83
- onError?: (error: unknown) => void;
84
- requestOptions?: AppflareRouteCallOptions;
85
- signal?: AbortSignal;
86
- };
87
-
88
- export type AppflareQueryRouteClient<
89
- TSchema extends import("zod").ZodObject<import("zod").ZodRawShape>,
90
- TOutput,
91
- > = AppflareRouteClient<TSchema, TOutput> & {
92
- subscribe: (
93
- options: AppflareQuerySubscribeOptions<import("zod").input<TSchema>, TOutput>,
94
- ) => AppflareRealtimeSubscription;
95
- };
96
-
97
- export type StorageSignedUrlResponse = {
98
- url: string;
99
- method: "GET" | "PUT" | "DELETE";
100
- path: string;
101
- disposition?: "attachment" | "inline";
102
- expiresIn?: number;
103
- };
104
-
105
- export type StorageListResponse = unknown;
106
-
107
- export type StorageClient = {
108
- upload: (args: {
109
- path: string;
110
- contentType?: string;
111
- expiresIn?: number;
112
- }) => Promise<StorageSignedUrlResponse>;
113
- download: (args: {
114
- path: string;
115
- expiresIn?: number;
116
- fileName?: string;
117
- }) => Promise<StorageSignedUrlResponse>;
118
- preview: (args: {
119
- path: string;
120
- expiresIn?: number;
121
- }) => Promise<StorageSignedUrlResponse>;
122
- delete: (args: { path: string }) => Promise<{ ok: boolean; path: string }>;
123
- list: (args?: {
124
- prefix?: string;
125
- cursor?: string;
126
- limit?: number;
127
- delimiter?: string;
128
- method?: "download" | "get" | "delete" | "list" | "put" | "preview";
129
- }) => Promise<StorageListResponse>;
130
- };
131
-
132
- export type RealtimeSubscriptionResponse = {
133
- token: string;
134
- signature: string;
135
- websocket: {
136
- url: string;
137
- protocol: string;
138
- params: {
139
- tokenParam: "token";
140
- authTokenParam: "authToken";
141
- };
142
- };
143
- };
144
- `;
145
- }
1
+ export { generateClientTypesSource } from "../client/types";
@@ -0,0 +1,39 @@
1
+ import { generateClientAppflareSource } from "./client/appflare";
2
+ import { generateClientHandlersSource } from "./client/handlers";
3
+ import { generateClientIndexSource } from "./client/index";
4
+ import { generateClientStorageSource } from "./client/storage";
5
+ import { generateClientTypesSource } from "./client/types";
6
+ import type { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
7
+
8
+ export type GeneratedClientArtifact = {
9
+ relativePath: string;
10
+ source: string;
11
+ };
12
+
13
+ export function generateClientArtifacts(
14
+ configPathImport: string,
15
+ operations: DiscoveredHandlerOperation[],
16
+ ): GeneratedClientArtifact[] {
17
+ return [
18
+ {
19
+ relativePath: "client/index.ts",
20
+ source: generateClientIndexSource(),
21
+ },
22
+ {
23
+ relativePath: "client/types.ts",
24
+ source: generateClientTypesSource(configPathImport),
25
+ },
26
+ {
27
+ relativePath: "client/storage.ts",
28
+ source: generateClientStorageSource(),
29
+ },
30
+ {
31
+ relativePath: "client/handlers.ts",
32
+ source: generateClientHandlersSource(operations),
33
+ },
34
+ {
35
+ relativePath: "client/appflare.ts",
36
+ source: generateClientAppflareSource(),
37
+ },
38
+ ];
39
+ }
@@ -1,39 +1,4 @@
1
- import { generateClientAppflareSource } from "./client-modules/appflare";
2
- import { generateClientHandlersSource } from "./client-modules/handlers";
3
- import { generateClientIndexSource } from "./client-modules/index";
4
- import { generateClientStorageSource } from "./client-modules/storage";
5
- import { generateClientTypesSource } from "./client-modules/types";
6
- import type { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
7
-
8
- export type GeneratedClientArtifact = {
9
- relativePath: string;
10
- source: string;
11
- };
12
-
13
- export function generateClientArtifacts(
14
- configPathImport: string,
15
- operations: DiscoveredHandlerOperation[],
16
- ): GeneratedClientArtifact[] {
17
- return [
18
- {
19
- relativePath: "client/index.ts",
20
- source: generateClientIndexSource(),
21
- },
22
- {
23
- relativePath: "client/types.ts",
24
- source: generateClientTypesSource(configPathImport),
25
- },
26
- {
27
- relativePath: "client/storage.ts",
28
- source: generateClientStorageSource(),
29
- },
30
- {
31
- relativePath: "client/handlers.ts",
32
- source: generateClientHandlersSource(operations),
33
- },
34
- {
35
- relativePath: "client/appflare.ts",
36
- source: generateClientAppflareSource(),
37
- },
38
- ];
39
- }
1
+ export {
2
+ generateClientArtifacts,
3
+ type GeneratedClientArtifact,
4
+ } from "./client.artifacts";
@@ -1,7 +1,7 @@
1
1
  import { generateAuthRoute } from "../auth/route";
2
2
  import { generateAppCreation } from "./app-creation";
3
3
  import { generateExport } from "./export";
4
- import { generateHandlersRoute } from "./handlers-route";
4
+ import { generateHandlersRoute } from "./handlers.route";
5
5
  import { generateImports } from "./imports";
6
6
  import { generateTypes } from "./types";
7
7
 
@@ -52,13 +52,10 @@ async function publishMutationEvents(
52
52
  options,
53
53
  subscription.authToken,
54
54
  );
55
- if (!authSession) {
56
- continue;
57
- }
58
55
 
59
56
  const subscriberCtx = await createExecutionContext(c as never, options);
60
- subscriberCtx.user = authSession.user as never;
61
- subscriberCtx.session = authSession.session as never;
57
+ subscriberCtx.user = authSession?.user as never;
58
+ subscriberCtx.session = authSession?.session as never;
62
59
 
63
60
  try {
64
61
  const parsedArgs = operation.schema.parse(subscription.args);
@@ -35,9 +35,7 @@ function registerRealtimeRoutes(
35
35
  options,
36
36
  authToken,
37
37
  );
38
- if (!authSession) {
39
- return c.json({ message: "Invalid auth token" }, 401);
40
- }
38
+
41
39
 
42
40
  let args: Record<string, unknown>;
43
41
  try {
@@ -68,7 +66,7 @@ function registerRealtimeRoutes(
68
66
  queryName,
69
67
  args,
70
68
  authToken,
71
- userId: extractUserId(authSession.user),
69
+ userId: extractUserId(authSession?.user),
72
70
  createdAt: Date.now(),
73
71
  }),
74
72
  }),
@@ -107,9 +105,7 @@ function registerRealtimeRoutes(
107
105
  options,
108
106
  authToken,
109
107
  );
110
- if (!authSession) {
111
- return c.json({ message: "Invalid auth token" }, 401);
112
- }
108
+
113
109
 
114
110
  const stub = getRealtimeStub(c.env, options);
115
111
  if (!stub) {
@@ -146,7 +142,7 @@ function registerRealtimeRoutes(
146
142
  const token = c.req.query("token") ?? "";
147
143
  const authToken = c.req.query("authToken") ?? "";
148
144
 
149
- if (!token || !authToken) {
145
+ if (!token) {
150
146
  return c.json({ message: "token and authToken are required" }, 400);
151
147
  }
152
148
 
@@ -156,9 +152,6 @@ function registerRealtimeRoutes(
156
152
  options,
157
153
  authToken,
158
154
  );
159
- if (!authSession) {
160
- return c.json({ message: "Invalid auth token" }, 401);
161
- }
162
155
 
163
156
  const stub = getRealtimeStub(c.env, options);
164
157
  if (!stub) {