@mhmdalimansour/mock-mock 1.0.5 → 1.0.7

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.
Files changed (2) hide show
  1. package/README.md +244 -108
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,194 +1,330 @@
1
1
  # MockMock
2
2
 
3
- > Production-grade CLI tool to generate mock API servers from Confluence documentation
3
+ MockMock is a TypeScript CLI that turns Confluence API documentation into a running mock API server.
4
4
 
5
- MockMock fetches API endpoint definitions from Confluence pages, parses them into a structured schema, and spins up an Express mock server with those endpoints.
5
+ It fetches a Confluence page or local HTML export, parses endpoint definitions into a normalized schema, and starts an Express server that returns generated yet stateful mock responses. It is useful when frontend or integration work needs a realistic API before the real backend is ready.
6
6
 
7
- ## Features
7
+ ## What This Project Does
8
8
 
9
- - 🚀 **Fast Setup**: Generate a mock server in seconds
10
- - 📄 **Confluence Integration**: Fetches API definitions directly from Confluence pages
11
- - 🔒 **Auth Support**: Optional Basic Auth for private Confluence pages
12
- - 🎯 **Type-Safe**: Built with TypeScript for reliability
13
- - 🧩 **Modular Architecture**: Clean separation of concerns
14
- - 🌐 **Express-Powered**: Reliable and extensible mock server
9
+ - Reads API definitions from Confluence pages or exported HTML files
10
+ - Extracts endpoints from both table-based ERD pages and code blocks
11
+ - Starts an Express server with dynamically registered routes
12
+ - Generates realistic response data with `@faker-js/faker`
13
+ - Keeps data in memory so `GET`, `POST`, `PUT`, `PATCH`, and `DELETE` behave like a lightweight API
14
+ - Optionally proxies unmatched requests to a fallback server
15
+ - Lets you simulate latency with startup flags, runtime endpoints, or stdin commands
16
+
17
+ ## How It Works
18
+
19
+ MockMock has four main steps:
20
+
21
+ 1. The CLI in `src/cli.ts` reads command-line options and environment variables.
22
+ 2. The fetcher in `src/fetcher/confluence.ts` loads HTML from:
23
+ - a `file://` path
24
+ - a public Confluence page
25
+ - an authenticated Atlassian Confluence page via REST API
26
+ 3. The parser in `src/parser/erd-parser.ts` converts the HTML into a `MockSchema`.
27
+ 4. The server in `src/server/mock-server.ts` registers routes and serves responses backed by the in-memory `DataStore`.
28
+
29
+ ### Runtime Behavior
30
+
31
+ - `GET` list endpoints return a generated collection if the response template contains an array.
32
+ - `GET` item endpoints return one stored record when a matching item exists.
33
+ - `POST` creates a new in-memory record.
34
+ - `PUT` and `PATCH` update an existing record by id.
35
+ - `DELETE` removes an existing record and returns `204`.
36
+ - If no stored collection exists for an endpoint, MockMock falls back to generating data from the response template on demand.
37
+
38
+ ## Architecture
39
+
40
+ ```text
41
+ mock-mock/
42
+ ├── src/
43
+ │ ├── cli.ts
44
+ │ ├── fetcher/
45
+ │ │ └── confluence.ts
46
+ │ ├── parser/
47
+ │ │ ├── erd-parser.ts
48
+ │ │ └── schema-types.ts
49
+ │ └── server/
50
+ │ ├── data-generator.ts
51
+ │ ├── data-store.ts
52
+ │ └── mock-server.ts
53
+ ├── example-confluence.html
54
+ ├── package.json
55
+ └── tsconfig.json
56
+ ```
57
+
58
+ ### Core Modules
59
+
60
+ - `src/cli.ts`: entrypoint, option parsing, orchestration
61
+ - `src/fetcher/confluence.ts`: Confluence and local-file fetching
62
+ - `src/parser/erd-parser.ts`: HTML parsing and endpoint extraction
63
+ - `src/parser/schema-types.ts`: shared contract between parser and server
64
+ - `src/server/data-generator.ts`: fake response generation
65
+ - `src/server/data-store.ts`: in-memory collections and CRUD-like behavior
66
+ - `src/server/mock-server.ts`: Express app, route registration, fallback proxy, health/config endpoints
15
67
 
16
68
  ## Installation
17
69
 
