@amritk/helpers 0.13.5 → 0.14.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/dist/fold-nullable.d.ts +19 -0
- package/dist/fold-nullable.js +42 -0
- package/dist/generate-type-definition.d.ts +8 -0
- package/dist/generate-type-definition.js +251 -184
- package/dist/walk-ref-graph.js +9 -1
- package/package.json +5 -1
- package/src/fold-nullable.ts +60 -0
- package/src/generate-type-definition.ts +416 -231
- package/src/walk-ref-graph.ts +18 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rewrites OpenAPI 3.0's `nullable: true` into the JSON Schema form the
|
|
3
|
+
* generators already enforce: a `null` member of the node's `type`.
|
|
4
|
+
*
|
|
5
|
+
* OpenAPI 3.0 has no `type: [...]` — it spells "or null" as a sibling boolean.
|
|
6
|
+
* Every generator here reads `type`, so an unfolded document produced parsers
|
|
7
|
+
* that rejected `null` outright (strict) or coerced it to a default (non-strict)
|
|
8
|
+
* while the schema declared it valid. Folding once, at the document level, means
|
|
9
|
+
* no downstream path needs to know the keyword exists.
|
|
10
|
+
*
|
|
11
|
+
* A node with `nullable: true` but no `type` is left alone: there is no type
|
|
12
|
+
* list to extend, and OpenAPI 3.0 ignores keywords alongside a `$ref` anyway.
|
|
13
|
+
* The `nullable` key itself is kept so the type generator can still widen the
|
|
14
|
+
* emitted TypeScript with `| null`.
|
|
15
|
+
*
|
|
16
|
+
* Returns the input unchanged (same reference) when the document contains no
|
|
17
|
+
* `nullable: true`, so the common case allocates nothing.
|
|
18
|
+
*/
|
|
19
|
+
export const foldNullable = <T>(schema: T): T => {
|
|
20
|
+
const folded = fold(schema)
|
|
21
|
+
return folded as T
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const fold = (node: unknown): unknown => {
|
|
25
|
+
if (Array.isArray(node)) {
|
|
26
|
+
let changed = false
|
|
27
|
+
const next = node.map((item) => {
|
|
28
|
+
const folded = fold(item)
|
|
29
|
+
if (folded !== item) changed = true
|
|
30
|
+
return folded
|
|
31
|
+
})
|
|
32
|
+
return changed ? next : node
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof node !== 'object' || node === null) return node
|
|
36
|
+
|
|
37
|
+
const record = node as Record<string, unknown>
|
|
38
|
+
let changed = false
|
|
39
|
+
const next: Record<string, unknown> = {}
|
|
40
|
+
|
|
41
|
+
for (const key in record) {
|
|
42
|
+
const value = record[key]
|
|
43
|
+
const folded = fold(value)
|
|
44
|
+
if (folded !== value) changed = true
|
|
45
|
+
next[key] = folded
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const type = record['type']
|
|
49
|
+
if (record['nullable'] === true) {
|
|
50
|
+
if (typeof type === 'string' && type !== 'null') {
|
|
51
|
+
next['type'] = [type, 'null']
|
|
52
|
+
changed = true
|
|
53
|
+
} else if (Array.isArray(type) && !type.includes('null')) {
|
|
54
|
+
next['type'] = [...type, 'null']
|
|
55
|
+
changed = true
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return changed ? next : node
|
|
60
|
+
}
|