@kubb/adapter-oas 5.0.0-alpha.4 → 5.0.0-alpha.40

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/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
@@ -1 +1,18 @@
1
1
  export { adapterOas, adapterOasName } from './adapter.ts'
2
+ export { mergeDocuments, parseDocument, parseFromConfig, validateDocument } from './factory.ts'
3
+ export { HttpMethods } from './types.ts'
4
+ export type {
5
+ AdapterOas,
6
+ AdapterOasOptions,
7
+ AdapterOasResolvedOptions,
8
+ ContentType,
9
+ DiscriminatorObject,
10
+ Document,
11
+ HttpMethod,
12
+ MediaTypeObject,
13
+ Operation,
14
+ ReferenceObject,
15
+ ResponseObject,
16
+ SchemaObject,
17
+ } from './types.ts'
18
+ export type { ParseOptions, ValidateDocumentOptions } from './factory.ts'