apimock-rs 4.7.1 β 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -170
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,15 +25,7 @@ All you have to do to start up is just use folders and JSON without any config s
|
|
|
25
25
|
- π¬οΈ Fast to boot, light on memory.
|
|
26
26
|
- πͺ File-based and rule-based matching. Scripting supported.
|
|
27
27
|
|
|
28
|
-
###
|
|
29
|
-
|
|
30
|
-
- The backend is not ready yet.
|
|
31
|
-
- You need stable API responses for UI testing.
|
|
32
|
-
- You want offline development.
|
|
33
|
-
- CI tests require a predictable API.
|
|
34
|
-
- Your mock data is becoming large.
|
|
35
|
-
|
|
36
|
-
### Handles real project scale
|
|
28
|
+
### `apimock-rs` handles real project scale
|
|
37
29
|
|
|
38
30
|
As your project grows, your mock API grows, too. Large mock datasets often cause problems:
|
|
39
31
|
|
|
@@ -42,6 +34,16 @@ As your project grows, your mock API grows, too. Large mock datasets often cause
|
|
|
42
34
|
- Crashes during UI testing
|
|
43
35
|
- Unstable CI runs
|
|
44
36
|
|
|
37
|
+
### When to use ?
|
|
38
|
+
|
|
39
|
+
- The backend is not ready yet.
|
|
40
|
+
- You need stable API responses for UI testing.
|
|
41
|
+
- You want offline development.
|
|
42
|
+
- CI tests require a predictable API.
|
|
43
|
+
- Your mock data is becoming large.
|
|
44
|
+
|
|
45
|
+
### Performance
|
|
46
|
+
|
|
45
47
|
apimock-rs does not preload responses. Each response file is read only when a request arrives using non-blocking I/O. This keeps:
|
|
46
48
|
|
|
47
49
|
- Startup nearly instant
|
|
@@ -51,10 +53,6 @@ apimock-rs does not preload responses. Each response file is read only when a re
|
|
|
51
53
|
as validated with k6 load testing.
|
|
52
54
|
You can run UI development and automated tests continuously without worrying about server instability.
|
|
53
55
|
|
|
54
|
-
### π Documentation
|
|
55
|
-
|
|
56
|
-
For more details, **π§ check out [the docs](https://apimokka.github.io/apimock-rs/)**.
|
|
57
|
-
|
|
58
56
|
---
|
|
59
57
|
|
|
60
58
|
## Quick start
|
|
@@ -83,6 +81,20 @@ You may also check it out with browser to visit http://localhost:3001/api/v1/hel
|
|
|
83
81
|
|
|
84
82
|
You now have a running REST endpoint.
|
|
85
83
|
|
|
84
|
+
### `npx apimock` variation
|
|
85
|
+
|
|
86
|
+
| command | result |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `npx apimock` | Run with all default parameters. |
|
|
89
|
+
| `npx apimock -p 4000` | Run with custom port. |
|
|
90
|
+
| `npx apimock -d tests/apimock-dyn-route` | Run with custom root dir on server response. |
|
|
91
|
+
| `npx apimock -c apimock.toml` | Run with config file giving rich features. Running `npx apimock --init` beforehand is required. |
|
|
92
|
+
| `npx apimock --init` | **Interactive setup.** Prompts for port / IP / fallback dir / whether to scaffold a rule-set file, middleware file, and TLS section, then writes `apimock.toml` (and optionally `apimock-rule-set.toml` / `apimock-middleware.rhai`) customised to your answers. |
|
|
93
|
+
| `npx apimock --init --yes` | Non-interactive setup: skip every prompt and write the same defaults 4.7.0 wrote (`127.0.0.1:3001`, rule-set file included, TLS commented out). Useful in CI or Docker builds. |
|
|
94
|
+
| `npx apimock --init --middleware` | Also scaffold `apimock-middleware.rhai`. Combines with `--yes`. |
|
|
95
|
+
|
|
96
|
+
When stdin is not a TTY (piped, CI, Docker build), `--init` silently falls back to defaults even without `--yes` β so existing non-interactive usage of 4.7.0 keeps working unchanged.
|
|
97
|
+
|
|
86
98
|
### Vite project integration
|
|
87
99
|
|
|
88
100
|
An example of **scripts** section in **package.json** is as below.
|
|
@@ -108,167 +120,13 @@ Run:
|
|
|
108
120
|
npm run dev
|
|
109
121
|
```
|
|
110
122
|
|
|
111
|
-
### `npx apimock` variation
|
|
112
|
-
|
|
113
|
-
| command | result |
|
|
114
|
-
| --- | --- |
|
|
115
|
-
| `npx apimock` | Run with all default parameters. |
|
|
116
|
-
| `npx apimock -p 4000` | Run with custom port. |
|
|
117
|
-
| `npx apimock -d tests/apimock-dyn-route` | Run with custom root dir on server response. |
|
|
118
|
-
| `npx apimock -c apimock.toml` | Run with config file giving rich features. Running `npx apimock --init` beforehand is required. |
|
|
119
|
-
|
|
120
123
|
---
|
|
121
124
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
Everything below describes the TOML config files. Run `npx apimock --init` (or
|
|
125
|
-
`apimock --init`) once to scaffold `apimock.toml` and `apimock-rule-set.toml`
|
|
126
|
-
into the current directory, then tweak to taste.
|
|
127
|
-
|
|
128
|
-
### `apimock.toml` β top-level file
|
|
129
|
-
|
|
130
|
-
```toml
|
|
131
|
-
[listener]
|
|
132
|
-
ip_address = "127.0.0.1" # interface to bind
|
|
133
|
-
port = 3001 # plaintext HTTP port
|
|
134
|
-
|
|
135
|
-
[listener.tls] # optional β enables HTTPS
|
|
136
|
-
cert = "./cert.pem"
|
|
137
|
-
key = "./key.pem"
|
|
138
|
-
# port = 3002 # optional β if omitted, HTTPS replaces HTTP on the port above
|
|
139
|
-
|
|
140
|
-
[log]
|
|
141
|
-
verbose = { header = true, body = true }
|
|
142
|
-
|
|
143
|
-
[service]
|
|
144
|
-
strategy = "first_match" # only value supported today
|
|
145
|
-
rule_sets = ["apimock-rule-set.toml"] # paths, resolved relative to this file
|
|
146
|
-
middlewares = [] # Rhai scripts, resolved relative to this file
|
|
147
|
-
fallback_respond_dir = "." # folder served when no rule matches
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
| Section / field | Required | Default | Notes |
|
|
151
|
-
| --- | --- | --- | --- |
|
|
152
|
-
| `listener.ip_address` | no | `127.0.0.1` | Bind address. `0.0.0.0` to accept from other machines. |
|
|
153
|
-
| `listener.port` | no | `3001` | Plaintext HTTP port. |
|
|
154
|
-
| `listener.tls.cert` / `.key` | yes if `[listener.tls]` set | β | PEM files. Any key format `rustls-pki-types` accepts (PKCS#8, PKCS#1, SEC1). |
|
|
155
|
-
| `listener.tls.port` | no | same as `listener.port` | When omitted, the single port serves HTTPS only β no plaintext listener is started. |
|
|
156
|
-
| `log.verbose.header` | no | `false` | Log each request's headers. |
|
|
157
|
-
| `log.verbose.body` | no | `false` | Log each request's URL query and JSON body (pretty-printed). |
|
|
158
|
-
| `service.strategy` | no | `first_match` | Currently the only strategy β the first rule that matches wins. |
|
|
159
|
-
| `service.rule_sets` | no | `[]` | See **Rule-set schema** below. Paths relative to this file. |
|
|
160
|
-
| `service.middlewares` | no | `[]` | Rhai script paths, relative to this file. |
|
|
161
|
-
| `service.fallback_respond_dir` | no | `.` | Folder served when nothing in `rule_sets` or `middlewares` handled the request. |
|
|
162
|
-
|
|
163
|
-
### Rule-set schema β `apimock-rule-set.toml`
|
|
164
|
-
|
|
165
|
-
```toml
|
|
166
|
-
[prefix] # optional
|
|
167
|
-
url_path = "/api/v1/" # prepended to every rule's url_path
|
|
168
|
-
respond_dir = "apimock-rule-sets/@respond-dir" # prepended to every rule's file_path
|
|
169
|
-
|
|
170
|
-
[default] # optional
|
|
171
|
-
delay_response_milliseconds = 0 # applied to every rule in this set unless overridden
|
|
172
|
-
|
|
173
|
-
[[rules]]
|
|
174
|
-
when.request.url_path = "complicated"
|
|
175
|
-
when.request.method = "POST"
|
|
176
|
-
when.request.headers.authorization = { value = "Bearer eyJhb", op = "starts_with" }
|
|
177
|
-
when.request.headers.user = { value = "user1" } # op defaults to "equal"
|
|
178
|
-
when.request.body.json."user.id" = { value = "42" }
|
|
179
|
-
respond = { text = "Strictly authorized !" }
|
|
180
|
-
|
|
181
|
-
[[rules]]
|
|
182
|
-
when.request.url_path = { value = "/public/", op = "starts_with" }
|
|
183
|
-
respond = { file_path = "public/index.json", csv_records_key = "records" }
|
|
184
|
-
|
|
185
|
-
[[rules]]
|
|
186
|
-
when.request.url_path = ""
|
|
187
|
-
respond = { status = 403 }
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
#### `when.request` β match conditions (AND-ed together)
|
|
191
|
-
|
|
192
|
-
| Field | Accepts | Purpose |
|
|
193
|
-
| --- | --- | --- |
|
|
194
|
-
| `url_path` | string *or* `{ value, op }` | URL path match. String form is shorthand for `op = "equal"`. |
|
|
195
|
-
| `method` | `"GET"` / `"POST"` / `"PUT"` / `"DELETE"` | HTTP method. Omit to match any method. |
|
|
196
|
-
| `headers` | `{ header-name = { value, op } }` map | Every listed header must match. Header names are matched case-insensitively per HTTP. |
|
|
197
|
-
| `body.json` | `{ json-path = { value, op } }` map | JSON body match β see **JSONPath form** below. |
|
|
198
|
-
|
|
199
|
-
#### `respond` β response shape
|
|
200
|
-
|
|
201
|
-
Exactly one of `file_path` / `text` / `status` must be present. (`text` may be combined with `status` to set the response code.)
|
|
202
|
-
|
|
203
|
-
| Field | Type | Purpose |
|
|
204
|
-
| --- | --- | --- |
|
|
205
|
-
| `file_path` | string | File under `prefix.respond_dir` (or the working dir if no prefix). Extension decides content type; `.json` / `.json5` / `.csv` served as JSON, other text types served verbatim with an inferred `Content-Type`, binary types (images, audio, video, `.pdf`, `.zip`) served with the corresponding binary `Content-Type`. |
|
|
206
|
-
| `text` | string | Return this string verbatim. Content type `text/plain; charset=utf-8`. |
|
|
207
|
-
| `status` | integer (100β999) | HTTP status code. With `text`, sets the status; alone, returns an empty body with that status. Cannot be combined with `file_path`. |
|
|
208
|
-
| `headers` | `{ name = value }` map | Extra response headers. |
|
|
209
|
-
| `delay_response_milliseconds` | integer | Sleep this long before responding β useful for simulating slow backends in UI tests. |
|
|
210
|
-
| `csv_records_key` | string | Only meaningful when `file_path` points at a `.csv`. The JSON response wraps the rows as `{ "<key>": [...] }` using this value; defaults to `"records"`. |
|
|
211
|
-
|
|
212
|
-
#### `op` β comparison operators
|
|
213
|
-
|
|
214
|
-
Every `{ value, op }` structure accepts these `op` values. If `op` is omitted it defaults to `equal`.
|
|
125
|
+
### π Documentation - guides and references
|
|
215
126
|
|
|
216
|
-
|
|
217
|
-
| --- | --- | --- |
|
|
218
|
-
| `equal` (default) | Exact string match | `{ value = "user1" }` matches `user1` only |
|
|
219
|
-
| `not_equal` | Anything *but* an exact match | `{ value = "guest", op = "not_equal" }` matches everything except `guest` |
|
|
220
|
-
| `starts_with` | String starts with value | `{ value = "Bearer ", op = "starts_with" }` matches any Bearer token |
|
|
221
|
-
| `contains` | Value appears anywhere inside the target | `{ value = "admin", op = "contains" }` matches `superadmin`, `admin-1`, etc. |
|
|
222
|
-
| `wild_card` | Glob match β see below | `{ value = "/api/v*/users/?", op = "wild_card" }` |
|
|
127
|
+
For more details, **π§ check out our [full documentation](https://apimokka.github.io/apimock-rs/)**.
|
|
223
128
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
The glob matcher supports exactly two metacharacters, matching the common case without the complexity of a full regex engine:
|
|
227
|
-
|
|
228
|
-
| Token | Matches |
|
|
229
|
-
| --- | --- |
|
|
230
|
-
| `?` | Exactly one character |
|
|
231
|
-
| `*` | Zero or more characters |
|
|
232
|
-
|
|
233
|
-
Examples:
|
|
234
|
-
|
|
235
|
-
| Pattern | Matches | Doesn't match |
|
|
236
|
-
| --- | --- | --- |
|
|
237
|
-
| `hello` | `hello` | `hell`, `hello!` |
|
|
238
|
-
| `file*.txt` | `file.txt`, `file123.txt`, `files/doc.txt` | `file.csv` |
|
|
239
|
-
| `file?.txt` | `file1.txt`, `fileX.txt` | `file.txt`, `file12.txt` |
|
|
240
|
-
| `a*b?c` | `axyzbxc`, `ab1c` | `abc` |
|
|
241
|
-
| `*test*` | `my_test_file`, `test` | `tes` |
|
|
242
|
-
| `γγγ«γ‘?δΈη` | `γγγ«γ‘γ―δΈη` | Unicode is respected per code point. |
|
|
243
|
-
|
|
244
|
-
This is NOT a regex. Sequences such as `.`, `[β¦]`, `(β¦)`, `+`, `^`, `$` are matched literally.
|
|
245
|
-
|
|
246
|
-
##### JSONPath form (`body.json` and `csv_records_key`)
|
|
247
|
-
|
|
248
|
-
The JSONPath support is deliberately minimal β only the "object key" and "array index" forms, joined by `.`. This covers every real-world body-match need without exposing a full jsonpath engine that users would then have to learn.
|
|
249
|
-
|
|
250
|
-
| Input | Meaning |
|
|
251
|
-
| --- | --- |
|
|
252
|
-
| `user` | Top-level key `user` |
|
|
253
|
-
| `user.id` | `user` β `id` (nested object) |
|
|
254
|
-
| `items.0` | First element of array `items` |
|
|
255
|
-
| `orders.2.total` | Array index 2 of `orders`, then key `total` |
|
|
256
|
-
| `a.b.c` | Three-level nested object lookup |
|
|
257
|
-
|
|
258
|
-
If any segment is missing or has the wrong shape (e.g. a numeric segment against an object), the rule simply does not match β no error, because "missing" is a useful matchable condition.
|
|
259
|
-
|
|
260
|
-
#### `prefix`
|
|
261
|
-
|
|
262
|
-
| Field | Effect |
|
|
263
|
-
| --- | --- |
|
|
264
|
-
| `url_path` | Prepended to every rule's `when.request.url_path` before matching. Leading/trailing slashes are normalised. The `contains` operator deliberately ignores the prefix β it matches against the raw `url_path` value β because "contains" usually means "substring anywhere in the full request path", and re-applying a prefix there would be surprising. |
|
|
265
|
-
| `respond_dir` | Prepended to every rule's `respond.file_path`. Resolved relative to the rule-set file's directory, not the working dir β so configs travel cleanly between machines. |
|
|
266
|
-
|
|
267
|
-
#### `default`
|
|
268
|
-
|
|
269
|
-
| Field | Effect |
|
|
270
|
-
| --- | --- |
|
|
271
|
-
| `delay_response_milliseconds` | Default delay for rules in this set that don't specify their own. |
|
|
129
|
+
- Configuration Reference: π [View all settings here](https://apimokka.github.io/apimock-rs/user-guide/configuration-reference.html)
|
|
272
130
|
|
|
273
131
|
---
|
|
274
132
|
|
package/package.json
CHANGED