helpscout / proxypack

🎭ProxyPack: Proxy Local Webpack Assets to your Production Domain
MIT License
6 stars 1 forks source link

Intercepting/mocking endpoints #4

Open ItsJonQ opened 5 years ago

ItsJonQ commented 5 years ago

Just an idea! I shared this with @tjbo yesterday when we 🍐 'ed.

What if.. there was a way to provide ProxyPack's webpack setup with a file.. maybe functions.js or something.

And within that file, you'd have something like:

// functions.js
const { handle } = require('@helpscout/proxypack')

handle('some-url', (req, res) => {
  const { data: originalData } = req

  return res.status(200).json({
    ...originalData,
    extraStuff: 'weeeee'
  })
})

This will allow us to adjust/test/mock data when developing. If proxypack + webpack could watch this file, we can update it while webpack serve is up!

How neat would that be :)

P.S. The above implementation was inspired by serverless cloud function like APIs (specifically Google/Firebase Cloud Functions. I'm most familiar with those)

tjbo commented 5 years ago

Great idea, I really like it. I'm not sure if maybe the functions should just be added to the Webpack config.... (then of course they could always be imported via a file. Meaning, I think there would have to be some glue in the plugin), kinda like this:

new ProxyPackPlugin({
    browser: 'chrome',
    domain: 'https://secure.helpscout.net',
    webpackMappings: ['https://*.cloudfront.net/*/js/apps/dist/*'],
    mocks: [
        ('some-url', (req, res) => {
            const { data: originalData } = req
            return {
                ...originalData,
                extraStuff: 'weeeee',
                status: 200
            }
        }),
        ('some-other-url', (req, res) => {
            const { data: originalData } = req
            return {
                status: 404
            }
        })
    ]
})