You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To generate fake API responses, you must create a configuration file in JSON format that defines the endpoints and fields for each endpoint. We recommended use `.json` type for files. Here's an example configuration file `config.json`:
28
+
To generate fake API responses, you must create a configuration file in JSON format that defines the endpoints and response template for each endpoint. We recommended use `.json` type for files. Here's an example configuration file `config.json`:
29
29
30
30
```json
31
31
{
32
-
"cache": 5,
33
32
"endpoints": [
34
33
{
35
34
"url": "/users",
36
-
"fields": {
37
-
"id": "uuid",
38
-
"name": "name",
39
-
"email": "email"
40
-
},
41
-
"response": "list"
35
+
"type": "GET", // default is GET if omitted
36
+
"cache": 5, // individual cache for this endpoint (5 requests)
37
+
"response": [
38
+
{
39
+
"id": "uuid",
40
+
"name": "name",
41
+
"email": "email"
42
+
}
43
+
]
42
44
},
43
45
{
44
46
"url": "/products",
45
-
"fields": {
47
+
"cache": 10, // different cache size for this endpoint
48
+
// single object response when response is an object
49
+
"response": {
46
50
"id": "uuid",
47
51
"name": "word",
48
-
"price": "price"
52
+
"price": "float"
49
53
}
50
54
},
51
55
{
52
56
"url": "/products/{id}",
53
-
"fields": {
57
+
"cache": 3, // smaller cache for individual product
58
+
"response": {
54
59
"id": "uuid",
55
60
"tags": [{
56
61
"id": "uuid",
@@ -66,56 +71,130 @@ To generate fake API responses, you must create a configuration file in JSON for
66
71
},
67
72
{
68
73
"url": "/list",
69
-
"fields": {
74
+
// no cache field - this endpoint won't be cached
75
+
"response": {
70
76
"names": ["name", "name", "name"]
71
77
}
78
+
},
79
+
{
80
+
"url": "/submit",
81
+
"type": "POST",
82
+
// POST endpoints typically don't need caching
83
+
"response": {"status": "word"}
84
+
},
85
+
{
86
+
"url": "/update/{id}",
87
+
"type": "PATCH",
88
+
"cache": 1, // cache only 1 response for updates
89
+
"response": {"updated": "word"}
90
+
},
91
+
{
92
+
"url": "/replace/{id}",
93
+
"type": "PUT",
94
+
"response": {"replaced": "word"}
95
+
},
96
+
{
97
+
"url": "/remove/{id}",
98
+
"type": "DELETE",
99
+
"response": {"removed": "word"}
72
100
}
73
101
]
74
102
}
75
103
```
76
-
This configuration file defines two endpoints: /users and /products. The /users endpoint returns a list of users with pagination (by default page = 1 and per_page = 10), while the /products endpoint returns a single product.
77
-
78
-
Cache is not required, but if existed it is count of request for caching. By default cache settings is 0 and every request will generate new data. If you specify -1, this will turn off further generation and all data will be used from the cache.
79
-
80
-
You can also specify the type Array, Object as a value. Each of these types can have nested values.
81
-
82
-
List of types:
83
-
- uuid
84
-
- city
85
-
- state
86
-
- country
87
-
- latitude
88
-
- longitude
89
-
- name
90
-
- name_prefix
91
-
- name_suffix
92
-
- first_name
93
-
- last_name
94
-
- gender
95
-
- ssn
96
-
- hobby
97
-
- email
98
-
- phone
99
-
- username
100
-
- password
101
-
- paragraph
102
-
- sentence
103
-
- phrase
104
-
- quote
105
-
- word
106
-
- date
107
-
- second
108
-
- minute
109
-
- hour
110
-
- month
111
-
- day
112
-
- year
113
-
- url
114
-
- domain
115
-
- ip
116
-
- int
117
-
- float
104
+
This configuration file defines several endpoints. The `/users` endpoint returns a list (top-level array in `response`) with pagination (by default `page=1` and `per_page=10`). The `/products` endpoint returns a single object (when `response` is an object). Endpoints may specify an HTTP method using `type` and support: `GET` (default), `POST`, `PATCH`, `PUT`, `DELETE`.
118
105
106
+
## Caching
107
+
108
+
Each endpoint can have its own individual cache configuration:
109
+
110
+
-**`cache: 5`** - Cache responses for 5 requests, then generate new data
111
+
-**`cache: 10`** - Cache responses for 10 requests, then generate new data
112
+
-**`cache: 1`** - Cache only 1 response, then generate new data
113
+
-**No `cache` field** - No caching, generate new data on every request
114
+
115
+
**Important notes:**
116
+
- Caching only works for `GET` requests
117
+
- Each endpoint has its own separate cache instance
118
+
- Cache is based on request URL and query parameters
119
+
- If `cache` is not specified, the endpoint will not use caching
120
+
121
+
You can specify arrays or objects inside `response`. A top-level object means a single-object response; a top-level array (e.g., `[ { ... } ]`) means a list response where the first item defines the item template. Nested arrays/objects are supported.
To generate fake API responses using this configuration file, you can run the fake command:
121
200
@@ -130,6 +209,91 @@ By default, the fake command starts a web server on port 8000 that responds to r
130
209
fake-cli -c path/to/config.json -p 8080
131
210
```
132
211
212
+
## Migration from Global Cache
213
+
214
+
If you're upgrading from a version with global cache, here's how to migrate:
215
+
216
+
**Old configuration (global cache):**
217
+
```json
218
+
{
219
+
"cache": 5,
220
+
"endpoints": [...]
221
+
}
222
+
```
223
+
224
+
**New configuration (individual cache):**
225
+
```json
226
+
{
227
+
"endpoints": [
228
+
{
229
+
"url": "/users",
230
+
"cache": 5, // Move cache setting to each endpoint
231
+
"response": {...}
232
+
},
233
+
{
234
+
"url": "/products",
235
+
"cache": 5, // Apply same cache to all endpoints
236
+
"response": {...}
237
+
}
238
+
]
239
+
}
240
+
```
241
+
242
+
**Benefits of individual cache:**
243
+
- Different cache sizes for different endpoints
244
+
- Better memory management
245
+
- More granular control
246
+
- No global cache conflicts
247
+
248
+
## Examples
249
+
250
+
### Individual Cache per Endpoint
251
+
252
+
Here's how different cache configurations work in practice:
253
+
254
+
```json
255
+
{
256
+
"endpoints": [
257
+
{
258
+
"url": "/api/users",
259
+
"cache": 5,
260
+
"response": {"id": "uuid", "name": "name"}
261
+
},
262
+
{
263
+
"url": "/api/products",
264
+
"cache": 10,
265
+
"response": {"id": "uuid", "title": "word"}
266
+
},
267
+
{
268
+
"url": "/api/orders",
269
+
"response": {"id": "uuid", "status": "word"}
270
+
}
271
+
]
272
+
}
273
+
```
274
+
275
+
**Behavior:**
276
+
-`/api/users` - Same response for 5 requests, then new data
277
+
-`/api/products` - Same response for 10 requests, then new data
278
+
-`/api/orders` - New data on every request (no caching)
279
+
280
+
### Cache Testing
281
+
282
+
Test your cache configuration:
283
+
284
+
```bash
285
+
# Start the server
286
+
./fake-cli config.json 8080
287
+
288
+
# Test cached endpoint (should return same data)
289
+
curl http://localhost:8080/api/users
290
+
curl http://localhost:8080/api/users # Same response
291
+
292
+
# Test non-cached endpoint (should return different data)
293
+
curl http://localhost:8080/api/orders
294
+
curl http://localhost:8080/api/orders # Different response
295
+
```
296
+
133
297
## Customization
134
298
135
299
You can customize the types of fake data generated by editing the handler/handler.go file. The MakeHandler function generates fake data based on the fields and response type defined in the configuration file.
If you find a bug or would like to suggest a new feature, you can create an issue on the GitHub repository for this project. If you'd like to contribute code, you can fork the repository and submit a pull request with your changes.
0 commit comments