-
Notifications
You must be signed in to change notification settings - Fork 400
feat: add Convex adapter #929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
b8862ed
35ff700
4936afa
aab7804
3474924
416014b
c59e92a
60b055a
47a9022
d6cd436
ecf7a83
a2c8aef
296cdfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // In a real Convex app, you'd create this file at `convex/http.ts` to set | ||
| // up HTTP routes for UploadThing. | ||
|
|
||
| import { httpRouter } from "convex/server"; | ||
| import { addUploadthingRoutes, createUploadthing, FileRouter } from "uploadthing/convex" | ||
|
|
||
| // In a real app, you can wire up `cereateUploadthing` to your Convex | ||
| // schema for increased type safety. | ||
| // ```ts | ||
| // import schema from "./schema"; | ||
| // const f = createUploadthing({ schema }) | ||
| // ``` | ||
| const f = createUploadthing(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be better to have a separate example showing the "real" way as you suggested in the PR comment. without it, this example doesnt really hold much value IMO
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good, I'll remove this and port it to a full example. |
||
|
|
||
| const router = { | ||
| imageUploader: f({ image: { maxFileSize: "4MB" } }) | ||
| .middleware(async ({ event }) => { | ||
| const identity = await event.auth.getUserIdentity(); | ||
| return { userId: identity?.subject ?? "nothing" }; | ||
| }) | ||
| .onUploadComplete((args) => { | ||
| return { uploadedBy: args.metadata.userId }; | ||
| }), | ||
| } satisfies FileRouter; | ||
|
|
||
| const http = httpRouter(); | ||
| addUploadthingRoutes(http, { router }); | ||
| export default http; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { | ||
| DataModelFromSchemaDefinition, | ||
| GenericActionCtx, | ||
| GenericDataModel, | ||
| HttpRouter, | ||
| SchemaDefinition, | ||
| } from "convex/server"; | ||
| import { httpActionGeneric, httpRouter } from "convex/server"; | ||
| import * as Effect from "effect/Effect"; | ||
|
|
||
| import type { Json } from "@uploadthing/shared"; | ||
|
|
||
| import { makeAdapterHandler } from "./internal/handler"; | ||
| import type { FileRouter, RouteHandlerOptions } from "./internal/types"; | ||
| import { createBuilder } from "./internal/upload-builder"; | ||
| import type { CreateBuilderOptions } from "./internal/upload-builder"; | ||
|
|
||
| export type { FileRouter }; | ||
|
|
||
| type MiddlewareArgs<DataModel extends GenericDataModel> = { | ||
| req: Request; | ||
| res: undefined; | ||
| event: GenericActionCtx<DataModel>; | ||
| }; | ||
|
|
||
| type ConvexBuilderOptions< | ||
| TErrorShape extends Json, | ||
| SchemaDef extends SchemaDefinition<any, boolean>, | ||
| > = CreateBuilderOptions<TErrorShape> & { | ||
| schema?: SchemaDef; | ||
| }; | ||
|
|
||
| export const createUploadthing = < | ||
| TErrorShape extends Json, | ||
| SchemaDef extends SchemaDefinition<any, boolean>, | ||
| >( | ||
| opts?: ConvexBuilderOptions<TErrorShape, SchemaDef>, | ||
| ) => | ||
| createBuilder< | ||
| MiddlewareArgs<DataModelFromSchemaDefinition<SchemaDef>>, | ||
| TErrorShape | ||
| >(opts); | ||
|
|
||
| export const addUploadthingRoutes = <TRouter extends FileRouter>( | ||
| router: HttpRouter, | ||
| opts: RouteHandlerOptions<TRouter>, | ||
| ) => { | ||
| const handler = makeAdapterHandler< | ||
| [GenericActionCtx<GenericDataModel>, Request] | ||
| >( | ||
| (ctx, req) => Effect.succeed({ req, res: undefined, event: ctx }), | ||
| (_, req) => Effect.succeed(req), | ||
| opts, | ||
| "convex", | ||
| ); | ||
|
|
||
| router.route({ | ||
| method: "GET", | ||
| path: "/api/uploadthing", | ||
| handler: httpActionGeneric(handler), | ||
| }); | ||
|
|
||
| router.route({ | ||
| method: "POST", | ||
| path: "/api/uploadthing", | ||
| handler: httpActionGeneric(handler), | ||
| }); | ||
|
|
||
| router.route({ | ||
| method: "OPTIONS", | ||
| path: "/api/uploadthing", | ||
| handler: httpActionGeneric((_ctx, { headers }) => { | ||
| const isCorsRequest = | ||
| headers.get("Origin") != null && | ||
| headers.get("Access-Control-Request-Method") != null && | ||
| headers.get("Access-Control-Request-Headers") != null; | ||
|
|
||
| if (!isCorsRequest) { | ||
| return Promise.resolve(new Response()); | ||
| } | ||
| return Promise.resolve( | ||
| new Response(null, { | ||
| status: 200, | ||
| headers: { | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Methods": "POST, GET, OPTIONS", | ||
| "Access-Control-Allow-Headers": "*", | ||
| "Access-Control-Max-Age": "86400", | ||
| }, | ||
| }), | ||
| ); | ||
| }), | ||
| }); | ||
juliusmarminge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| "build": { | ||
| "outputs": [ | ||
| "client/**", | ||
| "convex/**", | ||
| "effect-platform/**", | ||
| "express/**", | ||
| "fastify/**", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.