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

feat(fromOpenApi): add browser support #51

Merged
merged 6 commits into from
Aug 22, 2024
Merged

Conversation

tjosepo
Copy link
Contributor

@tjosepo tjosepo commented Jul 12, 2024

Closes #49

Changes

  • Replaces import { STATUS_CODES } from 'node:http' with import { STATUS_CODES } from './status-codes.js', this way, we don't depend on Node internals.
  • Replaces @apidevtools/swagger-parser with a custom JSON Schema dereferencing function that works on the browser
  • Adds @stoplight/json to the project, to help with dereferencing JSON schemas.
    • I tried using @stoplight/http-spec as was suggested by @weyert, but it had issues with nested references, required a lot more code change, and was generally lacking in documentation. Using @stoplight/json directly was simpler. With a bit of recursion, the dereferencing algorithm really wasn't that bad.
  • All tests are green ✅

Caution

The custom dereferencing function doesn't support remote references.

Only local references are currently supported. I think it's best this way, since figuring out how to support remote references in a way that works on both Node.js and the browser will require more time. Restricting the input while we figure it out seems best.

Copy link
Collaborator

@weyert weyert left a comment

Choose a reason for hiding this comment

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

I think it's a great start to get it working in the browser. Only it doesn't appear to work when using @vitest/browser for me but appears to be related to mswjs itself. Need to play around a bit with that.

import { pointerToPath } from '@stoplight/json'

/**
* TODO: Support remote references.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is fine for a first iteration. Remote reference can also easily open a can of worms, e.g. from which relative path would it load the remote files etc?

@tjosepo
Copy link
Contributor Author

tjosepo commented Jul 22, 2024

I think it's a great start to get it working in the browser. Only it doesn't appear to work when using @vitest/browser for me but appears to be related to mswjs itself. Need to play around a bit with that.

I tested my changes on Chrome using a small OpenAPI spec. It worked. Didn't try it with @vitest/browser.

@weyert
Copy link
Collaborator

weyert commented Jul 22, 2024

I think it's a great start to get it working in the browser. Only it doesn't appear to work when using @vitest/browser for me but appears to be related to mswjs itself. Need to play around a bit with that.

I tested my changes on Chrome using a small OpenAPI spec. It worked. Didn't try it with @vitest/browser.

I think this because in Vitest you typically try to use the Node version instead of the browser version needed for @vitest/browser. At least for me it's try to look for mswjs/node

@weyert weyert requested a review from kettanaito July 26, 2024 00:33
@kettanaito kettanaito changed the title feat(fromOpenApi): add browser support for fromOpenApi feat(fromOpenApi): add browser support Aug 22, 2024
@kettanaito
Copy link
Member

I've added a simple browser test using @vitest/browser. It passes locally 🎉

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.

This is absolutely incredible! Thank you for your work on this, @tjosepo 👏

@kettanaito kettanaito merged commit ef8104b into mswjs:main Aug 22, 2024
1 check passed
@weyert
Copy link
Collaborator

weyert commented Aug 22, 2024

Awesome great work! 🚀

@kettanaito
Copy link
Member

Released: v0.3.0 🎉

This has been released in v0.3.0!

Make sure to always update to the latest version (npm i @mswjs/source@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.

OpenAPI: Browser support without polyfills
3 participants