@apifn/core 0.1.0 → 0.2.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/README.md CHANGED
@@ -84,7 +84,15 @@ const doc = parseOpenAPI(await readFile("./openapi.yml", "utf8"));
84
84
 
85
85
  ### validateOpenAPI
86
86
 
87
- 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.
88
96
 
89
97
  ```typescript
90
98
  import { validateOpenAPI } from "@apifn/core";
@@ -178,10 +186,13 @@ const metrics = await fetchEndpointMetrics({
178
186
 
179
187
  const client = createWatchFnClient({ baseUrl: "https://watch.example.com" });
180
188
 
181
- // Middleware records per-endpoint timing to watchfn
189
+ // Middleware records per-endpoint timing and status to watchfn
190
+ const router = createRouter({ routes }); // from @superfunctions/http
182
191
  router.use(apifnWatchMiddleware({ baseUrl: "https://watch.example.com" }));
183
192
  ```
184
193
 
194
+ `apifnWatchMiddleware` is a `@superfunctions/http` `Middleware`, so it also works in `createRouter({ middleware: [...] })` and in a route's `middleware` list. It returns the downstream `Response` unchanged (status, headers and body, which it never reads) and records that response's status. When the router's error handler turns a thrown error into a response, it records that response's status. Telemetry failures never change the response: they are logged with `console.warn` unless `silent: true`. By default (`fireAndForget: true`) it returns the response without waiting for the event POST to finish.
195
+
185
196
  ### authfn
186
197
 
187
198
  Mint short-lived test tokens (TTL ≤ 3600s) and inject bearer auth into collection runs.
@@ -229,6 +240,9 @@ npm install @testfn/core @apifn/collections
229
240
  import { ApifnTestAdapter } from "@apifn/core/testfn";
230
241
  ```
231
242
 
243
+ 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.
244
+ Discovered test IDs use the collection-relative request path. Rediscover saved test selections if multiple requests in a suite share a display name.
245
+
232
246
  ---
233
247
 
234
248
  ## Exports
@@ -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;A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