Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

support selecting interceptors #2071

Closed
1 task
juliusmarminge opened this issue Mar 6, 2024 · 1 comment
Closed
1 task

support selecting interceptors #2071

juliusmarminge opened this issue Mar 6, 2024 · 1 comment
Labels

Comments

@juliusmarminge
Copy link

juliusmarminge commented Mar 6, 2024

Scope

Adds a new behavior

Compatibility

  • This is a breaking change

Feature description

maybe there's a clear reason but why are there no options when creating the server? seems quite limiting.

for example, if i only wanna mock xml request:

const server = new SetupServerApi(handlers, {
  interceptors: [XMLHttpInterceptor]
}
@kettanaito
Copy link
Member

Hi, @juliusmarminge. That's a good question!

MSW promotes the philosophy that you can describe the network regardless of the implementation details of that network. Providing a precise interceptor is not something we encourage directly because you want consistent behavior no matter how the request is made.

If you absolutely must narrow down the interception, you can do that by creating your own setupServer function using the public API:

import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
import { SetupServerCommonApi } from 'msw/node'

class SetupXMLHttpRequestServerApi extends SetupServerCommonApi {
  constructor(...handlers) {
    super([XMLHttpRequestInterceptor], handlers)
  }
}

function mySetupServer(...handlers) {
  return new SetupXMLHttpRequestServerApi(...handlers)
}

You will be limited in the feature set, however, since SetupServerCommonApi doesn't have Node-specific features like Server Boundary. We extend the same base class for Node.js and for React Native so that base class must remain neutral.

@mswjs mswjs locked and limited conversation to collaborators Mar 6, 2024
@kettanaito kettanaito converted this issue into discussion #2072 Mar 6, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants