uploadthing icon indicating copy to clipboard operation
uploadthing copied to clipboard

feat: add Convex adapter

Open sujayakar opened this issue 1 year ago • 5 comments

This PR adds a Convex adapter as a sibling to Express, Fastify, etc. It was pretty straightforward to add a new adapter!

Here's what it looks like for wiring it in to a Convex app. First, call createUploadthing as always:

// convex/http.ts
import { addUploadthingRoutes, convexCtx, createUploadthing, type FileRouter } from "uploadthing/convex"
import schema from "./schema"

const f = createUploadthing({
  errorFormatter: (err) => {
    console.log("Error uploading file", err.message);
    console.log("  - Above error caused by:", err.cause);

    return { message: err.message };
  },
  schema,
});

Note that the developer can optionally pass in their Convex schema here for more type-safety within their handlers.

Then, define the router, using the convexCtx function to access the request's current ctx value. This is necessary for calling into other Convex functions, accessing auth and the database, etc. Under the hood, this uses a hack where we stash the ctx when starting a request.

export const uploadRouter = {
  imageUploader: f({ image: { maxFileSize: "4MB" } })
    .middleware(async (args) => {
      const ctx = convexCtx(f);      
      const identity = await ctx.auth.getUserIdentity();
      ...
      return { userId: identity?.subject ?? "nothing" }
    })
    .onUploadComplete(async (args) => {      
      const ctx = convexCtx(f);
      ...      
      return { uploadedBy: args.metadata.userId };
    }),
} satisfies FileRouter;
export type OurFileRouter = typeof uploadRouter;

Finally, install the routes into Convex's HTTP router with addUploadthingRoutes:

const http = httpRouter();
addUploadthingRoutes(http, f, { router: uploadRouter })
export default http;    

Questions

  • The hack to sneak in ctx via a "global" with convexCtx is a bit ugly. Is there a better way to pass values to the middleware and completion callbacks?
  • Where should I add Convex to the docs? Would another entry next to https://docs.uploadthing.com/backend-adapters/express be the right spot?
  • Should I add my Uploadthing+Convex example app from the snippets above as a new directory under examples/? It'll require setting up a Convex account (or self hosting Convex) to actually run the demo.
  • Are there any tests, etc., that I should also be updating?

Summary by CodeRabbit

  • New Features

    • Introduced functionality for handling file uploads using the Convex framework.
    • Added new HTTP routes for file uploads at /api/uploadthing.
    • Configured an image upload route with a maximum file size of 4MB.
  • Enhancements

    • Updated package configuration to include a new export for convex and its optional peer dependency.
    • Expanded the build process to include files from the convex directory.
    • Added a dependency for convex in the backend adapter project.

sujayakar avatar Sep 03 '24 04:09 sujayakar

⚠️ No Changeset found

Latest commit: a2c8aef92fd640347c08f3055c0b83cad3c90108

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Sep 03 '24 04:09 changeset-bot[bot]

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs-uploadthing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 31, 2025 0:31am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
legacy-docs-uploadthing ⬜️ Ignored (Inspect) Visit Preview Jan 31, 2025 0:31am

vercel[bot] avatar Sep 03 '24 04:09 vercel[bot]

Walkthrough

The pull request introduces updates to the uploadthing package, including modifications to the package.json, the addition of a new convex.ts file, and changes to the turbo.json configuration. The package.json now includes a new export for convex and a peer dependency, while the convex.ts file implements functionality for file uploads using the Convex framework. Additionally, the turbo.json file has been updated to include the convex directory in the build outputs.

Changes

File Path Change Summary
packages/uploadthing/package.json - Added export: ./convex with import and require configurations.
- Added dependency: "convex": "^1.17.4".
- Added peer dependency: "convex": "*".
- Updated peerDependenciesMeta to indicate convex is optional.
packages/uploadthing/src/convex.ts - Introduced convex.ts with functions: createUploadthing and addUploadthingRoutes for file upload handling.
- Defined types for middleware arguments and builder options.
packages/uploadthing/turbo.json - Added output path: "convex/**" to build task in turbo.json.
examples/backend-adapters/server/package.json - Added dependency: "convex": "^1.17.4".
examples/backend-adapters/server/src/convex.ts - Introduced convex.ts for HTTP routes with imageUploader, handling file uploads with user identity.

Possibly related PRs

  • #987: The changes in this PR involve modifications to the withUt function, which is relevant to the uploadthing package's integration with Tailwind CSS, similar to the updates made in the main PR regarding the convex module and its integration.
  • #991: This PR enhances the getTypeFromFileName function to utilize the file.type property, which relates to file handling and upload processes, similar to the changes made in the main PR that involve file upload management.
  • #1013: This PR addresses compatibility issues with the @uploadthing/mime-types package, which is relevant to the overall file handling and upload functionality discussed in the main PR.

Suggested labels

uploadthing/client, sdk

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Oct 17 '24 08:10 coderabbitai[bot]

Any update on this? Would love to see this being merged soon :)

eliotgevers avatar Jul 18 '25 09:07 eliotgevers

Is this still on the feature roadmap?

eliotgevers avatar Sep 18 '25 22:09 eliotgevers