70
+ ### Install From npm
71
+
72
+ ```bash
73
+ npm install -g @mhmdalimansour/mock-mock
74
+ ```
75
+
76
+ This installs the package published on npm as `@mhmdalimansour/mock-mock` and exposes the CLI command `mock-mock`.
77
+
78
+ ### Local Development
79
+
18
80
  ```bash
19
81
  npm install
82
+ ```
83
+
84
+ ### Build the CLI
85
+
86
+ ```bash
20
87
  npm run build
21
88
  ```
22
89
 
23
- ## Usage
90
+ ### Global Usage
24
91
 
25
- ### Basic Usage
92
+ After installing globally, the CLI command is:
26
93
 
27
94
  ```bash
28
- npm run dev -- --url https://your-confluence.com/pages/123456 --port 4000
95
+ mock-mock --url <confluence-url>
29
96
  ```
30
97
 
31
- Or using the built CLI:
98
+ ## Quick Start
99
+
100
+ ### 1. Run Against the Example HTML
32
101
 
33
102
  ```bash
34
- mockgen --url <confluence-url> --port 4000
103
+ npm run dev -- --url file://C:/absolute/path/to/example-confluence.html --port 4000
35
104
  ```
36
105
 
37
- ### Options
38
-
39
- - `--url, -u` **(required)**: Confluence page URL containing API definitions
40
- - `--port, -p` (optional): Port for the mock server (default: 4000)
106
+ On macOS or Linux:
41
107
 
42
- ### Authentication
108
+ ```bash
109
+ npm run dev -- --url file:///absolute/path/to/example-confluence.html --port 4000
110
+ ```
43
111
 
44
- For private Confluence pages, set these environment variables:
112
+ ### 2. Run Against a Confluence Page
45
113
 
46
114
  ```bash
47
- export CONFLUENCE_EMAIL=your-email@example.com
48
- export CONFLUENCE_API_TOKEN=your-api-token
115
+ npm run dev -- --url https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title --port 4000
49
116
  ```
50
117
 
51
- Then run the CLI as normal.
118
+ ### 3. Test the Server
52
119
 
53
- ## API Definition Format
120
+ ```bash
121
+ curl http://localhost:4000/health
122
+ curl http://localhost:4000/api/users
123
+ curl -X POST http://localhost:4000/api/users -H "Content-Type: application/json" -d "{\"name\":\"John\"}"
124
+ ```
54
125
 
55
- MockMock expects code blocks in your Confluence page with this format:
126
+ ## CLI Usage
56
127
 
128
+ ### Development Mode
129
+
130
+ ```bash
131
+ npm run dev -- --url <url>
57
132
  ```
58
- POST /api/categories
59
133
 
60
- Request:
61
- {
62
- "name": "string"
63
- }
134
+ ### Run the Built CLI
64
135
 
65
- Response:
66
- {
67
- "id": 1,
68
- "name": "string"
69
- }
136
+ ```bash
137
+ npm start -- --url <url>
70
138
  ```
71
139
 
72
- ### Supported Fields
140
+ ### CLI Options
73
141
 
74
- - **HTTP Method**: GET, POST, PUT, DELETE
75
- - **Path**: The endpoint path (e.g., `/api/users`)
76
- - **Request** (optional): JSON body schema
77
- - **Response** (required): JSON response body
78
- - **Status** (optional): HTTP status code (default: 200)
142
+ | Option | Description | Default |
143
+ | --- | --- | --- |
144
+ | `-u, --url <url>` | Confluence page URL or `file://` HTML path | required |
145
+ | `-p, --port <port>` | Port for the mock server | `4000` |
146
+ | `-f, --fallback <url>` | Proxy target for unmatched requests | none |
147
+ | `--delay <ms>` | Response delay in milliseconds | `0` |
148
+ | `-e, --email <email>` | Confluence email, overrides env var | none |
149
+ | `-t, --token <token>` | Confluence API token, overrides env var | none |
150
+ | `-d, --debug` | Save fetched HTML to `debug.html` and print parsed schema | `false` |
79
151
 
80
- ### Example
152
+ ### Example Commands
81
153
 
154
+ ```bash
155
+ npm run dev -- --url file:///tmp/exported-page.html --port 4000
156
+ npm run dev -- --url https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title --delay 500
157
+ npm run dev -- --url https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title --fallback https://api.dev.example.com
82
158
  ```
83
- GET /api/users
84
159
 
