@apifn/core 0.0.4 → 0.1.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/README.md +23 -5
- package/dist/chunk-NWH3AIKS.js +68 -0
- package/dist/chunk-NWH3AIKS.js.map +1 -0
- package/dist/index.cjs +476 -6016
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -10
- package/dist/index.d.ts +19 -10
- package/dist/index.js +389 -191
- package/dist/index.js.map +1 -1
- package/dist/openapi-reference-context.cjs +92 -0
- package/dist/openapi-reference-context.cjs.map +1 -0
- package/dist/openapi-reference-context.d.cts +24 -0
- package/dist/openapi-reference-context.d.ts +24 -0
- package/dist/openapi-reference-context.js +8 -0
- package/dist/openapi-reference-context.js.map +1 -0
- package/dist/testfn.cjs +6063 -0
- package/dist/testfn.cjs.map +1 -0
- package/dist/testfn.d.cts +18 -0
- package/dist/testfn.d.ts +18 -0
- package/dist/testfn.js +229 -0
- package/dist/testfn.js.map +1 -0
- package/package.json +33 -8
package/README.md
CHANGED
|
@@ -5,9 +5,11 @@ Core JavaScript/TypeScript library for ApiFn — route introspection, schema con
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @apifn/core
|
|
8
|
+
npm install @apifn/core @superfunctions/http
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
`@superfunctions/http` (`>=0.2.0 <0.4.0`) is a required peer. `@sinclair/typebox` and `@testfn/core` are optional peers: install them only to convert TypeBox schemas or to use the testFn adapter from `@apifn/core/testfn`, which also needs `@apifn/collections`.
|
|
12
|
+
|
|
11
13
|
## Features
|
|
12
14
|
|
|
13
15
|
- **Route Introspection** — Walk a `@superfunctions/http` router and describe every route (parameters, request bodies, responses, security)
|
|
@@ -82,7 +84,15 @@ const doc = parseOpenAPI(await readFile("./openapi.yml", "utf8"));
|
|
|
82
84
|
|
|
83
85
|
### validateOpenAPI
|
|
84
86
|
|
|
85
|
-
Validate a document. Returns validation errors (`ValidationError[]`) for a report or gate.
|
|
87
|
+
Validate a document without changing it. Returns validation errors (`ValidationError[]`) for a report or gate.
|
|
88
|
+
Validation accepts local JSON pointers by default and refuses external references. If you validate a split document, pass trusted, preloaded JSON or YAML in `externalReferences`, keyed by resolved document URI or absolute path without a fragment. Use slash-delimited `#/` pointers within preloaded documents. Validation does not read files or make network requests.
|
|
89
|
+
Escape literal `%`, `?`, and `\\` within path segments as `%25`, `%3F`, and `%5C`.
|
|
90
|
+
Drive-qualified Windows paths and native network-share paths accept either slash style in allowlist keys. Use a URI key for an explicit `file:` reference.
|
|
91
|
+
Preloaded YAML supports anchors and aliases within an expansion limit. Expand YAML merge keys before preloading.
|
|
92
|
+
For recursion in a preloaded document, reference a distinct schema definition. A preloaded `$ref` cannot point directly to itself.
|
|
93
|
+
Relative references use the preloaded document's retrieval URI or path as their base, matching Swagger Parser.
|
|
94
|
+
Literal `$ref` strings in examples and defaults are kept as data, including in preloaded schemas.
|
|
95
|
+
Use `validateAndDereferenceOpenAPI` when you also need the validated copy with references resolved. Recursive schemas can produce circular objects in that copy.
|
|
86
96
|
|
|
87
97
|
```typescript
|
|
88
98
|
import { validateOpenAPI } from "@apifn/core";
|
|
@@ -217,12 +227,19 @@ const logger = createCliLogger({ /* CliLoggerOptions */ });
|
|
|
217
227
|
|
|
218
228
|
### testfn
|
|
219
229
|
|
|
220
|
-
Adapter that plugs ApiFn collection runs into testfn.
|
|
230
|
+
Adapter that plugs ApiFn collection runs into testfn. It lives in the `@apifn/core/testfn` subpath so that the root entry never loads testFn; install `@testfn/core` (`>=0.0.2 <0.1.0`) and `@apifn/collections` at the release that matches your `@apifn/core` yourself to use it. Importing the subpath without `@testfn/core` fails with a module-not-found error naming `@testfn/core`; the adapter loads `@apifn/collections` when `discover()` or `run()` is called, and those calls fail with an error naming `@apifn/collections` if it is missing. Installing `@apifn/core` alone installs neither package. `@testfn/core` is ESM-only: the CommonJS build of `@apifn/core/testfn` `require()`s it, so CommonJS use needs a Node.js version with `require(esm)` (20.19+ or 22.12+), and its CommonJS type declarations import `@testfn/core` with `resolution-mode` `import`, which needs TypeScript 5.3+.
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
npm install @testfn/core @apifn/collections
|
|
234
|
+
```
|
|
221
235
|
|
|
222
236
|
```typescript
|
|
223
|
-
import { ApifnTestAdapter } from "@apifn/core";
|
|
237
|
+
import { ApifnTestAdapter } from "@apifn/core/testfn";
|
|
224
238
|
```
|
|
225
239
|
|
|
240
|
+
The adapter requires `@apifn/collections` 0.0.5 or later. Pass `allowScripts: true` to the adapter constructor only for trusted collections. Pass a `processEnv` object with the specific values needed by collection templates.
|
|
241
|
+
Discovered test IDs use the collection-relative request path. Rediscover saved test selections if multiple requests in a suite share a display name.
|
|
242
|
+
|
|
226
243
|
---
|
|
227
244
|
|
|
228
245
|
## Exports
|
|
@@ -242,12 +259,13 @@ export { fetchEndpointMetrics, createWatchFnClient, apifnWatchMiddleware }
|
|
|
242
259
|
export { mintTestToken, createAuthfnCollectionMiddleware }
|
|
243
260
|
export { fetchRateLimits }
|
|
244
261
|
export { logfnReporter, createCliLogger }
|
|
245
|
-
export { ApifnTestAdapter }
|
|
246
262
|
|
|
247
263
|
// Types — HTTP, OpenAPI document objects, introspection descriptors,
|
|
248
264
|
// diff, validation, snippets, mock, and integration types
|
|
249
265
|
```
|
|
250
266
|
|
|
267
|
+
The `@apifn/core/testfn` subpath exports `ApifnTestAdapter` and needs the optional `@testfn/core` peer and `@apifn/collections`.
|
|
268
|
+
|
|
251
269
|
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
270
|
|
|
253
271
|
## License
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/openapi/reference-context.ts
|
|
2
|
+
var REFERENCE_MODES = /* @__PURE__ */ new Set(["object", "example", "link"]);
|
|
3
|
+
var OBJECT_MAPS = /* @__PURE__ */ new Set(["parameters", "requestBodies", "headers", "securitySchemes", "callbacks", "responses"]);
|
|
4
|
+
var NAME_MAPS = /* @__PURE__ */ new Set([
|
|
5
|
+
"properties",
|
|
6
|
+
"patternProperties",
|
|
7
|
+
"$defs",
|
|
8
|
+
"definitions",
|
|
9
|
+
"dependentSchemas",
|
|
10
|
+
"schemas",
|
|
11
|
+
"links",
|
|
12
|
+
"pathItems",
|
|
13
|
+
"paths",
|
|
14
|
+
"webhooks",
|
|
15
|
+
"content",
|
|
16
|
+
"encoding",
|
|
17
|
+
"variables"
|
|
18
|
+
]);
|
|
19
|
+
var LITERALS = /* @__PURE__ */ new Set(["example", "default", "const", "enum"]);
|
|
20
|
+
function childMode(key, value) {
|
|
21
|
+
if (LITERALS.has(key) || key.startsWith("x-")) return "literal";
|
|
22
|
+
if (key === "examples") return Array.isArray(value) ? "literal" : "examples";
|
|
23
|
+
if (key === "components") return "components";
|
|
24
|
+
if (key === "links") return "links";
|
|
25
|
+
if (key === "paths") return "extNames";
|
|
26
|
+
if (key === "responses") return "extObjects";
|
|
27
|
+
if (key === "requestBody") return "object";
|
|
28
|
+
if (OBJECT_MAPS.has(key)) return "objects";
|
|
29
|
+
return NAME_MAPS.has(key) ? "names" : "node";
|
|
30
|
+
}
|
|
31
|
+
function entryMode(mode, key, value) {
|
|
32
|
+
switch (mode) {
|
|
33
|
+
case "names":
|
|
34
|
+
return "node";
|
|
35
|
+
case "extNames":
|
|
36
|
+
return key.startsWith("x-") ? "literal" : "node";
|
|
37
|
+
case "objects":
|
|
38
|
+
return "object";
|
|
39
|
+
case "extObjects":
|
|
40
|
+
return key.startsWith("x-") ? "literal" : "object";
|
|
41
|
+
case "components":
|
|
42
|
+
if (key.startsWith("x-")) return "literal";
|
|
43
|
+
if (key === "examples" || key === "links") return key;
|
|
44
|
+
return OBJECT_MAPS.has(key) ? "objects" : "names";
|
|
45
|
+
case "examples":
|
|
46
|
+
return "example";
|
|
47
|
+
case "links":
|
|
48
|
+
return "link";
|
|
49
|
+
case "link":
|
|
50
|
+
return key === "parameters" || key === "requestBody" ? "literal" : childMode(key, value);
|
|
51
|
+
default:
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
var MAP_MODES = /* @__PURE__ */ new Set(["names", "extNames", "objects", "extObjects", "components", "examples", "links"]);
|
|
56
|
+
var MAP_ITEMS = { names: "node", extNames: "node", objects: "object", extObjects: "object" };
|
|
57
|
+
var openAPIReferenceContext = Object.freeze({
|
|
58
|
+
childMode,
|
|
59
|
+
entryMode,
|
|
60
|
+
isReferenceObject: (mode) => REFERENCE_MODES.has(mode),
|
|
61
|
+
isMap: (mode) => MAP_MODES.has(mode),
|
|
62
|
+
arrayItemMode: (mode) => MAP_ITEMS[mode] ?? mode
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export {
|
|
66
|
+
openAPIReferenceContext
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=chunk-NWH3AIKS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/openapi/reference-context.ts"],"sourcesContent":["/**\n * How a value is read: `node` (an OpenAPI or schema object, whose keys are keywords), `names` (a\n * map from names to nodes, such as `properties`), `extNames` (a names map that also takes `x-`\n * extensions: Paths and Responses), `components`, `examples` (a map of Example Objects), `example`\n * (one Example Object or its `$ref`), `links` and `link` (Link Objects, whose `parameters` and\n * `requestBody` are data), `objects` and `object` (a map of, or one, OpenAPI object that may be a\n * Reference Object: a Parameter, Response, Request Body, Header, Security Scheme or Callback),\n * `extObjects` (Responses: `objects` that also take `x-` extensions) or `literal` (data). A Callback\n * Object is an `object`: its `x-` keys are data and its expressions Path Items.\n */\nexport type OpenAPIReferenceMode =\n | \"node\" | \"names\" | \"extNames\" | \"objects\" | \"extObjects\" | \"object\" | \"components\" | \"examples\" | \"example\" | \"links\" | \"link\"\n | \"literal\";\n\n/** Modes whose `$ref` is a Reference Object: only `summary` and `description` beside it apply. */\nconst REFERENCE_MODES = new Set<OpenAPIReferenceMode>([\"object\", \"example\", \"link\"]);\n/** Maps whose entries may be Reference Objects (not Schema Objects). */\nconst OBJECT_MAPS = new Set([\"parameters\", \"requestBodies\", \"headers\", \"securitySchemes\", \"callbacks\", \"responses\"]);\n\n/** Keywords whose value maps names (any string, `default` and `$ref` included) to nodes. */\nconst NAME_MAPS = new Set([\n \"properties\", \"patternProperties\", \"$defs\", \"definitions\", \"dependentSchemas\", \"schemas\", \"links\", \"pathItems\",\n \"paths\", \"webhooks\", \"content\", \"encoding\", \"variables\",\n]);\n/** Keywords whose value is data. */\nconst LITERALS = new Set([\"example\", \"default\", \"const\", \"enum\"]);\n\n/** How the value under `key` of a node is read. */\nfunction childMode(key: string, value: unknown): OpenAPIReferenceMode {\n if (LITERALS.has(key) || key.startsWith(\"x-\")) return \"literal\";\n if (key === \"examples\") return Array.isArray(value) ? \"literal\" : \"examples\";\n if (key === \"components\") return \"components\";\n if (key === \"links\") return \"links\";\n if (key === \"paths\") return \"extNames\";\n if (key === \"responses\") return \"extObjects\";\n if (key === \"requestBody\") return \"object\";\n if (OBJECT_MAPS.has(key)) return \"objects\";\n return NAME_MAPS.has(key) ? \"names\" : \"node\";\n}\n\n/** How the value under `key` of a map read in `mode` is read (null: `mode` is not a map). */\nfunction entryMode(mode: OpenAPIReferenceMode, key: string, value: unknown): OpenAPIReferenceMode | null {\n switch (mode) {\n case \"names\":\n return \"node\";\n case \"extNames\":\n return key.startsWith(\"x-\") ? \"literal\" : \"node\";\n case \"objects\":\n return \"object\";\n case \"extObjects\":\n return key.startsWith(\"x-\") ? \"literal\" : \"object\";\n case \"components\":\n if (key.startsWith(\"x-\")) return \"literal\";\n if (key === \"examples\" || key === \"links\") return key;\n return OBJECT_MAPS.has(key) ? \"objects\" : \"names\";\n case \"examples\":\n return \"example\";\n case \"links\":\n return \"link\";\n case \"link\":\n return key === \"parameters\" || key === \"requestBody\" ? \"literal\" : childMode(key, value);\n default:\n return null;\n }\n}\n\nconst MAP_MODES = new Set<OpenAPIReferenceMode>([\"names\", \"extNames\", \"objects\", \"extObjects\", \"components\", \"examples\", \"links\"]);\n/** How the items of an array read in a map mode are read. */\nconst MAP_ITEMS: Partial<Record<OpenAPIReferenceMode, OpenAPIReferenceMode>> = { names: \"node\", extNames: \"node\", objects: \"object\", extObjects: \"object\" };\n\nexport const openAPIReferenceContext = Object.freeze({\n childMode,\n entryMode,\n isReferenceObject: (mode: OpenAPIReferenceMode) => REFERENCE_MODES.has(mode),\n isMap: (mode: OpenAPIReferenceMode) => MAP_MODES.has(mode),\n arrayItemMode: (mode: OpenAPIReferenceMode) => MAP_ITEMS[mode] ?? mode,\n});\n"],"mappings":";AAeA,IAAM,kBAAkB,oBAAI,IAA0B,CAAC,UAAU,WAAW,MAAM,CAAC;AAEnF,IAAM,cAAc,oBAAI,IAAI,CAAC,cAAc,iBAAiB,WAAW,mBAAmB,aAAa,WAAW,CAAC;AAGnH,IAAM,YAAY,oBAAI,IAAI;AAAA,EACxB;AAAA,EAAc;AAAA,EAAqB;AAAA,EAAS;AAAA,EAAe;AAAA,EAAoB;AAAA,EAAW;AAAA,EAAS;AAAA,EACnG;AAAA,EAAS;AAAA,EAAY;AAAA,EAAW;AAAA,EAAY;AAC9C,CAAC;AAED,IAAM,WAAW,oBAAI,IAAI,CAAC,WAAW,WAAW,SAAS,MAAM,CAAC;AAGhE,SAAS,UAAU,KAAa,OAAsC;AACpE,MAAI,SAAS,IAAI,GAAG,KAAK,IAAI,WAAW,IAAI,EAAG,QAAO;AACtD,MAAI,QAAQ,WAAY,QAAO,MAAM,QAAQ,KAAK,IAAI,YAAY;AAClE,MAAI,QAAQ,aAAc,QAAO;AACjC,MAAI,QAAQ,QAAS,QAAO;AAC5B,MAAI,QAAQ,QAAS,QAAO;AAC5B,MAAI,QAAQ,YAAa,QAAO;AAChC,MAAI,QAAQ,cAAe,QAAO;AACl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