pingdotgg / uploadthing

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

[bug]: 'req' in middleware for Sveltekit has changed and types are wrong (since migrating to v7) #1019

Closed Pascaltib closed 1 month ago

Pascaltib commented 1 month ago

Provide environment information

Btw you should add the svelte package to the envinfo command on the bug reports  

System:
    OS: macOS 15.0.1
    CPU: (8) arm64 Apple M1
    Memory: 92.80 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.5.0 - ~/.nvm/versions/node/v21.5.0/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v21.5.0/bin/npm
  Browsers:
    Chrome: 129.0.6668.101
    Safari: 18.0.1
  npmPackages:
    @uploadthing/svelte: ^7.0.3 => 7.0.3
    typescript: ^5.6.3 => 5.6.3
    uploadthing: ^7.1.0 => 7.1.0

Describe the bug

I can see that in the middleware, the request object is not the same shape as before. I tested it with the exact same code in hooks.server.ts which sets some locales. The new request object is actually a Sveltekit RequestEvent and is not typed correctly. It is no longer a standard web request.

This is an issue with the types of course but it also broke my authentication code which relied on the previous standard Request. It should be stated in the migration guide if this is intended and the types fixed.

.middleware(async ({ req }) => {
            // This code runs on your server before upload
      console.log('req:', req)
      ...
      }
...

Old request:

req: Request {
  [Symbol(realm)]: {
    settingsObject: { baseUrl: undefined, origin: [Getter], policyContainer: [Object] }
  },
  [Symbol(state)]: {
    method: 'POST',
    localURLsOnly: false,
    unsafeRequest: false,
    body: { stream: undefined, source: null, length: null },
    client: { baseUrl: undefined, origin: [Getter], policyContainer: [Object] },
    reservedClient: null,
...

New request (you can see locals I set from hooks.server.ts now):

req: {
  cookies: {
    get: [Function: get],
    getAll: [Function: getAll],
    set: [Function: set],
    delete: [Function: delete],
    serialize: [Function: serialize]
  },
  fetch: [Function (anonymous)],
  getClientAddress: [Function: getClientAddress],
  locals: {
    user: {
      email: 
...

Link to reproduction

None provided!

To reproduce

Check type mismatch of req in middleware on Sveltekit with uploadthing 7.XX

Additional information

No response

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

Code of Conduct

juliusmarminge commented 1 month ago

Fixed by #1017