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

fix: add "listHandlers" method to server and worker #1369

Merged
merged 4 commits into from Aug 30, 2022

Conversation

AlexeiDarmin
Copy link
Contributor

@AlexeiDarmin AlexeiDarmin commented Aug 24, 2022

@kettanaito is something like this what you had in mind?

Change overview

This PR adds a listHandlers() method that returns a list of currently active handlers as suggested in #474 (comment).

You can list currently active handlers via server.printHandlers(). Alas, it prints the handlers summary stdout instead of returning a list. If you are interested, it'd be awesome to have a change where printHandlers() returns a list of handlers:

Additional change proposal

Now that devs would have access to individual handlers, handlers would benefit from having a .toString() or .stringSummary() to easily see a string summary of the handler. The logging logic within printHandlers could be moved to handler.toString(), then printHandlers would simply be:

 printHandlers() {
        currentHandlers.forEach((handler) => {
          console.log(handler.toString())
        })
      }

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 24, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 50f6b9b:

Sandbox Source
MSW React Configuration

@kettanaito kettanaito changed the title feat(createsetupserver.ts): add listHandlers method that returns acti… feat: add "listHandlers" method Aug 25, 2022
@kettanaito
Copy link
Member

Hey, @AlexeiDarmin. Thank you for opening this on such a short notice!

I absolutely agree with the summary proposal and .toString() on individual handlers. A great idea. Would you have a chance to add it to this pull request?

@@ -137,6 +137,10 @@ export function createSetupServer(
)
},

listHandlers() {
return currentHandlers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be cautious exposing internals, and currentHandlers is internals. I fear that developers will be tempted to meddle with this array directly. That should never happen. They should use explicit APIs like use or resetHandlers to affect the list of handlers, and listHandlers() must be purely readonly.

Maybe let's make this array readonly before exposing it?

// First, guard the array on the type level.
listHandlers(): ReadonlyArray<RequestHandler> {
  // Second, prevent mutations on runtime.
  const immutableHandlers = [...currentHandlers]
  Object.freeze(immutableHandlers)
  return immutableHandlers
}

* Returns lists of all active request handlers.
* @see {@link https://mswjs.io/docs/api/setup-server/list-handlers `server.list-handlers()`}
*/
listHandlers(): RequestHandler<RequestHandlerDefaultInfo, MockedRequest<DefaultBodyType>, any, MockedRequest<DefaultBodyType>>[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go to the readonly list route (which I think we should), let's adjust this return type as well.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely love the changes! The only suggestion I have is to make sure that we expose a readonly array from listHandlers() so that people are not tempted to mutate it.

@kettanaito
Copy link
Member

Since we're not deprecating printHandlers() just yet, I will rename this change to be a fix.

@kettanaito kettanaito changed the title feat: add "listHandlers" method fix: add "listHandlers" method Aug 25, 2022
@kettanaito
Copy link
Member

I've covered some of the previous suggestions, also added listHandlers to the worker API. Let's see if the tests pass.

@kettanaito kettanaito changed the title fix: add "listHandlers" method fix: add "listHandlers" method to server and worker Aug 30, 2022
@kettanaito kettanaito merged commit 18f5778 into mswjs:main Aug 30, 2022
@AlexeiDarmin
Copy link
Contributor Author

@kettanaito thanks for applying the changes! I appreciate your feedback and was going to incorporate when I had a minute.

@kettanaito
Copy link
Member

Released: v0.46.0 🎉

This has been released in v0.46.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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

Successfully merging this pull request may close these issues.

None yet

2 participants