Fetch-API requests seem to be skipped and only XMLHttp is being caught
See original GitHub issueDescription
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:
- OIDC requests (e.g. GET https://my-auth-provider.com/realm/master/token) (using oidc-client-ts which is using XMLHttp requests
- API requests (using a custom generated package by the use of swagger-codegen which is using Fetch-API requests
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:
But the network clearly shows that my fetch requests are also being executed:
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:
- Created a year ago
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
portable-fetch
as a dependencyisomorphic-fetch
as a dependendySolution
Configuring those consumed API-packages A & B to using the
globalThis.fetch
orwindow.fetch
(or justfetch
), 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 !
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 🚀