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

Match by HTTP Header #1549

Closed
1 task
donaldpipowitch opened this issue Feb 20, 2023 · 0 comments
Closed
1 task

Match by HTTP Header #1549

donaldpipowitch opened this issue Feb 20, 2023 · 0 comments
Labels

Comments

@donaldpipowitch
Copy link

donaldpipowitch commented Feb 20, 2023

Scope

Adds a new behavior

Compatibility

  • This is a breaking change

Feature description

There is a great official Storybook plugin for MSW (msw-storybook-addon), but it doesn't support sophisticated documentation of multiple stories (which use the same endpoints) within the same page.

This could be fixed on a architecture level (isolating every Story within in an iframe with independent workers), but imho it can be fixed in a simpler way similar to how CSS-in-JS usually fixed "global CSS" (by a proper "scoping" concept).

I'm a new MSW user and if I saw correctly it is currently only possible to match by method and URL. It would actually be nice to match by HTTP Headers as well. It would be pretty easy to append a Story-specific unique HTTP Header to every request. If this would be the case I could easily match against it. With more sophisticated matching options we could support mswjs/msw-storybook-addon#83 in user land.

export const Example1: Story = {
  parameters: {
    msw: {
      handlers: [
        rest.get(
          {
            path: '/example',
            headers: {
              'x-msw-mock': 'example-1',
            },
          },
          (req, res, ctx) => {
            return res(ctx.json('Hello'));
          }
        ),
      ],
    },
  },
};

export const Example2: Story = {
  parameters: {
    msw: {
      handlers: [
        rest.get(
          {
            path: '/example',
            headers: {
              'x-msw-mock': 'example-2',
            },
          },
          (req, res, ctx) => {
            return res(ctx.json('World'));
          }
        ),
      ],
    },
  },
};

There's definitely room for improving the API (e.g. by providing a rest that is already scoped):

export const Example1: Story = {
  parameters: {
    msw: {
      scope: 'example-1',
      handlers: ({ rest }) => [
        rest.get('/example', (req, res, ctx) => {
          return res(ctx.json('Hello'));
        }),
      ],
    },
  },
};

export const Example2: Story = {
  parameters: {
    msw: {
      scope: 'example-2',
      handlers: ({ rest }) => [
        rest.get('/example', (req, res, ctx) => {
          return res(ctx.json('World'));
        }),
      ],
    },
  },
};

In our application code we would need to access this scope (e.g. via a React hook) and append it to all requests (e.g. in an Axios interceptor, if you use Axios).

@mswjs mswjs locked and limited conversation to collaborators Feb 21, 2023
@kettanaito kettanaito converted this issue into discussion #1550 Feb 21, 2023

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

1 participant