From 55254f4f397210095e6d8d6b4daa20085e6d76cf Mon Sep 17 00:00:00 2001 From: reslear Date: Wed, 25 Oct 2023 02:56:39 +0200 Subject: [PATCH 1/4] docs: add `baseUrl` for Node.js example --- src/content/docs/basics/intercepting-requests.mdx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/content/docs/basics/intercepting-requests.mdx b/src/content/docs/basics/intercepting-requests.mdx index c9be157a..553b3ac7 100644 --- a/src/content/docs/basics/intercepting-requests.mdx +++ b/src/content/docs/basics/intercepting-requests.mdx @@ -195,7 +195,20 @@ export const handlers = [ ### How do I use relative URLs in Node.js? -You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object). +You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object), + +### How to set `baseURL` in Node.js? +Create a custom high-order function if you wish to reuse the same base path for multiple handlers. Like so: + +``` ts +const github = (path: string) => { + return new URL(path, 'https://github.com').toString() +} + +rest.get(github('/repos/:owner/:repo'), resolver) +``` + +See [the reasoning](https://github.com/mswjs/msw/pull/406#issuecomment-708519619) behind why this solution is recommended. ## Related materials From 2549de9d45bedcc8cb77c7fc1459d2db325b866a Mon Sep 17 00:00:00 2001 From: reslear Date: Wed, 25 Oct 2023 17:14:38 +0200 Subject: [PATCH 2/4] resolve changes --- src/content/docs/basics/intercepting-requests.mdx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/content/docs/basics/intercepting-requests.mdx b/src/content/docs/basics/intercepting-requests.mdx index 553b3ac7..05e8c6fc 100644 --- a/src/content/docs/basics/intercepting-requests.mdx +++ b/src/content/docs/basics/intercepting-requests.mdx @@ -195,17 +195,17 @@ export const handlers = [ ### How do I use relative URLs in Node.js? -You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object), +You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object). ### How to set `baseURL` in Node.js? Create a custom high-order function if you wish to reuse the same base path for multiple handlers. Like so: -``` ts -const github = (path: string) => { - return new URL(path, 'https://github.com').toString() +``` js +const github = (path) => { + return new URL(path, 'https://github.com').href } -rest.get(github('/repos/:owner/:repo'), resolver) +http.rest.get(github('/repos/:owner/:repo'), resolver) ``` See [the reasoning](https://github.com/mswjs/msw/pull/406#issuecomment-708519619) behind why this solution is recommended. From 26827d645d2a35fd595a689b096313ac59d556e8 Mon Sep 17 00:00:00 2001 From: reslear Date: Wed, 25 Oct 2023 17:16:34 +0200 Subject: [PATCH 3/4] move to faq --- src/content/docs/basics/intercepting-requests.mdx | 13 ------------- src/content/docs/faq.mdx | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/content/docs/basics/intercepting-requests.mdx b/src/content/docs/basics/intercepting-requests.mdx index 05e8c6fc..19bf0376 100644 --- a/src/content/docs/basics/intercepting-requests.mdx +++ b/src/content/docs/basics/intercepting-requests.mdx @@ -197,19 +197,6 @@ export const handlers = [ You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object). -### How to set `baseURL` in Node.js? -Create a custom high-order function if you wish to reuse the same base path for multiple handlers. Like so: - -``` js -const github = (path) => { - return new URL(path, 'https://github.com').href -} - -http.rest.get(github('/repos/:owner/:repo'), resolver) -``` - -See [the reasoning](https://github.com/mswjs/msw/pull/406#issuecomment-708519619) behind why this solution is recommended. - ## Related materials - [Browser integration](/docs/integrations/browser) diff --git a/src/content/docs/faq.mdx b/src/content/docs/faq.mdx index 917aefd4..0b78d189 100644 --- a/src/content/docs/faq.mdx +++ b/src/content/docs/faq.mdx @@ -150,6 +150,19 @@ beforeEach(() => { }) ``` +### How to set `baseURL` in Node.js? +Create a custom higher-order function for reusing the same base path with multiple handlers. + +``` js +const github = (path) => { + return new URL(path, 'https://github.com').href +} + +http.rest.get(github('/repos/:owner/:repo'), resolver) +``` + +Check out [the rationale](https://github.com/mswjs/msw/pull/406#issuecomment-708519619) for the recommended approach. + ## Light theme when? Whenever you have time to [open a pull request](https://github.com/mswjs/mswjs.io). From 55303530febb489459a94ac8d874fef87c405571 Mon Sep 17 00:00:00 2001 From: reslear Date: Wed, 25 Oct 2023 17:17:19 +0200 Subject: [PATCH 4/4] remove extra space --- src/content/docs/basics/intercepting-requests.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/basics/intercepting-requests.mdx b/src/content/docs/basics/intercepting-requests.mdx index 19bf0376..c9be157a 100644 --- a/src/content/docs/basics/intercepting-requests.mdx +++ b/src/content/docs/basics/intercepting-requests.mdx @@ -195,7 +195,7 @@ export const handlers = [ ### How do I use relative URLs in Node.js? -You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object). +You don't. In Node.js, there is nothing to be relative to. If you are describing a network behavior for a Node.js application, use the absolute URLs you are requesting. If you are using MSW with a Node.js-based test runner, like Jest or Vitest, configure those runners accordingly to support relative URLs (i.e. polyfill the `window.location` object). ## Related materials