mswjs / source

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

Is it possible to use the library with vite in a browser? #48

Closed catalin-bratu closed 2 months ago

catalin-bratu commented 2 months ago

Description

I'm trying to use the library in a vue vite application, however process.env and process.cwd are undefined in dev mode.

Is the library solely for usage with Node.js?

It looks quite promising in compassion to your competitors.

kettanaito commented 2 months ago

Hi, @catalin-bratu.

Yes, you can use @mswjs/source in the browser. The code it ships is neutral and doesn't rely on the Node.js API.

however process.env and process.cwd are undefined in dev mode.

These are likely coming from your code. There's no dependency on process in Source.

catalin-bratu commented 2 months ago

I will try to set up a sandbox to see if I can reproduce the issue in a fresh install of vite.

catalin-bratu commented 2 months ago

Maybe, I wasn't very clear. I tried to use this in the browser.

import { fromOpenApi } from '@mswjs/source/open-api' import spec from './api.spec.json'

const handlers = await fromOpenApi(spec)

syhner commented 1 month ago

@catalin-bratu there's an upstream dependency @mswjs/source -> @apidevtools/swagger-parser -> @apidevtools/json-schema-ref-parser

which is probably the source of your error see https://github.com/APIDevTools/json-schema-ref-parser/issues/354#issuecomment-2263894801 for solution

@kettanaito if you want to add a short sentence about browser support somewhere in the docs