@apifn/mock 0.0.2 → 0.0.4

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 +151 -0
  2. package/package.json +6 -5
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # @apifn/mock
2
+
3
+ Mock server for ApiFn. Spin up a framework-free HTTP server from any OpenAPI document that returns realistic responses, optionally validating incoming requests against the spec.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @apifn/mock
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Instant mock server** — Serve every operation in an OpenAPI spec with no framework
14
+ - **Three response modes** — Deterministic from schema, from examples, or randomized
15
+ - **Request validation** — Optionally validate query/path params and JSON bodies against schemas
16
+ - **Latency simulation** — Add a fixed response delay
17
+ - **CORS** — Built-in CORS handling with sensible defaults or custom options
18
+ - **Safety** — Configurable max request body size (default 10 MiB)
19
+
20
+ ---
21
+
22
+ ## createMockServer
23
+
24
+ ```typescript
25
+ import { createMockServer } from "@apifn/mock";
26
+ import { parseOpenAPI } from "@apifn/core";
27
+ import { readFile } from "node:fs/promises";
28
+
29
+ const spec = parseOpenAPI(await readFile("./openapi.yml", "utf8"));
30
+
31
+ const mock = createMockServer({
32
+ spec,
33
+ port: 4010,
34
+ responseMode: "schema", // "schema" | "examples" | "random"
35
+ validateRequests: true,
36
+ delay: 0,
37
+ cors: true,
38
+ });
39
+
40
+ await mock.start();
41
+ console.log(`Mock server listening on http://localhost:${mock.port}`);
42
+
43
+ // later
44
+ await mock.stop();
45
+ ```
46
+
47
+ ### MockServerOptions
48
+
49
+ | Option | Type | Default | Description |
50
+ |--------|------|---------|-------------|
51
+ | `spec` | `OpenAPIDocument` | — | The OpenAPI document to mock (required) |
52
+ | `port` | `number` | `4010` | Port to bind (use `0` for a random free port) |
53
+ | `responseMode` | `"schema" \| "examples" \| "random"` | `"schema"` | How response bodies are generated |
54
+ | `validateRequests` | `boolean` | `false` | Validate incoming params and JSON bodies |
55
+ | `delay` | `number` | `0` | Response delay in ms (simulate latency) |
56
+ | `maxRequestBodyBytes` | `number` | `10485760` | Max accepted request body size (10 MiB) |
57
+ | `cors` | `boolean \| CorsOptions` | — | Enable/configure CORS |
58
+
59
+ ### MockServer
60
+
61
+ ```typescript
62
+ interface MockServer {
63
+ start(): Promise<void>;
64
+ stop(): Promise<void>;
65
+ server: http.Server; // underlying Node server
66
+ port: number; // the actually-bound port
67
+ }
68
+ ```
69
+
70
+ Unmatched routes return `404 { error: "Not Found", path }`. `OPTIONS` preflight requests return `204`.
71
+
72
+ ---
73
+
74
+ ## Response Generation
75
+
76
+ Use the generators directly when you need a mock payload without a server.
77
+
78
+ ### generateResponse
79
+
80
+ Pick the first 2xx response for an operation and produce a body for the given mode.
81
+
82
+ ```typescript
83
+ import { generateResponse } from "@apifn/mock";
84
+
85
+ const { statusCode, body } = generateResponse(operation, "schema");
86
+ ```
87
+
88
+ ### generateFromSchema
89
+
90
+ Deterministic values from a JSON schema — strings become `"string"` (format-aware for `date-time`, `email`, `uri`, `uuid`, …), numbers `0`, booleans `true`, arrays a single generated item, objects recurse over their properties.
91
+
92
+ ```typescript
93
+ import { generateFromSchema } from "@apifn/mock";
94
+
95
+ generateFromSchema({ type: "object", properties: { id: { type: "string", format: "uuid" } } });
96
+ // { id: "00000000-0000-0000-0000-000000000000" }
97
+ ```
98
+
99
+ ### generateRandom
100
+
101
+ Like `generateFromSchema`, but produces randomized primitive values. It respects `type`, string `enum`, and a small set of formats; it does not currently enforce constraints such as `minimum`, `maximum`, `minLength`, `maxLength`, `pattern`, or array bounds.
102
+
103
+ ```typescript
104
+ import { generateRandom } from "@apifn/mock";
105
+
106
+ const value = generateRandom(schema);
107
+ ```
108
+
109
+ ---
110
+
111
+ ## Request Validation
112
+
113
+ Self-contained JSON-schema validation (no external dependencies).
114
+
115
+ ### validateRequestBody
116
+
117
+ ```typescript
118
+ import { validateRequestBody } from "@apifn/mock";
119
+
120
+ const result = validateRequestBody(body, operation);
121
+ // { valid: boolean, errors: ValidationError[] } where ValidationError = { path, message }
122
+ ```
123
+
124
+ Reads `operation.requestBody.content["application/json"].schema`; if the body is required but missing, returns an error.
125
+
126
+ ### validateParameters
127
+
128
+ ```typescript
129
+ import { validateParameters } from "@apifn/mock";
130
+
131
+ const result = validateParameters(queryParams, pathParams, operation);
132
+ ```
133
+
134
+ Checks `operation.parameters` for missing required query/path params and basic numeric coercion.
135
+
136
+ ---
137
+
138
+ ## Exports
139
+
140
+ ```typescript
141
+ export { createMockServer }
142
+ export type { MockServer }
143
+ export { generateFromSchema, generateRandom, generateResponse }
144
+ export type { ResponseMode }
145
+ export { validateRequestBody, validateParameters }
146
+ export type { ValidationError, ValidationResult }
147
+ ```
148
+
149
+ ## License
150
+
151
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apifn/mock",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Mock server for ApiFn",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -20,13 +20,13 @@
20
20
  "lint": "tsc --noEmit"
21
21
  },
22
22
  "dependencies": {
23
- "@apifn/core": "0.0.2"
23
+ "@apifn/core": "0.1.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^20.0.0",
27
27
  "tsup": "^8.0.0",
28
28
  "typescript": "^5.3.0",
29
- "vitest": "^1.0.0"
29
+ "vitest": "^3.2.4"
30
30
  },
31
31
  "author": "21n",
32
32
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "repository": {
37
37
  "type": "git",
38
- "url": "git+https://github.com/21nCo/super-functions.git",
38
+ "url": "git+https://github.com/21nCo/superfunctions.git",
39
39
  "directory": "apifn/mock"
40
40
  },
41
41
  "publishConfig": {
@@ -43,5 +43,6 @@
43
43
  },
44
44
  "files": [
45
45
  "dist"
46
- ]
46
+ ],
47
+ "gitHead": "b8ace8ea1da0fd09dbd5ab1600cd12a81a000c6e"
47
48
  }