Route
continue
Continue will execute the request on the client and pass the response back to the server.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.continue();
});
client.route("**/api", (route) => {
return route.continue();
});
You can optionally override aspects of the request.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.continue({
// Will override all request headers rather than extend them
headers: {
"X-Override": "value",
},
});
});
client.route("**/api", (route) => {
return route.continue({
// Will override all request headers rather than extend them
headers: {
"X-Override": "value",
},
});
});
error
Error will simulate a network level error causing fetch
to throw an error.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.error();
});
client.route("**/api", (route) => {
return route.error();
});
fallback
Fallback to the next route handler. If there are no more route handlers the request will default to passthrough.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.fallback();
});
client.route("**/api", (route) => {
return route.fallback();
});
fetch
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.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
const response = await route.fetch();
response.headers.set("X-Custom-Header", "Value");
return response;
});
client.route("**/api", (route) => {
const response = await route.fetch();
response.headers.set("X-Custom-Header", "Value");
return response;
});
fulfill
Useful when you want to fixture the response with mock data. This allows you to fully control the response body, status, and headers.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.fulfill({
status: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ hello: "world" }),
});
});
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.
Make sure to use absolute paths for the file path using the path module.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.fulfill({
path: "/path/to/file.json",
});
});
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()
.
- TypeScript
- JavaScript
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;
});
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
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()
.
- TypeScript
- JavaScript
client.route("**/api", (route) => {
return route.passthrough();
});
client.route("**/api", (route) => {
return route.passthrough();
});
request (property)
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.
- TypeScript
- JavaScript
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" }),
});
});
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" }),
});
});