@xirconsss/zero-mock 0.2.2 → 1.0.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 +25 -135
- package/dist/__tests__/basic.test.js +8 -0
- package/package.json +20 -4
package/README.md
CHANGED
|
@@ -1,147 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Frontend Consumer Test
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](tsconfig.json)
|
|
5
|
-
[](https://www.npmjs.com/package/@xirconsss/zero-mock)
|
|
3
|
+
This branch tests `zero-mock` as an end-user using the published npm package.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
## How to run
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
1. **Install the CLI globally (or run via npx):**
|
|
8
|
+
```bash
|
|
9
|
+
npm install -g @xirconsss/zero-mock
|
|
10
|
+
```
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
2. **Start the mock server:**
|
|
13
|
+
```bash
|
|
14
|
+
npx @xirconsss/zero-mock -- --file ./data.json --port 3000
|
|
15
|
+
```
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
With simulated delay (2s) and watch, use **long flags** so `npx`/`npm` never treats `-d` as its own debug flag:
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
```bash
|
|
20
|
+
npx @xirconsss/zero-mock -- --file ./data.json --port 3000 --delay 2000 --watch
|
|
21
|
+
```
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
npm install -g @xirconsss/zero-mock
|
|
19
|
-
```
|
|
23
|
+
If you still see `required option '-f, --file' not specified`, use **`npm exec`** instead:
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
```bash
|
|
26
|
+
npm exec -- @xirconsss/zero-mock -- --file ./data.json --port 3000 --delay 2000
|
|
27
|
+
```
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
Or install globally and avoid `npx` parsing entirely:
|
|
24
30
|
|
|
25
|
-
```bash
|
|
26
|
-
|
|
27
|
-
```
|
|
31
|
+
```bash
|
|
32
|
+
zero-mock --file ./data.json --port 3000 --delay 2000
|
|
33
|
+
```
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
3. **Run the Frontend:**
|
|
36
|
+
Simply open `index.html` in your browser.
|
|
30
37
|
|
|
31
|
-
## Usage
|
|
32
|
-
|
|
33
|
-
| Flag | Description |
|
|
34
|
-
| ---- | ----------- |
|
|
35
|
-
| **`-f` / `--file`** | Required. Path to the JSON file. |
|
|
36
|
-
| **`-p` / `--port`** | HTTP port (default `3000`, must be 1–65535). |
|
|
37
|
-
| **`-d` / `--delay`** | Optional. Delay every request by this many milliseconds. Must be a non-negative integer using digits only (default `0`). |
|
|
38
|
-
| **`-w` / `--watch`** | Optional. Watch the JSON file and reload the in-memory data when it changes. If a save produces invalid JSON, the server prints `[watch] Could not reload "<path>": ...` to stderr and keeps the last good data until the file is valid again. Only one reload runs at a time. When watch starts, you also get `[watch] Watching "<path>" for changes.` on stdout. |
|
|
39
|
-
|
|
40
|
-
Examples:
|
|
41
|
-
|
|
42
|
-
```bash
|
|
43
|
-
zero-mock -f ./data.json -p 3000 -d 200
|
|
44
|
-
zero-mock -f ./data.json -w
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Quick start
|
|
48
|
-
|
|
49
|
-
From the repo root (or any directory containing the example file):
|
|
50
|
-
|
|
51
|
-
```bash
|
|
52
|
-
npx @xirconsss/zero-mock -f ./example/db.json -p 3000
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
Optional:
|
|
56
|
-
|
|
57
|
-
```bash
|
|
58
|
-
npx @xirconsss/zero-mock -f ./example/db.json -p 3000 -d 200 -w
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
In another terminal:
|
|
62
|
-
|
|
63
|
-
```bash
|
|
64
|
-
curl http://localhost:3000/users
|
|
65
|
-
curl http://localhost:3000/users/1
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
The server logs the exact URLs for each collection when it starts.
|
|
69
|
-
|
|
70
|
-
## Development
|
|
71
|
-
|
|
72
|
-
From a clone: `npm install`, then `npm run build` (or `npm run dev` with `ts-node`). Run the built CLI with:
|
|
73
|
-
|
|
74
|
-
```bash
|
|
75
|
-
node dist/index.js -f ./example/db.json -p 3000
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Add `-d` / `-w` the same way as the published CLI.
|
|
79
|
-
|
|
80
|
-
## Features
|
|
81
|
-
|
|
82
|
-
- **Zero-config** — one JSON file defines your API surface; no schemas or generators to run.
|
|
83
|
-
- **Full CRUD REST API** — `GET`, `POST`, `PUT`, `PATCH`, and `DELETE` per collection, with CORS and JSON bodies enabled.
|
|
84
|
-
- **Atomic file persistence** — writes go through a temp file and rename, with serialized saves so concurrent requests do not corrupt the file.
|
|
85
|
-
- **Smart ID generation** — new rows get the next **numeric** id when existing ids are integers or all-digit strings; otherwise new ids use a **UUID**.
|
|
86
|
-
- **Request logging** — each finished request logs as `[METHOD] <path> - <status>` (path is Express `req.path`, no query string).
|
|
87
|
-
- **Optional delay** — `-d` adds a fixed pause before route handling (after JSON body parsing).
|
|
88
|
-
- **List filtering and pagination** — see [List GET](#list-get) on `GET /{resource}`.
|
|
89
|
-
- **Watch mode** — `-w` reloads data from disk on file change without restarting the process (see [Usage](#usage)).
|
|
90
|
-
|
|
91
|
-
## Auto-generated API
|
|
92
|
-
|
|
93
|
-
Each **top-level key** in your JSON (e.g. `users`, `posts`) becomes a **resource** name. Replace `{resource}` with that key and `{id}` with a row’s `id` (string params match numeric ids loosely).
|
|
94
|
-
|
|
95
|
-
| Method | Path | Description |
|
|
96
|
-
| -------- | -------------------- | ----------- |
|
|
97
|
-
| `GET` | `/{resource}` | List items (optionally [filtered and paginated](#list-get)). |
|
|
98
|
-
| `POST` | `/{resource}` | Create an item; server assigns `id` and returns `201` with the new body. |
|
|
99
|
-
| `GET` | `/{resource}/{id}` | Return one item by `id`; `404` if missing. |
|
|
100
|
-
| `PUT` | `/{resource}/{id}` | Replace the item; `id` in the URL wins; `404` if missing. |
|
|
101
|
-
| `PATCH` | `/{resource}/{id}` | Shallow-merge fields into the item; `404` if missing. |
|
|
102
|
-
| `DELETE` | `/{resource}/{id}` | Remove the item; `204` on success; `404` if missing. |
|
|
103
|
-
|
|
104
|
-
Invalid JSON bodies (non-objects for write routes) receive **`400`** with a JSON error message. Persistence failures surface as **`500`**.
|
|
105
|
-
|
|
106
|
-
### List GET
|
|
107
|
-
|
|
108
|
-
**Filtering:** Every query parameter except `_page` and `_limit` is a filter. Only plain-object rows are kept. For each filter key, the row must have that property, and the value must match the query value with loose equality (`==`). Query values are strings (first value wins if repeated). Rows missing a filter key are dropped.
|
|
109
|
-
|
|
110
|
-
**Pagination:** If **both** `_page` and `_limit` are present and are positive integers (digit strings only), the list is sliced after filtering. `_page` is 1-based. If either is missing or invalid, the full filtered list is returned (no error).
|
|
111
|
-
|
|
112
|
-
Examples using [example/db.json](example/db.json):
|
|
113
|
-
|
|
114
|
-
```bash
|
|
115
|
-
curl 'http://localhost:3000/users?role=admin'
|
|
116
|
-
curl 'http://localhost:3000/users?_page=1&_limit=2'
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## JSON file shape
|
|
120
|
-
|
|
121
|
-
The root must be a JSON **object**. Each property must be an **array** (your “tables”). Each item you want to address by URL should include an **`id`** field (number or string).
|
|
122
|
-
|
|
123
|
-
## Publishing to npm (maintainers)
|
|
124
|
-
|
|
125
|
-
1. Use an [npmjs.com](https://www.npmjs.com/) account with **2FA** enabled and permission to publish the **`@xirconsss`** scope (user or org on npm).
|
|
126
|
-
2. Log in locally: `npm login` (or `npm login --auth-type=web`).
|
|
127
|
-
3. Install deps and build: `npm install` (so `tsc` is available), then bump **`version`** in `package.json` when releasing.
|
|
128
|
-
4. Publish: `npm publish` (`publishConfig.access` is already `public` for this scoped package).
|
|
129
|
-
|
|
130
|
-
**GitHub Actions:** the workflow [`.github/workflows/publish-npm.yml`](.github/workflows/publish-npm.yml) runs on **workflow_dispatch** or when a **GitHub Release** is published. It uses the GitHub **Environment** named **`NPM_TOKEN`** with a secret also named **`NPM_TOKEN`** (repo → **Settings** → **Environments** → **NPM_TOKEN** → **Environment secrets**).
|
|
131
|
-
|
|
132
|
-
**Token on npm (required):**
|
|
133
|
-
|
|
134
|
-
1. Create a classic **[Automation](https://docs.npmjs.com/creating-and-viewing-access-tokens#creating-classic-tokens)** token, **or** a **[granular access token](https://docs.npmjs.com/creating-and-viewing-access-tokens#creating-granular-access-tokens)** with **read and write** on **`@xirconsss/zero-mock`** (and org **`xirconsss`** if npm asks).
|
|
135
|
-
2. For granular tokens: turn **Bypass two-factor authentication (2FA)** **on** so CI does not hit **`EOTP`**. Do **not** use **`NPM_OTP`** secrets (codes expire in ~30 seconds).
|
|
136
|
-
3. Paste the token into the **`NPM_TOKEN`** environment secret on GitHub, then run the workflow.
|
|
137
|
-
|
|
138
|
-
**Optional — OIDC trusted publishing:** You can later move to [npm trusted publishing](https://docs.npmjs.com/trusted-publishers) and drop the secret; if you see **`E404`** on `PUT` with OIDC, the Trusted Publisher settings on npm (repo, workflow filename, environment name) do not match this workflow—token auth avoids that until it is configured correctly.
|
|
139
|
-
|
|
140
|
-
## License
|
|
141
|
-
|
|
142
|
-
MIT - see [LICENSE](LICENSE).
|
|
143
|
-
|
|
144
|
-
## Links
|
|
145
|
-
|
|
146
|
-
- **Repository:** [github.com/xircons/zero-mock](https://github.com/xircons/zero-mock)
|
|
147
|
-
- **Issues:** [github.com/xircons/zero-mock/issues](https://github.com/xircons/zero-mock/issues)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
(0, vitest_1.describe)('Environment Check', () => {
|
|
5
|
+
(0, vitest_1.it)('should pass a basic truthy test', () => {
|
|
6
|
+
(0, vitest_1.expect)(true).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xirconsss/zero-mock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Zero-config CLI that generates REST APIs from JSON files",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -29,10 +29,14 @@
|
|
|
29
29
|
"build": "tsc",
|
|
30
30
|
"start": "node dist/index.js",
|
|
31
31
|
"dev": "ts-node src/index.ts",
|
|
32
|
-
"
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"test:coverage": "vitest run --coverage",
|
|
34
|
+
"prepublishOnly": "npm run build",
|
|
35
|
+
"prepare": "husky"
|
|
33
36
|
},
|
|
34
37
|
"publishConfig": {
|
|
35
|
-
"access": "public"
|
|
38
|
+
"access": "public",
|
|
39
|
+
"provenance": true
|
|
36
40
|
},
|
|
37
41
|
"repository": {
|
|
38
42
|
"type": "git",
|
|
@@ -48,10 +52,22 @@
|
|
|
48
52
|
"express": "^4.21.2"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
55
|
+
"@commitlint/cli": "^21.0.2",
|
|
56
|
+
"@commitlint/config-conventional": "^21.0.2",
|
|
57
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
58
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
59
|
+
"@semantic-release/git": "^10.0.1",
|
|
60
|
+
"@semantic-release/github": "^12.0.8",
|
|
61
|
+
"@semantic-release/npm": "^12.0.2",
|
|
62
|
+
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
51
63
|
"@types/cors": "^2.8.17",
|
|
52
64
|
"@types/express": "^4.17.21",
|
|
53
65
|
"@types/node": "^22.14.0",
|
|
66
|
+
"@vitest/coverage-v8": "^2.1.9",
|
|
67
|
+
"husky": "^9.1.7",
|
|
68
|
+
"semantic-release": "^25.0.5",
|
|
54
69
|
"ts-node": "^10.9.2",
|
|
55
|
-
"typescript": "^5.8.2"
|
|
70
|
+
"typescript": "^5.8.2",
|
|
71
|
+
"vitest": "^2.1.9"
|
|
56
72
|
}
|
|
57
73
|
}
|