|
1 |
| -# micro-http-proxy |
| 1 | +# @particular./micro-cors-http-proxy |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@particular./micro-cors-http-proxy) [](https://github.com/semantic-release/semantic-release) [](https://github.com/prettier/prettier) [](https://circleci.com/gh/uniquelyparticular/micro-cors-http-proxy) |
| 4 | + |
| 5 | +> 🎮 Minimal HTTP Proxy implementation to support secure whitelisting and CORS |
| 6 | +
|
| 7 | +Built with [Micro](https://github.com/zeit/micro)! 🤩 |
| 8 | + |
| 9 | +## 🛠 Setup |
| 10 | + |
| 11 | +Create a `.env` at the project root with the following credentials: |
| 12 | + |
| 13 | +```dosini |
| 14 | +PROXY_PREFIX=proxy |
| 15 | +PROXY_REFERER_WHITELIST=localhost,*.zendesk.com,*.myshopify.com,*.now.sh |
| 16 | +PROXY_DESTINATION_WHITELIST=api.stripe.com,api.goshippo.com,api.shipengine.com,api.moltin.com,*.myshopify.com,*.salesforce.com,*.demandware.net |
| 17 | +``` |
| 18 | + |
| 19 | +`PROXY_PREFIX` is optional and will default to `'proxy'` but is used in the URI patterns to determine where to find the encoded uri to proxy request to (ie. `https://12345678.ngrok.io/<<<PROXY_PREFIX>>>/https%3A%2F%2Fapi.somethingsecure.com%2Fadmin%2Fcustomers.json`) |
| 20 | + |
| 21 | +`PROXY_REFERER_WHITELIST` is a comma separated list of patterns to match against the incoming requests 'Referer' header (ex. `localhost,*.myawesomesite.com,*.now.sh`) |
| 22 | +_(and yes, 'REFERER' is intentionally misspelled to match the http header! 😉)_ |
| 23 | + |
| 24 | +`PROXY_DESTINATION_WHITELIST` is a comma separated list of patterns to match against the URI you are proxying requests to. (ex. `api.somethingsecure.com,*.somotherapi.com`) |
| 25 | + |
| 26 | +## 📦 Package |
| 27 | + |
| 28 | +Run the following command to build the app |
| 29 | + |
| 30 | +```bash |
| 31 | +yarn install |
| 32 | +``` |
| 33 | + |
| 34 | +Start the development server |
| 35 | + |
| 36 | +```bash |
| 37 | +yarn dev |
| 38 | +``` |
| 39 | + |
| 40 | +The server will typically start on PORT `3000`. If not, you'll need to restart ngrok to point to whatever server port you've started here. |
| 41 | + |
| 42 | +## ⛽️ Usage |
| 43 | + |
| 44 | +To send requests to the proxy, the calling implementation should use `encodeURIComponent` to encode the portion of the URI after the `PROXY_PREFIX`. |
| 45 | + |
| 46 | +Once your server is up and running, you can send `GET`, `DELETE`, `OPTIONS`, `PATCH`, `PUT` and `POST` requests to it, ensuring that you are URIencoding the value after your `PROXY_PREFIX`. (ex. `https://<<<NGROK_URL>>>/<<<PROXY_PREFIX>>>/https%3A%2F%2Fapi.somewheresecure.com%2Fsomemethod`) |
| 47 | + |
| 48 | +Sample call using [jQuery.get()](https://api.jquery.com/jquery.get/) below: |
| 49 | + |
| 50 | +```js |
| 51 | +const sampleEndpoint = `https://12345678.ngrok.io//proxy/${encodeURIComponent( |
| 52 | + 'https://23456789.myshopify.com/admin/customers.json' |
| 53 | +)}` |
| 54 | + |
| 55 | +$.ajax({ |
| 56 | + url: sampleEndpoint, |
| 57 | + type: 'GET', |
| 58 | + corse: true, |
| 59 | + beforeSend: xhr => { |
| 60 | + xhr.setRequestHeader( |
| 61 | + 'X-Shopify-Access-Token', |
| 62 | + '1XXXXXxxxxxXXXXXxxxxxXXXXXxxxxx1' |
| 63 | + ) |
| 64 | + } |
| 65 | +}) |
| 66 | + .done(console.log) |
| 67 | + .fail(console.error) |
| 68 | +``` |
| 69 | + |
| 70 | +## 🚀 Deploy |
| 71 | + |
| 72 | +You can easily deploy this function to [now](https://now.sh). |
| 73 | + |
| 74 | +_Contact [Adam Grohs](https://www.linkedin.com/in/adamgrohs/) @ [Particular.](https://uniquelyparticular.com) for any questions._ |
0 commit comments