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

Support Dynamic and Customizable Data in Handlers for @kubb/plugin-msw #1427

Open
wmahad opened this issue Nov 20, 2024 · 1 comment
Open
Labels
enhancement New feature or request @kubb/plugin-msw v3 Kubb v3

Comments

@wmahad
Copy link

wmahad commented Nov 20, 2024

What is the problem this feature would solve?

I propose adding support to @kubb/plugin-msw for passing either a data object or a function as a parameter to handlers. This would allow users to customize the behavior of handlers dynamically, making mocking and testing much more flexible.

Example

Here's an example implementation to illustrate the idea:

export function apiKeysUpdateHandler(
  data?: ApiKeysUpdateMutationResponse | Function,
  options = {}
) {
  return http.put('*/api/v2/api-keys/:uuid/', function handler(info) {
    if (isFunction(data)) return data(info);

    return new Response(
      JSON.stringify(createApiKeysUpdateMutationResponse(data)),
      {
        headers: {
          'Content-Type': 'application/json',
        },
        ...options,
      }
    );
  });
}

Use Case

This would enable users to customize handlers as shown below:

apiKeysUpdateHandler(({ params }) => {
  if (params.someKey) {
    return new Response(
      JSON.stringify({ error: 'some error response' }),
      { status: 400 }
    );
  }
  return new Response(
    JSON.stringify({ newData: 'new data' }),
    { status: 200 }
  );
});

Benefits

  • Makes it easier to mock APIs that require dynamic responses.
  • Improves testing scenarios by allowing users to modify handlers based on the request context.
  • Adds flexibility for advanced use cases like conditional errors or dynamic responses.

Reference

This feature is inspired by a similar capability in Orval.

@wmahad wmahad added the enhancement New feature or request label Nov 20, 2024
Copy link

linear bot commented Nov 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request @kubb/plugin-msw v3 Kubb v3
Projects
None yet
Development

No branches or pull requests

2 participants