-
Notifications
You must be signed in to change notification settings - Fork 346
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
feat: add Convex adapter #929
Open
sujayakar
wants to merge
13
commits into
pingdotgg:main
Choose a base branch
from
sujayakar:sujayakar/add-convex-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+491
−8
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b8862ed
Add convex adapter
sujayakar 35ff700
Add convexCtx accessor
sujayakar 4936afa
Allow-Origin *
sujayakar aab7804
Merge branch 'main' into sujayakar/add-convex-adapter
juliusmarminge 3474924
v7
juliusmarminge 416014b
Move TEST code to examples/backend-adapters
sujayakar c59e92a
Merge branch 'main' into sujayakar/add-convex-adapter
juliusmarminge 60b055a
Update examples/backend-adapters/server/src/convex.ts
sujayakar 47a9022
Merge branch 'main' into sujayakar/add-convex-adapter
juliusmarminge d6cd436
Merge branch 'main' into sujayakar/add-convex-adapter
juliusmarminge ecf7a83
Merge branch 'main' into sujayakar/add-convex-adapter
juliusmarminge a2c8aef
Merge branch 'main' into sujayakar/add-convex-adapter
juliusmarminge 296cdfd
fix err
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 `createUploadthing` to your Convex | ||
// schema for increased type safety. | ||
// ```ts | ||
// import schema from "./schema"; | ||
// const f = createUploadthing({ schema }) | ||
// ``` | ||
const f = createUploadthing(); | ||
|
||
const router = { | ||
imageUploader: f({ image: { maxFileSize: "4MB" } }) | ||
.middleware(async ({ event }) => { | ||
const identity = await event.auth.getUserIdentity(); | ||
return { userId: identity?.subject ?? "nothing" }; | ||
}) | ||
.onUploadComplete(({ metadata, event }) => { | ||
console.log("Upload complete!", metadata); | ||
return { uploadedBy: metadata.userId }; | ||
}), | ||
} satisfies FileRouter; | ||
|
||
const http = httpRouter(); | ||
addUploadthingRoutes(http, { router }); | ||
export default http; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } from "convex/server"; | ||
import * as Effect from "effect/Effect"; | ||
|
||
import type { Json } from "@uploadthing/shared"; | ||
|
||
import { makeAdapterHandler } from "./_internal/handler"; | ||
import { createBuilder } from "./_internal/upload-builder"; | ||
import type { CreateBuilderOptions } from "./_internal/upload-builder"; | ||
import type { FileRouter, RouteHandlerOptions } from "./types"; | ||
|
||
export type { FileRouter }; | ||
|
||
type AdapterArgs<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< | ||
AdapterArgs<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
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"build": { | ||
"outputs": [ | ||
"client/**", | ||
"convex/**", | ||
"dist/**", | ||
"effect-platform/**", | ||
"express/**", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.