@bagelink/sdk 1.6.47 → 1.6.51
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/bin/index.ts +1 -1
- package/bin/splitClientGen.ts +40 -40
- package/dist/index.cjs +7 -7
- package/dist/index.mjs +7 -7
- package/package.json +1 -1
- package/src/openAPITools/index.ts +2 -2
- package/src/openAPITools/types/utils.ts +2 -2
- package/src/openAPITools/utils.ts +11 -11
- package/src/utils.ts +1 -1
package/bin/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ export async function loadTypes() {
|
|
|
14
14
|
'import.meta.env.VITE_BAGEL_BASE_URL'
|
|
15
15
|
)
|
|
16
16
|
|
|
17
|
-
if (!fs.existsSync(bagelinkDir)) {fs.mkdirSync(bagelinkDir)}
|
|
17
|
+
if (!fs.existsSync(bagelinkDir)) { fs.mkdirSync(bagelinkDir) }
|
|
18
18
|
|
|
19
19
|
// Generate monolithic files first
|
|
20
20
|
const typesPath = join(bagelinkDir, 'types.d.ts')
|
package/bin/splitClientGen.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { formatAndWriteCode } from './utils'
|
|
|
10
10
|
* @returns The camelCase version of the string
|
|
11
11
|
*/
|
|
12
12
|
function toCamelCase(str: string): string {
|
|
13
|
-
if (!str) {return ''}
|
|
13
|
+
if (!str) { return '' }
|
|
14
14
|
return str.charAt(0).toLowerCase() + str.slice(1)
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -67,7 +67,7 @@ export function createDefaultGroupingStrategy(groupingPattern = /^([A-Z][a-z]+)/
|
|
|
67
67
|
|
|
68
68
|
getFileName(group: string, kind: 'types' | 'api'): string {
|
|
69
69
|
// Use the utility function for consistent camelCase conversion
|
|
70
|
-
const suffix = 'types'
|
|
70
|
+
const suffix = kind === 'types' ? 'Types' : 'Api'
|
|
71
71
|
return `${toCamelCase(group)}${suffix}.ts`
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -108,8 +108,8 @@ export async function splitClientCode(options: SplitClientOptions): Promise<void
|
|
|
108
108
|
const apisDir = useDirectories ? path.join(bagelinkDir, 'api') : bagelinkDir
|
|
109
109
|
|
|
110
110
|
if (useDirectories) {
|
|
111
|
-
if (!fs.existsSync(typesDir)) {fs.mkdirSync(typesDir, { recursive: true })}
|
|
112
|
-
if (!fs.existsSync(apisDir)) {fs.mkdirSync(apisDir, { recursive: true })}
|
|
111
|
+
if (!fs.existsSync(typesDir)) { fs.mkdirSync(typesDir, { recursive: true }) }
|
|
112
|
+
if (!fs.existsSync(apisDir)) { fs.mkdirSync(apisDir, { recursive: true }) }
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
// Read the generated files
|
|
@@ -152,7 +152,7 @@ export async function splitClientCode(options: SplitClientOptions): Promise<void
|
|
|
152
152
|
// Generate and write files for each group
|
|
153
153
|
for (const [group, info] of Object.entries(groups)) {
|
|
154
154
|
// Write types file
|
|
155
|
-
if (
|
|
155
|
+
if (info.types.size > 0) {
|
|
156
156
|
const dependencies = getDependencyImports(info.dependencies, groups, 'types')
|
|
157
157
|
const typesOutput = generateTypesFile(group, info.types, typesContent, dependencies)
|
|
158
158
|
const typesFileName = useDirectories
|
|
@@ -163,7 +163,7 @@ export async function splitClientCode(options: SplitClientOptions): Promise<void
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
// Write API file
|
|
166
|
-
if (
|
|
166
|
+
if (info.apis.size > 0) {
|
|
167
167
|
const dependencies = getDependencyImports(info.dependencies, groups, 'apis')
|
|
168
168
|
const apisOutput = generateApiFile(
|
|
169
169
|
group,
|
|
@@ -186,7 +186,7 @@ export async function splitClientCode(options: SplitClientOptions): Promise<void
|
|
|
186
186
|
if (useDirectories) {
|
|
187
187
|
// Types index
|
|
188
188
|
const typesIndexContent = Object.keys(groups)
|
|
189
|
-
.filter(group =>
|
|
189
|
+
.filter(group => groups[group].types.size > 0)
|
|
190
190
|
.map(group => `export * from './${toCamelCase(group)}Types';`)
|
|
191
191
|
.join('\n')
|
|
192
192
|
|
|
@@ -199,7 +199,7 @@ export async function splitClientCode(options: SplitClientOptions): Promise<void
|
|
|
199
199
|
const apiIndexContent
|
|
200
200
|
= `export { axios } from './axios';\n${
|
|
201
201
|
Object.keys(groups)
|
|
202
|
-
.filter(group =>
|
|
202
|
+
.filter(group => groups[group].apis.size > 0)
|
|
203
203
|
.map(group => `export * from './${toCamelCase(group)}Api';`)
|
|
204
204
|
.join('\n')}`
|
|
205
205
|
|
|
@@ -218,10 +218,10 @@ export async function splitClientCode(options: SplitClientOptions): Promise<void
|
|
|
218
218
|
const indexContent = Object.keys(groups)
|
|
219
219
|
.map((group) => {
|
|
220
220
|
const exports = []
|
|
221
|
-
if (
|
|
221
|
+
if (groups[group].types.size > 0) {
|
|
222
222
|
exports.push(`export * from './${toCamelCase(group)}Types';`)
|
|
223
223
|
}
|
|
224
|
-
if (
|
|
224
|
+
if (groups[group].apis.size > 0) {
|
|
225
225
|
exports.push(`export * from './${toCamelCase(group)}Api';`)
|
|
226
226
|
}
|
|
227
227
|
return exports.join('\n')
|
|
@@ -256,20 +256,20 @@ function extractCommonCode(apiContent: string): { commonCode: string, axiosInsta
|
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
// Get imports
|
|
259
|
-
if ('ImportDeclaration'
|
|
259
|
+
if (node.type === 'ImportDeclaration') {
|
|
260
260
|
imports.push(apiContent.substring(node.range[0], node.range[1]))
|
|
261
261
|
}
|
|
262
262
|
|
|
263
263
|
// Get axios setup
|
|
264
|
-
if ('ExportNamedDeclaration'
|
|
265
|
-
&&
|
|
266
|
-
&& node.declaration.declarations.some(d =>
|
|
264
|
+
if (node.type === 'ExportNamedDeclaration'
|
|
265
|
+
&& node.declaration?.type === 'VariableDeclaration'
|
|
266
|
+
&& node.declaration.declarations.some(d => d.id.type === 'Identifier' && d.id.name === 'axios')) {
|
|
267
267
|
axiosDefinition.push(apiContent.substring(node.range[0], node.range[1]))
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
// Get utility interfaces like UploadOptions
|
|
271
|
-
if ('ExportNamedDeclaration'
|
|
272
|
-
&&
|
|
271
|
+
if (node.type === 'ExportNamedDeclaration'
|
|
272
|
+
&& node.declaration?.type === 'TSInterfaceDeclaration') {
|
|
273
273
|
utilityTypes.push(apiContent.substring(node.range[0], node.range[1]))
|
|
274
274
|
}
|
|
275
275
|
}
|
|
@@ -296,11 +296,11 @@ function organizeTypes(typesContent: string, groupingStrategy: GroupingStrategy)
|
|
|
296
296
|
|
|
297
297
|
for (const node of ast.body) {
|
|
298
298
|
// Skip nodes without range info
|
|
299
|
-
if (!node.range) {continue}
|
|
299
|
+
if (!node.range) { continue }
|
|
300
300
|
|
|
301
|
-
if ('ExportNamedDeclaration'
|
|
302
|
-
&& (
|
|
303
|
-
||
|
|
301
|
+
if (node.type === 'ExportNamedDeclaration'
|
|
302
|
+
&& (node.declaration?.type === 'TSTypeAliasDeclaration'
|
|
303
|
+
|| node.declaration?.type === 'TSInterfaceDeclaration')) {
|
|
304
304
|
const typeName = node.declaration.id.name
|
|
305
305
|
const group = groupingStrategy.getGroupName(typeName)
|
|
306
306
|
|
|
@@ -334,12 +334,12 @@ function organizeApiFunctions(apiContent: string, groupingStrategy: GroupingStra
|
|
|
334
334
|
|
|
335
335
|
// First pass: identify top-level exports
|
|
336
336
|
for (const node of ast.body) {
|
|
337
|
-
if (!node.range) {continue}
|
|
337
|
+
if (!node.range) { continue }
|
|
338
338
|
|
|
339
339
|
// Handle direct function exports
|
|
340
|
-
if (
|
|
340
|
+
if (node.type === 'ExportNamedDeclaration' && node.declaration?.type === 'VariableDeclaration') {
|
|
341
341
|
for (const declarator of node.declaration.declarations) {
|
|
342
|
-
if (
|
|
342
|
+
if (declarator.id.type === 'Identifier') {
|
|
343
343
|
const functionName = declarator.id.name
|
|
344
344
|
|
|
345
345
|
// Skip utility exports
|
|
@@ -348,7 +348,7 @@ function organizeApiFunctions(apiContent: string, groupingStrategy: GroupingStra
|
|
|
348
348
|
}
|
|
349
349
|
|
|
350
350
|
// Check if this is a nested API object (like `auth: {...}` or `dataExports: {...}`)
|
|
351
|
-
if (
|
|
351
|
+
if (declarator.init?.type === 'ObjectExpression') {
|
|
352
352
|
// For object groups, use the object name as the group name directly
|
|
353
353
|
// rather than applying the grouping strategy
|
|
354
354
|
const groupName = functionName
|
|
@@ -523,11 +523,11 @@ function findTypeDependencies(node: ProgramStatement, _typesContent: string): st
|
|
|
523
523
|
|
|
524
524
|
// Type guard functions for type safety
|
|
525
525
|
function isTypeReference(node: unknown): node is TSESTree.TSTypeReference {
|
|
526
|
-
return
|
|
526
|
+
return node !== null && typeof node === 'object' && 'type' in node && node.type === AST_NODE_TYPES.TSTypeReference
|
|
527
527
|
}
|
|
528
528
|
|
|
529
529
|
function isIdentifier(node: unknown): node is TSESTree.Identifier {
|
|
530
|
-
return
|
|
530
|
+
return node !== null && typeof node === 'object' && 'type' in node && node.type === AST_NODE_TYPES.Identifier
|
|
531
531
|
}
|
|
532
532
|
|
|
533
533
|
// Simple approach - extract all identifiers from type references
|
|
@@ -540,7 +540,7 @@ function findTypeDependencies(node: ProgramStatement, _typesContent: string): st
|
|
|
540
540
|
}
|
|
541
541
|
|
|
542
542
|
// Recursively visit child nodes
|
|
543
|
-
if (node && 'object'
|
|
543
|
+
if (node && typeof node === 'object') {
|
|
544
544
|
// Handle arrays
|
|
545
545
|
if (Array.isArray(node)) {
|
|
546
546
|
node.forEach((item) => { visit(item) })
|
|
@@ -552,7 +552,7 @@ function findTypeDependencies(node: ProgramStatement, _typesContent: string): st
|
|
|
552
552
|
|
|
553
553
|
// Visit each property
|
|
554
554
|
Object.entries(node).forEach(([key, value]) => {
|
|
555
|
-
if (!skipProps.includes(key) && value && 'object'
|
|
555
|
+
if (!skipProps.includes(key) && value && typeof value === 'object') {
|
|
556
556
|
visit(value)
|
|
557
557
|
}
|
|
558
558
|
})
|
|
@@ -610,11 +610,11 @@ function generateTypesFile(
|
|
|
610
610
|
const declarations: string[] = []
|
|
611
611
|
for (const node of ast.body) {
|
|
612
612
|
// Skip nodes without range info
|
|
613
|
-
if (!node.range) {continue}
|
|
613
|
+
if (!node.range) { continue }
|
|
614
614
|
|
|
615
|
-
if ('ExportNamedDeclaration'
|
|
616
|
-
&& (
|
|
617
|
-
||
|
|
615
|
+
if (node.type === 'ExportNamedDeclaration'
|
|
616
|
+
&& (node.declaration?.type === 'TSTypeAliasDeclaration'
|
|
617
|
+
|| node.declaration?.type === 'TSInterfaceDeclaration')) {
|
|
618
618
|
const typeName = node.declaration.id.name
|
|
619
619
|
if (typeNames.has(typeName)) {
|
|
620
620
|
declarations.push(typesContent.substring(node.range[0], node.range[1]))
|
|
@@ -658,16 +658,16 @@ function generateApiFile(
|
|
|
658
658
|
if (useDirectories) {
|
|
659
659
|
Object.entries(dependencies).forEach(([depGroup, types]) => {
|
|
660
660
|
// Filter out AxiosResponse as we're importing it directly
|
|
661
|
-
const filteredTypes = types.filter(t => 'AxiosResponse'
|
|
662
|
-
if (
|
|
661
|
+
const filteredTypes = types.filter(t => t !== 'AxiosResponse')
|
|
662
|
+
if (filteredTypes.length > 0) {
|
|
663
663
|
imports.push(`import type { ${filteredTypes.sort().join(', ')} } from '../types/${toCamelCase(depGroup)}Types';`)
|
|
664
664
|
}
|
|
665
665
|
})
|
|
666
666
|
} else {
|
|
667
667
|
Object.entries(dependencies).forEach(([depGroup, types]) => {
|
|
668
668
|
// Filter out AxiosResponse as we're importing it directly
|
|
669
|
-
const filteredTypes = types.filter(t => 'AxiosResponse'
|
|
670
|
-
if (
|
|
669
|
+
const filteredTypes = types.filter(t => t !== 'AxiosResponse')
|
|
670
|
+
if (filteredTypes.length > 0) {
|
|
671
671
|
imports.push(`import type { ${filteredTypes.sort().join(', ')} } from './${toCamelCase(depGroup)}Types';`)
|
|
672
672
|
}
|
|
673
673
|
})
|
|
@@ -677,12 +677,12 @@ function generateApiFile(
|
|
|
677
677
|
const declarations: string[] = []
|
|
678
678
|
|
|
679
679
|
for (const node of ast.body) {
|
|
680
|
-
if (!node.range) {continue}
|
|
680
|
+
if (!node.range) { continue }
|
|
681
681
|
|
|
682
682
|
// Handle export declarations
|
|
683
|
-
if (
|
|
683
|
+
if (node.type === 'ExportNamedDeclaration' && node.declaration?.type === 'VariableDeclaration') {
|
|
684
684
|
for (const declarator of node.declaration.declarations) {
|
|
685
|
-
if (
|
|
685
|
+
if (declarator.id.type === 'Identifier') {
|
|
686
686
|
const functionName = declarator.id.name
|
|
687
687
|
|
|
688
688
|
// Check if this function should be included
|
|
@@ -698,7 +698,7 @@ function generateApiFile(
|
|
|
698
698
|
&& !comments.some(c => comment.range[1] < c.range[1] && c.range[1] <= node.range[0])
|
|
699
699
|
)
|
|
700
700
|
|
|
701
|
-
if (
|
|
701
|
+
if (nodeLeadingComments.length > 0) {
|
|
702
702
|
startPos = nodeLeadingComments[0].range[0]
|
|
703
703
|
}
|
|
704
704
|
|
package/dist/index.cjs
CHANGED
|
@@ -68,14 +68,14 @@ function schemaToType(schema) {
|
|
|
68
68
|
return "any";
|
|
69
69
|
}
|
|
70
70
|
if (schema.anyOf) {
|
|
71
|
-
let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => "any"
|
|
72
|
-
if (""
|
|
71
|
+
let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" | ");
|
|
72
|
+
if (_t === "" || _t === "null") {
|
|
73
73
|
_t = "any";
|
|
74
74
|
}
|
|
75
75
|
return _t;
|
|
76
76
|
}
|
|
77
77
|
if (schema.allOf) {
|
|
78
|
-
return schema.allOf.map((s) => schemaToType(s)).filter((p) => "any"
|
|
78
|
+
return schema.allOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" & ");
|
|
79
79
|
}
|
|
80
80
|
switch (schema.type) {
|
|
81
81
|
case "object":
|
|
@@ -112,7 +112,7 @@ function isOptional(schema) {
|
|
|
112
112
|
return includesNull || includesUndefined || schema?.default !== void 0;
|
|
113
113
|
}
|
|
114
114
|
function cleanOptionals(str) {
|
|
115
|
-
return str.split(" | ").filter((t) => "null"
|
|
115
|
+
return str.split(" | ").filter((t) => t !== "null" && t !== "undefined").join(" | ");
|
|
116
116
|
}
|
|
117
117
|
function formatVarType({
|
|
118
118
|
varName,
|
|
@@ -124,9 +124,9 @@ function formatVarType({
|
|
|
124
124
|
type = cleanOptionals(type);
|
|
125
125
|
let defaultStr = "";
|
|
126
126
|
if (defaultValue) {
|
|
127
|
-
if ("string"
|
|
127
|
+
if (typeof defaultValue === "string") {
|
|
128
128
|
defaultStr = ` = '${defaultValue}'`;
|
|
129
|
-
} else if ("object"
|
|
129
|
+
} else if (typeof defaultValue === "object") {
|
|
130
130
|
defaultStr = ` = ${JSON.stringify(defaultValue)}`;
|
|
131
131
|
} else {
|
|
132
132
|
defaultStr = ` = ${defaultValue}`;
|
|
@@ -736,7 +736,7 @@ function formatAPIErrorMessage(err) {
|
|
|
736
736
|
case 500:
|
|
737
737
|
return "An internal server error occurred. Please try again later.";
|
|
738
738
|
}
|
|
739
|
-
if (data.detail &&
|
|
739
|
+
if (data.detail && typeof data.detail === "string") {
|
|
740
740
|
return data.detail;
|
|
741
741
|
}
|
|
742
742
|
if (data.message) {
|
package/dist/index.mjs
CHANGED
|
@@ -62,14 +62,14 @@ function schemaToType(schema) {
|
|
|
62
62
|
return "any";
|
|
63
63
|
}
|
|
64
64
|
if (schema.anyOf) {
|
|
65
|
-
let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => "any"
|
|
66
|
-
if (""
|
|
65
|
+
let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" | ");
|
|
66
|
+
if (_t === "" || _t === "null") {
|
|
67
67
|
_t = "any";
|
|
68
68
|
}
|
|
69
69
|
return _t;
|
|
70
70
|
}
|
|
71
71
|
if (schema.allOf) {
|
|
72
|
-
return schema.allOf.map((s) => schemaToType(s)).filter((p) => "any"
|
|
72
|
+
return schema.allOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" & ");
|
|
73
73
|
}
|
|
74
74
|
switch (schema.type) {
|
|
75
75
|
case "object":
|
|
@@ -106,7 +106,7 @@ function isOptional(schema) {
|
|
|
106
106
|
return includesNull || includesUndefined || schema?.default !== void 0;
|
|
107
107
|
}
|
|
108
108
|
function cleanOptionals(str) {
|
|
109
|
-
return str.split(" | ").filter((t) => "null"
|
|
109
|
+
return str.split(" | ").filter((t) => t !== "null" && t !== "undefined").join(" | ");
|
|
110
110
|
}
|
|
111
111
|
function formatVarType({
|
|
112
112
|
varName,
|
|
@@ -118,9 +118,9 @@ function formatVarType({
|
|
|
118
118
|
type = cleanOptionals(type);
|
|
119
119
|
let defaultStr = "";
|
|
120
120
|
if (defaultValue) {
|
|
121
|
-
if ("string"
|
|
121
|
+
if (typeof defaultValue === "string") {
|
|
122
122
|
defaultStr = ` = '${defaultValue}'`;
|
|
123
|
-
} else if ("object"
|
|
123
|
+
} else if (typeof defaultValue === "object") {
|
|
124
124
|
defaultStr = ` = ${JSON.stringify(defaultValue)}`;
|
|
125
125
|
} else {
|
|
126
126
|
defaultStr = ` = ${defaultValue}`;
|
|
@@ -730,7 +730,7 @@ function formatAPIErrorMessage(err) {
|
|
|
730
730
|
case 500:
|
|
731
731
|
return "An internal server error occurred. Please try again later.";
|
|
732
732
|
}
|
|
733
|
-
if (data.detail &&
|
|
733
|
+
if (data.detail && typeof data.detail === "string") {
|
|
734
734
|
return data.detail;
|
|
735
735
|
}
|
|
736
736
|
if (data.message) {
|
package/package.json
CHANGED
|
@@ -13,11 +13,11 @@ export default async (openApiUrl: string, baseUrl: string): Promise<OpenAPIRespo
|
|
|
13
13
|
try {
|
|
14
14
|
const { data: openApi } = await axios.get<OpenAPIObject>(openApiUrl, { headers: basicAuthHeader })
|
|
15
15
|
const schemas = openApi.components?.schemas
|
|
16
|
-
if (!schemas) {throw new Error('No schemas found in OpenAPI document')}
|
|
16
|
+
if (!schemas) { throw new Error('No schemas found in OpenAPI document') }
|
|
17
17
|
const types = generateTypes(schemas)
|
|
18
18
|
// Generate Functions
|
|
19
19
|
const { paths } = openApi
|
|
20
|
-
if (!paths) {throw new Error('No paths found in OpenAPI document')}
|
|
20
|
+
if (!paths) { throw new Error('No paths found in OpenAPI document') }
|
|
21
21
|
const code = generateFunctions(paths, baseUrl) // TODO baseURL should not be set here, but should be instatiated in runtime somehow
|
|
22
22
|
return { types, code }
|
|
23
23
|
} catch (error: any) {
|
|
@@ -14,7 +14,7 @@ export function getPath(
|
|
|
14
14
|
* @param obj The value to check.
|
|
15
15
|
*/
|
|
16
16
|
export function isReferenceObject(obj: any): obj is ReferenceObject {
|
|
17
|
-
if (!obj) {return false}
|
|
17
|
+
if (!obj) { return false }
|
|
18
18
|
return Object.hasOwn(obj, '$ref')
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -28,7 +28,7 @@ export function isReferenceObject(obj: any): obj is ReferenceObject {
|
|
|
28
28
|
* @param schema The value to check.
|
|
29
29
|
*/
|
|
30
30
|
export function isSchemaObject(schema: SchemaObject | ReferenceObject): schema is SchemaObject {
|
|
31
|
-
return !
|
|
31
|
+
return !Object.hasOwn(schema, '$ref')
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export function dereference<T>(property: ReferenceObject | T) {
|
|
@@ -26,7 +26,7 @@ export function formatType(typeName: string): string {
|
|
|
26
26
|
|
|
27
27
|
function resolveReference(ref: string): string {
|
|
28
28
|
const t = ref.split('/').pop()
|
|
29
|
-
if (!t) {return 'any'}
|
|
29
|
+
if (!t) { return 'any' }
|
|
30
30
|
return formatType(t)
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -53,9 +53,9 @@ function handleArrayItems(schema: SchemaObject): string {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export function schemaToType(schema?: SchemaObject | ReferenceObject): string {
|
|
56
|
-
if (!schema) {return 'any'}
|
|
56
|
+
if (!schema) { return 'any' }
|
|
57
57
|
if (isReferenceObject(schema)) {
|
|
58
|
-
if (schema.$ref) {return resolveReference(schema.$ref)}
|
|
58
|
+
if (schema.$ref) { return resolveReference(schema.$ref) }
|
|
59
59
|
}
|
|
60
60
|
if (!isSchemaObject(schema)) {
|
|
61
61
|
console.warn('Schema is not a SchemaObject:', schema)
|
|
@@ -65,15 +65,15 @@ export function schemaToType(schema?: SchemaObject | ReferenceObject): string {
|
|
|
65
65
|
if (schema.anyOf) {
|
|
66
66
|
let _t = schema.anyOf
|
|
67
67
|
.map(s => schemaToType(s))
|
|
68
|
-
.filter(p => 'any'
|
|
68
|
+
.filter(p => p !== 'any')
|
|
69
69
|
.join(' | ')
|
|
70
|
-
if (''
|
|
70
|
+
if (_t === '' || _t === 'null') { _t = 'any' }
|
|
71
71
|
return _t
|
|
72
72
|
}
|
|
73
73
|
if (schema.allOf) {
|
|
74
74
|
return schema.allOf
|
|
75
75
|
.map(s => schemaToType(s))
|
|
76
|
-
.filter(p => 'any'
|
|
76
|
+
.filter(p => p !== 'any')
|
|
77
77
|
.join(' & ')
|
|
78
78
|
}
|
|
79
79
|
// TODO: handle Array.isArray(schema.type) case b4 switch
|
|
@@ -108,7 +108,7 @@ export function isOptional(schema?: SchemaObject) {
|
|
|
108
108
|
export function cleanOptionals(str: string) {
|
|
109
109
|
return str
|
|
110
110
|
.split(' | ')
|
|
111
|
-
.filter(t => 'null'
|
|
111
|
+
.filter(t => t !== 'null' && t !== 'undefined')
|
|
112
112
|
.join(' | ')
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -128,9 +128,9 @@ export function formatVarType({
|
|
|
128
128
|
|
|
129
129
|
let defaultStr = ''
|
|
130
130
|
if (defaultValue) {
|
|
131
|
-
if ('string'
|
|
131
|
+
if (typeof defaultValue === 'string') {
|
|
132
132
|
defaultStr = ` = '${defaultValue}'`
|
|
133
|
-
} else if ('object'
|
|
133
|
+
} else if (typeof defaultValue === 'object') {
|
|
134
134
|
defaultStr = ` = ${JSON.stringify(defaultValue)}`
|
|
135
135
|
} else {
|
|
136
136
|
defaultStr = ` = ${defaultValue}`
|
|
@@ -139,7 +139,7 @@ export function formatVarType({
|
|
|
139
139
|
|
|
140
140
|
let optionalStr = (!required && isOptional(schema)) ? '?' : ''
|
|
141
141
|
|
|
142
|
-
if (defaultStr) {optionalStr = ''}
|
|
142
|
+
if (defaultStr) { optionalStr = '' }
|
|
143
143
|
|
|
144
144
|
return `${varName}${optionalStr}: ${type}${defaultStr}`
|
|
145
145
|
}
|
|
@@ -263,7 +263,7 @@ const RESERVED_KEYWORDS = new Set([
|
|
|
263
263
|
* @returns A safe variable name
|
|
264
264
|
*/
|
|
265
265
|
export function sanitizeVarName(varName: string): string {
|
|
266
|
-
if (!varName) {return varName}
|
|
266
|
+
if (!varName) { return varName }
|
|
267
267
|
|
|
268
268
|
// If it's a reserved keyword, append underscore
|
|
269
269
|
if (RESERVED_KEYWORDS.has(varName.toLowerCase())) {
|
package/src/utils.ts
CHANGED