pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
4.26k stars 315 forks source link

refactor: simplify types for built file route #1044

Closed juliusmarminge closed 2 weeks ago

juliusmarminge commented 2 weeks ago

This PR simplifies the types of a built file route making hover much nicer and grokable. The idea is simple, the built uploader (i.e. after calling .onUploadComplete) does not need to keep around all the internal types, we just need to expose the types that the client needs for inference

Before

Server

CleanShot 2024-11-05 at 21 33 33@2x

Client

CleanShot 2024-11-05 at 21 34 47@2x image

After

Server

CleanShot 2024-11-05 at 22 34 27@2x

Client

CleanShot 2024-11-05 at 22 34 11@2x CleanShot 2024-11-05 at 23 11 54@2x

Summary by CodeRabbit

Release Notes

vercel[bot] commented 2 weeks ago

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 Nov 5, 2024 9:56pm
1 Skipped Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **legacy-docs-uploadthing** | ⬜️ Ignored ([Inspect](https://vercel.com/pinglabs/legacy-docs-uploadthing/CEZ3mSdcNnAvEXorv3QoV97yeSgG)) | [Visit Preview](https://legacy-docs-uploadthing-git-simplify-types-pinglabs.vercel.app) | | Nov 5, 2024 9:56pm |
changeset-bot[bot] commented 2 weeks ago

πŸ¦‹ Changeset detected

Latest commit: 8e1bcbd8c2c0128661ba53454617ebf501831e6c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages | Name | Type | | ------------------- | ----- | | @uploadthing/expo | Patch | | @uploadthing/nuxt | Patch | | @uploadthing/react | Patch | | @uploadthing/solid | Patch | | @uploadthing/svelte | Patch | | uploadthing | Patch | | @uploadthing/vue | Patch |

Not sure what this means? Click here to learn what changesets are.

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

coderabbitai[bot] commented 2 weeks ago

[!WARNING]

Rate limit exceeded

@juliusmarminge has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 0 seconds before requesting another review.

βŒ› How to resolve this issue? After the wait time has elapsed, a review can be triggered using the `@coderabbitai review` command as a PR comment. Alternatively, push new commits to this PR. We recommend that you space out your commits to avoid hitting the rate limit.
🚦 How do rate limits work? CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our [FAQ](https://coderabbit.ai/docs/faq) for further information.
πŸ“₯ Commits Reviewing files that changed from the base of the PR and between 5d0ad5edb8dd3ae15a6fbcf4facbe26ad849c9b3 and 8e1bcbd8c2c0128661ba53454617ebf501831e6c.

Walkthrough

This pull request introduces significant changes across multiple files related to upload handling in the uploadthing package. Key modifications include simplifying property access in various functions by removing unnecessary intermediate objects, updating type definitions to enhance flexibility and clarity, and refining error handling practices. The changes affect how uploadable entities and middleware functions are defined and utilized, ultimately restructuring the internal logic for better maintainability.

Changes

File Path Change Summary
packages/uploadthing/src/internal/error-formatter.ts Updated formatError function to access errorFormatter directly from the router object, removing the intermediate _def property.
packages/uploadthing/src/internal/handler.ts Replaced Uploader<AnyParams> with AnyFileRoute in multiple function signatures. Updated handleErrorRequest, handleCallbackRequest, and handleUploadAction to access properties directly on uploadable. Enhanced error handling in the POST request handler.
packages/uploadthing/src/internal/route-config.ts Modified extractRouterConfig function to access routerConfig directly, removing the _def layer. Error handling logic in assertFilesMeetConfig remains unchanged.
packages/uploadthing/src/internal/types.ts Restructured type definitions, including changes to ResolverOptions, MiddlewareFn, and Uploader interface. Introduced UploadCompleteFn and updated inferencing types for better type safety.
packages/uploadthing/src/internal/upload-builder.ts Replaced UploadBuilderDef with AnyFileRoute. Updated internalCreateBuilder to accept Partial<AnyFileRoute>. Adjusted the return type of onUploadComplete method to include the onUploadComplete property.
packages/uploadthing/test/upload-builder.test.ts Updated test cases to reflect changes in property access, removing underscores from routerConfig and middleware. Ensured tests validate error scenarios and maintain type safety throughout.
packages/expo/src/index.ts Modified import statement for FileRouter, changing source from uploadthing/internal/types to uploadthing/types.
packages/nuxt/src/module.ts Modified import statement for RouteHandlerConfig, changing source from uploadthing/internal/types to uploadthing/types.
packages/uploadthing/src/client.ts Added FileRouter and inferEndpointOutput types to imports, enhancing type definitions.
packages/uploadthing/src/effect-platform.ts Updated import statements for FileRouter and RouteHandlerConfig, changing source from ./internal/types to ./types.
packages/uploadthing/src/express.ts Modified import paths for FileRouter and RouteHandlerOptions, changing source from ./internal/types to ./types.
packages/uploadthing/src/fastify.ts Updated import paths for FileRouter and RouteHandlerOptions, changing source from ./internal/types to ./types.
packages/uploadthing/src/h3.ts Changed import paths for FileRouter and RouteHandlerOptions, and adjusted export statements.
packages/uploadthing/src/next-legacy.ts Modified import statement for FileRouter and RouteHandlerOptions, changing source from ./internal/types to ./types.
packages/uploadthing/src/next.ts Updated import paths for FileRouter and RouteHandlerOptions, changing source from ./internal/types to ./types.
packages/uploadthing/src/remix.ts Changed import paths for FileRouter and RouteHandlerOptions, changing source from ./internal/types to ./types.
packages/uploadthing/src/server.ts Modified import paths for FileRouter and RouteHandlerOptions, changing source from ./internal/types to ./types.
packages/uploadthing/src/types.ts Introduced new types and redefined FileRouter, enhancing type safety and flexibility in handling uploads and route configurations.

Possibly related PRs

Suggested labels

examples

Suggested reviewers

Poem

🐰 In the land of uploads, so bright and fair,
Changes abound, with a hop and a flair.
Error handling's clearer, types now align,
A smoother path for uploads, oh how divine!
With each little tweak, our code takes a leap,
In the burrow of progress, we joyfully leap! πŸ₯•βœ¨


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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
πŸͺ§ Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , 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 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
github-actions[bot] commented 2 weeks ago

πŸ“¦ Bundle size comparison

Bundle Size (gzip) Visualization
Main 27.51KB See Treemap πŸ“Š
PR (f19fb3a3b778ae4b23e62b865027e138dd69220c) 27.51KB See Treemap πŸ“Š
Diff No change
pkg-pr-new[bot] commented 2 weeks ago

Open in Stackblitz

More templates

- [@example/minimal-appdir](https://pkg.pr.new/template/a4f035ae-023d-48b2-b4bf-d56096c4050e) - [@example/minimal-astro-react](https://pkg.pr.new/template/3027a0ed-2c7f-4c76-aba4-3a969a22f79f) - [@example/minimal-expo](https://pkg.pr.new/template/44679e19-cc12-4df9-9f49-779274809bb6) - [@example/minimal-nuxt](https://pkg.pr.new/template/7a74e832-25e0-4db1-9c50-d36c46a381d8) - [@example/minimal-solidstart](https://pkg.pr.new/template/3be3e35d-9d4b-43cb-a1f6-a7cff808aeae) - [@example/minimal-pagedir](https://pkg.pr.new/template/b9966c02-230e-4519-979c-73391d3839fe) - [@example/minimal-sveltekit](https://pkg.pr.new/template/d5080009-d5f7-46a7-8737-2f3f3f7337fe) - [@example/minimal-tanstack-start](https://pkg.pr.new/template/992f81cd-0660-4d20-b532-7ab98083e4be)

@uploadthing/expo

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing/@uploadthing/expo@1044 ```

@uploadthing/nuxt

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing/@uploadthing/nuxt@1044 ```

@uploadthing/react

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing/@uploadthing/react@1044 ```

@uploadthing/solid

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing/@uploadthing/solid@1044 ```

@uploadthing/svelte

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing/@uploadthing/svelte@1044 ```

uploadthing

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing@1044 ```

@uploadthing/vue

``` pnpm add https://pkg.pr.new/pingdotgg/uploadthing/@uploadthing/vue@1044 ```

commit: 8e1bcbd