0

I'm trying to proxy backend call to API with specific query parameter. Say, API path is /path/to/api?param1=foo&param2=bar, and I need to proxy only requests with param2=bar. Proxying calls to /path/to/api without or with any query parameters is easy and not my case.

I started digging from official documentation, but it containts only basic examples. Then I found this article, where it's said that I can use custom matcher function, but according to this comment it's not supported in Angular. Then I visited webpack documentation and found that custom matcher could probably be passed to proxy config context property. Though, I can't figure out how to use it in Angular proxy config, since it has another schema. This article seems to have the most close hint to construct the path I need, and I played with different options, though none of them really worked. I figured out that path uses either micromatch or glob format, but this knowledge didn't help me as well. I also found this answer to similar question and played a bit with suggested regex, adapting it to my case, but again with no luck.

// ...
"**/path/to/api?*param2=bar*": {
  "target": "https://localhost:4200",
  "secure": false,
  "pathRewrite": () => "/mocks/api.mock.json"
},
// ...

This is one of many approaches I tried, and it doesn't proxy needed request.

1 Answer 1

1

Try to use pathRewrite callback to retrieve information about url path.

Example:

"**/v1/myApi": {
  "pathRewrite": (path) => {
      if (path.includes('param=bar')) {
          return "/mocks/response1.mock.json";
      }
      return "/mocks/response2.mock.json";
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

How can you add the callback if the proxy-config file is a .json?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.