From 7b4dc9834e629da2f0d4bda5ecb099b4878e020e Mon Sep 17 00:00:00 2001 From: Armand Abric Date: Mon, 17 Aug 2020 10:28:12 +0200 Subject: [PATCH] `/proxy` endpoint and some docs --- README.md | 27 +++++++++++++++++++++++++++ api/proxy.js | 1 - vercel.json | 9 ++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a81c055..6aa6b34 100644 --- a/README.md +++ b/README.md @@ -1 +1,28 @@ # fake-sanity-image-cdn + +## What is it? + +A proxy to use Sanity @sanity/image-url helper with +fake image. Usefull in tests or storybook. + +## How to use it? + +Use this proxy service as the `baseUrl` when you create the +imageUrlBuilder instanse: + +```js +import imageUrlBuilder from '@sanity/image-url'; + +const myConfiguredSanityClient = { +/_ ... _/, +baseUrl: "https://fake-sanity-image-cdn.vercel.app/proxy/" +}; + +const builder = imageUrlBuilder(myConfiguredSanityClient); + +console.log(builder.image("...").url()) +``` + +## Limitations + +- Do not support `webp` image transformations (`jpg` image will alwails be returned) diff --git a/api/proxy.js b/api/proxy.js index d6a760e..2d64de5 100644 --- a/api/proxy.js +++ b/api/proxy.js @@ -1,6 +1,5 @@ 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) => { diff --git a/vercel.json b/vercel.json index b053352..2e0bf78 100644 --- a/vercel.json +++ b/vercel.json @@ -1,3 +1,10 @@ { - "rewrites": [{ "source": "/bar(/.*)", "destination": "/api/proxy?path=$1" }] + "redirects": [ + { + "source": "/", + "statusCode": 301, + "destination": "https://github.com/gogaille/fake-sanity-image-cdn" + } + ], + "rewrites": [{ "source": "/proxy(/.*)", "destination": "/api/proxy?path=$1" }] }