@apicircle/mock-server-core 1.0.1 → 1.0.2

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 CHANGED
@@ -1,110 +1,110 @@
1
- API Circle Studio License
2
- Custom Source-Available License, v1.0
3
-
4
- Copyright (c) 2026 Deva Prakash ("Licensor")
5
-
6
- The source code in this repository ("the Software") is made available for
7
- the purposes of transparency, security review, contribution, and personal
8
- evaluation. This is NOT an open-source license as defined by the Open
9
- Source Initiative.
10
-
11
- 0. Definitions
12
-
13
- "Commercial Use" means any use of the Software that is intended for
14
- or directed toward commercial advantage or monetary compensation,
15
- whether direct or indirect, including without limitation:
16
-
17
- (a) using the Software to build, test, develop, deploy, or operate
18
- any product, service, application, or system that is sold,
19
- licensed, hosted, distributed, or otherwise made available to
20
- any third party for a fee or other consideration;
21
- (b) using the Software in the course of paid employment, paid
22
- consulting, paid contracting, freelance work, or any other
23
- revenue-generating activity, regardless of whether the Software
24
- itself is sold or transferred;
25
- (c) using the Software internally within a for-profit organization
26
- beyond the Evaluation Period defined below;
27
- (d) integrating the Software, or any portion of it, into any tool,
28
- pipeline, automation, or workflow that supports the commercial
29
- operations of any business;
30
- (e) using the Software to provide a hosted, managed, or embedded
31
- service of any kind to a third party, whether free or paid.
32
-
33
- "Non-Commercial Use" means any use that is not Commercial Use,
34
- including personal hobby projects, individual learning, academic
35
- research, classroom instruction, and good-faith contribution to
36
- this repository.
37
-
38
- "Evaluation Period" means a single, continuous period of up to
39
- thirty (30) days during which a for-profit organization may
40
- internally evaluate the Software at no charge. After the Evaluation
41
- Period expires, any further use of the Software by that organization
42
- constitutes Commercial Use and requires a separate commercial
43
- license from the Licensor.
44
-
45
- 1. Permitted Use
46
-
47
- You may, without charge:
48
-
49
- (a) view, read, and study the source code of the Software;
50
- (b) run the Software, in source or compiled form, for your own
51
- Non-Commercial Use, or for Commercial Use solely within the
52
- Evaluation Period;
53
- (c) submit improvements to the Software back to this repository via
54
- pull request, subject to Section 3 (Contributions).
55
-
56
- 2. Prohibited Use
57
-
58
- Without prior written permission from the Licensor, you may NOT:
59
-
60
- (a) make any Commercial Use of the Software, in whole or in part,
61
- except during the Evaluation Period as defined in Section 0;
62
- (b) redistribute the Software, in source or compiled form, whether
63
- modified or unmodified, to any third party;
64
- (c) sublicense, sell, rent, lease, or otherwise transfer the
65
- Software or any rights granted herein;
66
- (d) remove, obscure, or alter any copyright, trademark, attribution,
67
- or license notice contained in the Software;
68
- (e) use the names "API Circle", "API Circle Studio", or any related
69
- logos or trademarks, except as required for accurate attribution.
70
-
71
- 3. Contributions
72
-
73
- By submitting any contribution (including but not limited to code,
74
- documentation, or assets) to this repository, you grant the Licensor
75
- a perpetual, worldwide, irrevocable, royalty-free, sublicensable
76
- license to use, modify, distribute, and relicense your contribution
77
- under any terms, including the terms of this license or any future
78
- version thereof.
79
-
80
- 4. No Trademark License
81
-
82
- This license does not grant permission to use the trade names,
83
- trademarks, service marks, or product names of the Licensor, except
84
- as required for reasonable and customary use in describing the
85
- origin of the Software.
86
-
87
- 5. Termination
88
-
89
- The rights granted in Section 1 terminate automatically if you
90
- breach any term of this license. Upon termination, you must cease
91
- all use of the Software and destroy all copies in your possession.
92
-
93
- 6. Disclaimer of Warranty
94
-
95
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
96
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
97
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
98
- NON-INFRINGEMENT.
99
-
100
- 7. Limitation of Liability
101
-
102
- IN NO EVENT SHALL THE LICENSOR BE LIABLE FOR ANY CLAIM, DAMAGES, OR
103
- OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR
104
- OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
105
- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
106
-
107
- 8. Commercial Licensing
108
-
109
- For commercial licensing, redistribution, or any use not permitted
110
- under Section 1, contact: apicircle365@gmail.com
1
+ API Circle Studio License
2
+ Custom Source-Available License, v1.0
3
+
4
+ Copyright (c) 2026 Deva Prakash ("Licensor")
5
+
6
+ The source code in this repository ("the Software") is made available for
7
+ the purposes of transparency, security review, contribution, and personal
8
+ evaluation. This is NOT an open-source license as defined by the Open
9
+ Source Initiative.
10
+
11
+ 0. Definitions
12
+
13
+ "Commercial Use" means any use of the Software that is intended for
14
+ or directed toward commercial advantage or monetary compensation,
15
+ whether direct or indirect, including without limitation:
16
+
17
+ (a) using the Software to build, test, develop, deploy, or operate
18
+ any product, service, application, or system that is sold,
19
+ licensed, hosted, distributed, or otherwise made available to
20
+ any third party for a fee or other consideration;
21
+ (b) using the Software in the course of paid employment, paid
22
+ consulting, paid contracting, freelance work, or any other
23
+ revenue-generating activity, regardless of whether the Software
24
+ itself is sold or transferred;
25
+ (c) using the Software internally within a for-profit organization
26
+ beyond the Evaluation Period defined below;
27
+ (d) integrating the Software, or any portion of it, into any tool,
28
+ pipeline, automation, or workflow that supports the commercial
29
+ operations of any business;
30
+ (e) using the Software to provide a hosted, managed, or embedded
31
+ service of any kind to a third party, whether free or paid.
32
+
33
+ "Non-Commercial Use" means any use that is not Commercial Use,
34
+ including personal hobby projects, individual learning, academic
35
+ research, classroom instruction, and good-faith contribution to
36
+ this repository.
37
+
38
+ "Evaluation Period" means a single, continuous period of up to
39
+ thirty (30) days during which a for-profit organization may
40
+ internally evaluate the Software at no charge. After the Evaluation
41
+ Period expires, any further use of the Software by that organization
42
+ constitutes Commercial Use and requires a separate commercial
43
+ license from the Licensor.
44
+
45
+ 1. Permitted Use
46
+
47
+ You may, without charge:
48
+
49
+ (a) view, read, and study the source code of the Software;
50
+ (b) run the Software, in source or compiled form, for your own
51
+ Non-Commercial Use, or for Commercial Use solely within the
52
+ Evaluation Period;
53
+ (c) submit improvements to the Software back to this repository via
54
+ pull request, subject to Section 3 (Contributions).
55
+
56
+ 2. Prohibited Use
57
+
58
+ Without prior written permission from the Licensor, you may NOT:
59
+
60
+ (a) make any Commercial Use of the Software, in whole or in part,
61
+ except during the Evaluation Period as defined in Section 0;
62
+ (b) redistribute the Software, in source or compiled form, whether
63
+ modified or unmodified, to any third party;
64
+ (c) sublicense, sell, rent, lease, or otherwise transfer the
65
+ Software or any rights granted herein;
66
+ (d) remove, obscure, or alter any copyright, trademark, attribution,
67
+ or license notice contained in the Software;
68
+ (e) use the names "API Circle", "API Circle Studio", or any related
69
+ logos or trademarks, except as required for accurate attribution.
70
+
71
+ 3. Contributions
72
+
73
+ By submitting any contribution (including but not limited to code,
74
+ documentation, or assets) to this repository, you grant the Licensor
75
+ a perpetual, worldwide, irrevocable, royalty-free, sublicensable
76
+ license to use, modify, distribute, and relicense your contribution
77
+ under any terms, including the terms of this license or any future
78
+ version thereof.
79
+
80
+ 4. No Trademark License
81
+
82
+ This license does not grant permission to use the trade names,
83
+ trademarks, service marks, or product names of the Licensor, except
84
+ as required for reasonable and customary use in describing the
85
+ origin of the Software.
86
+
87
+ 5. Termination
88
+
89
+ The rights granted in Section 1 terminate automatically if you
90
+ breach any term of this license. Upon termination, you must cease
91
+ all use of the Software and destroy all copies in your possession.
92
+
93
+ 6. Disclaimer of Warranty
94
+
95
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
96
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
97
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
98
+ NON-INFRINGEMENT.
99
+
100
+ 7. Limitation of Liability
101
+
102
+ IN NO EVENT SHALL THE LICENSOR BE LIABLE FOR ANY CLAIM, DAMAGES, OR
103
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR
104
+ OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
105
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
106
+
107
+ 8. Commercial Licensing
108
+
109
+ For commercial licensing, redistribution, or any use not permitted
110
+ under Section 1, contact: apicircle365@gmail.com
package/README.md CHANGED
@@ -4,25 +4,97 @@
4
4
 
