mswjs / source

Generate MSW request handlers from various sources (HAR files, OpenAPI documents, etc).
https://source.mswjs.io
134 stars 5 forks source link

fix(fromOpenApi): support subdirectories in the server URL #59

Closed jimmymcpeter closed 1 month ago

jimmymcpeter commented 1 month ago

Fixes #58

jimmymcpeter commented 1 month ago

Hi, maybe I'm confused but I was trying to follow the OAS for paths

A relative path to an individual endpoint. The field name MUST begin with a forward slash (/). The path is appended (no relative URL resolution) to the expanded URL from the Server Object’s url field in order to construct the full URL

kettanaito commented 1 month ago

My bad, got things confused. There are two key points to solving this:

  1. The server URL must end with a trailing slash.
  2. The path segment must start with a dot to denote relative path to the full server URL.
new URL('/bar', 'https://example.com/foo')
// https://example.com/bar

new URL('./bar', 'https://example.com/foo/')
// https://example.com/foo/bar
jimmymcpeter commented 1 month ago

I'm curious why not just append the path to the server URL like the spec says? It shouldn't be doing any URL relative resolution, which is what URL() does when you pass a base parameter to it

kettanaito commented 1 month ago

@jimmymcpeter, behavior-wise, there should be no difference. I just prefer using the baseURL argument of URL, but in this case it may redundant, I agree.

I wish URL handled double slashes // but those are legitimate URL tokens.

kettanaito commented 1 month ago

Released: v0.2.1 🎉

This has been released in v0.2.1!

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.