85
- Response:
86
- {
87
- "users": [
88
- {
89
- "id": 1,
90
- "name": "John Doe"
91
- }
92
- ]
93
- }
160
+ ## Authentication
161
+
162
+ Private Confluence pages usually require credentials.
163
+
164
+ Create a `.env` file or export the following variables:
165
+
166
+ ```bash
167
+ CONFLUENCE_EMAIL=your-email@example.com
168
+ CONFLUENCE_API_TOKEN=your-api-token
94
169
  ```
95
170
 
171
+ You can also pass them directly:
172
+
173
+ ```bash
174
+ npm run dev -- --url <url> --email your-email@example.com --token your-api-token
96
175
  ```
97
- POST /api/users
176
+
177
+ ### Fetching Rules
178
+
179
+ - `file://...` reads a local HTML file
180
+ - Atlassian Cloud URLs with credentials use the Confluence REST API
181
+ - Other URLs are fetched as raw HTML
182
+ - If Confluence returns a login page instead of the document, MockMock exits with an authentication error
183
+
184
+ For more setup details, see `AUTH_SETUP.md`.
185
+
186
+ ## Supported Input Formats
187
+
188
+ MockMock currently supports two Confluence patterns:
189
+
190
+ - table-based endpoint documentation
191
+ - code blocks containing endpoint definitions
192
+
193
+ ### Code Block Format
194
+
195
+ ```text
196
+ POST /api/categories
98
197
 
99
198
  Request:
100
199
  {
101
- "name": "string",
102
- "email": "string"
200
+ "name": "string"
103
201
  }
104
202
 
105
203
  Response:
106
204
  {
107
205
  "id": 1,
108
- "name": "string",
109
- "email": "string"
206
+ "name": "string"
110
207
  }
111
208
 
112
209
  Status: 201
113
210
  ```
114
211
 
115
- ## Architecture
212
+ ### Supported Methods
116
213
 
117
- ```
118
- mock-mock/
119
- src/
120
- cli.ts # CLI entry point
121
- fetcher/
122
- confluence.ts # Confluence page fetcher
123
- parser/
124
- erd-parser.ts # Endpoint parser
125
- schema-types.ts # Schema definitions
126
- server/
127
- mock-server.ts # Express mock server
128
- package.json
129
- tsconfig.json
130
- ```
214
+ - Table-based parsing supports `GET`, `POST`, `PUT`, `PATCH`, and `DELETE`.
215
+ - Plain code-block parsing currently recognizes `GET`, `POST`, `PUT`, and `DELETE`.
216
+ - The server runtime supports `PATCH` once an endpoint is present in the parsed schema.
131
217
 
132
- ### Layer Independence
218
+ ### Notes About Response Templates
133
219
 
134
- Each layer is independent and communicates through the `MockSchema` interface:
220
+ - Response objects are used as templates for fake data generation.
221
+ - Response arrays are expanded into collections with about 15 to 30 records.
222
+ - Wrapped collections such as `{ "errors": false, "data": [...] }` are preserved.
223
+ - Path parameters in documentation can use `{id}` and are converted to Express-style params internally.
224
+ - Plain code-block parsing is best with object-shaped JSON responses; table-based ERD pages are more flexible.
135
225
 
136
- 1. **Fetcher**: Retrieves raw HTML from Confluence
137
- 2. **Parser**: Extracts and normalizes endpoints into `MockSchema`
138
- 3. **Server**: Consumes `MockSchema` and creates Express routes
226
+ ## Server Endpoints
139
227
 
140
- ## Development
228
+ In addition to parsed API routes, MockMock exposes a few built-in endpoints:
141
229
 
142
- ### Run in Development Mode
230
+ | Endpoint | Method | Purpose |
231
+ | --- | --- | --- |
232
+ | `/health` | `GET` | Shows registered endpoints and current configuration |
233
+ | `/_config/delay` | `GET` | Returns current response delay |
234
+ | `/_config/delay` | `PUT` | Updates response delay at runtime |
143
235
 
144
- ```bash
145
- npm run dev -- --url <confluence-url>
236
+ You can also change delay from stdin while the server is running:
237
+
238
+ ```text
239
+ delay 500
240
+ delay
146
241
  ```
147
242
 
148
- ### Build
243
+ ## Developer Guide
244
+
245
+ ### Scripts
149
246
 
150
247
  ```bash
248
+ npm install
151
249
  npm run build
