Skip to content
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

[Question] How to skip certain URL (some URL use mock data and some still go to real API )? #169

Open
yanzou opened this issue Sep 29, 2023 · 1 comment

Comments

@yanzou
Copy link

yanzou commented Sep 29, 2023

Hi team, thanks for this great tool!
I would like to ask a more detail question reguarding to the fake-response example:

Say I working a new feature work with a new API entry api/newFeature, so I would like mock only this new API entry:

xhook.before(function(request, callback) {
   swtich (request.url) {
      case 'api/newFeature': 
          callback({
             status: 200,
             text: JSON.stringify({ hello: 'world' }),  // mock data, so I have to wait for API team's code
                ...
          });
         break;

       default: 
          ???    // <------- how can I let other URL by passed and get real API resonse ?
          break;
   }
});
@yanzou yanzou changed the title [Question] How to skip certain URL? [Question] How to skip certain URL (some URL use mock and some still go to real API )? Sep 29, 2023
@yanzou yanzou changed the title [Question] How to skip certain URL (some URL use mock and some still go to real API )? [Question] How to skip certain URL (some URL use mock data and some still go to real API )? Sep 29, 2023
@condorheroblog
Copy link

I think what you need is the next function provided by the connect repository.

xhook is fully supported, you have two options depending on the xhook.before mock response type

  1. return response
xhook.before(function (request) {
	switch (request.url) {
		case "api/newFeature":
			return {
				status: 200,
				text: JSON.stringify({ hello: "world" }), // mock data, so I have to wait for API team's code
			};

		default:
			// next, will fetch external link
			return;
	}
});
  1. callback response
xhook.before(function (request, callback) {
	switch (request.url) {
		case "api/newFeature":
			callback({
				status: 200,
				text: JSON.stringify({ hello: "world" }), // mock data, so I have to wait for API team's code
			});

		default:
			// next, will fetch external link
			callback();
	}
});

The callback function I use is very easy to use: fetch external link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants