Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using wildcard/parameters in baseUrl in toHandlers method #267

Closed
timkolotov opened this issue Mar 2, 2023 · 4 comments
Closed

Using wildcard/parameters in baseUrl in toHandlers method #267

timkolotov opened this issue Mar 2, 2023 · 4 comments

Comments

@timkolotov
Copy link
Contributor

I recently started using MSW for both jest and storybook tests.
In our project, we use relative paths for the API, so the resulting URL depends on the environment the call is made. E.g. if storybook is run on http://127.0.0.1:6006/ this host will be used in the API call. But it can also be run on http://localhost:6006/

So in MSW handlers, I used baseUrl with parameters such as http://:hostname/:lang/api/v1/ and it works perfectly. Now I wanted to reduce the amount of boilerplate and add mswjs/data with automatic generation of handles, but it seems to be not possible anymore as the URL constructor in createUrlBuilder fails to parse such a string as that is not a valid URL.

Is there any other way for manipulating the resulting paths in handlers, like just adding prefixes? Or the only way is to manually add all handlers?

@kettanaito
Copy link
Member

Hey, @timkolotov. I think this should be fixed on the library's side. The createUrlBuilder function, as you've rightfully mentioned, should support path strings as well.

export function createUrlBuilder(baseUrl?: string) {
return (path: string) => {
const url = new URL(path, baseUrl || 'http://localhost')
return baseUrl ? url.toString() : url.pathname
}
}

From the look of it, I don't see any implications of supporting that. Would you be interested in opening a pull request with this feature?

@timkolotov
Copy link
Contributor Author

Yeah, will do. Probably this weekend.

@timkolotov
Copy link
Contributor Author

@kettanaito hey, I created a PR for this. It seems I can't request a review for it, so just FYI 🙂

@kettanaito
Copy link
Member

Thanks, @timkolotov! Not sure what's with the review request permissions but I will look into it regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants