-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passthrough external requests with wildcard #46
Comments
Sorry about the delayed response! I'm behind on issues a bit due to some consulting/training work. Yes – But it looks like you found the function version of (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.) |
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:
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. |
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:
setPassThroughUrl(`${url}/**`)
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. |
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! |
Currently I am having a problem. I have a few of external services I am using mirage to stub api responses for one service and I want to passthrough the rest of services with wildcard. But when I add a regex it passes through even the requests that has a matched pattern in
routes()
This is what I am doing:
this.passthrough((request) => request.url.match(/.*/s));
If I just do
this.passthrough()
it doesn't work for external apis on a different origin and I don't want to out each url manually.Thanks
The text was updated successfully, but these errors were encountered: