@apifn/core 0.0.2 → 0.0.3

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 ADDED
@@ -0,0 +1,255 @@
1
+ # @apifn/core
2
+
3
+ Core JavaScript/TypeScript library for ApiFn — route introspection, schema conversion, OpenAPI generation, and diff. The other ApiFn npm packages build on `@apifn/core`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @apifn/core
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Route Introspection** — Walk a `@superfunctions/http` router and describe every route (parameters, request bodies, responses, security)
14
+ - **Schema Conversion** — Convert Zod and TypeBox schemas to JSON Schema, with auto-detection and deduplication
15
+ - **OpenAPI Generation** — Produce an OpenAPI 3.1 document directly from a router
16
+ - **OpenAPI Parsing & Validation** — Parse YAML/JSON specs and validate them against the OpenAPI schema
17
+ - **Diff** — Compare two OpenAPI documents and classify changes as breaking / non-breaking
18
+ - **Integrations** — First-class hooks for authfn, secfn, watchfn, logfn, and testfn
19
+
20
+ ---
21
+
22
+ ## OpenAPI Generation
23
+
24
+ ### fromRouter
25
+
26
+ Generate an OpenAPI document from a `@superfunctions/http` router in one step. `FromRouterOptions` combines `OpenAPIGenerateOptions` (info, servers, security schemes) and `IntrospectOptions` (include/exclude/basePath).
27
+
28
+ ```typescript
29
+ import { fromRouter } from "@apifn/core";
30
+
31
+ const doc = fromRouter(router, {
32
+ info: {
33
+ title: "My API",
34
+ version: "1.0.0",
35
+ description: "Public API surface",
36
+ },
37
+ servers: [{ url: "https://api.example.com" }],
38
+ // IntrospectOptions
39
+ include: ["/v1"],
40
+ exclude: ["/internal"],
41
+ basePath: "/api",
42
+ });
43
+ ```
44
+
45
+ ### introspectRouter
46
+
47
+ Describe a router's routes without generating a full document. Returns `IntrospectedRoute[]`.
48
+
49
+ ```typescript
50
+ import { introspectRouter } from "@apifn/core";
51
+
52
+ const routes = introspectRouter(router, { include: ["/v1"] });
53
+ // [{ method, path, parameters, requestBody, responses, security }, ...]
54
+ ```
55
+
56
+ ### generateOpenAPI
57
+
58
+ Build the document from already-introspected routes. `fromRouter` is `introspectRouter` + `generateOpenAPI`.
59
+
60
+ ```typescript
61
+ import { introspectRouter, generateOpenAPI } from "@apifn/core";
62
+
63
+ const doc = generateOpenAPI(introspectRouter(router), {
64
+ info: { title: "My API", version: "1.0.0" },
65
+ });
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Parsing & Validation
71
+
72
+ ### parseOpenAPI
73
+
74
+ Parse a YAML or JSON OpenAPI string into an `OpenAPIDocument`.
75
+
76
+ ```typescript
77
+ import { parseOpenAPI } from "@apifn/core";
78
+ import { readFile } from "node:fs/promises";
79
+
80
+ const doc = parseOpenAPI(await readFile("./openapi.yml", "utf8"));
81
+ ```
82
+
83
+ ### validateOpenAPI
84
+
85
+ Validate a document. Returns validation errors (`ValidationError[]`) for a report or gate.
86
+
87
+ ```typescript
88
+ import { validateOpenAPI } from "@apifn/core";
89
+
90
+ const errors = await validateOpenAPI(doc);
91
+ if (errors.length > 0) {
92
+ // handle invalid spec
93
+ }
94
+ ```
95
+
96
+ ### upconvertFrom3_0
97
+
98
+ Up-convert an OpenAPI 3.0 document to 3.1.
99
+
100
+ ```typescript
101
+ import { upconvertFrom3_0 } from "@apifn/core";
102
+
103
+ const doc31 = upconvertFrom3_0(doc30);
104
+ ```
105
+
106
+ ---
107
+
108
+ ## Schema Conversion
109
+
110
+ ApiFn converts framework-native schemas to JSON Schema for OpenAPI output.
111
+
112
+ | Function | Description |
113
+ |----------|-------------|
114
+ | `detectSchemaType(schema)` | Detect whether a value is a Zod, TypeBox, or plain JSON schema (`SchemaType`) |
115
+ | `convertSchema(schema)` | Convert any supported schema to JSON Schema, auto-detecting the type |
116
+ | `convertZodSchema(schema)` | Convert a Zod schema to JSON Schema |
117
+ | `convertTypeBoxSchema(schema)` | Convert a TypeBox schema to JSON Schema |
118
+ | `deduplicateSchemas(input)` | Extract shared/duplicated schemas into reusable `components` (`DeduplicateSchemasResult`) |
119
+
120
+ ```typescript
121
+ import { convertSchema, detectSchemaType } from "@apifn/core";
122
+ import { z } from "zod";
123
+
124
+ detectSchemaType(z.object({ id: z.string() })); // "zod"
125
+ const jsonSchema = convertSchema(z.object({ id: z.string() }));
126
+ ```
127
+
128
+ ### apiSchema
129
+
130
+ Attach a schema descriptor to a route so introspection can pick up request/response shapes.
131
+
132
+ ```typescript
133
+ import { apiSchema } from "@apifn/core";
134
+ ```
135
+
136
+ ---
137
+
138
+ ## Diff
139
+
140
+ ### diffOpenAPI
141
+
142
+ Compare two OpenAPI documents. Returns a `DiffResult` with a list of `DiffEntry` changes, each classified by severity.
143
+
144
+ ```typescript
145
+ import { diffOpenAPI, formatDiffAsText, formatDiffAsJson } from "@apifn/core";
146
+
147
+ const result = diffOpenAPI(beforeDoc, afterDoc);
148
+
149
+ console.log(formatDiffAsText(result)); // human-readable
150
+ const report = formatDiffAsJson(result); // DiffReportJson (machine-readable)
151
+ ```
152
+
153
+ - `formatDiffAsText(result)` — colored/plain text summary
154
+ - `formatDiffAsJson(result)` — structured `DiffReportJson` for CI artifacts
155
+
156
+ ---
157
+
158
+ ## Integrations
159
+
160
+ ### watchfn
161
+
162
+ Fetch endpoint performance metrics and stream request telemetry to a watchfn instance.
163
+
164
+ ```typescript
165
+ import {
166
+ fetchEndpointMetrics,
167
+ createWatchFnClient,
168
+ apifnWatchMiddleware,
169
+ } from "@apifn/core";
170
+
171
+ // Fetch p50/p95/p99 + error-rate metrics; degrades gracefully to [] on failure
172
+ const metrics = await fetchEndpointMetrics({
173
+ baseUrl: "https://watch.example.com",
174
+ timeRange: "PT1H", // ISO 8601 duration
175
+ });
176
+
177
+ const client = createWatchFnClient({ baseUrl: "https://watch.example.com" });
178
+
179
+ // Middleware records per-endpoint timing to watchfn
180
+ router.use(apifnWatchMiddleware({ baseUrl: "https://watch.example.com" }));
181
+ ```
182
+
183
+ ### authfn
184
+
185
+ Mint short-lived test tokens (TTL ≤ 3600s) and inject bearer auth into collection runs.
186
+
187
+ ```typescript
188
+ import { mintTestToken, createAuthfnCollectionMiddleware } from "@apifn/core";
189
+
190
+ const { token, expiresAt } = await mintTestToken(
191
+ { baseUrl: "https://auth.example.com", adminKey: process.env.AUTHFN_ADMIN_KEY },
192
+ { ttl: 900, scopes: ["read"] },
193
+ );
194
+
195
+ const client = createAuthfnCollectionMiddleware(baseHttpClient, token);
196
+ ```
197
+
198
+ ### secfn
199
+
200
+ Fetch (non-consuming) rate-limit information for endpoints.
201
+
202
+ ```typescript
203
+ import { fetchRateLimits } from "@apifn/core";
204
+
205
+ const limits = await fetchRateLimits(rateLimiter, ["GET /users"]); // RateLimitInfo[]
206
+ ```
207
+
208
+ ### logfn
209
+
210
+ Structured reporter and CLI logger.
211
+
212
+ ```typescript
213
+ import { logfnReporter, createCliLogger } from "@apifn/core";
214
+
215
+ const logger = createCliLogger({ /* CliLoggerOptions */ });
216
+ ```
217
+
218
+ ### testfn
219
+
220
+ Adapter that plugs ApiFn collection runs into testfn.
221
+
222
+ ```typescript
223
+ import { ApifnTestAdapter } from "@apifn/core";
224
+ ```
225
+
226
+ ---
227
+
228
+ ## Exports
229
+
230
+ ```typescript
231
+ // OpenAPI
232
+ export { fromRouter, introspectRouter, generateOpenAPI, parseOpenAPI, validateOpenAPI, upconvertFrom3_0 }
233
+
234
+ // Schema
235
+ export { detectSchemaType, convertSchema, convertZodSchema, convertTypeBoxSchema, deduplicateSchemas, apiSchema }
236
+
237
+ // Diff
238
+ export { diffOpenAPI, formatDiffAsText, formatDiffAsJson }
239
+
240
+ // Integrations
241
+ export { fetchEndpointMetrics, createWatchFnClient, apifnWatchMiddleware }
242
+ export { mintTestToken, createAuthfnCollectionMiddleware }
243
+ export { fetchRateLimits }
244
+ export { logfnReporter, createCliLogger }
245
+ export { ApifnTestAdapter }
246
+
247
+ // Types — HTTP, OpenAPI document objects, introspection descriptors,
248
+ // diff, validation, snippets, mock, and integration types
249
+ ```
250
+
251
+ The `@apifn/core/types` subpath exports the shared model types declared by the core package, including HTTP/OpenAPI, schema, diff, validation, snippet, mock, and integration data shapes. Types declared alongside runtime modules, such as `FromRouterOptions`, `SchemaType`, and `DiffReportJson`, are exported from the package root.
252
+
253
+ ## License
254
+
255
+ MIT
package/dist/index.cjs CHANGED
@@ -4505,11 +4505,12 @@ var require_queue = __commonJS({
4505
4505
  current.value = null;
4506
4506
  current.callback = noop;
4507
4507
  current.errorHandler = null;
4508
+ current.next = null;
4508
4509
  if (errorHandler2) {
4509
4510
  errorHandler2(new Error("abort"), val);
4510
4511
  }
4511
4512
  callback.call(context2, new Error("abort"));
4512
- current.release(current);
4513
+ cache.release(current);
4513
4514
  current = next;
4514
4515
  }
4515
4516
  self.drain = noop;