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

Refactor adapters #76

Merged
merged 16 commits into from
Jun 7, 2024
Merged

Refactor adapters #76

merged 16 commits into from
Jun 7, 2024

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Jun 6, 2024

Problem

To reuse the adapters with a different REST dialect, developers must rewrite everything.
Also, middlewares are adapter-specific.

Solution

Use dependency injection instead of inheritance for server logic.

Rename servers to adapters

TODO

  • Make BaseServer work only with a context
  • Update FetchMockServer to delegate handling to server
  • Update MswServer to delegate handling to server
  • Update SinonServer to delegate handling to server
  • Rename BaseServer to SimpleRestServer
  • Fix validation middleware in Msw and Sinon
  • Fix tests
  • Update doc & upgrade guide

@fzaninotto fzaninotto added the WIP Work In Progress label Jun 6, 2024
expect(request.status).toEqual(418);
// @ts-ignore
expect(request.responseText).toEqual(
'{"data":[{"id":1,"name":"foo"},{"id":2,"name":"bar"}],"status":200}',
);
});

it('should pass request in response interceptor', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is no longer true

@fzaninotto fzaninotto changed the title [WIP] Refactor adapters Refactor adapters Jun 7, 2024
@fzaninotto fzaninotto added RFR Ready For Review and removed WIP Work In Progress labels Jun 7, 2024
Copy link
Contributor

@djhi djhi left a comment

Choose a reason for hiding this comment

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

This is great! 🔥

const adapter = new MswAdapter({ server });
const handler = adapter.getHandler();
```

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO we should go a bit further and at least mention the required methods of a Server object and their input/output

Copy link
Contributor

@djhi djhi left a comment

Choose a reason for hiding this comment

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

Otherwise 🔥

README.md Outdated
Intercept AJAX calls to fake a REST server based on JSON data. Use it on top of [Sinon.js](http://sinonjs.org/) (for `XMLHTTPRequest`) or [fetch-mock](https://github.com/wheresrhys/fetch-mock) (for `fetch`) to test JavaScript REST clients on the browser side (e.g. single page apps) without a server.
A browser library that intercepts AJAX calls to mock a REST server based on JSON data.

Use it on top of [Sinon.js](http://sinonjs.org/) (for `XMLHttpRequest`) or [fetch-mock](https://github.com/wheresrhys/fetch-mock) (for `fetch`) to test JavaScript REST clients on the browser side (e.g. single page apps) without a server.
Copy link
Contributor

Choose a reason for hiding this comment

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

Promote fetch and MSW first?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@djhi djhi merged commit 848c1b2 into master Jun 7, 2024
3 checks passed
@djhi djhi deleted the refacto-server branch June 7, 2024 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants