@samebase/convex-hosting 0.0.0-stage → 0.3.1
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/CHANGELOG.md +133 -0
- package/INTEGRATION.md +399 -0
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +471 -2
- package/dist/cli/args.d.ts +28 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +148 -0
- package/dist/cli/args.js.map +1 -0
- package/dist/cli/argumentChunks.d.ts +3 -0
- package/dist/cli/argumentChunks.d.ts.map +1 -0
- package/dist/cli/argumentChunks.js +29 -0
- package/dist/cli/argumentChunks.js.map +1 -0
- package/dist/cli/collectFiles.d.ts +10 -0
- package/dist/cli/collectFiles.d.ts.map +1 -0
- package/dist/cli/collectFiles.js +53 -0
- package/dist/cli/collectFiles.js.map +1 -0
- package/dist/cli/commands.d.ts +8 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +56 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/componentName.d.ts +32 -0
- package/dist/cli/componentName.d.ts.map +1 -0
- package/dist/cli/componentName.js +43 -0
- package/dist/cli/componentName.js.map +1 -0
- package/dist/cli/deploy.d.ts +16 -0
- package/dist/cli/deploy.d.ts.map +1 -0
- package/dist/cli/deploy.js +283 -0
- package/dist/cli/deploy.js.map +1 -0
- package/dist/cli/deployEnvironment.d.ts +10 -0
- package/dist/cli/deployEnvironment.d.ts.map +1 -0
- package/dist/cli/deployEnvironment.js +12 -0
- package/dist/cli/deployEnvironment.js.map +1 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +60 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/upload.d.ts +22 -0
- package/dist/cli/upload.d.ts.map +1 -0
- package/dist/cli/upload.js +630 -0
- package/dist/cli/upload.js.map +1 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/index.d.ts +119 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +272 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +38 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +43 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/http.d.ts +4 -0
- package/dist/component/http.d.ts.map +1 -0
- package/dist/component/http.js +125 -0
- package/dist/component/http.js.map +1 -0
- package/dist/component/lib.d.ts +164 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +768 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +84 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +69 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/serving.d.ts +17 -0
- package/dist/component/serving.d.ts.map +1 -0
- package/dist/component/serving.js +150 -0
- package/dist/component/serving.js.map +1 -0
- package/dist/react/index.d.ts +59 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +137 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +78 -3
- package/src/cli/args.test.ts +79 -0
- package/src/cli/args.ts +153 -0
- package/src/cli/argumentChunks.test.ts +27 -0
- package/src/cli/argumentChunks.ts +40 -0
- package/src/cli/collectFiles.test.ts +26 -0
- package/src/cli/collectFiles.ts +59 -0
- package/src/cli/commands.ts +68 -0
- package/src/cli/componentName.test.ts +41 -0
- package/src/cli/componentName.ts +46 -0
- package/src/cli/deploy.ts +336 -0
- package/src/cli/deployEnvironment.test.ts +27 -0
- package/src/cli/deployEnvironment.ts +17 -0
- package/src/cli/index.ts +65 -0
- package/src/cli/upload.ts +837 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/index.test.ts +510 -0
- package/src/client/index.ts +355 -0
- package/src/client/setup.test.ts +22 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +55 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/http.test.ts +223 -0
- package/src/component/http.ts +144 -0
- package/src/component/lib.test.ts +1007 -0
- package/src/component/lib.ts +862 -0
- package/src/component/schema.ts +75 -0
- package/src/component/serving.ts +158 -0
- package/src/component/setup.test.ts +11 -0
- package/src/react/index.tsx +197 -0
- package/src/test.ts +23 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {
|
|
2
|
+
httpActionGeneric,
|
|
3
|
+
queryGeneric,
|
|
4
|
+
type GenericActionCtx,
|
|
5
|
+
type GenericDataModel,
|
|
6
|
+
type HttpRouter,
|
|
7
|
+
} from "convex/server";
|
|
8
|
+
import { v, type GenericId } from "convex/values";
|
|
9
|
+
import type { ComponentApi } from "../component/_generated/component.js";
|
|
10
|
+
import {
|
|
11
|
+
cacheControlFor,
|
|
12
|
+
decodeRequestPath,
|
|
13
|
+
etagMatches,
|
|
14
|
+
getMimeType,
|
|
15
|
+
getSetupHtml,
|
|
16
|
+
isHtmlContentType,
|
|
17
|
+
} from "../component/serving.js";
|
|
18
|
+
|
|
19
|
+
const deploymentInfoValidator = v.object({
|
|
20
|
+
_id: v.string(),
|
|
21
|
+
_creationTime: v.number(),
|
|
22
|
+
currentDeploymentId: v.string(),
|
|
23
|
+
deployedAt: v.number(),
|
|
24
|
+
spaFallback: v.optional(v.boolean()),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
type CdnBaseUrl = string | ((request: Request) => string);
|
|
28
|
+
|
|
29
|
+
/** The parts of an action context needed to serve a static file. */
|
|
30
|
+
type AssetServingCtx = Pick<GenericActionCtx<GenericDataModel>, "runQuery" | "storage">;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Register app-owned HTTP routes for the hosted site.
|
|
34
|
+
*
|
|
35
|
+
* Prefer mounting the component's own HTTP routes when it can own the URL
|
|
36
|
+
* prefix. Use this compatibility mode when existing app HTTP routes must stay
|
|
37
|
+
* at the same root. Exact app routes take precedence over this catch-all.
|
|
38
|
+
* Assets and deployment state still live in the component.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // convex/convex.config.ts
|
|
43
|
+
* const app = defineApp();
|
|
44
|
+
* app.use(hosting); // no httpPrefix: the app owns HTTP routing
|
|
45
|
+
*
|
|
46
|
+
* // convex/http.ts
|
|
47
|
+
* const http = httpRouter();
|
|
48
|
+
* auth.addHttpRoutes(http);
|
|
49
|
+
* registerStaticRoutes(http, components.hosting);
|
|
50
|
+
* export default http;
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function registerStaticRoutes(
|
|
54
|
+
http: HttpRouter,
|
|
55
|
+
component: ComponentApi,
|
|
56
|
+
{
|
|
57
|
+
pathPrefix = "/",
|
|
58
|
+
spaFallback,
|
|
59
|
+
cdnBaseUrl,
|
|
60
|
+
}: {
|
|
61
|
+
/** URL prefix where the app should serve static files. */
|
|
62
|
+
pathPrefix?: string;
|
|
63
|
+
/** Override the deployment's SPA fallback setting. */
|
|
64
|
+
spaFallback?: boolean;
|
|
65
|
+
/** Optional custom base URL for convex-fs blob redirects. */
|
|
66
|
+
cdnBaseUrl?: CdnBaseUrl;
|
|
67
|
+
} = {},
|
|
68
|
+
) {
|
|
69
|
+
if (!pathPrefix.startsWith("/")) {
|
|
70
|
+
throw new Error("pathPrefix must start with /");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const normalizedPrefix = pathPrefix === "/" ? "" : pathPrefix.replace(/\/$/, "");
|
|
74
|
+
|
|
75
|
+
const serveStaticFile = httpActionGeneric(async (ctx, request) => {
|
|
76
|
+
const decodedPath = decodeRequestPath(new URL(request.url).pathname);
|
|
77
|
+
if (decodedPath === null) {
|
|
78
|
+
return new Response("Bad Request", {
|
|
79
|
+
status: 400,
|
|
80
|
+
headers: { "Content-Type": "text/plain" },
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
let path = decodedPath;
|
|
84
|
+
|
|
85
|
+
if (normalizedPrefix && path.startsWith(normalizedPrefix)) {
|
|
86
|
+
path = path.slice(normalizedPrefix.length) || "/";
|
|
87
|
+
}
|
|
88
|
+
if (path === "" || path === "/") {
|
|
89
|
+
path = "/index.html";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const response = await serveAsset(ctx, component, request, path, {
|
|
93
|
+
spaFallback,
|
|
94
|
+
cdnBaseUrl,
|
|
95
|
+
});
|
|
96
|
+
if (response) return response;
|
|
97
|
+
|
|
98
|
+
if (path === "/index.html") {
|
|
99
|
+
return new Response(getSetupHtml(), {
|
|
100
|
+
status: 503,
|
|
101
|
+
headers: {
|
|
102
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
103
|
+
"Cache-Control": "no-store",
|
|
104
|
+
"Retry-After": "5",
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return new Response("Not Found", {
|
|
109
|
+
status: 404,
|
|
110
|
+
headers: { "Content-Type": "text/plain" },
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
http.route({
|
|
115
|
+
pathPrefix: pathPrefix === "/" ? "/" : `${normalizedPrefix}/`,
|
|
116
|
+
method: "GET",
|
|
117
|
+
handler: serveStaticFile,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (normalizedPrefix) {
|
|
121
|
+
http.route({
|
|
122
|
+
path: normalizedPrefix,
|
|
123
|
+
method: "GET",
|
|
124
|
+
handler: serveStaticFile,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Serve an uploaded static file from your own HTTP action, or return null when
|
|
131
|
+
* no file matches so the action can continue with its own routing: server
|
|
132
|
+
* rendering, prerendered pages, redirects.
|
|
133
|
+
*
|
|
134
|
+
* Found files get the same response as `registerStaticRoutes`: content type,
|
|
135
|
+
* cache headers, ETag revalidation, and legacy CDN redirects. A found file
|
|
136
|
+
* whose storage can't be read gets a 500 response. A request path with
|
|
137
|
+
* malformed percent-encoding matches no file, so it returns null.
|
|
138
|
+
*
|
|
139
|
+
* The lookup is exact by default. `/` is not mapped to `/index.html`, and there
|
|
140
|
+
* is no SPA fallback, so the SPA shell never answers for a route your action
|
|
141
|
+
* handles. To serve the shell, pass `{ path: "/index.html" }`. When your route
|
|
142
|
+
* has a prefix, pass `path` with the prefix removed.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* // convex/http.ts
|
|
147
|
+
* const http = httpRouter();
|
|
148
|
+
* http.route({
|
|
149
|
+
* pathPrefix: "/",
|
|
150
|
+
* method: "GET",
|
|
151
|
+
* handler: httpAction(async (ctx, request) => {
|
|
152
|
+
* const file = await serveStaticAsset(ctx, components.hosting, request);
|
|
153
|
+
* if (file) return file;
|
|
154
|
+
* return await renderPage(request);
|
|
155
|
+
* }),
|
|
156
|
+
* });
|
|
157
|
+
* export default http;
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export async function serveStaticAsset(
|
|
161
|
+
ctx: AssetServingCtx,
|
|
162
|
+
component: ComponentApi,
|
|
163
|
+
request: Request,
|
|
164
|
+
{
|
|
165
|
+
path,
|
|
166
|
+
spaFallback = false,
|
|
167
|
+
cdnBaseUrl,
|
|
168
|
+
}: {
|
|
169
|
+
/**
|
|
170
|
+
* Decoded file path to look up, starting with `/`, such as
|
|
171
|
+
* `/about/index.html`. Defaults to the decoded request path.
|
|
172
|
+
*/
|
|
173
|
+
path?: string;
|
|
174
|
+
/** Serve `/index.html` for a missing path without a file extension. */
|
|
175
|
+
spaFallback?: boolean;
|
|
176
|
+
/** Optional custom base URL for convex-fs blob redirects. */
|
|
177
|
+
cdnBaseUrl?: CdnBaseUrl;
|
|
178
|
+
} = {},
|
|
179
|
+
): Promise<Response | null> {
|
|
180
|
+
if (path !== undefined && !path.startsWith("/")) {
|
|
181
|
+
throw new Error("path must start with /");
|
|
182
|
+
}
|
|
183
|
+
const filePath = path ?? decodeRequestPath(new URL(request.url).pathname);
|
|
184
|
+
if (filePath === null) return null;
|
|
185
|
+
return await serveAsset(ctx, component, request, filePath, {
|
|
186
|
+
spaFallback,
|
|
187
|
+
cdnBaseUrl,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export { decodeRequestPath };
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Serve the uploaded file at `path` (decoded, relative to the mount prefix),
|
|
195
|
+
* or return null when no file matches. An undefined `spaFallback` uses the
|
|
196
|
+
* deployment's stored setting.
|
|
197
|
+
*/
|
|
198
|
+
async function serveAsset(
|
|
199
|
+
ctx: AssetServingCtx,
|
|
200
|
+
component: ComponentApi,
|
|
201
|
+
request: Request,
|
|
202
|
+
path: string,
|
|
203
|
+
{
|
|
204
|
+
spaFallback,
|
|
205
|
+
cdnBaseUrl,
|
|
206
|
+
}: {
|
|
207
|
+
spaFallback: boolean | undefined;
|
|
208
|
+
cdnBaseUrl: CdnBaseUrl | undefined;
|
|
209
|
+
},
|
|
210
|
+
): Promise<Response | null> {
|
|
211
|
+
const asset = await ctx.runQuery(component.lib.resolveAssetForHttp, {
|
|
212
|
+
path,
|
|
213
|
+
...(spaFallback === undefined ? {} : { spaFallback }),
|
|
214
|
+
});
|
|
215
|
+
if (!asset) return null;
|
|
216
|
+
|
|
217
|
+
const contentType = asset.contentType || getMimeType(path);
|
|
218
|
+
const cacheControl = cacheControlFor(path);
|
|
219
|
+
|
|
220
|
+
if (asset.blobId && !isHtmlContentType(contentType)) {
|
|
221
|
+
const configuredBase = typeof cdnBaseUrl === "function" ? cdnBaseUrl(request) : cdnBaseUrl;
|
|
222
|
+
const baseUrl = configuredBase ?? `${new URL(request.url).origin}/fs/blobs`;
|
|
223
|
+
return new Response(null, {
|
|
224
|
+
status: 302,
|
|
225
|
+
headers: {
|
|
226
|
+
Location: `${baseUrl.replace(/\/$/, "")}/${asset.blobId}`,
|
|
227
|
+
"Cache-Control": cacheControl,
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// TODO(remove in a future major): v1→v2 transitional path. The file still
|
|
233
|
+
// lives in the app's own storage (a same-name migration inherited the v1
|
|
234
|
+
// row; see the component's resolveAssetForHttp). Serve it directly from app
|
|
235
|
+
// storage so the site stays up during migration with no re-upload. Once v1
|
|
236
|
+
// app-storage assets are no longer supported, delete this branch.
|
|
237
|
+
if (asset.appStorageId) {
|
|
238
|
+
if (asset.etag && etagMatches(request.headers.get("If-None-Match"), asset.etag)) {
|
|
239
|
+
return new Response(null, {
|
|
240
|
+
status: 304,
|
|
241
|
+
headers: { ETag: asset.etag, "Cache-Control": cacheControl },
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const blob = await ctx.storage.get(asset.appStorageId as GenericId<"_storage">);
|
|
245
|
+
if (!blob) {
|
|
246
|
+
// The component surfaces appStorageId whenever it can't resolve a
|
|
247
|
+
// storage URL, which also covers a genuinely deleted file (the two are
|
|
248
|
+
// indistinguishable from the component). If it isn't in app storage
|
|
249
|
+
// either, treat it as missing, like an empty deployment, rather than
|
|
250
|
+
// erroring.
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
return new Response(blob, {
|
|
254
|
+
status: 200,
|
|
255
|
+
headers: {
|
|
256
|
+
"Content-Type": contentType,
|
|
257
|
+
"Cache-Control": cacheControl,
|
|
258
|
+
...(asset.etag ? { ETag: asset.etag } : {}),
|
|
259
|
+
"X-Content-Type-Options": "nosniff",
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (!asset.storageUrl) {
|
|
265
|
+
return new Response("Asset not available", {
|
|
266
|
+
status: 500,
|
|
267
|
+
headers: { "Content-Type": "text/plain" },
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (asset.etag && etagMatches(request.headers.get("If-None-Match"), asset.etag)) {
|
|
272
|
+
return new Response(null, {
|
|
273
|
+
status: 304,
|
|
274
|
+
headers: { ETag: asset.etag, "Cache-Control": cacheControl },
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const storageResponse = await fetch(asset.storageUrl);
|
|
279
|
+
if (!storageResponse.ok || !storageResponse.body) {
|
|
280
|
+
return new Response("Storage error", {
|
|
281
|
+
status: 500,
|
|
282
|
+
headers: { "Content-Type": "text/plain" },
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return new Response(storageResponse.body, {
|
|
287
|
+
status: 200,
|
|
288
|
+
headers: {
|
|
289
|
+
"Content-Type": contentType,
|
|
290
|
+
"Cache-Control": cacheControl,
|
|
291
|
+
...(asset.etag ? { ETag: asset.etag } : {}),
|
|
292
|
+
"X-Content-Type-Options": "nosniff",
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Expose a query that clients can subscribe to for live reload on deploy.
|
|
299
|
+
* This is only needed if you use `UpdateBanner` / `useDeploymentUpdates` from
|
|
300
|
+
* `@samebase/convex-hosting/react`. If you don't surface deployment updates
|
|
301
|
+
* in your app, you don't need to call this.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```typescript
|
|
305
|
+
* // convex/hosting.ts
|
|
306
|
+
* import { exposeDeploymentQuery } from "@samebase/convex-hosting";
|
|
307
|
+
* import { components } from "./_generated/api";
|
|
308
|
+
*
|
|
309
|
+
* export const { getCurrentDeployment } = exposeDeploymentQuery(
|
|
310
|
+
* components.hosting,
|
|
311
|
+
* );
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
export function exposeDeploymentQuery(component: ComponentApi) {
|
|
315
|
+
return {
|
|
316
|
+
getCurrentDeployment: queryGeneric({
|
|
317
|
+
args: {},
|
|
318
|
+
returns: v.union(deploymentInfoValidator, v.null()),
|
|
319
|
+
handler: async (ctx) => {
|
|
320
|
+
const deployment = await ctx.runQuery(component.lib.getCurrentDeployment, {});
|
|
321
|
+
if (!deployment) return null;
|
|
322
|
+
// Cleanup accounting is private component state. Returning it through
|
|
323
|
+
// this public wrapper would fail the narrower response validator.
|
|
324
|
+
const deploymentWithCleanup = deployment as typeof deployment & {
|
|
325
|
+
pendingBlobCleanupCount?: number;
|
|
326
|
+
};
|
|
327
|
+
const { pendingBlobCleanupCount: _pending, ...publicDeployment } = deploymentWithCleanup;
|
|
328
|
+
return publicDeployment;
|
|
329
|
+
},
|
|
330
|
+
}),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Derive the Convex cloud URL from a `.convex.site` hostname.
|
|
336
|
+
* Useful when your frontend is served by this component and needs
|
|
337
|
+
* to connect to its own Convex backend without an explicit env var.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```typescript
|
|
341
|
+
* import { getConvexUrl } from "@samebase/convex-hosting";
|
|
342
|
+
*
|
|
343
|
+
* const convexUrl = import.meta.env.VITE_CONVEX_URL ?? getConvexUrl();
|
|
344
|
+
* const convex = new ConvexReactClient(convexUrl);
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
export function getConvexUrl(): string {
|
|
348
|
+
if (typeof window === "undefined") {
|
|
349
|
+
throw new Error("getConvexUrl() can only be called in a browser context");
|
|
350
|
+
}
|
|
351
|
+
if (window.location.hostname.endsWith(".convex.site")) {
|
|
352
|
+
return `https://${window.location.hostname.replace(".convex.site", ".convex.cloud")}`;
|
|
353
|
+
}
|
|
354
|
+
throw new Error("Unable to derive Convex URL. Please set VITE_CONVEX_URL environment variable.");
|
|
355
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
import { test } from "vite-plus/test";
|
|
3
|
+
import { convexTest } from "convex-test";
|
|
4
|
+
export const modules = import.meta.glob("./**/*.*s");
|
|
5
|
+
|
|
6
|
+
import { defineSchema, type GenericSchema, type SchemaDefinition } from "convex/server";
|
|
7
|
+
import { type ComponentApi } from "../component/_generated/component.js";
|
|
8
|
+
import { componentsGeneric } from "convex/server";
|
|
9
|
+
import { register } from "../test.js";
|
|
10
|
+
|
|
11
|
+
export function initConvexTest<Schema extends SchemaDefinition<GenericSchema, boolean>>(
|
|
12
|
+
schema?: Schema,
|
|
13
|
+
) {
|
|
14
|
+
const t = convexTest(schema ?? defineSchema({}), modules);
|
|
15
|
+
register(t);
|
|
16
|
+
return t;
|
|
17
|
+
}
|
|
18
|
+
export const components = componentsGeneric() as unknown as {
|
|
19
|
+
hosting: ComponentApi;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
test("setup", () => {});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type * as http from "../http.js";
|
|
12
|
+
import type * as lib from "../lib.js";
|
|
13
|
+
import type * as serving from "../serving.js";
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
ApiFromModules,
|
|
17
|
+
FilterApi,
|
|
18
|
+
FunctionReference,
|
|
19
|
+
} from "convex/server";
|
|
20
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
21
|
+
|
|
22
|
+
const fullApi: ApiFromModules<{
|
|
23
|
+
http: typeof http;
|
|
24
|
+
lib: typeof lib;
|
|
25
|
+
serving: typeof serving;
|
|
26
|
+
}> = anyApi as any;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
30
|
+
*
|
|
31
|
+
* Usage:
|
|
32
|
+
* ```js
|
|
33
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const api: FilterApi<
|
|
37
|
+
typeof fullApi,
|
|
38
|
+
FunctionReference<any, "public">
|
|
39
|
+
> = anyApi as any;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
43
|
+
*
|
|
44
|
+
* Usage:
|
|
45
|
+
* ```js
|
|
46
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const internal: FilterApi<
|
|
50
|
+
typeof fullApi,
|
|
51
|
+
FunctionReference<any, "internal">
|
|
52
|
+
> = anyApi as any;
|
|
53
|
+
|
|
54
|
+
export const components = componentsGeneric() as unknown as {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `ComponentApi` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { FunctionReference } from "convex/server";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A utility for referencing a Convex component's exposed API.
|
|
15
|
+
*
|
|
16
|
+
* Useful when expecting a parameter like `components.myComponent`.
|
|
17
|
+
* Usage:
|
|
18
|
+
* ```ts
|
|
19
|
+
* async function myFunction(ctx: QueryCtx, component: ComponentApi) {
|
|
20
|
+
* return ctx.runQuery(component.someFile.someQuery, { ...args });
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
25
|
+
{
|
|
26
|
+
lib: {
|
|
27
|
+
getCurrentDeployment: FunctionReference<
|
|
28
|
+
"query",
|
|
29
|
+
"internal",
|
|
30
|
+
{},
|
|
31
|
+
{
|
|
32
|
+
_creationTime: number;
|
|
33
|
+
_id: string;
|
|
34
|
+
currentDeploymentId: string;
|
|
35
|
+
deployedAt: number;
|
|
36
|
+
pendingBlobCleanupCount?: number;
|
|
37
|
+
spaFallback?: boolean;
|
|
38
|
+
} | null,
|
|
39
|
+
Name
|
|
40
|
+
>;
|
|
41
|
+
resolveAssetForHttp: FunctionReference<
|
|
42
|
+
"query",
|
|
43
|
+
"internal",
|
|
44
|
+
{ path: string; spaFallback?: boolean },
|
|
45
|
+
{
|
|
46
|
+
appStorageId?: string;
|
|
47
|
+
blobId?: string;
|
|
48
|
+
contentType: string;
|
|
49
|
+
etag?: string;
|
|
50
|
+
storageUrl?: string;
|
|
51
|
+
} | null,
|
|
52
|
+
Name
|
|
53
|
+
>;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated data model types.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
DataModelFromSchemaDefinition,
|
|
13
|
+
DocumentByName,
|
|
14
|
+
TableNamesInDataModel,
|
|
15
|
+
SystemTableNames,
|
|
16
|
+
} from "convex/server";
|
|
17
|
+
import type { GenericId } from "convex/values";
|
|
18
|
+
import schema from "../schema.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The names of all of your Convex tables.
|
|
22
|
+
*/
|
|
23
|
+
export type TableNames = TableNamesInDataModel<DataModel>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The type of a document stored in Convex.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
29
|
+
*/
|
|
30
|
+
export type Doc<TableName extends TableNames> = DocumentByName<
|
|
31
|
+
DataModel,
|
|
32
|
+
TableName
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An identifier for a document in Convex.
|
|
37
|
+
*
|
|
38
|
+
* Convex documents are uniquely identified by their `Id`, which is accessible
|
|
39
|
+
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
|
|
40
|
+
*
|
|
41
|
+
* Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
|
|
42
|
+
*
|
|
43
|
+
* IDs are just strings at runtime, but this type can be used to distinguish them from other
|
|
44
|
+
* strings when type checking.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
47
|
+
*/
|
|
48
|
+
export type Id<TableName extends TableNames | SystemTableNames> =
|
|
49
|
+
GenericId<TableName>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A type describing your Convex data model.
|
|
53
|
+
*
|
|
54
|
+
* This type includes information about what tables you have, the type of
|
|
55
|
+
* documents stored in those tables, and the indexes defined on them.
|
|
56
|
+
*
|
|
57
|
+
* This type is used to parameterize methods like `queryGeneric` and
|
|
58
|
+
* `mutationGeneric` to make them type-safe.
|
|
59
|
+
*/
|
|
60
|
+
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ActionBuilder,
|
|
13
|
+
HttpActionBuilder,
|
|
14
|
+
MutationBuilder,
|
|
15
|
+
QueryBuilder,
|
|
16
|
+
GenericActionCtx,
|
|
17
|
+
GenericMutationCtx,
|
|
18
|
+
GenericQueryCtx,
|
|
19
|
+
GenericDatabaseReader,
|
|
20
|
+
GenericDatabaseWriter,
|
|
21
|
+
} from "convex/server";
|
|
22
|
+
import {
|
|
23
|
+
actionGeneric,
|
|
24
|
+
httpActionGeneric,
|
|
25
|
+
queryGeneric,
|
|
26
|
+
mutationGeneric,
|
|
27
|
+
internalActionGeneric,
|
|
28
|
+
internalMutationGeneric,
|
|
29
|
+
internalQueryGeneric,
|
|
30
|
+
} from "convex/server";
|
|
31
|
+
import type { DataModel } from "./dataModel.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Define a query in this Convex app's public API.
|
|
35
|
+
*
|
|
36
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
37
|
+
*
|
|
38
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
39
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
40
|
+
*/
|
|
41
|
+
export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
45
|
+
*
|
|
46
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
47
|
+
*
|
|
48
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
49
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
50
|
+
*/
|
|
51
|
+
export const internalQuery: QueryBuilder<DataModel, "internal"> =
|
|
52
|
+
internalQueryGeneric;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Define a mutation in this Convex app's public API.
|
|
56
|
+
*
|
|
57
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
58
|
+
*
|
|
59
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
60
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
61
|
+
*/
|
|
62
|
+
export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
66
|
+
*
|
|
67
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
68
|
+
*
|
|
69
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
70
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
71
|
+
*/
|
|
72
|
+
export const internalMutation: MutationBuilder<DataModel, "internal"> =
|
|
73
|
+
internalMutationGeneric;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Define an action in this Convex app's public API.
|
|
77
|
+
*
|
|
78
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
79
|
+
* code and code with side-effects, like calling third-party services.
|
|
80
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
81
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
82
|
+
*
|
|
83
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
84
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
85
|
+
*/
|
|
86
|
+
export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
90
|
+
*
|
|
91
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
92
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
93
|
+
*/
|
|
94
|
+
export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
95
|
+
internalActionGeneric;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Define an HTTP action.
|
|
99
|
+
*
|
|
100
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
101
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
102
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
103
|
+
*
|
|
104
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
105
|
+
* and a Fetch API `Request` object as its second.
|
|
106
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
107
|
+
*/
|
|
108
|
+
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A set of services for use within Convex query functions.
|
|
112
|
+
*
|
|
113
|
+
* The query context is passed as the first argument to any Convex query
|
|
114
|
+
* function run on the server.
|
|
115
|
+
*
|
|
116
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
117
|
+
*/
|
|
118
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A set of services for use within Convex mutation functions.
|
|
122
|
+
*
|
|
123
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
124
|
+
* function run on the server.
|
|
125
|
+
*
|
|
126
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
127
|
+
*/
|
|
128
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A set of services for use within Convex action functions.
|
|
132
|
+
*
|
|
133
|
+
* The action context is passed as the first argument to any Convex action
|
|
134
|
+
* function run on the server.
|
|
135
|
+
*/
|
|
136
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* An interface to read from the database within Convex query functions.
|
|
140
|
+
*
|
|
141
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
142
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
143
|
+
* building a query.
|
|
144
|
+
*/
|
|
145
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* An interface to read from and write to the database within Convex mutation
|
|
149
|
+
* functions.
|
|
150
|
+
*
|
|
151
|
+
* Convex guarantees that all writes within a single mutation are
|
|
152
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
153
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
154
|
+
* for the guarantees Convex provides your functions.
|
|
155
|
+
*/
|
|
156
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|