@kubb/adapter-oas 5.0.0-alpha.2 → 5.0.0-alpha.20
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/index.cjs +989 -1070
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +83 -128
- package/dist/index.js +988 -1069
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/adapter.ts +79 -63
- package/src/constants.ts +63 -63
- package/src/discriminator.ts +99 -0
- package/src/factory.ts +151 -0
- package/src/guards.ts +68 -0
- package/src/index.ts +1 -0
- package/src/parser.ts +352 -667
- package/src/refs.ts +57 -0
- package/src/resolvers.ts +490 -0
- package/src/types.ts +178 -56
- package/src/oas/Oas.ts +0 -616
- package/src/oas/resolveServerUrl.ts +0 -47
- package/src/oas/types.ts +0 -52
- package/src/oas/utils.ts +0 -402
package/src/guards.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
|
|
2
|
+
import { isPlainObject } from 'remeda'
|
|
3
|
+
import type { DiscriminatorObject, SchemaObject } from './types.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns `true` when `doc` is a Swagger 2.0 document (no `openapi` key).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* if (isOpenApiV2Document(doc)) {
|
|
11
|
+
* // doc is OpenAPIV2.Document
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function isOpenApiV2Document(doc: unknown): doc is OpenAPIV2.Document {
|
|
16
|
+
return !!doc && isPlainObject(doc) && !('openapi' in doc)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns `true` when a schema should be treated as nullable.
|
|
21
|
+
*
|
|
22
|
+
* Recognizes all nullable signals across OAS versions: `nullable: true` (OAS 3.0),
|
|
23
|
+
* `x-nullable: true` (vendor extension), `type: 'null'`, and `type: ['null', ...]` (OAS 3.1).
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* isNullable({ type: 'string', nullable: true }) // true
|
|
28
|
+
* isNullable({ type: ['string', 'null'] }) // true
|
|
29
|
+
* isNullable({ type: 'string' }) // false
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {
|
|
33
|
+
const explicitNullable = schema?.nullable ?? schema?.['x-nullable']
|
|
34
|
+
if (explicitNullable === true) return true
|
|
35
|
+
|
|
36
|
+
const schemaType = schema?.type
|
|
37
|
+
if (schemaType === 'null') return true
|
|
38
|
+
if (Array.isArray(schemaType)) return schemaType.includes('null')
|
|
39
|
+
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns `true` when `obj` is an OpenAPI `$ref` pointer object.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* isReference({ $ref: '#/components/schemas/Pet' }) // true
|
|
49
|
+
* isReference({ type: 'string' }) // false
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function isReference(obj?: unknown): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {
|
|
53
|
+
return !!obj && typeof obj === 'object' && '$ref' in obj
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns `true` when `obj` is a schema with a structured OAS 3.x `discriminator` object.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* isDiscriminator({ discriminator: { propertyName: 'type', mapping: {} } }) // true
|
|
62
|
+
* isDiscriminator({ discriminator: 'type' }) // false (Swagger 2 string form)
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function isDiscriminator(obj?: unknown): obj is SchemaObject & { discriminator: DiscriminatorObject } {
|
|
66
|
+
const record = obj as Record<string, unknown>
|
|
67
|
+
return !!obj && !!record['discriminator'] && typeof record['discriminator'] !== 'string'
|
|
68
|
+
}
|
package/src/index.ts
CHANGED