@bagelink/sdk 1.5.30 → 1.6.2
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 +19 -11
- package/dist/index.mjs +19 -11
- package/package.json +1 -1
- package/src/openAPITools/functionGenerator.ts +40 -24
package/dist/index.cjs
CHANGED
|
@@ -317,7 +317,7 @@ function generateResponseType(responses) {
|
|
|
317
317
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
318
318
|
if (statusCode.startsWith("2")) {
|
|
319
319
|
const responseType = getResponseType(response);
|
|
320
|
-
if (responseType && "any"
|
|
320
|
+
if (responseType && responseType !== "any") {
|
|
321
321
|
types.push(responseType);
|
|
322
322
|
}
|
|
323
323
|
}
|
|
@@ -337,7 +337,7 @@ function formatPathWithParams(path) {
|
|
|
337
337
|
}
|
|
338
338
|
function generateRequestBody(requestBody) {
|
|
339
339
|
const content = dereference(requestBody)?.content;
|
|
340
|
-
if (!content ||
|
|
340
|
+
if (!content || Object.keys(content).length === 0) {
|
|
341
341
|
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
342
342
|
}
|
|
343
343
|
if (content["multipart/form-data"]) {
|
|
@@ -390,7 +390,7 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
390
390
|
collectTypeForImportStatement(paramType);
|
|
391
391
|
const paramName = param.name;
|
|
392
392
|
const varName = toCamelCaseSafe(param.name);
|
|
393
|
-
if ("path"
|
|
393
|
+
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
394
394
|
functionParams.push(
|
|
395
395
|
formatVarType({
|
|
396
396
|
varName,
|
|
@@ -400,7 +400,7 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
400
400
|
})
|
|
401
401
|
);
|
|
402
402
|
}
|
|
403
|
-
if ("query"
|
|
403
|
+
if (param.in === "query" || param.in === "header") {
|
|
404
404
|
queryParams.push(
|
|
405
405
|
paramName === varName ? paramName : `'${paramName}': ${varName}`
|
|
406
406
|
);
|
|
@@ -430,7 +430,7 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
430
430
|
if (requestBodyParam) {
|
|
431
431
|
allParamsArray.push(requestBodyParam.trim());
|
|
432
432
|
}
|
|
433
|
-
if (
|
|
433
|
+
if (allParamsArray.length === 0) {
|
|
434
434
|
return "";
|
|
435
435
|
}
|
|
436
436
|
const parsedParams = allParamsArray.filter(Boolean).map(parseParameter);
|
|
@@ -441,12 +441,12 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
441
441
|
return `{ ${destructuredNames} }: { ${typeDefinition} } = {}`;
|
|
442
442
|
}
|
|
443
443
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
444
|
-
if ("undefined"
|
|
444
|
+
if (allParams === "undefined") {
|
|
445
445
|
allParams = "";
|
|
446
446
|
}
|
|
447
447
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
448
448
|
const paramStr = parameters.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
449
|
-
if ("formData"
|
|
449
|
+
if (requestBodyPayload === "formData") {
|
|
450
450
|
if (allParams.includes("file: File")) {
|
|
451
451
|
axiosFunction += `
|
|
452
452
|
const formData = new FormData()
|
|
@@ -524,7 +524,7 @@ function hasConflict(path, method) {
|
|
|
524
524
|
(p) => p.path === cleanPathName && p.method === method
|
|
525
525
|
);
|
|
526
526
|
pathOperations.push({ path: cleanPathName, method });
|
|
527
|
-
return
|
|
527
|
+
return matchingPaths.length > 0;
|
|
528
528
|
}
|
|
529
529
|
function generateRandomString() {
|
|
530
530
|
return Math.random().toString(36).slice(7);
|
|
@@ -570,13 +570,21 @@ function generateFunctions(paths, baseUrl) {
|
|
|
570
570
|
return acc;
|
|
571
571
|
}
|
|
572
572
|
const methods = Object.keys(operation);
|
|
573
|
-
|
|
573
|
+
const currentPathClean = cleanPath(path);
|
|
574
|
+
const currentPathPrefix = splitPath.slice(0, index + 1).join("/");
|
|
575
|
+
const prefixWithSlash = `${currentPathPrefix}/`;
|
|
576
|
+
const hasChildPaths = allPathsClean.some(
|
|
577
|
+
(otherPath) => otherPath !== currentPathClean && otherPath.startsWith(prefixWithSlash)
|
|
578
|
+
);
|
|
579
|
+
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === currentPathClean).length === 1 && !hasChildPaths) {
|
|
574
580
|
const method = methods[0];
|
|
575
581
|
const opp = operation[method];
|
|
576
582
|
acc[objFuncKey] = createFunctionPlaceholder(path, method, opp);
|
|
577
583
|
} else if (index === array.length - 1) {
|
|
578
|
-
|
|
579
|
-
|
|
584
|
+
const existingValue = acc[objFuncKey];
|
|
585
|
+
const existingObj = existingValue && typeof existingValue === "object" && !Array.isArray(existingValue) ? existingValue : {};
|
|
586
|
+
acc[objFuncKey] = handlePathSegment(path, operation, existingObj);
|
|
587
|
+
} else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
|
|
580
588
|
acc[objFuncKey] = {};
|
|
581
589
|
}
|
|
582
590
|
return acc[objFuncKey];
|
package/dist/index.mjs
CHANGED
|
@@ -311,7 +311,7 @@ function generateResponseType(responses) {
|
|
|
311
311
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
312
312
|
if (statusCode.startsWith("2")) {
|
|
313
313
|
const responseType = getResponseType(response);
|
|
314
|
-
if (responseType && "any"
|
|
314
|
+
if (responseType && responseType !== "any") {
|
|
315
315
|
types.push(responseType);
|
|
316
316
|
}
|
|
317
317
|
}
|
|
@@ -331,7 +331,7 @@ function formatPathWithParams(path) {
|
|
|
331
331
|
}
|
|
332
332
|
function generateRequestBody(requestBody) {
|
|
333
333
|
const content = dereference(requestBody)?.content;
|
|
334
|
-
if (!content ||
|
|
334
|
+
if (!content || Object.keys(content).length === 0) {
|
|
335
335
|
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
336
336
|
}
|
|
337
337
|
if (content["multipart/form-data"]) {
|
|
@@ -384,7 +384,7 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
384
384
|
collectTypeForImportStatement(paramType);
|
|
385
385
|
const paramName = param.name;
|
|
386
386
|
const varName = toCamelCaseSafe(param.name);
|
|
387
|
-
if ("path"
|
|
387
|
+
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
388
388
|
functionParams.push(
|
|
389
389
|
formatVarType({
|
|
390
390
|
varName,
|
|
@@ -394,7 +394,7 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
394
394
|
})
|
|
395
395
|
);
|
|
396
396
|
}
|
|
397
|
-
if ("query"
|
|
397
|
+
if (param.in === "query" || param.in === "header") {
|
|
398
398
|
queryParams.push(
|
|
399
399
|
paramName === varName ? paramName : `'${paramName}': ${varName}`
|
|
400
400
|
);
|
|
@@ -424,7 +424,7 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
424
424
|
if (requestBodyParam) {
|
|
425
425
|
allParamsArray.push(requestBodyParam.trim());
|
|
426
426
|
}
|
|
427
|
-
if (
|
|
427
|
+
if (allParamsArray.length === 0) {
|
|
428
428
|
return "";
|
|
429
429
|
}
|
|
430
430
|
const parsedParams = allParamsArray.filter(Boolean).map(parseParameter);
|
|
@@ -435,12 +435,12 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
435
435
|
return `{ ${destructuredNames} }: { ${typeDefinition} } = {}`;
|
|
436
436
|
}
|
|
437
437
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
438
|
-
if ("undefined"
|
|
438
|
+
if (allParams === "undefined") {
|
|
439
439
|
allParams = "";
|
|
440
440
|
}
|
|
441
441
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
442
442
|
const paramStr = parameters.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
443
|
-
if ("formData"
|
|
443
|
+
if (requestBodyPayload === "formData") {
|
|
444
444
|
if (allParams.includes("file: File")) {
|
|
445
445
|
axiosFunction += `
|
|
446
446
|
const formData = new FormData()
|
|
@@ -518,7 +518,7 @@ function hasConflict(path, method) {
|
|
|
518
518
|
(p) => p.path === cleanPathName && p.method === method
|
|
519
519
|
);
|
|
520
520
|
pathOperations.push({ path: cleanPathName, method });
|
|
521
|
-
return
|
|
521
|
+
return matchingPaths.length > 0;
|
|
522
522
|
}
|
|
523
523
|
function generateRandomString() {
|
|
524
524
|
return Math.random().toString(36).slice(7);
|
|
@@ -564,13 +564,21 @@ function generateFunctions(paths, baseUrl) {
|
|
|
564
564
|
return acc;
|
|
565
565
|
}
|
|
566
566
|
const methods = Object.keys(operation);
|
|
567
|
-
|
|
567
|
+
const currentPathClean = cleanPath(path);
|
|
568
|
+
const currentPathPrefix = splitPath.slice(0, index + 1).join("/");
|
|
569
|
+
const prefixWithSlash = `${currentPathPrefix}/`;
|
|
570
|
+
const hasChildPaths = allPathsClean.some(
|
|
571
|
+
(otherPath) => otherPath !== currentPathClean && otherPath.startsWith(prefixWithSlash)
|
|
572
|
+
);
|
|
573
|
+
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === currentPathClean).length === 1 && !hasChildPaths) {
|
|
568
574
|
const method = methods[0];
|
|
569
575
|
const opp = operation[method];
|
|
570
576
|
acc[objFuncKey] = createFunctionPlaceholder(path, method, opp);
|
|
571
577
|
} else if (index === array.length - 1) {
|
|
572
|
-
|
|
573
|
-
|
|
578
|
+
const existingValue = acc[objFuncKey];
|
|
579
|
+
const existingObj = existingValue && typeof existingValue === "object" && !Array.isArray(existingValue) ? existingValue : {};
|
|
580
|
+
acc[objFuncKey] = handlePathSegment(path, operation, existingObj);
|
|
581
|
+
} else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
|
|
574
582
|
acc[objFuncKey] = {};
|
|
575
583
|
}
|
|
576
584
|
return acc[objFuncKey];
|
package/package.json
CHANGED
|
@@ -75,8 +75,8 @@ function collectTypeForImportStatement(typeName: string): void {
|
|
|
75
75
|
const isPrimitive = primitiveTypes.includes(typeName)
|
|
76
76
|
typeName = formatType(typeName)
|
|
77
77
|
|
|
78
|
-
if (!typeName || isPrimitive) {return}
|
|
79
|
-
if (!allTypes.includes(typeName)) {allTypes.push(typeName)}
|
|
78
|
+
if (!typeName || isPrimitive) { return }
|
|
79
|
+
if (!allTypes.includes(typeName)) { allTypes.push(typeName) }
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/**
|
|
@@ -100,7 +100,7 @@ function getResponseType(response: OpenAPIResponse | ReferenceObject): string |
|
|
|
100
100
|
return undefined // TODO: handle references properly
|
|
101
101
|
}
|
|
102
102
|
const mediaTypeObject = response.content?.['application/json']
|
|
103
|
-
if (!mediaTypeObject?.schema) {return undefined}
|
|
103
|
+
if (!mediaTypeObject?.schema) { return undefined }
|
|
104
104
|
return schemaToTypeWithCollection(mediaTypeObject.schema)
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -110,13 +110,13 @@ function getResponseType(response: OpenAPIResponse | ReferenceObject): string |
|
|
|
110
110
|
* @returns The TypeScript response type string
|
|
111
111
|
*/
|
|
112
112
|
function generateResponseType(responses?: OpenAPIResponses): string {
|
|
113
|
-
if (!responses) {return ''}
|
|
113
|
+
if (!responses) { return '' }
|
|
114
114
|
|
|
115
115
|
const types: string[] = []
|
|
116
116
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
117
117
|
if (statusCode.startsWith('2')) {
|
|
118
118
|
const responseType = getResponseType(response)
|
|
119
|
-
if (responseType && 'any'
|
|
119
|
+
if (responseType && responseType !== 'any') {
|
|
120
120
|
types.push(responseType)
|
|
121
121
|
}
|
|
122
122
|
}
|
|
@@ -142,7 +142,7 @@ function getParamsFromPath(path: string): string[] | undefined {
|
|
|
142
142
|
*/
|
|
143
143
|
function formatPathWithParams(path: string): string {
|
|
144
144
|
const params = getParamsFromPath(path)
|
|
145
|
-
if (!params) {return `'${path}'`}
|
|
145
|
+
if (!params) { return `'${path}'` }
|
|
146
146
|
|
|
147
147
|
return `\`${path.replace(/\{([^}]+)\}/g, (_, paramName) => `\${${toCamelCase(paramName)}}`)}\``
|
|
148
148
|
}
|
|
@@ -160,7 +160,7 @@ function generateRequestBody(requestBody?: OpenAPIOperation['requestBody']): {
|
|
|
160
160
|
// return { requestBodyParam: '', requestBodyPayload: '' }
|
|
161
161
|
|
|
162
162
|
const content = dereference(requestBody)?.content
|
|
163
|
-
if (!content ||
|
|
163
|
+
if (!content || Object.keys(content).length === 0) {
|
|
164
164
|
return { requestBodyParam: '', requestBodyPayload: '' }
|
|
165
165
|
}
|
|
166
166
|
|
|
@@ -217,7 +217,7 @@ function generateFunctionParameters(
|
|
|
217
217
|
params?: OpenAPIOperation['parameters'],
|
|
218
218
|
isFileUpload = false
|
|
219
219
|
): { params?: string, config?: { params?: string } } {
|
|
220
|
-
if (!params?.length) {return {}}
|
|
220
|
+
if (!params?.length) { return {} }
|
|
221
221
|
|
|
222
222
|
// Special handling for file uploads
|
|
223
223
|
if (isFileUpload) {
|
|
@@ -241,7 +241,7 @@ function generateFunctionParameters(
|
|
|
241
241
|
const paramName = param.name
|
|
242
242
|
const varName = toCamelCaseSafe(param.name)
|
|
243
243
|
|
|
244
|
-
if ('path'
|
|
244
|
+
if (param.in === 'path' || param.in === 'query' || param.in === 'header') {
|
|
245
245
|
functionParams.push(
|
|
246
246
|
formatVarType({
|
|
247
247
|
varName,
|
|
@@ -252,7 +252,7 @@ function generateFunctionParameters(
|
|
|
252
252
|
)
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
if ('query'
|
|
255
|
+
if (param.in === 'query' || param.in === 'header') {
|
|
256
256
|
queryParams.push(
|
|
257
257
|
paramName === varName ? paramName : `'${paramName}': ${varName}`
|
|
258
258
|
)
|
|
@@ -316,7 +316,7 @@ function combineAllParams(
|
|
|
316
316
|
allParamsArray.push(requestBodyParam.trim())
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
-
if (
|
|
319
|
+
if (allParamsArray.length === 0) {
|
|
320
320
|
return ''
|
|
321
321
|
}
|
|
322
322
|
|
|
@@ -357,14 +357,14 @@ function generateAxiosFunction(
|
|
|
357
357
|
parameters: { config?: { params?: string } },
|
|
358
358
|
requestBodyPayload: string
|
|
359
359
|
): string {
|
|
360
|
-
if ('undefined'
|
|
360
|
+
if (allParams === 'undefined') { allParams = '' }
|
|
361
361
|
|
|
362
362
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`
|
|
363
363
|
const paramStr = parameters.config?.params
|
|
364
364
|
? `params: {${parameters.config.params}}`
|
|
365
365
|
: ''
|
|
366
366
|
|
|
367
|
-
if ('formData'
|
|
367
|
+
if (requestBodyPayload === 'formData') {
|
|
368
368
|
// Check if this is a file upload with specific file parameter
|
|
369
369
|
if (allParams.includes('file: File')) {
|
|
370
370
|
axiosFunction += `
|
|
@@ -415,7 +415,7 @@ function buildJSDocComment(operation: OpenAPIOperation, method: string, path: st
|
|
|
415
415
|
// Add description if available
|
|
416
416
|
if (operation.description) {
|
|
417
417
|
// If there's already a summary, add a line break
|
|
418
|
-
if (operation.summary) {functionComment += ` *\n`}
|
|
418
|
+
if (operation.summary) { functionComment += ` *\n` }
|
|
419
419
|
|
|
420
420
|
// Split description into lines and add each line with proper JSDoc formatting
|
|
421
421
|
const descriptionLines = operation.description.split('\n')
|
|
@@ -443,7 +443,7 @@ function generateFunctionForOperation(
|
|
|
443
443
|
path: string,
|
|
444
444
|
operation: OpenAPIOperation
|
|
445
445
|
): string {
|
|
446
|
-
if (!operation) {return ''}
|
|
446
|
+
if (!operation) { return '' }
|
|
447
447
|
|
|
448
448
|
// Check if this is a file upload
|
|
449
449
|
const multiPartSchema = dereference(operation.requestBody)?.content?.[
|
|
@@ -500,7 +500,7 @@ function hasConflict(path: string, method: string): boolean {
|
|
|
500
500
|
)
|
|
501
501
|
|
|
502
502
|
pathOperations.push({ path: cleanPathName, method })
|
|
503
|
-
return
|
|
503
|
+
return matchingPaths.length > 0
|
|
504
504
|
}
|
|
505
505
|
|
|
506
506
|
/**
|
|
@@ -585,24 +585,40 @@ export function generateFunctions(paths: OpenAPIPaths, baseUrl: string): string
|
|
|
585
585
|
|
|
586
586
|
splitPath.reduce((acc, key: string, index: number, array: string[]) => {
|
|
587
587
|
const objFuncKey = toCamelCaseSafe(key)
|
|
588
|
-
if (!objFuncKey) {return acc}
|
|
588
|
+
if (!objFuncKey) { return acc }
|
|
589
589
|
|
|
590
590
|
const methods = Object.keys(operation) as Array<keyof OpenAPIPath>
|
|
591
|
+
const currentPathClean = cleanPath(path)
|
|
592
|
+
const currentPathPrefix = splitPath.slice(0, index + 1).join('/')
|
|
593
|
+
|
|
594
|
+
// Check if any other path has this path as a prefix (indicating child paths exist)
|
|
595
|
+
const prefixWithSlash = `${currentPathPrefix}/`
|
|
596
|
+
const hasChildPaths = allPathsClean.some(
|
|
597
|
+
otherPath => otherPath !== currentPathClean && otherPath.startsWith(prefixWithSlash)
|
|
598
|
+
)
|
|
591
599
|
|
|
592
600
|
if (
|
|
593
601
|
index === array.length - 1
|
|
594
|
-
&&
|
|
595
|
-
&&
|
|
602
|
+
&& methods.length === 1
|
|
603
|
+
&& allPathsClean.filter(p => p === currentPathClean).length === 1
|
|
604
|
+
&& !hasChildPaths
|
|
596
605
|
) {
|
|
597
|
-
// Single method endpoint with unique path
|
|
606
|
+
// Single method endpoint with unique path and no child paths
|
|
598
607
|
const method = methods[0]
|
|
599
608
|
const opp = operation[method!] as OpenAPIOperation
|
|
600
609
|
acc[objFuncKey] = createFunctionPlaceholder(path, method!, opp)
|
|
601
610
|
} else if (index === array.length - 1) {
|
|
602
|
-
// Multiple methods endpoint or path with conflicts
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
611
|
+
// Multiple methods endpoint or path with conflicts or has child paths
|
|
612
|
+
// Ensure we have an object to merge with (handlePathSegment expects an object)
|
|
613
|
+
const existingValue = acc[objFuncKey]
|
|
614
|
+
const existingObj = (existingValue && typeof existingValue === 'object' && !Array.isArray(existingValue))
|
|
615
|
+
? existingValue
|
|
616
|
+
: {}
|
|
617
|
+
acc[objFuncKey] = handlePathSegment(path, operation, existingObj)
|
|
618
|
+
} else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== 'object') {
|
|
619
|
+
// Intermediate path segment - ensure it's an object
|
|
620
|
+
// If it's already a function, we need to preserve it but this shouldn't happen
|
|
621
|
+
// if hasChildPaths check works correctly
|
|
606
622
|
acc[objFuncKey] = {}
|
|
607
623
|
}
|
|
608
624
|
|