question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetch-API requests seem to be skipped and only XMLHttp is being caught

See original GitHub issue

Description

I have implemented a simple mock server:

import { createServer } from 'miragejs'

export function makeServer (environment: string = 'development') {
  console.debug('Starting mock server')
  let server = createServer({
    environment,

    routes: function () {
      this.logging = true
      this.urlPrefix = 'https://my-api.com'
      this.namespace = '/'

      this.passthrough(request => {
        console.debug('passthrough', request.url)
        return request.url.startsWith(`${import.meta.env.VITE_EXTERNAL_API_URL}`) || request.url.startsWith(`${import.meta.env.VITE_OAUTH_PROVIDER}`)
      });

      this.get('company', () => {
        console.debug('Handling GET company')
        const companiesResponse = [{
          address: 'Some address',
          avatar_url: 'https://some-image-server.com/my-company-image.png',
          description: 'The company',
          name: 'Company INC',
          phone: '+49 30 555 555 555',
          id: 1
        }]

        return companiesResponse
      })
    }
  })

  return server
}

My setup is consisting out of the following XHR:

Project is based on:

  • Vite 3.1.0
  • Vue 3.2.37
  • TypeScript 4.6.4
  • CORS is active for the used API services - but that shouldn’t matter if the requests are being intercepted correctly by miragejs

Problem

In my console, I only get logged the XMLHttp requests. The Fetch-API requests are not even being caught in the passthrough-function …

Screenshot:

Bildschirmfoto 2022-10-11 um 17 58 48

But the network clearly shows that my fetch requests are also being executed:

Bildschirmfoto 2022-10-11 um 18 02 46


Any ideas?

Note: In the source-code of the miragejs project I could only find TS types for XMLHttp, nothing for fetch. Do we have support for the fetch-API? At least, the documentation says that both should work.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
waldemarennsaedcommented, Oct 11, 2022

Resolved

I was wondering about the following scenario:

Applying a custom, native fetch-request into my application (not using any swagger-codegen generated client-API), resolved in miragejs catching those fetch-requests that I do by just using the native fetch-API.

The fetch-requests from the API-packages, were not caught. Inspecting the client-APIs and their types, they provide the option for passing a fetch API. Further inspection of my API-packages, resolved into the following insights:

  • package A was using portable-fetch as a dependency
  • package B was using isomorphic-fetch as a dependendy

Solution

Configuring those consumed API-packages A & B to using the globalThis.fetch or window.fetch (or just fetch), resolved the issue. Now those requests of those packages are being intercepted correctly by miragejs.

Thanks for all the support and quick resposes @cah-brian-gantzler !

0reactions
waldemarennsaedcommented, Oct 12, 2022

Note: There is also the alpha option of using MSW https://mswjs.io/docs/ instead of pretender with mirage. Let me know if you want to try that out. Assume that would have worked with your previous code as the intercept point is in a different place.

Thanks, just looked into MSWJS and it looks pretty nice. I am sure it should work with my code since it’s intercepting on the outgoing network request on service-worker level.

I will post my results here after trying it out 🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

[XMLHttpRequest] Unable to catch http status with fetch api
My react-native app is communicating with a node.js server app. I am able to receive response data but upon sending specific http response ......
Read more >
Javascript fetch not catching error, skipping past catch statement
I'm sending HTTP-requests to a server and when I receive them, I check for the status. If the status isn't 200, I want...
Read more >
Ajax Battle: XMLHttpRequest vs the Fetch API
In this article we examine the pros and cons of the ancient XMLHttpRequest and its modern Fetch equivalent, to decide which Ajax API...
Read more >
Why I still use XHR instead of the Fetch API - Go Make Things
Today, I want to explain why. A quick overview of XHR vs. fetch() JSON Placeholder is an awesome service for testing API calls....
Read more >
Fetch API, XMLHTTPRequest replacement - YouTube
More Software engineering videos https://www.youtube.com/playlist?list=PLQnljOFTspQXOkIpdwjsMlVqkIffdqZ2KFetch API is a new web standard by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found