Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
armandabric committed Aug 17, 2020
0 parents commit 80b055d
Show file tree
Hide file tree
Showing 7 changed files with 746 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vercel
node_modules/
1 change: 1 addition & 0 deletions .vercelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# fake-sanity-image-cdn
36 changes: 36 additions & 0 deletions api/proxy.js
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();
};
14 changes: 14 additions & 0 deletions package.json
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"
}
}
3 changes: 3 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rewrites": [{ "source": "/bar(/.*)", "destination": "/api/proxy?path=$1" }]
}
Loading

0 comments on commit 80b055d

Please sign in to comment.