contract-drift-detection 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # Contract Drift Detection
2
+
3
+ Contract Drift Detection is a stateful OpenAPI mock server with proxy-based response validation.
4
+
5
+ It solves the frontend/backend integration gap by combining:
6
+
7
+ 1. **Spec-first mocking** (OpenAPI 3.x)
8
+ 2. **Persistent local API state** (CRUD that actually mutates)
9
+ 3. **Live contract drift detection** against real backend responses
10
+
11
+ If your frontend is blocked by backend readiness, or your backend keeps returning payloads that quietly break UI assumptions, this package is built for that exact pain.
12
+
13
+ ## What problem this resolves
14
+
15
+ Frontend and backend teams usually have to choose between two incomplete options:
16
+
17
+ - Stateless OpenAPI mockers that validate contracts but do not persist mutations.
18
+ - Fake JSON backends that persist state but know nothing about the OpenAPI spec the frontend was actually built against.
19
+
20
+ This project combines both and adds a third layer: drift detection against a live backend.
21
+
22
+ ### Typical outcomes
23
+
24
+ - Frontend can develop end-to-end before backend is complete.
25
+ - Backend drift is detected immediately in development, not in QA/production.
26
+ - Teams converge faster on a stable API contract.
27
+
28
+ ## Features
29
+
30
+ - Generates Fastify routes directly from an OpenAPI 3.x file.
31
+ - Persists state in a local JSON file so POST, PATCH, and DELETE affect future requests.
32
+ - Seeds collections from component schemas using realistic sample data.
33
+ - Supports action-style workflows with `x-mock-state` vendor extensions instead of a separate DSL.
34
+ - Proxies to a live backend with `--drift-check` and validates real responses against the contract.
35
+ - Ships as a CLI and as a reusable library.
36
+
37
+ ## Install and run
38
+
39
+ ### Use directly with `npx` (no install)
40
+
41
+ ```bash
42
+ npx contract-drift-detection@latest serve --spec ./openapi.yaml --port 4010
43
+ ```
44
+
45
+ ### Install globally
46
+
47
+ ```bash
48
+ npm i -g contract-drift-detection
49
+ cdd --help
50
+ ```
51
+
52
+ ### Install in a project
53
+
54
+ ```bash
55
+ npm install
56
+ npm run build
57
+ ```
58
+
59
+ For local development:
60
+
61
+ ```bash
62
+ npm run dev
63
+ ```
64
+
65
+ ## 30-second quick start
66
+
67
+ Run the included example API:
68
+
69
+ ```bash
70
+ npx tsx src/cli.ts serve --spec ./examples/helpdesk.openapi.yaml --port 4010
71
+ ```
72
+
73
+ Create a starter config file in any project:
74
+
75
+ ```bash
76
+ npx tsx src/cli.ts init
77
+ ```
78
+
79
+ Start in drift detection mode against a real backend:
80
+
81
+ ```bash
82
+ npx tsx src/cli.ts serve \
83
+ --spec ./examples/helpdesk.openapi.yaml \
84
+ --port 4010 \
85
+ --drift-check http://localhost:8080
86
+ ```
87
+
88
+ If the backend is flaky and you still want local progress, add `--fallback-to-mock`.
89
+
90
+ Point your frontend API base URL to `http://localhost:4010`.
91
+
92
+ ## CLI
93
+
94
+ ```text
95
+ contract-drift-detection serve --spec <path> [--port 4010] [--host 0.0.0.0] [--db .mock-db.json] [--cors-origin *] [--drift-check <url>] [--fallback-to-mock] [--verbose]
96
+ contract-drift-detection init [--spec openapi.yaml] [--db .mock-db.json] [--port 4010] [--host 0.0.0.0]
97
+ ```
98
+
99
+ `--cors-origin` defaults to `*` for frictionless browser testing.
100
+
101
+ ## Recommended workflows
102
+
103
+ ### A) Frontend-first (mock only)
104
+
105
+ ```bash
106
+ npx cdd serve --spec ./openapi.yaml --port 4010
107
+ ```
108
+
109
+ Use this when backend endpoints are not ready.
110
+
111
+ ### B) Integration mode (proxy + drift detection)
112
+
113
+ ```bash
114
+ npx cdd serve \
115
+ --spec ./openapi.yaml \
116
+ --port 4010 \
117
+ --drift-check http://localhost:8080
118
+ ```
119
+
120
+ Use this when backend is partially/fully available and you want immediate contract mismatch alerts.
121
+
122
+ ## OpenAPI workflow actions
123
+
124
+ Instead of inventing a separate configuration language, action endpoints are defined directly in the OpenAPI document with vendor extensions.
125
+
126
+ ```yaml
127
+ paths:
128
+ /tickets/{id}/resolve:
129
+ post:
130
+ x-mock-state:
131
+ action: update
132
+ target: tickets
133
+ find_by: id
134
+ set:
135
+ status: resolved
136
+ ```
137
+
138
+ Supported actions:
139
+
140
+ - `create`
141
+ - `update`
142
+ - `delete`
143
+ - `replace`
144
+ - `append`
145
+
146
+ Values inside `assign` and `set` can reference request input with `{{body.field}}`, `{{params.id}}`, `{{query.key}}`, and `{{headers.header_name}}`.
147
+
148
+ ## Runtime endpoints
149
+
150
+ - `GET /__health` returns a simple health payload.
151
+ - `GET /__spec` returns the dereferenced OpenAPI document currently in memory.
152
+
153
+ ## Browser/CORS notes
154
+
155
+ - Default CORS is enabled for all origins (`*`) so local frontend apps can call the mock server immediately.
156
+ - For stricter setups, set a specific origin:
157
+
158
+ ```bash
159
+ npx cdd serve --spec ./openapi.yaml --cors-origin http://localhost:5173
160
+ ```
161
+
162
+ - If you see `EADDRINUSE`, choose a different port (`--port 4020`) or stop the process already using that port.
163
+
164
+ ## Example product workflow
165
+
166
+ 1. Start the mock server from the OpenAPI spec before the backend is ready.
167
+ 2. Build the frontend against persistent local state instead of hand-written fixtures.
168
+ 3. Switch on `--drift-check` when backend endpoints start coming online.
169
+ 4. Keep frontend traffic pointed at this tool until contract mismatches are resolved.
170
+
171
+ ## Development
172
+
173
+ ```bash
174
+ npm run typecheck
175
+ npm test
176
+ npm run lint
177
+ npm run build
178
+ ```
179
+
180
+ ## Release verification
181
+
182
+ ```bash
183
+ npm run release:verify
184
+ ```
185
+
186
+ This runs typecheck, tests, lint, build, and `npm pack --dry-run`.
187
+
188
+ ## Real frontend + backend test
189
+
190
+ Use the built-in demo harness to validate this package in a full request chain:
191
+
192
+ 1. `npm run demo:backend`
193
+ 2. `npm run demo:mock:drift`
194
+ 3. `npm run demo:frontend`
195
+
196
+ Then open `http://localhost:5173` and exercise requests through the browser.
197
+
198
+ For the complete flow and drift simulation, see `docs/REAL_STACK_TEST.md`.
199
+
200
+ ## Launch
201
+
202
+ For release automation and go-to-market steps, follow:
203
+
204
+ - `docs/RELEASE.md` for versioning and npm publish
205
+ - `docs/LAUNCH_CHECKLIST.md` for launch channels and success metrics
206
+
207
+ ## Shipping notes
208
+
209
+ - The package exposes the `cdd` and `contract-drift-detection` binaries.
210
+ - `prepublishOnly` runs typecheck, tests, and build to block broken releases.
211
+ - The included example spec under `examples/` is intended for demos, screenshots, and onboarding.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }