Skip to main content

Route

API Reference

continue

API Reference

Continue will execute the request on the client and pass the response back to the server.

client.route("**/api", (route) => {
return route.continue();
});

You can optionally override aspects of the request.

client.route("**/api", (route) => {
return route.continue({
// Will override all request headers rather than extend them
headers: {
"X-Override": "value",
},
});
});

error

API Reference

Error will simulate a network level error causing fetch to throw an error.

client.route("**/api", (route) => {
return route.error();
});

fallback

API Reference

Fallback to the next route handler. If there are no more route handlers the request will default to passthrough.

client.route("**/api", (route) => {
return route.fallback();
});

fetch

API Reference

Similar to continue but will not pass the response back to the server. This is useful when you want to fetch the data from the remote server and modify the response before passing it back to the server.

client.route("**/api", (route) => {
const response = await route.fetch();
response.headers.set("X-Custom-Header", "Value");
return response;
});

fulfill

API Reference

Useful when you want to fixture the response with mock data. This allows you to fully control the response body, status, and headers.

client.route("**/api", (route) => {
return route.fulfill({
status: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ hello: "world" }),
});
});

You can also fulfill the response with a file. Any file type is supported, allowing you to mock binary data. The content-type header (if not explicitly set) will be derived from the file extension using the mime-types package.

tip

Make sure to use absolute paths for the file path using the path module.

client.route("**/api", (route) => {
return route.fulfill({
path: "/path/to/file.json",
});
});

modifyReponse

Useful helper function when you want to modify the response before passing it back to the server, as with route.fetch().

client.route("**/api", (route) => {
const response = await route.fetch();
const responseJson = await response.json();

// Add custom data field to the original response
const modifiedResponse = client.modifiedResponse({
body: JSON.stringify({
...responseJson,
modified: true,
}),
});

return modifiedResponse;
});

passthrough

API Reference

Passthrough results in the request being fulfilled on the server over the network without any interference. This is the default behaviour when no route handlers are defined, or there is no match on the URL for any of the route handlers. It's also the fallback behaviour when the last matched route handler returns route.fallback().

client.route("**/api", (route) => {
return route.passthrough();
});

request (property)

info

First introduced in @mocky-balboa/client@1.1.0

The request property is the original request object that was passed to the route handler, an instance of the Request class. You can use this to differentiate between how you might want to handle different requests on the same route pattern.

client.route("**/api", (route) => {
const requestMethod = route.request.method;
if (requestMethod === "GET") {
return route.fulfill({
status: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ hello: "world" }),
});
}

return route.fulfill({
status: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ hello: "again world" }),
});
});