@ts-pf/openapi 0.1.12 → 0.5.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 +63 -5
- package/dist/close-inputs.d.ts +9 -0
- package/dist/close-inputs.d.ts.map +1 -0
- package/dist/close-inputs.js +102 -0
- package/dist/close-inputs.js.map +1 -0
- package/dist/const-enum.d.ts +7 -0
- package/dist/const-enum.d.ts.map +1 -0
- package/dist/const-enum.js +76 -0
- package/dist/const-enum.js.map +1 -0
- package/dist/discriminators.d.ts +9 -0
- package/dist/discriminators.d.ts.map +1 -0
- package/dist/discriminators.js +57 -0
- package/dist/discriminators.js.map +1 -0
- package/dist/document.d.ts +18 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +86 -0
- package/dist/document.js.map +1 -0
- package/dist/enum-names.d.ts +14 -0
- package/dist/enum-names.d.ts.map +1 -0
- package/dist/enum-names.js +103 -0
- package/dist/enum-names.js.map +1 -0
- package/dist/envelope.d.ts +5 -0
- package/dist/envelope.d.ts.map +1 -1
- package/dist/envelope.js +24 -0
- package/dist/envelope.js.map +1 -1
- package/dist/errors.d.ts +34 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +109 -42
- package/dist/errors.js.map +1 -1
- package/dist/format-patterns.d.ts +4 -0
- package/dist/format-patterns.d.ts.map +1 -0
- package/dist/format-patterns.js +39 -0
- package/dist/format-patterns.js.map +1 -0
- package/dist/hoist-objects.d.ts +15 -0
- package/dist/hoist-objects.d.ts.map +1 -0
- package/dist/hoist-objects.js +141 -0
- package/dist/hoist-objects.js.map +1 -0
- package/dist/hoist.d.ts +32 -0
- package/dist/hoist.d.ts.map +1 -0
- package/dist/hoist.js +189 -0
- package/dist/hoist.js.map +1 -0
- package/dist/names.d.ts +9 -0
- package/dist/names.d.ts.map +1 -1
- package/dist/names.js +21 -0
- package/dist/names.js.map +1 -1
- package/dist/naming.d.ts +13 -0
- package/dist/naming.d.ts.map +1 -0
- package/dist/naming.js +95 -0
- package/dist/naming.js.map +1 -0
- package/dist/normalize.d.ts +11 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +18 -0
- package/dist/normalize.js.map +1 -0
- package/dist/openapi.d.ts +1 -1
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +57 -17
- package/dist/openapi.js.map +1 -1
- package/dist/operation.d.ts +20 -2
- package/dist/operation.d.ts.map +1 -1
- package/dist/operation.js +46 -10
- package/dist/operation.js.map +1 -1
- package/dist/profile.d.ts +32 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +107 -0
- package/dist/profile.js.map +1 -0
- package/dist/refs.d.ts +13 -1
- package/dist/refs.d.ts.map +1 -1
- package/dist/refs.js +36 -27
- package/dist/refs.js.map +1 -1
- package/dist/shared-errors.d.ts +14 -0
- package/dist/shared-errors.d.ts.map +1 -0
- package/dist/shared-errors.js +73 -0
- package/dist/shared-errors.js.map +1 -0
- package/dist/streams.d.ts +13 -0
- package/dist/streams.d.ts.map +1 -0
- package/dist/streams.js +26 -0
- package/dist/streams.js.map +1 -0
- package/dist/tags.d.ts +14 -0
- package/dist/tags.d.ts.map +1 -0
- package/dist/tags.js +40 -0
- package/dist/tags.js.map +1 -0
- package/dist/types.d.ts +100 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/walk.d.ts +13 -0
- package/dist/walk.d.ts.map +1 -0
- package/dist/walk.js +65 -0
- package/dist/walk.js.map +1 -0
- package/package.json +4 -4
- package/skills/ts-pf-openapi/SKILL.md +63 -5
package/README.md
CHANGED
|
@@ -34,17 +34,75 @@ const result = await handler.handle(req, { prefix: '/rpc', context })
|
|
|
34
34
|
|
|
35
35
|
Point Scalar or Swagger UI at that URL. Do not put those UIs in this package.
|
|
36
36
|
|
|
37
|
-
`openapi()` is a
|
|
37
|
+
`openapi()` is a projection of the catalog (its schemas are normalized, see below). Filter/hide procedures with `catalog(..., { filter })`. Mutate the returned object for `security`, extra `components`, or operation overrides.
|
|
38
38
|
|
|
39
|
-
Stream procedures advertise `application/jsonl` (item schema from the catalog). Pass `sse: true` to also list `text/event-stream`. Pass `multipart: true` to also list `multipart/form-data` on unary requests. Default is JSON (and JSONL for streams).
|
|
39
|
+
Stream procedures advertise `application/jsonl` (item schema from the catalog), unless the `streams` option says otherwise (see [Codegen profile](#codegen-profile)). Pass `sse: true` to also list `text/event-stream`. Pass `multipart: true` to also list `multipart/form-data` on unary requests. Default is JSON (and JSONL for streams).
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
### Schemas and component names
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
The document describes the same schemas the catalog holds. It normalizes them so code generators (openapi-generator, Kiota, orval, swagger_parser) get one model per type:
|
|
44
|
+
|
|
45
|
+
- **No nested `$schema`.** The document's `jsonSchemaDialect` declares JSON Schema 2020-12 once.
|
|
46
|
+
- **Outputs are open.** `additionalProperties: false` is dropped from output and error `data` schemas, so a server that adds a field does not break older generated clients. Input schemas stay as the catalog gives them. The `{ ok, output }` / `{ ok, error }` envelopes stay closed (the protocol defines them).
|
|
47
|
+
- **`$defs` are hoisted.** Every root `$defs` / `definitions` key other than `__schema<N>` (see below) is treated as an id: a Zod `.meta({ id: 'Book' })`, a TypeBox `Cyclic` key, or another converter's def name becomes `components.schemas.Book`, and every `$ref` points there. A DTO used by several procedures is one component. Each procedure keeps its `<path>.Input` / `<path>.Output` component; when the schema is just a named type, that component is a `$ref` to it (`book.find.Output` → `Book`).
|
|
48
|
+
- **Input and output shapes of one id.** An id used on one side only, or with the same shape on both, is `<id>`. When the shapes differ, the output keeps `<id>` and the input becomes `<id>Input`. Typical causes: a field with a default (required in the output, optional in the input), or a closed object (`z.strictObject`, `additionalProperties: false`), because inputs stay closed while outputs open. A type that contains a split type splits too.
|
|
49
|
+
- **Collisions throw.** Two different shapes for one id on the same side, an `<id>Input` that is already an id, or an id that equals another component name throws an `Error` naming the id (or, for a name clash, the component). Give one of the schemas a distinct `.meta({ id })`. Ids are sanitized to `[A-Za-z0-9._-]`.
|
|
50
|
+
- **Unnamed recursion is named after its host.** Zod names an unnamed recursive schema `__schemaN` in each conversion, so the same key means different schemas in different procedures. Any def key matching `__schema<N>` is therefore not an id: it becomes `<host>.__schemaN` (for example `book.reviews.Output.__schema0`), shared across procedures only when side and shape are identical (named after the first host that resolves it). Give the schema a `.meta({ id })` for a stable name.
|
|
51
|
+
- **Errors are shared per code.** When every procedure that declares a code has the same `data` shape (or none), there is one `Error.<CODE>` envelope and one `Error.<CODE>.Data`. That includes a code only one procedure declares. When the data differs, every procedure that declares the code keeps its own `<path>.Error.<CODE>` (and `.Data`). Each response keeps its procedure's status and message. Protocol errors are `TsPf.Error.<CODE>` (and `TsPf.Error.VALIDATION.Data`).
|
|
52
|
+
|
|
53
|
+
`$anchor` / `$dynamicAnchor` across schemas are not rewritten, and only root `$defs` / `definitions` are hoisted. If you post-process the document by component name, use these names.
|
|
54
|
+
|
|
55
|
+
### Codegen profile
|
|
56
|
+
|
|
57
|
+
The default document is a plain projection that reads well in Scalar. Code generators (openapi-generator, Kiota, orval, swagger_parser) want a few more choices made for them. `profile: 'codegen'` makes them:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { catalog } from '@ts-pf/docs'
|
|
61
|
+
import { openapi } from '@ts-pf/openapi'
|
|
62
|
+
|
|
63
|
+
const spec = openapi(catalog(contract, { prefix: '/rpc' }), {
|
|
64
|
+
info: { title: 'Planet API', version: '1.0.0' },
|
|
65
|
+
profile: 'codegen',
|
|
66
|
+
})
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The profile describes the same POST JSON RPC paths and envelopes: no GET, no path params, no REST. It sets the options below to their `codegen` column (`streams`, `enumNames`, and `catalogHash` are never set by the profile). Each one can also be set alone, and an option you pass yourself wins over the profile (`{ profile: 'codegen', naming: 'path' }` keeps dotted names).
|
|
70
|
+
|
|
71
|
+
| Option | Default | `codegen` | Effect |
|
|
72
|
+
| --- | --- | --- | --- |
|
|
73
|
+
| `envelopes` | `'components'` | `'inline'` | Request / success envelopes inline in each operation: no `<path>.Request` / `<path>.Success` components. |
|
|
74
|
+
| `errors` | `'per-code'` | `'shared'` | One `RpcError` envelope (`{ ok: false, error: { code: ErrorCode, message, data? } }`), an `ErrorCode` string enum (every code an operation can return), one `<Code>Data` per code with data (`NOT_FOUND` → `NotFoundData`; protocol `VALIDATION` → `ValidationErrorData`), and `x-ts-pf-errors: [codes]` on each operation. Error responses `$ref` `RpcError`. Nothing `$ref`s a `<Code>Data` component: map it by name from `error.code` (or `x-ts-pf-errors`). Throws when two procedures declare one code with different `data` shapes; a procedure that declares the code without data is fine. |
|
|
75
|
+
| `naming` | `'path'` | `'pascal'` | `auth.verifyCode.Input` → `AuthVerifyCodeInput` (`…Output`, `…Request`, `…Success`; `book.reviews.Output.__schema0` → `BookReviewsOutputSchema0`; with `errors: 'per-code'`, `Error.NOT_FOUND` → `NotFoundError`, `TsPf.Error.VALIDATION` → `TsPfValidationError`, `<path>.Error.<CODE>` → `<Path><Code>Error`, and `.Data` → `…Data`). Ids keep their names as given, and a procedure whose input or output is just an id uses the id (`BookFindOutput` is not emitted; the operation refs `Book`). Throws when two different schemas get one name: give one a distinct `.meta({ id })`. |
|
|
76
|
+
| `closeInputs` | `false` | `true` | `additionalProperties: false` on objects that only requests reach. This is stricter than the server, which strips unknown keys. A component a response (or error data) also reaches stays open, and so do `allOf` members and objects that combine subschemas (`allOf` / `anyOf` / `oneOf`). |
|
|
77
|
+
| `constAsEnum` | `false` | `'exceptTags'` | `const: v` → `enum: [v]`, except on the tag property of a union of objects (a property with a different `const` in every variant). |
|
|
78
|
+
| `dropFormatPatterns` | `false` | `true` | Drops `pattern` where a standard `format` (`date-time`, `email`, `uuid`, `uri`, …) is set. Zod emits both. Non-standard formats keep their pattern. |
|
|
79
|
+
| `emptyInputNull` | `false` | `true` | A procedure with no input, or a `null` input, gets an inline `{ input: null }` request body and no `Input` component. (`z.void()` / `z.undefined()` are `unavailable` in the catalog and are not detected; omit `.input()` instead.) |
|
|
80
|
+
| `hoist` | `'none'` | `'objects'` | Inline objects with properties nested in a schema component (under `properties`, array `items`, record `additionalProperties`, or a `oneOf` / `anyOf` variant) become named components. Tuple (`prefixItems`) and `allOf` members stay inline (an `allOf` member's own nested objects are named after the parent). Names are structural: the parent component's name plus a segment per step (`naming: 'pascal'`: `BookFindOutput` + `author` → `BookFindOutputAuthor`; `naming: 'path'`: `book.find.Output.author`). Array items add `Item`, record values add `Value`. A union's inline object variants add their tag value (`ShapeOutputCircle`), else (or when two tag values give one name) `Option1`, `Option2`, …; a nested union with one inline object (`.nullable()`) names it like the union (at a component root, `Option1`). Identical shapes share one component, named by the alphabetically first of their names. Protocol envelopes (`RpcError`, `…Request`, `…Success`, error envelopes) keep their inline objects. Warns once (`console.warn`) listing the names it made up: give those objects a `.meta({ id })` to choose the name. Throws when one name would hold two different schemas, or names a component that already exists. |
|
|
81
|
+
| `discriminators` | `false` | `true` | Adds `discriminator: { propertyName, mapping }` to a `oneOf` / `anyOf` whose variants are all component `$ref`s, each requiring a different string `const` (or one-value `enum`) on one property; `mapping` points each value at its variant's component. If several properties qualify, the alphabetically first wins. Other unions are left as they are. |
|
|
82
|
+
| `canonical` | `false` | `true` | Sorts keys at every depth (`canonicalize` from `@ts-pf/docs`, as `catalogHash` does), so the same catalog always serializes to the same bytes. `paths` and `components.schemas` become alphabetical. |
|
|
83
|
+
| `streams` | `'operation'` | `'operation'` | See [Streams without paths](#streams-without-paths). |
|
|
84
|
+
| `enumNames` | unset | unset | `{ BookStatus: ['available', 'loaned', 'lost'] }`: every inline `enum` with exactly that value set (in any order) becomes a `$ref` to component `BookStatus` (values in the entry's order; the node's `description` / `default` stay next to the `$ref`). A heuristic, so the profile never sets it: prefer `.meta({ id })` on the enum. Throws on an invalid name (not `[A-Za-z0-9._-]`), an entry with no values, two entries with one value set, or a name an existing component has with a different schema; warns about entries that matched nothing. |
|
|
85
|
+
| `catalogHash` | unset | unset | Written to `info['x-ts-pf-catalog-hash']`. Pass `catalogHash(cat)` from `@ts-pf/codegen` so a generated client can tell which catalog it came from. |
|
|
86
|
+
|
|
87
|
+
The passes run in a fixed order, so each sees the previous one's result: `naming`, `enumNames`, `closeInputs`, `dropFormatPatterns`, `constAsEnum`, `hoist` (names come from final parent names; shapes are compared after the rewrites), `discriminators` (mappings use final names and see one-value enums), then `canonical`.
|
|
88
|
+
|
|
89
|
+
`x-ts-pf-errors`, `x-ts-pf-streams`, and `x-ts-pf-catalog-hash` are the only `x-` extensions the document uses. Generators ignore extensions they do not know.
|
|
90
|
+
|
|
91
|
+
#### Streams without paths
|
|
92
|
+
|
|
93
|
+
The catalog does not say which transport serves a procedure, so `openapi()` cannot tell a stream served over HTTP from one served only over a message transport (WebSocket, stdio, MessagePort). The `streams` option is your statement about the catalog you pass (`catalog(c, { filter })` still selects procedures), so the profile leaves it at `'operation'`:
|
|
94
|
+
|
|
95
|
+
- `'operation'` (default): each stream procedure is a `POST … application/jsonl` operation.
|
|
96
|
+
- `'components'`: stream procedures get no path. Their schemas are the same `<path>.Input` / `<path>.Output` components (named by `naming`), listed in a document-level map by catalog key: `x-ts-pf-streams: { "book/watch": { input: { $ref }, item: { $ref }, errors: [codes] } }`. `item` is each output item; a streamed input is `inputItem` (with the unary `output`, if any). `errors` lists the codes, as `x-ts-pf-errors` does. Pair it with `errors: 'shared'`, where each code's data is its `<Code>Data` component: with the default `errors: 'per-code'`, a code's data is `Error.<CODE>.Data` only when every procedure shares its shape, and is otherwise not in the document for a stream procedure.
|
|
97
|
+
- `'omit'`: stream procedures are left out, as if filtered from the catalog.
|
|
98
|
+
|
|
99
|
+
App-specific parts of the document (security schemes, extra components, operation overrides) stay in userland: mutate the returned document, as with the default output.
|
|
100
|
+
|
|
101
|
+
Message transports (WebSocket, stdio, MessagePort) are not HTTP and have no paths in this document. Their stream schemas can be described with `streams: 'components'`.
|
|
44
102
|
|
|
45
103
|
Typed split-repo clients: [`@ts-pf/codegen`](../codegen).
|
|
46
104
|
|
|
47
105
|
## API
|
|
48
106
|
|
|
49
107
|
- `openapi`
|
|
50
|
-
- `OpenAPIOptions` (`sse: true` / `multipart: true` advertise those content types), `OpenAPIDocument`
|
|
108
|
+
- `OpenAPIOptions` (`sse: true` / `multipart: true` advertise those content types; `profile: 'codegen'` and its options `envelopes`, `errors`, `naming`, `closeInputs`, `constAsEnum`, `dropFormatPatterns`, `emptyInputNull`, `hoist`, `discriminators`, `canonical`; also `streams`, `enumNames`, `catalogHash`), `OpenAPIDocument`
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { OpenAPIDocument } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* `closeInputs`: add `additionalProperties: false` to object schemas that
|
|
4
|
+
* only requests reach. A component that a response also reaches stays open,
|
|
5
|
+
* and so does an object that is a member of an `allOf` (closing it would
|
|
6
|
+
* reject the other members' properties).
|
|
7
|
+
*/
|
|
8
|
+
export declare function closeInputs(document: OpenAPIDocument): void;
|
|
9
|
+
//# sourceMappingURL=close-inputs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"close-inputs.d.ts","sourceRoot":"","sources":["../src/close-inputs.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAGjD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAe3D"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { componentTarget, mapDocumentSchemas } from './document.js';
|
|
2
|
+
import { forEachSchema, isSchemaObject, mapSchema } from './walk.js';
|
|
3
|
+
/**
|
|
4
|
+
* `closeInputs`: add `additionalProperties: false` to object schemas that
|
|
5
|
+
* only requests reach. A component that a response also reaches stays open,
|
|
6
|
+
* and so does an object that is a member of an `allOf` (closing it would
|
|
7
|
+
* reject the other members' properties).
|
|
8
|
+
*/
|
|
9
|
+
export function closeInputs(document) {
|
|
10
|
+
const { schemas } = document.components;
|
|
11
|
+
const reach = reachedComponents(document);
|
|
12
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
13
|
+
if (reach.input.has(name) &&
|
|
14
|
+
!reach.output.has(name) &&
|
|
15
|
+
!reach.allOfMembers.has(name)) {
|
|
16
|
+
schemas[name] = closeObjects(schema);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
mapDocumentSchemas(document, (schema, side) => side === 'input' ? closeObjects(schema) : schema);
|
|
20
|
+
}
|
|
21
|
+
/** Component names each side reaches, following `$ref`s transitively. */
|
|
22
|
+
function reachedComponents(document) {
|
|
23
|
+
const { schemas } = document.components;
|
|
24
|
+
const reach = {
|
|
25
|
+
input: new Set(),
|
|
26
|
+
output: new Set(),
|
|
27
|
+
allOfMembers: new Set(),
|
|
28
|
+
};
|
|
29
|
+
const visit = (schema, side) => {
|
|
30
|
+
forEachSchema(schema, (node) => {
|
|
31
|
+
for (const member of Array.isArray(node.allOf) ? node.allOf : []) {
|
|
32
|
+
const target = isSchemaObject(member)
|
|
33
|
+
? componentTarget(member.$ref)
|
|
34
|
+
: undefined;
|
|
35
|
+
if (target !== undefined) {
|
|
36
|
+
reach.allOfMembers.add(target.name);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const target = componentTarget(node.$ref);
|
|
40
|
+
if (target === undefined || reach[side].has(target.name)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
reach[side].add(target.name);
|
|
44
|
+
const component = schemas[target.name];
|
|
45
|
+
if (component !== undefined) {
|
|
46
|
+
visit(component, side);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
mapDocumentSchemas(document, (schema, side) => {
|
|
51
|
+
if (side !== undefined) {
|
|
52
|
+
visit(schema, side);
|
|
53
|
+
}
|
|
54
|
+
return schema;
|
|
55
|
+
});
|
|
56
|
+
// A component no operation refers to (`<Code>Data` under shared errors)
|
|
57
|
+
// documents a response, so it counts as output.
|
|
58
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
59
|
+
if (!reach.input.has(name) && !reach.output.has(name)) {
|
|
60
|
+
reach.output.add(name);
|
|
61
|
+
visit(schema, 'output');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return reach;
|
|
65
|
+
}
|
|
66
|
+
/** Closes every open object in `schema`, except direct `allOf` members. */
|
|
67
|
+
function closeObjects(schema) {
|
|
68
|
+
const closed = new WeakSet();
|
|
69
|
+
return mapSchema(schema, (node) => {
|
|
70
|
+
const out = Array.isArray(node.allOf)
|
|
71
|
+
? { ...node, allOf: node.allOf.map((member) => reopen(member, closed)) }
|
|
72
|
+
: node;
|
|
73
|
+
if (!isOpenObject(out)) {
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
const next = { ...out, additionalProperties: false };
|
|
77
|
+
closed.add(next);
|
|
78
|
+
return next;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function reopen(member, closed) {
|
|
82
|
+
if (!isSchemaObject(member) || !closed.has(member)) {
|
|
83
|
+
return member;
|
|
84
|
+
}
|
|
85
|
+
const { additionalProperties: _closed, ...open } = member;
|
|
86
|
+
return open;
|
|
87
|
+
}
|
|
88
|
+
// A node that combines subschemas gets properties from them, which its own
|
|
89
|
+
// `additionalProperties` would not see.
|
|
90
|
+
const COMBINATORS = ['allOf', 'anyOf', 'oneOf'];
|
|
91
|
+
function isOpenObject(node) {
|
|
92
|
+
if (Object.hasOwn(node, 'additionalProperties') ||
|
|
93
|
+
Object.hasOwn(node, 'unevaluatedProperties') ||
|
|
94
|
+
COMBINATORS.some((key) => Object.hasOwn(node, key))) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const { type } = node;
|
|
98
|
+
return (type === 'object' ||
|
|
99
|
+
(Array.isArray(type) && type.includes('object')) ||
|
|
100
|
+
(type === undefined && isSchemaObject(node.properties)));
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=close-inputs.js.map
|
|
@@ -0,0 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AAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACnD,OAAO,MAAM,CAAA;IACf,CAAC;IACD,MAAM,EAAE,oBAAoB,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAA;IACzD,OAAO,IAAI,CAAA;AACb,CAAC;AAED,2EAA2E;AAC3E,wCAAwC;AACxC,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAU,CAAA;AAExD,SAAS,YAAY,CAAC,IAAgB;IACpC,IACE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,sBAAsB,CAAC;QAC3C,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,uBAAuB,CAAC;QAC5C,WAAW,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,EACnD,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IACD,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,CAAA;IACrB,OAAO,CACL,IAAI,KAAK,QAAQ;QACjB,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAChD,CAAC,IAAI,KAAK,SAAS,IAAI,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CACxD,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { OpenAPIDocument } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* `constAsEnum: 'exceptTags'`: rewrite `const: v` as `enum: [v]`, except on
|
|
4
|
+
* discriminator tags, which generators read from `const`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function constAsEnum(document: OpenAPIDocument): void;
|
|
7
|
+
//# sourceMappingURL=const-enum.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const-enum.d.ts","sourceRoot":"","sources":["../src/const-enum.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAGjD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAW3D"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { mapDocumentSchemas, resolveComponent } from './document.js';
|
|
2
|
+
import { forEachSchema, isSchemaObject, mapSchema } from './walk.js';
|
|
3
|
+
/**
|
|
4
|
+
* `constAsEnum: 'exceptTags'`: rewrite `const: v` as `enum: [v]`, except on
|
|
5
|
+
* discriminator tags, which generators read from `const`.
|
|
6
|
+
*/
|
|
7
|
+
export function constAsEnum(document) {
|
|
8
|
+
const tags = tagSchemas(document);
|
|
9
|
+
mapDocumentSchemas(document, (schema) => mapSchema(schema, (node, source) => Object.hasOwn(node, 'const') && !tags.has(source)
|
|
10
|
+
? constToEnum(node)
|
|
11
|
+
: node));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The property schemas that tag a union: in a `oneOf` / `anyOf` of two or
|
|
15
|
+
* more objects (after following `$ref`s), a property that has a `const` in
|
|
16
|
+
* every variant, with a different value in each.
|
|
17
|
+
*/
|
|
18
|
+
function tagSchemas(document) {
|
|
19
|
+
const { schemas } = document.components;
|
|
20
|
+
const tags = new Set();
|
|
21
|
+
mapDocumentSchemas(document, (root) => {
|
|
22
|
+
forEachSchema(root, (node) => {
|
|
23
|
+
for (const key of ['oneOf', 'anyOf']) {
|
|
24
|
+
const variants = node[key];
|
|
25
|
+
if (Array.isArray(variants) && variants.length > 1) {
|
|
26
|
+
for (const tag of unionTags(variants, schemas)) {
|
|
27
|
+
tags.add(tag);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return root;
|
|
33
|
+
});
|
|
34
|
+
return tags;
|
|
35
|
+
}
|
|
36
|
+
function unionTags(variants, schemas) {
|
|
37
|
+
const objects = [];
|
|
38
|
+
for (const variant of variants) {
|
|
39
|
+
const resolved = isSchemaObject(variant)
|
|
40
|
+
? resolveComponent(schemas, variant)
|
|
41
|
+
: undefined;
|
|
42
|
+
if (resolved === undefined || !isSchemaObject(resolved.properties)) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
objects.push(resolved.properties);
|
|
46
|
+
}
|
|
47
|
+
const tags = [];
|
|
48
|
+
for (const name of Object.keys(objects[0] ?? {})) {
|
|
49
|
+
const props = objects.map((properties) => properties[name]);
|
|
50
|
+
if (!props.every(hasConst)) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const values = new Set(props.map((prop) => JSON.stringify(prop.const)));
|
|
54
|
+
if (values.size === props.length) {
|
|
55
|
+
tags.push(...props);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return tags;
|
|
59
|
+
}
|
|
60
|
+
function hasConst(value) {
|
|
61
|
+
return isSchemaObject(value) && Object.hasOwn(value, 'const');
|
|
62
|
+
}
|
|
63
|
+
/** `{ type, const: v, … }` → `{ type, enum: [v], … }`, keeping key order. */
|
|
64
|
+
function constToEnum(node) {
|
|
65
|
+
const out = {};
|
|
66
|
+
for (const [key, value] of Object.entries(node)) {
|
|
67
|
+
if (key === 'const') {
|
|
68
|
+
out.enum = [value];
|
|
69
|
+
}
|
|
70
|
+
else if (key !== 'enum') {
|
|
71
|
+
out[key] = value;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=const-enum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const-enum.js","sourceRoot":"","sources":["../src/const-enum.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAEpE,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAEpE;;;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,CAAA;AAC/D,CAAC;AAED,6EAA6E;AAC7E,SAAS,WAAW,CAAC,IAAgB;IACnC,MAAM,GAAG,GAAe,EAAE,CAAA;IAC1B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAChD,IAAI,GAAG,KAAK,OAAO,EAAE,CAAC;YACpB,GAAG,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAA;QACpB,CAAC;aAAM,IAAI,GAAG,KAAK,MAAM,EAAE,CAAC;YAC1B,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QAClB,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { OpenAPIDocument } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* `discriminators`: add `discriminator: { propertyName, mapping }` to every
|
|
4
|
+
* `oneOf` / `anyOf` whose variants are all whole-component `$ref`s with a
|
|
5
|
+
* tag (see `unionTag`). Each mapping value is the variant's `$ref`. A union
|
|
6
|
+
* that does not qualify (an inline variant, no common tag) is left as is.
|
|
7
|
+
*/
|
|
8
|
+
export declare function discriminators(document: OpenAPIDocument): void;
|
|
9
|
+
//# sourceMappingURL=discriminators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discriminators.d.ts","sourceRoot":"","sources":["../src/discriminators.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAGjD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAS9D"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { componentTarget, mapDocumentSchemas, resolveComponent, } from './document.js';
|
|
2
|
+
import { unionTag } from './tags.js';
|
|
3
|
+
import { isSchemaObject, mapSchema } from './walk.js';
|
|
4
|
+
/**
|
|
5
|
+
* `discriminators`: add `discriminator: { propertyName, mapping }` to every
|
|
6
|
+
* `oneOf` / `anyOf` whose variants are all whole-component `$ref`s with a
|
|
7
|
+
* tag (see `unionTag`). Each mapping value is the variant's `$ref`. A union
|
|
8
|
+
* that does not qualify (an inline variant, no common tag) is left as is.
|
|
9
|
+
*/
|
|
10
|
+
export function discriminators(document) {
|
|
11
|
+
const { schemas } = document.components;
|
|
12
|
+
mapDocumentSchemas(document, (schema) => mapSchema(schema, (node) => withDiscriminator(node, schemas)));
|
|
13
|
+
}
|
|
14
|
+
function withDiscriminator(node, schemas) {
|
|
15
|
+
if (Object.hasOwn(node, 'discriminator')) {
|
|
16
|
+
return node;
|
|
17
|
+
}
|
|
18
|
+
for (const key of ['oneOf', 'anyOf']) {
|
|
19
|
+
const refs = componentRefs(node[key]);
|
|
20
|
+
const variants = refs?.map((ref) => resolveComponent(schemas, { $ref: ref }));
|
|
21
|
+
if (refs === undefined || variants === undefined) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (!variants.every((variant) => variant !== undefined)) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const tag = unionTag(variants);
|
|
28
|
+
if (tag === undefined) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const mapping = Object.fromEntries(tag.values.map((value, index) => [value, refs[index]]));
|
|
32
|
+
return {
|
|
33
|
+
...node,
|
|
34
|
+
discriminator: { propertyName: tag.property, mapping },
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return node;
|
|
38
|
+
}
|
|
39
|
+
/** The `$ref` strings of a list made only of whole-component `$ref`s. */
|
|
40
|
+
function componentRefs(variants) {
|
|
41
|
+
if (!Array.isArray(variants)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const refs = [];
|
|
45
|
+
for (const variant of variants) {
|
|
46
|
+
if (!isSchemaObject(variant) || Object.keys(variant).length !== 1) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const target = componentTarget(variant.$ref);
|
|
50
|
+
if (target === undefined || target.rest !== '') {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
refs.push(variant.$ref);
|
|
54
|
+
}
|
|
55
|
+
return refs;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=discriminators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discriminators.js","sourceRoot":"","sources":["../src/discriminators.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAEpC,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErD;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,QAAyB;IACtD,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC,UAAU,CAAA;IACvC,kBAAkB,CAChB,QAAQ,EACR,CAAC,MAAM,EAAE,EAAE,CACT,SAAS,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CACzB,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CACnB,CAClB,CAAA;AACH,CAAC;AAED,SAAS,iBAAiB,CACxB,IAAgB,EAChB,OAAmC;IAEnC,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,eAAe,CAAC,EAAE,CAAC;QACzC,OAAO,IAAI,CAAA;IACb,CAAC;IACD,KAAK,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,OAAO,CAAU,EAAE,CAAC;QAC9C,MAAM,IAAI,GAAG,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;QACrC,MAAM,QAAQ,GAAG,IAAI,EAAE,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CACjC,gBAAgB,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE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AI,KAAK,EAAE,EAAE,CAAC;YAC/C,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAc,CAAC,CAAA;IACnC,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { JsonSchema } from '@ts-pf/docs';
|
|
2
|
+
import type { Side } from './normalize.js';
|
|
3
|
+
import type { OpenAPIDocument } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Replaces every schema root in the document with `map(root, side)`:
|
|
6
|
+
* component schemas and parameter schemas (no side), then each operation's
|
|
7
|
+
* request bodies (`'input'`) and response headers and bodies (`'output'`),
|
|
8
|
+
* then each `x-ts-pf-streams` entry's schemas.
|
|
9
|
+
*/
|
|
10
|
+
export declare function mapDocumentSchemas(document: OpenAPIDocument, map: (schema: JsonSchema, side: Side | undefined) => JsonSchema): void;
|
|
11
|
+
/** `#/components/schemas/<name>…` → the component name and the rest. */
|
|
12
|
+
export declare function componentTarget(ref: unknown): {
|
|
13
|
+
name: string;
|
|
14
|
+
rest: string;
|
|
15
|
+
} | undefined;
|
|
16
|
+
/** Follows whole-component `$ref`s from `schema` to the schema they name. */
|
|
17
|
+
export declare function resolveComponent(schemas: Record<string, JsonSchema>, schema: JsonSchema): JsonSchema | undefined;
|
|
18
|
+
//# sourceMappingURL=document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAYjD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,eAAe,EACzB,GAAG,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,GAAG,SAAS,KAAK,UAAU,GAC9D,IAAI,CA0CN;AAED,wEAAwE;AACxE,wBAAgB,eAAe,CAC7B,GAAG,EAAE,OAAO,GACX;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAS5C;AAED,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EACnC,MAAM,EAAE,UAAU,GACjB,UAAU,GAAG,SAAS,CAexB"}
|
package/dist/document.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const COMPONENT_REF = /^#\/components\/schemas\/([^/]+)(.*)$/;
|
|
2
|
+
// `x-ts-pf-streams` entry fields and the side each describes.
|
|
3
|
+
const STREAM_SIDES = {
|
|
4
|
+
input: 'input',
|
|
5
|
+
inputItem: 'input',
|
|
6
|
+
item: 'output',
|
|
7
|
+
output: 'output',
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Replaces every schema root in the document with `map(root, side)`:
|
|
11
|
+
* component schemas and parameter schemas (no side), then each operation's
|
|
12
|
+
* request bodies (`'input'`) and response headers and bodies (`'output'`),
|
|
13
|
+
* then each `x-ts-pf-streams` entry's schemas.
|
|
14
|
+
*/
|
|
15
|
+
export function mapDocumentSchemas(document, map) {
|
|
16
|
+
const { schemas, parameters } = document.components;
|
|
17
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
18
|
+
schemas[name] = map(schema, undefined);
|
|
19
|
+
}
|
|
20
|
+
for (const parameter of Object.values(parameters ?? {})) {
|
|
21
|
+
if (parameter.schema !== undefined) {
|
|
22
|
+
parameter.schema = map(parameter.schema, undefined);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
for (const item of Object.values(document.paths)) {
|
|
26
|
+
const operation = item.post;
|
|
27
|
+
if (operation === undefined) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
for (const media of Object.values(operation.requestBody?.content ?? {})) {
|
|
31
|
+
if (media.schema !== undefined) {
|
|
32
|
+
media.schema = map(media.schema, 'input');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const response of Object.values(operation.responses)) {
|
|
36
|
+
for (const header of Object.values(response.headers ?? {})) {
|
|
37
|
+
if (header.schema !== undefined) {
|
|
38
|
+
header.schema = map(header.schema, 'output');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (const media of Object.values(response.content ?? {})) {
|
|
42
|
+
if (media.schema !== undefined) {
|
|
43
|
+
media.schema = map(media.schema, 'output');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const entry of Object.values(document['x-ts-pf-streams'] ?? {})) {
|
|
49
|
+
for (const [field, side] of Object.entries(STREAM_SIDES)) {
|
|
50
|
+
const key = field;
|
|
51
|
+
const schema = entry[key];
|
|
52
|
+
if (schema !== undefined) {
|
|
53
|
+
entry[key] = map(schema, side);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** `#/components/schemas/<name>…` → the component name and the rest. */
|
|
59
|
+
export function componentTarget(ref) {
|
|
60
|
+
if (typeof ref !== 'string') {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const match = COMPONENT_REF.exec(ref);
|
|
64
|
+
if (match === null) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return { name: match[1] ?? '', rest: match[2] ?? '' };
|
|
68
|
+
}
|
|
69
|
+
/** Follows whole-component `$ref`s from `schema` to the schema they name. */
|
|
70
|
+
export function resolveComponent(schemas, schema) {
|
|
71
|
+
let current = schema;
|
|
72
|
+
const seen = new Set();
|
|
73
|
+
while (current !== undefined) {
|
|
74
|
+
const target = componentTarget(current.$ref);
|
|
75
|
+
if (target === undefined || target.rest !== '') {
|
|
76
|
+
return current;
|
|
77
|
+
}
|
|
78
|
+
if (seen.has(target.name)) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
seen.add(target.name);
|
|
82
|
+
current = schemas[target.name];
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=document.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document.js","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAIA,MAAM,aAAa,GAAG,uCAAuC,CAAA;AAE7D,8DAA8D;AAC9D,MAAM,YAAY,GAAG;IACnB,KAAK,EAAE,OAAO;IACd,SAAS,EAAE,OAAO;IAClB,IAAI,EAAE,QAAQ;IACd,MAAM,EAAE,QAAQ;CACuB,CAAA;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAAyB,EACzB,GAA+D;IAE/D,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,QAAQ,CAAC,UAAU,CAAA;IACnD,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACrD,OAAO,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IACxC,CAAC;IACD,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,CAAC;QACxD,IAAI,SAAS,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACnC,SAAS,CAAC,MAAM,GAAG,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;QACrD,CAAC;IACH,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACjD,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAA;QAC3B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AO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { OpenAPIDocument, OpenAPIOptions } from './types.js';
|
|
2
|
+
export type EnumNames = NonNullable<OpenAPIOptions['enumNames']>;
|
|
3
|
+
/**
|
|
4
|
+
* `enumNames`: every inline `enum` whose value set (order-insensitive)
|
|
5
|
+
* equals an entry's becomes a `$ref` to a component with the entry's name
|
|
6
|
+
* and values (in the entry's order). The node's other keywords (a
|
|
7
|
+
* `description`, a `default`) stay next to the `$ref`. Component roots are
|
|
8
|
+
* already named and are left alone; an existing component with the entry's
|
|
9
|
+
* name is reused when it is the same enum. Throws on an invalid or empty
|
|
10
|
+
* entry, two entries with one value set, or a name taken by another schema.
|
|
11
|
+
* Warns once about entries that matched nothing.
|
|
12
|
+
*/
|
|
13
|
+
export declare function enumNames(document: OpenAPIDocument, names: EnumNames): void;
|
|
14
|
+
//# sourceMappingURL=enum-names.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum-names.d.ts","sourceRoot":"","sources":["../src/enum-names.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAGjE,MAAM,MAAM,SAAS,GAAG,WAAW,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAA;AAEhE;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,eAAe,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAsD3E"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { mapDocumentSchemas } from './document.js';
|
|
2
|
+
import { idSchemaName } from './names.js';
|
|
3
|
+
import { componentRef } from './refs.js';
|
|
4
|
+
import { forEachSchema, mapSchema } from './walk.js';
|
|
5
|
+
/**
|
|
6
|
+
* `enumNames`: every inline `enum` whose value set (order-insensitive)
|
|
7
|
+
* equals an entry's becomes a `$ref` to a component with the entry's name
|
|
8
|
+
* and values (in the entry's order). The node's other keywords (a
|
|
9
|
+
* `description`, a `default`) stay next to the `$ref`. Component roots are
|
|
10
|
+
* already named and are left alone; an existing component with the entry's
|
|
11
|
+
* name is reused when it is the same enum. Throws on an invalid or empty
|
|
12
|
+
* entry, two entries with one value set, or a name taken by another schema.
|
|
13
|
+
* Warns once about entries that matched nothing.
|
|
14
|
+
*/
|
|
15
|
+
export function enumNames(document, names) {
|
|
16
|
+
const byValues = nameByValues(names);
|
|
17
|
+
if (byValues.size === 0) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const isMatch = (node) => Array.isArray(node.enum) && byValues.has(valueSetKey(node.enum));
|
|
21
|
+
const nameOf = (node) => byValues.get(valueSetKey(node.enum)) ?? '';
|
|
22
|
+
// First pass: the `type` each name's matches agree on, if any.
|
|
23
|
+
const types = new Map();
|
|
24
|
+
forEachInline(document, (node) => {
|
|
25
|
+
if (isMatch(node)) {
|
|
26
|
+
const name = nameOf(node);
|
|
27
|
+
types.set(name, [...(types.get(name) ?? []), node.type]);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
const { schemas } = document.components;
|
|
31
|
+
for (const [name, seen] of types) {
|
|
32
|
+
const body = enumBody(seen, names[name] ?? []);
|
|
33
|
+
const existing = schemas[name];
|
|
34
|
+
if (existing === undefined) {
|
|
35
|
+
schemas[name] = body;
|
|
36
|
+
}
|
|
37
|
+
else if (!sameEnum(existing, body)) {
|
|
38
|
+
throw new Error(`openapi(): enumNames "${name}" is already a component with a different schema. Rename the entry.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
mapDocumentSchemas(document, (root, side) => mapSchema(root, (node, source) => {
|
|
42
|
+
if ((side === undefined && source === root) || !isMatch(node)) {
|
|
43
|
+
return node;
|
|
44
|
+
}
|
|
45
|
+
const name = nameOf(node);
|
|
46
|
+
const { enum: _values, ...rest } = node;
|
|
47
|
+
if (schemas[name]?.type !== undefined) {
|
|
48
|
+
delete rest.type;
|
|
49
|
+
}
|
|
50
|
+
return { ...componentRef(name), ...rest };
|
|
51
|
+
}));
|
|
52
|
+
const unused = Object.keys(names).filter((name) => !types.has(name));
|
|
53
|
+
if (unused.length > 0) {
|
|
54
|
+
console.warn(`openapi(): enumNames entries matched no inline enum: ${unused.join(', ')}.`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Value-set key → entry name, after validating every entry. */
|
|
58
|
+
function nameByValues(names) {
|
|
59
|
+
const byValues = new Map();
|
|
60
|
+
for (const [name, values] of Object.entries(names)) {
|
|
61
|
+
if (idSchemaName(name) !== name) {
|
|
62
|
+
throw new Error(`openapi(): enumNames "${name}" is not a valid component name (use [A-Za-z0-9._-]).`);
|
|
63
|
+
}
|
|
64
|
+
if (values.length === 0) {
|
|
65
|
+
throw new Error(`openapi(): enumNames "${name}" has no values.`);
|
|
66
|
+
}
|
|
67
|
+
const key = valueSetKey(values);
|
|
68
|
+
const other = byValues.get(key);
|
|
69
|
+
if (other !== undefined) {
|
|
70
|
+
throw new Error(`openapi(): enumNames "${other}" and "${name}" have the same values. Keep one.`);
|
|
71
|
+
}
|
|
72
|
+
byValues.set(key, name);
|
|
73
|
+
}
|
|
74
|
+
return byValues;
|
|
75
|
+
}
|
|
76
|
+
/** Calls `visit` with every schema below the component and parameter roots. */
|
|
77
|
+
function forEachInline(document, visit) {
|
|
78
|
+
mapDocumentSchemas(document, (root, side) => {
|
|
79
|
+
forEachSchema(root, (node) => {
|
|
80
|
+
if (side !== undefined || node !== root) {
|
|
81
|
+
visit(node);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return root;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function enumBody(types, values) {
|
|
88
|
+
const type = types[0];
|
|
89
|
+
const agreed = type !== undefined &&
|
|
90
|
+
types.every((other) => JSON.stringify(other) === JSON.stringify(type));
|
|
91
|
+
return agreed ? { type, enum: [...values] } : { enum: [...values] };
|
|
92
|
+
}
|
|
93
|
+
function sameEnum(existing, body) {
|
|
94
|
+
const keys = Object.keys(existing).sort().join();
|
|
95
|
+
return (Array.isArray(existing.enum) &&
|
|
96
|
+
keys === Object.keys(body).sort().join() &&
|
|
97
|
+
valueSetKey(existing.enum) === valueSetKey(body.enum) &&
|
|
98
|
+
JSON.stringify(existing.type) === JSON.stringify(body.type));
|
|
99
|
+
}
|
|
100
|
+
function valueSetKey(values) {
|
|
101
|
+
return JSON.stringify([...new Set(values.map((value) => JSON.stringify(value)))].sort());
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=enum-names.js.map
|