5
5
  <h1 align="center">@apicircle/mock-server-core</h1>
6
6
 
7
- Hono-based mock-server engine for [API Circle Studio](https://github.com/apicircle/studio). Parses OpenAPI / Swagger / Postman / Insomnia files into a `MockEndpoint[]`, then serves them on Node, Bun, or any edge runtime that runs Hono.
7
+ <p align="center">
8
+ <strong>Turn any OpenAPI, Postman, Insomnia, or Swagger file into a real, running HTTP server — in three lines of code.</strong><br />
9
+ Built on Hono. Runs on Node, Bun, Deno, and every edge runtime Hono supports.
10
+ </p>
11
+
12
+ <p align="center">
13
+ <a href="https://www.npmjs.com/package/@apicircle/mock-server-core"><img src="https://img.shields.io/npm/v/@apicircle/mock-server-core?color=cb3837&logo=npm" alt="npm version" /></a>
14
+ <img src="https://img.shields.io/badge/parses-OpenAPI%203%20%C2%B7%20Swagger%202%20%C2%B7%20Postman%20v2.x%20%C2%B7%20Insomnia%20v4-blue" alt="Parsers" />
15
+ <img src="https://img.shields.io/badge/runtime-Hono-orange" alt="Hono" />
16
+ <img src="https://img.shields.io/badge/runtimes-Node%20%C2%B7%20Bun%20%C2%B7%20Edge-success" alt="Runtimes" />
17
+ <img src="https://img.shields.io/badge/node-%E2%89%A5%2020-brightgreen" alt="Node ≥ 20" />
18
+ </p>
19
+
20
+ ---
21
+
22
+ ## Why a developer would reach for this
23
+
24
+ You have a spec, and you need a server that responds **right now** — before
25
+ the backend team merges anything, before staging is provisioned, before a
26
+ mobile build can hit it on a flight.
27
+
28
+ `@apicircle/mock-server-core` is the engine that ships with API Circle Studio,
29
+ extracted as a standalone npm package. Point it at your spec; get a real HTTP
30
+ server. No config DSL, no YAML rules file, no Docker image.
31
+
32
+ ```ts
33
+ import { startMockServer, parseSourceToEndpoints } from '@apicircle/mock-server-core';
34
+
35
+ const { endpoints } = await parseSourceToEndpoints({
36
+ kind: 'openapi',
37
+ spec: rawYamlOrJson,
38
+ format: 'yaml',
39
+ });
40
+
41
+ const mock = await startMockServer({ name: 'Petstore', endpoints, defaultPort: 4040 });
42
+ // → POST http://localhost:4040/pets, GET http://localhost:4040/pets/{id}, ...
43
+ ```
44
+
45
+ That's the whole API surface for the simple case. Everything below is what
46
+ makes the simple case scale.
47
+
48
+ ## What makes it different
49
+
50
+ - **Four importers, one output shape.** OpenAPI 3.x, Swagger 2.0, Postman
51
+ v2/v2.1, and Insomnia v4 all reduce to the same typed `MockEndpoint[]`.
52
+ Swap your team's spec format and nothing downstream changes.
53
+ - **Built on Hono.** A 14 kB router with native support for Node, Bun, Deno,
54
+ Cloudflare Workers, Vercel Edge, Lambda, and more. Run mocks in CI, in a
55
+ desktop app, on a developer's laptop, or behind a tunnel — same code.
56
+ - **Per-endpoint overrides.** Inject error responses, custom headers, delays,
57
+ or bespoke bodies on a single endpoint without touching the source spec.
58
+ Perfect for testing error paths and slow networks.
59
+ - **Real example bodies.** Postman? Picks the first saved response. OpenAPI?
60
+ Uses the `example` / `examples` / `default` from the spec. No `"string"`
61
+ / `0` placeholder data — what's in the spec is what you get.
62
+ - **CORS, on by default.** Browser-friendly out of the box, configurable when
63
+ you need to lock it down.
64
+ - **Strict $ref resolution.** OpenAPI parsing flows through
65
+ [`@apidevtools/swagger-parser`](https://www.npmjs.com/package/@apidevtools/swagger-parser)
66
+ for compliant dereferencing — `$ref` chains, recursive schemas, file refs.
8
67
 
9
68
  ## Install
10
69
 
11
70
  ```bash
12
71
  npm install @apicircle/mock-server-core
72
+ # pnpm add @apicircle/mock-server-core
13
73
  ```
14
74
 
75
+ Pulls in Hono and the swagger parser; no native modules.
76
+
15
77
  ## Quickstart
16
78
 
79
+ ### 1. Parse a spec
80
+
17
81
  ```ts
18
- import { startMockServer, parseSourceToEndpoints } from '@apicircle/mock-server-core';
82
+ import { parseSourceToEndpoints } from '@apicircle/mock-server-core';
19
83
 
20
84
  const { endpoints, warnings } = await parseSourceToEndpoints({
21
- kind: 'openapi',
85
+ kind: 'openapi', // 'openapi' | 'postman' | 'insomnia' | 'manual'
22
86
  spec: rawYamlOrJson,
23
- format: 'yaml',
87
+ format: 'yaml', // 'yaml' | 'json'
24
88
  });
25
89
 
90
+ warnings.forEach((w) => console.warn(w));
91
+ ```
92
+
93
+ ### 2. Start the server
94
+
95
+ ```ts
96
+ import { startMockServer } from '@apicircle/mock-server-core';
97
+
26
98
  const handle = await startMockServer({
27
99
  id: 'm1',
28
100
  name: 'Petstore',
@@ -35,20 +107,79 @@ const handle = await startMockServer({
35
107
  updatedAt: new Date().toISOString(),
36
108
  });
37
109
 
38
- // later
110
+ console.log(handle.url); // http://localhost:4040
111
+ ```
112
+
113
+ ### 3. Override one endpoint
114
+
115
+ ```ts
116
+ await startMockServer({
117
+ // ...
118
+ overrides: {
119
+ [endpoints[0].id]: {
120
+ status: 503,
121
+ headers: { 'Retry-After': '30' },
122
+ delayMs: 1200,
123
+ },
124
+ },
125
+ });
126
+ ```
127
+
128
+ Per-endpoint overrides are how you simulate flaky backends, rate-limiters,
129
+ slow regions, and "what does the app do when this call returns 500?".
130
+
131
+ ### 4. Stop it
132
+
133
+ ```ts
39
134
  await handle.close();
40
135
  ```
41
136
 
42
- ## What it parses
137
+ ## Format support matrix
138
+
139
+ | Format | Versions | What we pull out |
140
+ | -------- | ---------- | ------------------------------------------------------------ |
141
+ | OpenAPI | 3.0, 3.1 | Path + method, parameters, request bodies, example responses |
142
+ | Swagger | 2.0 | Same, dereferenced via `@apidevtools/swagger-parser` |
143
+ | Postman | v2.0, v2.1 | Recursive item walks; first saved response → fallback 200 |
144
+ | Insomnia | v4 export | Filters `_type: 'request'` |
145
+ | Manual | n/a | Hand-rolled `MockEndpoint[]` you build yourself |
146
+
147
+ ## Where it runs
148
+
149
+ Because the engine is just a Hono app builder, anywhere Hono runs, this runs:
150
+
151
+ - **Node** ≥ 20 — the common case, used by the API Circle Studio desktop app.
152
+ - **Bun** — single-binary mocks in CI.
153
+ - **Cloudflare Workers / Vercel Edge / Deno Deploy** — public, always-on mocks
154
+ served from the edge.
155
+ - **Lambda** — pay-per-request mock servers for short-lived environments.
156
+
157
+ ## Use cases
158
+
159
+ - **Unblock the frontend** while the backend is still being designed.
160
+ - **Spin up CI mocks** for integration tests without a docker-compose file.
161
+ - **Demo a public API** to prospects before the rate-limited beta is ready.
162
+ - **Reproduce production bugs** by overriding one endpoint with the exact
163
+ response that broke the client.
164
+ - **Power AI agents** — let an LLM drive a real HTTP server it can probe.
165
+
166
+ ## Where it fits
167
+
168
+ ```
169
+ @apicircle/shared (types + utilities)
170
+ └── @apicircle/mock-server-core ◀── you are here
171
+ └── @apicircle/cli (`apicircle mock ./spec.yaml`)
172
+ └── @apicircle/mcp-server (mock.start / mock.stop tools)
173
+ ```
43
174
 
44
- - **OpenAPI 3.x and Swagger 2.0** JSON or YAML, including `$ref` deref via `@apidevtools/swagger-parser`.
45
- - **Postman v2 / v2.1 collections** recursive item walks, picks first saved response, falls back to a 200 default.
46
- - **Insomnia v4 exports** — filters resources of `_type: 'request'`.
47
- - **Manual** — pre-built `MockEndpoint[]` you supply yourself.
175
+ The exact same factory powers the desktop `MockManager`, the
176
+ `apicircle mock` CLI command, and the MCP `mock.start` tool. One engine,
177
+ three runtimes.
48
178
 
49
- ## Per-endpoint overrides
179
+ ## Learn more
50
180
 
51
- `MockServer.overrides[endpointId]` lets you change status, headers, body, or delay for a single endpoint without touching the source spec — perfect for testing error paths.
181
+ - **Full mock server guide**: <https://github.com/apicircle/studio/blob/main/docs/mock-server.md>
182
+ - **Hono docs**: <https://hono.dev>
52
183
 
53
184
  ## License
54
185
 
package/dist/index.cjs CHANGED
@@ -1002,6 +1002,7 @@ async function isPortFree(port) {
1002
1002
  }
1003
1003
 
1004
1004
  // src/runtime/nodeAdapter.ts
1005
+ var CLOSE_TIMEOUT_MS = 3e3;
1005
1006
  async function serveOnNode(app, opts = {}) {
1006
1007
  const port = opts.port && opts.port > 0 ? opts.port : await getFreePort();
1007
1008
  const host = opts.host ?? "127.0.0.1";
@@ -1023,12 +1024,24 @@ async function serveOnNode(app, opts = {}) {
1023
1024
  });
1024
1025
  return {
1025
1026
  port,
1026
- close: () => new Promise((resolve, reject) => {
1027
+ close: () => new Promise((resolve) => {
1027
1028
  if (!server) return resolve();
1028
- server.close((err) => {
1029
- if (err) reject(err);
1030
- else resolve();
1031
- });
1029
+ const s = server;
1030
+ const withCloseHelpers = s;
1031
+ try {
1032
+ withCloseHelpers.closeIdleConnections?.();
1033
+ withCloseHelpers.closeAllConnections?.();
1034
+ } catch {
1035
+ }
1036
+ let settled = false;
1037
+ const settle = () => {
1038
+ if (settled) return;
1039
+ settled = true;
1040
+ clearTimeout(timer);
1041
+ resolve();
1042
+ };
1043
+ const timer = setTimeout(settle, CLOSE_TIMEOUT_MS);
1044
+ s.close(() => settle());
1032
1045
  })
1033
1046
  };
1034
1047
  }