pingdotgg / uploadthing

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

[bug]: Setting `callbackUrl: undefined` doesn't use default value #1027

Closed jthrilly closed 1 month ago

jthrilly commented 1 month ago

Provide environment information

System:
    OS: macOS 15.0.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 360.53 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm
    pnpm: 9.1.1 - ~/.nvm/versions/node/v20.11.0/bin/pnpm
    Watchman: 2024.09.09.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 130.0.6723.60
    Safari: 18.0.1
  npmPackages:
    @uploadthing/react: ^7.0.3 => 7.0.3 
    typescript: ^5.6.2 => 5.6.2 
    uploadthing: ^7.1.0 => 7.1.0 

Describe the bug

In my app, users can optionally provide a PUBLIC_URL environment variable when deploying via docker. I used to conditionally add this to the route handler config:

  const handler = createRouteHandler({
    router: ourFileRouter,
    config: {
      // UploadThing attempts to automatically detect callbackUrl value based on the request URL and headers
      // However, the automatic detection fails in docker deployments
      // docs: https://docs.uploadthing.com/api-reference/server#config
      callbackUrl: env.PUBLIC_URL && `${env.PUBLIC_URL}/api/uploadthing`,
      token: uploadThingToken,
    },
  });

The result is that callbackUrl is typed as string | undefined which I believe is correct (PUBLIC_URL is defined using t3-env as z.string().url().optional()).

This fails in v7 with the following error:

    request: {
      _id: '@effect/platform/HttpClientRequest',
      method: 'POST',
      url: '',
      urlParams: [ [Array] ],
      hash: { _id: 'Option', _tag: 'None' },
      headers: Object <[Object: null prototype]> {
        'uploadthing-hook': 'callback',
        'x-uploadthing-signature': 'hmac-sha256=fc7c0e9ee5a3c7b997a97d9cc2d46b8ee3c69b5e6364d4db65dcc2b36252a1e8',
        'content-type': 'application/json',
        'content-length': '403'
      },
      body: {
        _id: '@effect/platform/HttpBody',
        _tag: 'Uint8Array',
        body: '{"status":"uploaded","metadata":{},"file":{"url":"https://utfs.io/f/KjImljYXVFrQB2QavYwBJgCUQhkzq0TevpPjcnD1roWLG7f2","appUrl":"https://utfs.io/a/7kquzh6xue/KjImljYXVFrQB2QavYwBJgCUQhkzq0TevpPjcnD1roWLG7f2","name":"d26a5880-8e77-11ec-80d2-23528725b944.csv","key":"KjImljYXVFrQB2QavYwBJgCUQhkzq0TevpPjcnD1roWLG7f2","size":3733,"type":"blob","customId":null,"fileHash":"5526a4d41a1ce51461e001dc7be60263"}}',
        contentType: 'application/json',
        contentLength: 403
      }
    },
    reason: 'InvalidUrl',
    _tag: 'RequestError',
    [cause]: TypeError: Invalid URL

Conditionally adding the property via ...(callbackUrl && { callbackUrl } ) works, so this seems to be an issue caused by the key being defined in the config, even if it is undefined.

Link to reproduction

https://github.com/complexdatacollective/Fresco/blob/bccb82009f2eaba4513195d8e45eab832040d841/app/api/uploadthing/route.ts

To reproduce

See description.

Additional information

No response

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributing

Code of Conduct

juliusmarminge commented 1 month ago

Hey,

This is handled by Effect which you can see here: https://effect.website/play#3ab9c1685fa3

I brought this up with the Effect team and they're looking at improving this on their end.

juliusmarminge commented 1 month ago

Will be fixed in next release https://github.com/Effect-TS/effect/pull/3833