question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set base URL for matching relative paths in request

See original GitHub issue

Is your feature request related to a problem? Please describe. I have different API URLs for different environments, for instance in development it’s http://localhost:8080 where in production it’s a relative path /api. The paths are also different for Storybook, and I am using MSW in those as well.

So right now I have to create the full request URL (absolute or relative) for all the MSW handlers, e.g.

function apiURL(path) { return process.env.REACT_APP_API_URL + path; }

setupWorker(
    rest.get(apiURL('/users'), (req, res, ctx) => {...} )
);

Describe the solution you’d like I’d like to be able to set a base URL for MSW, so that all relative paths are treated as extending from that, similar to the baseURL config property in Axios.

Describe alternatives you’ve considered The above approach works, but it’s annoying to have to wrap every path with a function like that.

I also tried using wildcards like */users, which works, but there is always the possibility that it would end up matching a different request, either from my application directly or from some other resource being pulled in from a library.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

10reactions
kettanaitocommented, Dec 25, 2020

Unfortunately, I have to close this. See the reasoning behind why this feature costs more than it brings.

It’s officially recommended to create a custom high-order function if you wish to reuse the same base path for multiple handlers. Like so:

const github = (path) => {
  return new URL(path, 'https://github.com').toString()
}

rest.get(github('/repos/:owner/:repo'), resolver)
5reactions
mpittkincommented, Sep 28, 2020

Hi @marcosvega91, @kettanaito. I initially imagined it as a part of the options to setupWorker or setupServer, similar to Axios (https://github.com/axios/axios#creating-an-instance)

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/'
});

That would probably be the simplest way, but the graphql implementation you linked above would offer more flexibility for mocking different APIs simultaneously.

In order to keep the API consistent across both rest and graphql maybe something that would be consumed like:

import { setupWorker, rest } from 'msw';
import mockUsers from './mockUsers';

const serverApi = rest.link('https://myapi.seriousbusiness.com');

const worker = setupWorker(
    serverApi.get("/users", (req, res, ctx) => {
        return res(ctx.json(mockUsers));
    });
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to specify the base URL for all relative URLs in a ...
In the below code, we have set “https://www.geeksforgeeks.org/” as the root URL for every relative URL on the page.
Read more >
How to get a URL by a relative path ? - Javascript
You can use this function: function resolve(url, base_url) { var doc = document , old_base = doc.getElementsByTagName('base')[0] , old_href ...
Read more >
Configure relative paths - Axway Documentation Portal
When a request arrives that matches the path: Enter a relative path (for example, /test/path ) for the selected HTTP Services Group.
Read more >
Secret tricks for path-independent Angular apps - Symflower
The document base URL is a URL that all relative URLs on the page (including those of links, stylesheets, scripts and script-triggered HTTP ......
Read more >
Configuring Relative Paths - Oracle Help Center
A Relative Path is associated with each Static File Provider so that requests received on this path are dispatched directly to the file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found