Comments (4)
update for anyone else coming across this requirement and until a clearer solution within miragejs is added.
my miragejs server setup. Some imports and custom method missing but the important stuff is here.
export default function makeServer({
environment = 'test',
url = undefined,
}: {
environment: string
url?: string
}) {
const server = createServer({
environment,
models: {
business: Model,
},
// @see https://miragejs.com/docs/main-concepts/route-handlers/
routes() {
if (typeof url != 'undefined') {
console.log('set passthrough url to new request', url)
this.passthrough(url)
}
this.namespace = 'api'
this.get('/businesses', handleGet)
this.passthrough()
},
})
return server
}
/**
*
* @param environment
* @param url e.g. 'https://api.trello.com/1/**'
* @returns
*/
export const runDemoServer = (environment = 'demo', url = undefined) => {
const server = makeServer({ environment, url })
server.db.loadData({
businesses: businessesData,
})
return server
}
A few things to note from the snippet above:
- the url (e.g.
https://api.trello.com/1/**
is the domain with a wildcard which needs to be dynamically crafted to then pass into therunDemoServer
method - I'm not showing how I crafted the url because it can be different depending on the needs but if anyone needs it let me know.
- implemented the dynamic url update via redux using this action. Not showing redux because that can change per project as well. The url in this example was
https://api.trello.com/1
setPassThroughUrl(`${url}/**`)
- the miragejs server needs to be shutdown and restarted to take effect. This could be improved like I said. In my case, I added it to the
App.js
file at the root where I have access to the redux store and passed the url from redux as a dependency to its ownuseEffect
to avoid it restarting and creating multiple mock servers. I notice this still happens sometimes with live reload using react dev server but a refresh solves that and I have not found it intrusive just yet.
useEffect(() => {
let server
if (typeof server != 'undefined') {
console.log('shutdown miragejs server')
server.shutdown()
}
server = runDemoServer('demo', props.passThroughUrl)
console.log('passThroughUrl<>', props.passThroughUrl, '\n server', server)
}, [props.passThroughUrl])
I do hope to move away from this patch to a clearer solution but hopefully my solution helps someone and the miragejs maintainers come up with a better solution.
Thanks.
from discuss.
Sorry about the delayed response! I'm behind on issues a bit due to some consulting/training work.
Yes – this.passthrough()
only works for the current domain, and due to limitations in one of our dependencies it's not possible to do something like this.passthrough('*')
to pass through all domains.
But it looks like you found the function version of passthrough()
which is exactly what I was going to recommend. Is that working? If so, that's public API and you should be all set using it 👍
(FYI we are planning on adding a simple option to Mirage to let it passthrough all requests that haven't been explicitly mocked out. The passthrough API trips up a lot of folks and I think this will help.)
from discuss.
is there any movement on this feature @samselikoff ? Was not sure if I should create a similar issue since I found this one. My use case is: I'm actually using miragejs to power the demo portion of the app mocking certain routes but I need it to pass all others. On the docs you shared, there is a line that says:
If you want all requests on the current domain to pass through, simply invoke the method with no argument
The key is the current domain part. I don't want to have to figure out how to dynamically add external urls when my app will change this from the BE and there is no easy way to do this without major refactoring. The easiest solution is for miragejs to let me pass all traffic but those specifically set in the routes like this issue initially posted.
I could not figure out how to use the passthrough method to do that.
from discuss.
Glad you figured it out, I haven't been actively working on mirage in a while but happy to help others if they want to work on a middleware api or improvements to passthrough!
from discuss.
Related Issues (20)
- Add Middleware support to Mirage HOT 1
- Custom exception post-processing HOT 2
- Support multiple server instance. HOT 5
- Tracking issue for routing DSL / passthrough / namespace improvements
- Model hooks / default attributes HOT 5
- hasMany to hasMany (manyToMany) fixture associations not created HOT 1
- nativescript-vue with miragejs HOT 1
- Question: Expose mocked API as standalone on a specific port? HOT 3
- Be able to mock websocket calls HOT 2
- More array functions for models / collections HOT 8
- cannot use trait with afterCreate HOT 8
- routes for related resources HOT 2
- [Suggestion] Automatically parse request body based on content type HOT 2
- Feature Request: Generate API docs from test runs HOT 3
- How to only intercept request that defined in routes HOT 2
- server.createList() and server.create() creates duplicate values with dynamic factory definition
- Is it possible to use a combination of mock API's and actual API calls ? HOT 1
- Using a nested primary id for update and deletes
- Adding context object to handler to mock auth
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from discuss.