-
Notifications
You must be signed in to change notification settings - Fork 400
feat: Add Convex Adapter #1235
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?
feat: Add Convex Adapter #1235
Changes from 7 commits
b5b704b
50e71a9
084228b
e20ad2b
b4ab843
6b8145d
45a1f12
2d4bb76
b481f13
de178e7
121f21a
b79d51b
681e127
f840666
75f4383
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,5 @@ | ||
| --- | ||
| "uploadthing": minor | ||
| --- | ||
|
|
||
| Add Convex Adapter |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { docsMetadata } from "@/lib/utils"; | ||
|
|
||
| export const metadata = docsMetadata({ | ||
| title: "Convex", | ||
| description: "Adapter to integrate UploadThing into your Convex application", | ||
| category: "Backend Adapters", | ||
| }); | ||
|
|
||
| # Getting started with Convex | ||
|
|
||
| > Added in `v7.8` | ||
|
|
||
| ## Package Setup | ||
|
|
||
| ### Install the package | ||
|
|
||
| ```sh npm2yarn | ||
| npm install uploadthing | ||
| ``` | ||
|
|
||
| ### Add env variables (in Convex dashboard) | ||
|
|
||
| <Note> | ||
| If you don't already have a uploadthing secret key, [sign | ||
| up](https://uploadthing.com/sign-in) and create one from the | ||
| [dashboard!](https://uploadthing.com/dashboard) | ||
| </Note> | ||
|
|
||
| ```bash | ||
| UPLOADTHING_TOKEN=... # A token for interacting with the SDK | ||
| CLIENT_ORIGIN=... # The origin of the client | ||
| ``` | ||
|
|
||
| ## Set Up A FileRouter | ||
|
|
||
| ### Creating your first FileRoute | ||
|
|
||
| All files uploaded to uploadthing are associated with a FileRoute. The following | ||
| is a very minimalistic example, with a single FileRoute "imageUploader". Think | ||
| of a FileRoute similar to an endpoint, it has: | ||
|
|
||
| - Permitted types ["image", "video", etc] | ||
| - Max file size | ||
| - How many files are allowed to be uploaded | ||
| - (Optional) `input` validation to validate client-side data sent to the route | ||
| - (Optional) `middleware` to authenticate and tag requests | ||
| - `onUploadComplete` callback for when uploads are completed | ||
|
|
||
| To get full insight into what you can do with the FileRoutes, please refer to | ||
| the [File Router API](/file-routes). | ||
|
|
||
| ```ts {{ title: "convex/uploadthing.ts" }} | ||
| "use node"; | ||
|
|
||
| import crypto from "node:crypto"; | ||
|
|
||
| import { | ||
| createInternalAction, | ||
| createUploadthing, | ||
| FileRouter, | ||
| } from "uploadthing/convex"; | ||
|
|
||
| import { api } from "./_generated/api"; | ||
|
|
||
| globalThis.crypto = crypto as Crypto; | ||
|
|
||
| const f = createUploadthing(); | ||
|
|
||
| const router = { | ||
| // Define as many FileRoutes as you like, each with a unique routeSlug | ||
| imageUploader: f({ | ||
| image: { | ||
| /** | ||
| * For full list of options and defaults, see the File Route API reference | ||
| * @see https://docs.uploadthing.com/file-routes#route-config | ||
| */ | ||
| maxFileSize: "4MB", | ||
| maxFileCount: 1, | ||
| }, | ||
| }).onUploadComplete((data) => { | ||
| console.log("upload completed", data); | ||
| }), | ||
| } satisfies FileRouter; | ||
|
|
||
| export type OurFileRouter = typeof router; | ||
|
|
||
| export const handler = createInternalAction({ router }); | ||
| ``` | ||
|
|
||
| ### Create an API route using the FileRouter | ||
|
|
||
| <Note> | ||
| File path here doesn't matter, you can serve this from any route. We recommend | ||
| serving it from `/api/uploadthing`. | ||
| </Note> | ||
|
|
||
| ```ts {{ title: "convex/http.ts" }} | ||
| import { httpRouter } from "convex/server"; | ||
|
|
||
| import { createRouteHandler } from "uploadthing/convex-helpers"; | ||
|
|
||
| import { internal } from "./_generated/api"; | ||
|
|
||
| const http = httpRouter(); | ||
|
|
||
| createRouteHandler({ | ||
| http, | ||
| internalAction: internal.uploadthing.handler, | ||
| path: "/api/uploadthing", | ||
| }); | ||
|
|
||
| export default http; | ||
| ``` | ||
|
|
||
| > See configuration options in | ||
| > [server API reference](/api-reference/server#create-route-handler) | ||
|
|
||
| ### Use the FileRouter in your app | ||
|
|
||
| Client side usage differs ever so slightly from the fullstack framework setups | ||
| when using a separate backend server. You'll need to set the URL of your server | ||
| when you generate the components and helpers. | ||
|
|
||
| ```tsx | ||
| import { generateUploadButton } from "@uploadthing/react"; | ||
|
|
||
| export const UploadButton = generateUploadButton({ | ||
| url: "https://your-server.com/api/uploadthing", | ||
| }); | ||
| // ... | ||
| ``` | ||
|
|
||
| For the remaining usage, please refer to client side examples of the fullstack | ||
| framework guides: | ||
|
|
||
| - [Next.js](/getting-started/appdir#create-the-upload-thing-components) | ||
| - [Solid.js](/getting-started/solid#creating-the-upload-thing-components) | ||
| - [Vue](https://github.com/pingdotgg/uploadthing/tree/main/examples/backend-adapters/client-vue) | ||
| - [Svelte](/getting-started/svelte#creating-the-upload-thing-helpers) | ||
|
|
||
| or check out the full API reference: | ||
|
|
||
| - [`@uploadthing/react`](/api-reference/react) | ||
| - [`uploadthing/client`](/api-reference/client) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Deployment used by `npx convex dev` | ||
| CONVEX_DEPLOYMENT= | ||
|
|
||
| NEXT_PUBLIC_CONVEX_URL= | ||
| NEXT_PUBLIC_CONVEX_SITE_URL= | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
|
|
||
| .env.local |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # @example/appdir-minimal | ||
|
|
||
| ## null | ||
|
|
||
| ### Patch Changes | ||
|
|
||
| - Updated dependencies | ||
| [[`2e8b410`](https://github.com/pingdotgg/uploadthing/commit/2e8b410bb15c2688e9b6938c4a2cd17cf6110289)]: | ||
| - [email protected] | ||
|
|
||
|
||
| ## null | ||
|
|
||
| ### Patch Changes | ||
|
|
||
| - Updated dependencies | ||
| [[`8cfdade`](https://github.com/pingdotgg/uploadthing/commit/8cfdade9fee61a636fa1c88bc9380d4ac77e91d9)]: | ||
| - [email protected] | ||
|
|
||
| ## null | ||
|
|
||
| ### Patch Changes | ||
|
|
||
| - Updated dependencies | ||
| [[`353f6d0`](https://github.com/pingdotgg/uploadthing/commit/353f6d026fbee7480573d735d0406477dcb9e0bc)]: | ||
| - [email protected] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Minimal Next.js App Router + Convex example for UploadThing | ||
|
|
||
| <a href="https://stackblitz.com/github/pingdotgg/uploadthing/tree/main/examples/minimal-convex"> | ||
| <img height="64" src="https://github.com/pingdotgg/uploadthing/assets/51714798/45907a4e-aa64-401a-afb3-b6c6df6eb71f" /> | ||
| </a> | ||
|
|
||
| ## QuickStart | ||
|
|
||
| 1. `pnpm i` | ||
| 2. `pnpm dev:setup` | ||
| 3. Add the `NEXT_PUBLIC_CONVEX_SITE_URL` to the .env file | ||
| 4. Grab an API key from the UploadThing dashboard: | ||
| https://uploadthing.com/dashboard | ||
| 5. `pnpm dev:convex` | ||
| 6. `pnpx convex env set UPLOADTHING_SECRET=<your-secret>` | ||
vercel[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 7. `pnpx convex env set CLIENT_ORIGIN=http://localhost:3000` | ||
| 8. `pnpm dev` | ||
| 9. Upload files! | ||
|
|
||
| ## Further reference | ||
|
|
||
| Check out the docs at: https://docs.uploadthing.com/backend-adapters/convex | ||
Uh oh!
There was an error while loading. Please reload this page.