250
+ npm run dev -- --url file://C:/absolute/path/to/example-confluence.html
251
+ npm start -- --url file://C:/absolute/path/to/example-confluence.html
152
252
  ```
153
253
 
154
- ### Run Built Version
254
+ ### Tech Stack
155
255
 
156
- ```bash
157
- npm start -- --url <confluence-url>
158
- ```
256
+ - TypeScript
257
+ - Commander
258
+ - Axios
259
+ - Cheerio
260
+ - Express
261
+ - Faker
159
262
 
160
- ## Error Handling
263
+ ### Development Flow
161
264
 
162
- MockMock gracefully handles:
265
+ 1. Update fetching logic in `src/fetcher/` if the Confluence source format changes.
266
+ 2. Update parsing logic in `src/parser/` when new ERD/table/code patterns need to be supported.
267
+ 3. Keep `MockEndpoint` and `MockSchema` in `src/parser/schema-types.ts` as the contract between layers.
268
+ 4. Update server behavior in `src/server/` when changing how routes, state, or fake responses work.
269
+ 5. Run `npm run build` to verify TypeScript compilation.
163
270
 
164
- - Invalid Confluence URLs
165
- - ❌ Network errors
166
- - ❌ Malformed JSON in code blocks
167
- - ❌ Missing endpoints
168
- - ❌ Invalid port numbers
271
+ ### Important Design Choices
169
272
 
170
- ## Example Output
273
+ - The parser and server communicate through a simple schema contract.
274
+ - The server is stateful only in memory; restarting the process resets all data.
275
+ - Fake data generation is template-driven, so response structure depends on the documentation input.
276
+ - Collections are derived from `GET` endpoints that expose arrays in the response template.
171
277
 
172
- ```
173
- 🚀 MockMock CLI
278
+ ### Project Structure for Contributors
279
+
280
+ - `example-confluence.html`: local test fixture
281
+ - `README.md`: main project documentation
282
+ - `AUTH_SETUP.md`: authentication help
283
+ - `QUICKSTART.md`: short setup walkthrough
284
+ - `CONTRIBUTING.md`: contributor notes
174
285
 
175
- 📄 Fetching Confluence page: https://confluence.example.com/123456
176
- ✅ Page fetched successfully
286
+ ## Troubleshooting
287
+
288
+ ### No endpoints found
289
+
290
+ - Check that the Confluence page contains supported tables or code blocks.
291
+ - Use `--debug` to save `debug.html` and inspect the fetched HTML.
292
+
293
+ ### Authentication failed
294
+
295
+ - Verify `CONFLUENCE_EMAIL` and `CONFLUENCE_API_TOKEN`.
296
+ - Confirm the page is accessible to that account.
297
+ - For Atlassian Cloud, make sure the URL contains a valid `/pages/<id>/` segment.
298
+
299
+ ### Unexpected response shape
300
+
301
+ - Check the response JSON in Confluence.
302
+ - Make sure wrapped arrays or nested objects are valid JSON.
303
+ - Remember that fake data is inferred from field names and primitive types.
304
+
305
+ ### Changes disappear after restart
306
+
307
+ This is expected. Data is stored only in memory.
308
+
309
+ ## Example Output
177
310
 
178
- 🔍 Parsing API endpoints...
179
- Found 3 endpoint(s)
311
+ ```text
312
+ MockMock CLI
180
313
 
181
- 📡 Registering endpoints:
314
+ Fetching Confluence page: https://confluence.example.com/pages/123456
315
+ Page fetched successfully
182
316
 
183
- GET /api/users
184
- POST /api/users
185
- DELETE /api/users/1
317
+ Parsing API endpoints...
318
+ Found 3 endpoint(s)
186
319
 
187
- Mock server started successfully!
320
+ Registering endpoints:
321
+ GET /api/users
322
+ POST /api/users
323
+ DELETE /api/users/:id
188
324
 
189
- 🌐 Base URL: http://localhost:4000
190
- 📊 Total endpoints: 3
191
- 💚 Health check: http://localhost:4000/health
325
+ Mock server started successfully
326
+ Base URL: http://localhost:4000
327
+ Health check: http://localhost:4000/health
192
328
  ```
193
329
 
194
330
  ## License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mhmdalimansour/mock-mock",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Production-grade CLI tool to generate mock servers from Confluence API documentation",
5
5
  "main": "dist/cli.js",
6
6
  "bin": {