@bagelink/sdk 1.4.22 → 1.4.28

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.
@@ -1,4 +1,5 @@
1
- import type { SchemaObject } from './openApiTypes'
1
+ import type { ReferenceObject, SchemaObject } from './types'
2
+ import { isReferenceObject, isSchemaObject } from './types/utils'
2
3
 
3
4
  export function toCamelCase(str?: string) {
4
5
  return str
@@ -31,7 +32,7 @@ function resolveReference(ref: string): string {
31
32
 
32
33
  function handleArrayItems(schema: SchemaObject): string {
33
34
  // Handle array items that might be union types
34
- const itemType = schemaToType(schema.items)
35
+ const itemType = schemaToType(schema.items as SchemaObject)
35
36
 
36
37
  // Add parentheses around union types to ensure correct syntax
37
38
  const needsParentheses = itemType.includes('|') || itemType.includes('&')
@@ -51,8 +52,16 @@ function handleArrayItems(schema: SchemaObject): string {
51
52
  return res
52
53
  }
53
54
 
54
- export function schemaToType(schema?: SchemaObject): string {
55
+ export function schemaToType(schema?: SchemaObject | ReferenceObject): string {
55
56
  if (!schema) return 'any'
57
+ if (isReferenceObject(schema)) {
58
+ if (schema.$ref) return resolveReference(schema.$ref)
59
+ }
60
+ if (!isSchemaObject(schema)) {
61
+ console.warn('Schema is not a SchemaObject:', schema)
62
+ return 'any'
63
+ }
64
+
56
65
  if (schema.anyOf) {
57
66
  let _t = schema.anyOf
58
67
  .map(s => schemaToType(s))
@@ -67,7 +76,7 @@ export function schemaToType(schema?: SchemaObject): string {
67
76
  .filter(p => p !== 'any')
68
77
  .join(' & ')
69
78
  }
70
- if (schema.$ref) return resolveReference(schema.$ref)
79
+ // TODO: handle Array.isArray(schema.type) case b4 switch
71
80
  switch (schema.type) {
72
81
  case 'object': return '{ [key: string]: any }'
73
82
  case 'string':
@@ -86,14 +95,14 @@ export function schemaToType(schema?: SchemaObject): string {
86
95
  }
87
96
  }
88
97
 
89
- export function isOptional(schema: SchemaObject) {
98
+ export function isOptional(schema?: SchemaObject) {
90
99
  /// / !schema?.required?.includes(schema.title || '')
91
100
  const type = schemaToType(schema)
92
101
  const splitType = type.split(/\s+\|\s+/)
93
102
  const includesNull = splitType.includes('null')
94
103
  const includesUndefined = splitType.includes('undefined')
95
104
 
96
- return includesNull || includesUndefined || schema.default !== undefined
105
+ return includesNull || includesUndefined || schema?.default !== undefined
97
106
  }
98
107
 
99
108
  export function cleanOptionals(str: string) {
@@ -110,7 +119,7 @@ export function formatVarType({
110
119
  defaultValue,
111
120
  }: {
112
121
  varName: string
113
- schema: SchemaObject
122
+ schema?: SchemaObject
114
123
  required?: boolean
115
124
  defaultValue?: unknown
116
125
  }) {
@@ -1,96 +0,0 @@
1
- export interface OpenAPIDocument {
2
- openapi: string
3
- info: InfoObject
4
- paths: PathsObject
5
- components?: ComponentsObject
6
- }
7
-
8
- export interface PathsObject { [path: string]: PathItemObject }
9
-
10
- export interface InfoObject {
11
- title: string
12
- version: string
13
- }
14
-
15
- export interface PathItemObject {
16
- get?: OperationObject
17
- put?: OperationObject
18
- post?: OperationObject
19
- delete?: OperationObject
20
- patch?: OperationObject
21
- options?: OperationObject
22
- head?: OperationObject
23
- }
24
-
25
- export interface SchemasObject { [schema: string]: SchemaObject }
26
-
27
- export interface ComponentsObject {
28
- schemas?: SchemasObject
29
- }
30
-
31
- export interface OperationObject {
32
- summary?: string
33
- description?: string
34
- operationId?: string
35
- parameters?: ParameterObject[]
36
- requestBody?: RequestBodyObject
37
- responses: ResponsesObject
38
- }
39
-
40
- export interface ResponsesObject { [statusCode: string]: ResponseObject }
41
-
42
- export interface SchemaObject {
43
- type?: string
44
- const?: string
45
- format?: string
46
- properties?: { [property: string]: SchemaObject }
47
- prefixItems?: SchemaObject[]
48
- items?: SchemaObject
49
- $ref?: string
50
- enum?: string[]
51
- anyOf?: SchemaObject[]
52
- allOf?: SchemaObject[]
53
- title?: string
54
- description?: string
55
- default?: any
56
- required?: string[]
57
- additionalProperties?: boolean | SchemaObject
58
- }
59
-
60
- export interface ParameterObject {
61
- name: string
62
- in: string
63
- description?: string
64
- schema: SchemaObject
65
- required?: boolean
66
- }
67
-
68
- export interface RequestBodyObject {
69
- description?: string
70
- content: { [mediaType: string]: MediaTypeObject }
71
- required?: boolean
72
- }
73
-
74
- export interface ResponseObject {
75
- description: string
76
- content?: { [mediaType: string]: MediaTypeObject }
77
- }
78
-
79
- export interface MediaTypeObject {
80
- schema?: SchemaObject
81
- encoding?: { [key: string]: EncodingObject }
82
- }
83
-
84
- export interface EncodingObject {
85
- contentType?: string
86
- headers?: { [key: string]: HeaderObject }
87
- style?: string
88
- explode?: boolean
89
- allowReserved?: boolean
90
- }
91
-
92
- export interface HeaderObject {
93
- description?: string
94
- required?: boolean
95
- schema?: SchemaObject
96
- }