-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customize request sent to API route (#73)
Send extra info with requests so the key function can use user data to customize the path
- Loading branch information
Showing
7 changed files
with
257 additions
and
8 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/docs-site/cypress/integration/custom-key-based-on-request.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe("Custom key paths based on requests", () => { | ||
it("should upload to S3 using a custom key path based on the request", () => { | ||
cy.visit("/examples/custom-key-based-on-request"); | ||
|
||
cy.get("[data-test=header-name-input]").type("header"); | ||
cy.get("[data-test=body-name-input]").type("body"); | ||
cy.get("[data-test=file-input]").attachFile("woods.jpg"); | ||
|
||
cy.get("[data-test=image]").isFixtureImage("woods.jpg"); | ||
cy.get("[data-test=url]") | ||
.contains("/header/body/woods.jpg") | ||
.should("exist"); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/docs-site/src/pages/api/custom-key-based-on-request.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { APIRoute } from "next-s3-upload"; | ||
|
||
export default APIRoute.configure({ | ||
async key(req, filename) { | ||
return new Promise(resolve => { | ||
let bodyName = req.body.bodyName; | ||
let headerName = req.headers["x-header-name"]; | ||
|
||
let path = `${headerName}/${bodyName}/${filename}`; | ||
|
||
setTimeout(() => { | ||
resolve(path); | ||
}, 1000); | ||
}); | ||
} | ||
}); |
75 changes: 75 additions & 0 deletions
75
packages/docs-site/src/pages/examples/custom-key-based-on-request.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useState } from "react"; | ||
import { useS3Upload } from "next-s3-upload"; | ||
|
||
export default function UploadTest() { | ||
let [headerName, setHeaderName] = useState(""); | ||
let [bodyName, setBodyName] = useState(""); | ||
let [imageUrl, setImageUrl] = useState(); | ||
|
||
let { uploadToS3 } = useS3Upload({ | ||
endpoint: "/api/custom-key-based-on-request" | ||
}); | ||
|
||
const handleFileChange = async ({ target }) => { | ||
let file = target.files[0]; | ||
|
||
let { url } = await uploadToS3(file, { | ||
endpoint: { | ||
request: { | ||
headers: { | ||
"X-Header-Name": headerName | ||
}, | ||
body: { | ||
bodyName | ||
} | ||
} | ||
} | ||
}); | ||
|
||
setImageUrl(url); | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-col space-y-4"> | ||
<div> | ||
<input | ||
type="text" | ||
value={headerName} | ||
onChange={e => setHeaderName(e.target.value)} | ||
data-test="header-name-input" | ||
/> | ||
</div> | ||
|
||
<div> | ||
<input | ||
type="text" | ||
value={bodyName} | ||
onChange={e => setBodyName(e.target.value)} | ||
data-test="body-name-input" | ||
/> | ||
</div> | ||
|
||
<div> | ||
<input | ||
type="file" | ||
name="file" | ||
multiple={true} | ||
data-test="file-input" | ||
onChange={handleFileChange} | ||
/> | ||
</div> | ||
|
||
{imageUrl && ( | ||
<div className="flex space-x-6"> | ||
<div className="w-1/2"> | ||
<img className="object-contain" src={imageUrl} data-test="image" /> | ||
</div> | ||
<div className="w-1/2"> | ||
<div className="mt-2 text-sm text-gray-500">URL:</div> | ||
<div data-test="url">{imageUrl}</div> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27531f5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-s3-upload – ./
next-s3-upload.vercel.app
next-s3-upload-ryanto.vercel.app
next-s3-upload.codingvalue.com
next-s3-upload-git-master-ryanto.vercel.app