-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 80b055d
Showing
7 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
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,2 @@ | ||
.vercel | ||
node_modules/ |
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 @@ | ||
.vscode |
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 @@ | ||
# fake-sanity-image-cdn |
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,36 @@ | ||
const EXTRACT_INFO_FROM_PATH_REGEX = /([a-zA-Z0-9]+)-([0-9]+)x([0-9]+)\.([a-z]+)$/i; | ||
|
||
// TODO: Rename endpoint to `/api/sanity-endpoint` | ||
// TODO: Extract width and height from the query to overload the one in the path | ||
// TODO: Extract the format from the query to validate it (picsum.photos only support jpg) | ||
export default (req, res) => { | ||
console.log({ | ||
body: req.body, | ||
query: req.query, | ||
cookies: req.cookies, | ||
}); | ||
|
||
const { | ||
query: { path: originalPath = "/" }, | ||
} = req; | ||
|
||
const infos = EXTRACT_INFO_FROM_PATH_REGEX.exec(originalPath); | ||
if (!infos) { | ||
return res.status(503).end("Not a sanity URL"); | ||
} | ||
|
||
const [, photoId, width, height, format] = infos; | ||
|
||
if (!["jpg", "jpeg", "png", "gif"].includes(format)) { | ||
return res.status(503).end("Only image could be faked"); | ||
} | ||
|
||
const destination = `https://picsum.photos/id/${photoId}/${width}/${height}.${format}`; | ||
|
||
console.log({ | ||
destination, | ||
}); | ||
|
||
res.writeHead(301, { Location: destination }); | ||
res.end(); | ||
}; |
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,14 @@ | ||
{ | ||
"name": "fake-sanity-image-cdn", | ||
"version": "0.0.1", | ||
"description": "An endpoint that emulate sanity image CDN behavior but return placeholder image instead", | ||
"main": "index.js", | ||
"author": "Armand Abric <[email protected]>", | ||
"license": "MIT", | ||
"private": false, | ||
"scripts": {}, | ||
"devDependencies": { | ||
"prettier": "^2.0.5", | ||
"vercel": "^19.2.0" | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"rewrites": [{ "source": "/bar(/.*)", "destination": "/api/proxy?path=$1" }] | ||
} |
Oops, something went wrong.