apimock-rs 4.7.0 β 4.7.1
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 +283 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# apimock-rs (API Mock)
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/apimock-rs)
|
|
4
|
+
[](https://crates.io/crates/apimock)
|
|
5
|
+
[](https://github.com/apimokka/apimock-rs/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
[](https://docs.rs/apimock)
|
|
8
|
+
[](https://deps.rs/crate/apimock)
|
|
9
|
+
[](https://github.com/apimokka/apimock-rs/actions/workflows/release-executable.yaml)
|
|
10
|
+
[](https://github.com/apimokka/apimock-rs/actions/workflows/docs.yaml)
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
Build a working REST API in seconds β without a backend.
|
|
15
|
+
Frontend blocked by an unfinished backend ?
|
|
16
|
+
Need stable API responses for UI tests or offline development ?
|
|
17
|
+
Drop JSON files into a folder and your API immediately exists.
|
|
18
|
+
|
|
19
|
+
## Mock APIs easily π β just JSON and go
|
|
20
|
+
|
|
21
|
+
If youβre building or testing APIs, this tool makes mocking painless. Itβs super fast, efficient, and flexible when you need it to be.
|
|
22
|
+
All you have to do to start up is just use folders and JSON without any config set.
|
|
23
|
+
|
|
24
|
+
- βοΈ Zero-config start.
|
|
25
|
+
- π¬οΈ Fast to boot, light on memory.
|
|
26
|
+
- πͺ File-based and rule-based matching. Scripting supported.
|
|
27
|
+
|
|
28
|
+
### Why `apimock-rs` ?
|
|
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
|
|
37
|
+
|
|
38
|
+
As your project grows, your mock API grows, too. Large mock datasets often cause problems:
|
|
39
|
+
|
|
40
|
+
- Slow startup
|
|
41
|
+
- High memory usage
|
|
42
|
+
- Crashes during UI testing
|
|
43
|
+
- Unstable CI runs
|
|
44
|
+
|
|
45
|
+
apimock-rs does not preload responses. Each response file is read only when a request arrives using non-blocking I/O. This keeps:
|
|
46
|
+
|
|
47
|
+
- Startup nearly instant
|
|
48
|
+
- Memory usage minimal
|
|
49
|
+
- Stable behavior under repeated requests
|
|
50
|
+
|
|
51
|
+
as validated with k6 load testing.
|
|
52
|
+
You can run UI development and automated tests continuously without worrying about server instability.
|
|
53
|
+
|
|
54
|
+
### π Documentation
|
|
55
|
+
|
|
56
|
+
For more details, **π§ check out [the docs](https://apimokka.github.io/apimock-rs/)**.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Quick start
|
|
61
|
+
|
|
62
|
+
Easy to start with [npm package](https://www.npmjs.com/package/apimock-rs).
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
# install into your app project
|
|
66
|
+
npm install -D apimock-rs
|
|
67
|
+
# and go
|
|
68
|
+
npx apimock
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
# just use folders and JSON
|
|
73
|
+
mkdir -p api/v1/
|
|
74
|
+
echo '{"hello": "world"}' > api/v1/hello.json
|
|
75
|
+
npx apimock
|
|
76
|
+
|
|
77
|
+
# response
|
|
78
|
+
curl http://localhost:3001/api/v1/hello
|
|
79
|
+
# --> {"hello":"world"}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
You may also check it out with browser to visit http://localhost:3001/api/v1/hello .
|
|
83
|
+
|
|
84
|
+
You now have a running REST endpoint.
|
|
85
|
+
|
|
86
|
+
### Vite project integration
|
|
87
|
+
|
|
88
|
+
An example of **scripts** section in **package.json** is as below.
|
|
89
|
+
|
|
90
|
+
**concurrently** is used to run the Vite and API mock servers simultaneously, while **cross-env** enables terminal output coloring. Before starting, ensure you run:
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
npm install -D concurrently cross-env
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Edit package.json:
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
"scripts": {
|
|
100
|
+
"apimock": "npx apimock",
|
|
101
|
+
"dev": "cross-env CLICOLOR_FORCE=1 concurrently \"vite\" \"npm run apimock\""
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Run:
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
npm run dev
|
|
109
|
+
```
|
|
110
|
+
|
|
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
|
+
---
|
|
121
|
+
|
|
122
|
+
## Configuration reference
|
|
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`.
|
|
215
|
+
|
|
216
|
+
| `op` | Meaning | Example |
|
|
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" }` |
|
|
223
|
+
|
|
224
|
+
##### Wildcard / glob syntax (`wild_card`)
|
|
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. |
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## Open-source, with care
|
|
276
|
+
|
|
277
|
+
This project is lovingly built and maintained by volunteers.
|
|
278
|
+
We hope it helps streamline your API development.
|
|
279
|
+
Please understand that the project has its own direction β while we welcome feedback, it might not fit every edge case π±
|
|
280
|
+
|
|
281
|
+
## Acknowledgements
|
|
282
|
+
|
|
283
|
+
Depends on [tokio](https://github.com/tokio-rs/tokio) / [hyper](https://hyper.rs/) / [toml](https://github.com/toml-rs/toml) / [serde](https://serde.rs/) / [serde_json](https://github.com/serde-rs/json) / [json5](https://github.com/callum-oakley/json5-rs) / [console](https://github.com/console-rs/console) / [rhai](https://github.com/rhaiscript/rhai) / [thiserror](https://crates.io/crates/thiserror) / [anyhow](https://crates.io/crates/anyhow). In addition, [mdbook](https://github.com/rust-lang/mdBook) (as to workflows).
|
package/package.json
CHANGED