next-s3-upload icon indicating copy to clipboard operation
next-s3-upload copied to clipboard

How to add a basePath to the api endpoint

Open Kipitup opened this issue 2 years ago • 3 comments

Hello,

My app is using a basePath app for all my routing. It seems that next-s3-upload is not aware of this. He is trying to access http://localhost:3000/api/s3-upload, where he should access http://localhost:3000/app/api/s3-upload, at least, I think.

I have checked the source code, and it seems that I can put it in the uploadToS3 Options. Is it right ?

How can I give it right path ? I don't really understand where to put the url in :

uploadToS3(file, {
  endpoint: {
    request: {
      body: {},
      headers: {}
    }
  }
});

thanks a lot

Kipitup avatar Jul 11 '22 11:07 Kipitup

Hey, you can pass an endpoint option to the hook with the path. This isn't documented, but it'll work :)

See this file for an example: https://github.com/ryanto/next-s3-upload/blob/master/packages/docs-site/src/pages/examples/custom-key.js#L6

Let me know if you need any more help!

ryanto avatar Jul 11 '22 13:07 ryanto

Awesome, it works like a charm, thanks a lot for the super quick answer. 😄

I think it would be worth it to add it to the documentation. With Next.js multi zone feature , adding a BasePath to your app might become more mainstream. What do you think?

Kipitup avatar Jul 11 '22 14:07 Kipitup

Yup totally. This was originally added just for testing but since there's a real use case it should be part of the public API.

I think I might add it to the uploadToS3() function where the other request options like body and headers are. What do you think?

ryanto avatar Jul 11 '22 14:07 ryanto

Ok finally got this added to the docs: https://next-s3-upload.codingvalue.com/use-s3-upload#uploadtos3-options

The way to do this now is to use:

uploadToS3(file, {
  endpoint: {
    request: {
      url: "/my/custom/api-route"
    }
  }
});

ryanto avatar Feb 25 '23 18:02 ryanto