Skip to content

Commit 1af9805

Browse files
authored
feat: Add response creator function support (#45)
* feat: Ability to specif function as route response * feat: Add function support for response creation fixes #38 * Remove console.log * Try removing function tests to appease Node 18 * Exclude test * Downgrade Mocha * Add --exit to mocha run
1 parent c2aa675 commit 1af9805

11 files changed

+351
-422
lines changed

docs/src/content/docs/credentials/cookie-credentials.mdx

Whitespace-only changes.

docs/src/content/docs/mock-servers/extended-response-patterns.mdx

+53-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Extended Response Patterns
33
description: Learn how to use extended response patterns with a mock server
44
---
55

6-
The second argument of any route method in method in a `MockServer` instance is the response pattern. This pattern can be a number (the HTTP status to return) or an object specifying additional data to return with the response. This is helpful when you're making requests that expect a specific response format.
6+
The second argument of any route method in method in a `MockServer` instance is the response pattern. This pattern can be a number (the HTTP status to return), an object specifying additional data to return with the response, or a function. This is helpful when you're making requests that expect a specific response format.
77

88
## Extended response pattern keys
99

@@ -16,7 +16,7 @@ The following keys can be used in the response pattern object:
1616

1717
The response pattern keys are used to create a new `Response` object that is returned when the associated request pattern matches the request.
1818

19-
## Responding with a specific status code
19+
### Respond with a specific status code
2020

2121
If you'd like to respond to a request with a specific status code, you can use the `status` key in the response pattern object. Here's an example:
2222

@@ -36,7 +36,7 @@ This route will respond to any GET request to `/users` with a status code of 200
3636
server.get("/users", 200);
3737
```
3838

39-
## Responding with specific headers
39+
### Respond with specific headers
4040

4141
If you'd like to respond to a request with specific headers, you can use the `headers` key in the response pattern object. Here's an example that simulates a redirect:
4242

@@ -55,7 +55,7 @@ server.get("/redirect", {
5555

5656
This route will respond to any GET request to `/redirect` with a status code of 301 and a `Location` header that redirects the client to `https://example.com`.
5757

58-
## Responding with a specific body
58+
### Respond with a specific body
5959

6060
If you'd like to respond to a request with a specific body, you can use the `body` key in the response pattern object. Here's an example:
6161

@@ -75,7 +75,7 @@ server.get("/users", {
7575

7676
This route will respond to any GET request to `/users` with a status code of 200 and a JSON response body containing an `id` and `name` property. There's no need to call `JSON.stringify` on the object; Mentoss will handle that for you.
7777

78-
## Responding with a specific body and headers
78+
### Respond with a specific body and headers
7979

8080
You can combine the `headers` and `body` keys in the response pattern object to respond to a request with specific headers and a specific body. Here's an example:
8181

@@ -98,7 +98,7 @@ server.get("/users", {
9898

9999
This route will respond to any GET request to `/users` with a status code of 200, a `Content-Type` header of `application/json`, and a JSON response body containing an `id` and `name` property.
100100

101-
## Delaying the response
101+
### Delay the response
102102

103103
If you'd like to delay the response to a request, you can use the `delay` key in the response pattern object. The `delay` property is the number of milliseconds to wait before returning the response. Here's an example:
104104

@@ -114,3 +114,50 @@ server.get("/users", {
114114
```
115115

116116
This route will respond to any GET request to `/users` with a status code of 200, but it will wait for 1 second before returning the response.
117+
118+
## Use functions for dynamic responses
119+
120+
If you'd like to respond to a request with a dynamic value, you can use a function as the response pattern. The function will receive the request object as its only argument and should return a response pattern object. Here's an example:
121+
122+
```js
123+
import { MockServer } from "mentoss";
124+
125+
const server = new MockServer("https://api.example.com");
126+
127+
server.get("/users/123", request => {
128+
129+
console.log(`Received request: ${request.id}`);
130+
131+
return {
132+
status: 200,
133+
body: {
134+
id: "123",
135+
name: "Alice",
136+
},
137+
};
138+
});
139+
```
140+
141+
This route will respond to any GET request to `/users/123` with a status code of 200 and a JSON response body containing an `id` and `name` property. The response is generated dynamically based on the request object. The `request` object contains information about the request, such as the URL, headers, and body, and contains an additional `id` property that is a unique identifier for the request.
142+
143+
Response creator functions can also by asynchronous, so you can use `await` to wait for a promise to resolve before returning the response pattern object. Here's an example:
144+
145+
```js
146+
import { MockServer } from "mentoss";
147+
148+
const server = new MockServer("https://api.example.com");
149+
150+
server.get("/users/123", async request => {
151+
await new Promise(resolve => setTimeout(resolve, 1000));
152+
153+
return {
154+
status: 200,
155+
body: {
156+
id: "123",
157+
name: "Alice",
158+
},
159+
};
160+
});
161+
```
162+
163+
This route will respond to any GET request to `/users/123` with a status code of 200 and a JSON response body containing an `id` and `name` property, but it will wait for 1 second before returning the response. You can use this to simulate network latency or other asynchronous behavior when the `delay` key is not sufficient.

eslint.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default [
1313
console: false,
1414
},
1515
},
16+
rules: {
17+
"no-console": "error"
18+
}
1619
},
1720
{
1821
files: ["tests/**/*.js"],

0 commit comments

Comments
 (0)