@bagelink/sdk 0.0.1027 → 0.0.1033
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 +73 -34
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +73 -34
- package/package.json +1 -1
- package/src/index.ts +3 -1
- package/src/openAPITools/functionGenerator.ts +94 -32
- package/src/openAPITools/openApiTypes.ts +23 -2
- package/src/openAPITools/utils.ts +2 -2
package/dist/index.cjs
CHANGED
|
@@ -6,8 +6,8 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
6
6
|
|
|
7
7
|
const axios__default = /*#__PURE__*/_interopDefaultCompat(axios$1);
|
|
8
8
|
|
|
9
|
-
const toCamelCase = (str) => str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toLowerCase()) || str;
|
|
10
|
-
const toPascalCase = (str) => str?.replaceAll(/[-_\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toUpperCase()) || str;
|
|
9
|
+
const toCamelCase = (str) => str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toLowerCase()) || str || "";
|
|
10
|
+
const toPascalCase = (str) => str?.replaceAll(/[-_\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toUpperCase()) || str || "";
|
|
11
11
|
function formatType(typeName) {
|
|
12
12
|
typeName = typeName.replaceAll("-", "").replaceAll(/(post|put)$/gi, "").replaceAll(/^body_/gi, "");
|
|
13
13
|
return toPascalCase(typeName);
|
|
@@ -122,15 +122,25 @@ function generateResponseType(responses) {
|
|
|
122
122
|
return types.join(" | ");
|
|
123
123
|
}
|
|
124
124
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
if (allParams === "undefined")
|
|
126
|
+
allParams = "";
|
|
127
|
+
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
128
|
+
if (requestBodyPayload === "formData") {
|
|
129
|
+
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
130
|
+
axiosFunction += `
|
|
131
|
+
const formData = new FormData()
|
|
132
|
+
formData.append('upload', file)
|
|
133
|
+
return axios.${method}(${formattedPath}, formData, {
|
|
134
|
+
headers: { 'Content-Type': 'multipart/form-data' },
|
|
135
|
+
onUploadProgress: options?.onUploadProgress${paramStr ? `,
|
|
136
|
+
${paramStr}` : ""}
|
|
137
|
+
})`;
|
|
138
|
+
} else {
|
|
139
|
+
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : "";
|
|
130
140
|
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : "{}";
|
|
131
|
-
axiosFunction +=
|
|
141
|
+
axiosFunction += `return axios.${method}(${formattedPath}${["get", "delete"].includes(method) ? paramStr : `, ${bodyVar}${paramStr}`})`;
|
|
132
142
|
}
|
|
133
|
-
axiosFunction += "
|
|
143
|
+
axiosFunction += "}";
|
|
134
144
|
return axiosFunction;
|
|
135
145
|
}
|
|
136
146
|
const pathParamRegex = /\{([^}]+)\}/g;
|
|
@@ -144,14 +154,25 @@ function formatPathWithParams(path) {
|
|
|
144
154
|
return formattedPath;
|
|
145
155
|
}
|
|
146
156
|
function generateRequestBody(requestBody) {
|
|
147
|
-
|
|
148
|
-
if (!bodySchema)
|
|
157
|
+
if (!requestBody?.content)
|
|
149
158
|
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
159
|
+
const { content } = requestBody;
|
|
160
|
+
const multipartFormData = content["multipart/form-data"] || {};
|
|
161
|
+
if (multipartFormData.schema?.$ref?.includes("Body_upload_files")) {
|
|
162
|
+
return {
|
|
163
|
+
requestBodyParam: "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }",
|
|
164
|
+
requestBodyPayload: "formData"
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const jsonContent = requestBody.content["application/json"];
|
|
168
|
+
if (!jsonContent || !jsonContent.schema) {
|
|
169
|
+
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
170
|
+
}
|
|
171
|
+
const bodySchema = jsonContent.schema;
|
|
150
172
|
const requestBodyType = schemaToType(bodySchema);
|
|
151
173
|
collectTypeForImportStatement(requestBodyType);
|
|
152
174
|
const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || "requestBody";
|
|
153
|
-
const
|
|
154
|
-
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, defaultValue);
|
|
175
|
+
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, bodySchema.default);
|
|
155
176
|
return { requestBodyParam, requestBodyPayload };
|
|
156
177
|
}
|
|
157
178
|
function combineAllParams(parameters, requestBodyParam) {
|
|
@@ -163,44 +184,54 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
163
184
|
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
164
185
|
return allParamsArray.join(", ");
|
|
165
186
|
}
|
|
166
|
-
function generateFunctionParameters(params) {
|
|
167
|
-
if (!params
|
|
187
|
+
function generateFunctionParameters(params, isFileUpload = false) {
|
|
188
|
+
if (!params?.length)
|
|
168
189
|
return {};
|
|
190
|
+
if (isFileUpload) {
|
|
191
|
+
return {
|
|
192
|
+
config: {
|
|
193
|
+
params: "dir_path: options?.dirPath, tags: options?.tags"
|
|
194
|
+
},
|
|
195
|
+
params: ""
|
|
196
|
+
// Empty string to ensure file is the first parameter
|
|
197
|
+
};
|
|
198
|
+
}
|
|
169
199
|
const functionParams = [];
|
|
170
200
|
const paramList = [];
|
|
171
201
|
for (const param of params) {
|
|
172
202
|
const paramType = schemaToType(param.schema);
|
|
173
203
|
collectTypeForImportStatement(paramType);
|
|
174
204
|
const paramName = param.name;
|
|
175
|
-
const varName = toCamelCase(param.name)
|
|
205
|
+
const varName = toCamelCase(param.name);
|
|
176
206
|
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
177
|
-
|
|
178
|
-
const varType = formatVarType(varName, param.schema, param.required, defaultValue);
|
|
179
|
-
functionParams.push(varType);
|
|
207
|
+
functionParams.push(formatVarType(varName, param.schema, param.required, param.schema.default));
|
|
180
208
|
}
|
|
181
209
|
if (param.in === "query" || param.in === "header") {
|
|
182
|
-
|
|
183
|
-
paramList.push(paramName);
|
|
184
|
-
else
|
|
185
|
-
paramList.push(`'${paramName}': ${varName}`);
|
|
210
|
+
paramList.push(paramName === varName ? paramName : `'${paramName}': ${varName}`);
|
|
186
211
|
}
|
|
187
212
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return { params: paramsString, config };
|
|
213
|
+
return {
|
|
214
|
+
params: functionParams.join(", "),
|
|
215
|
+
config: paramList.length ? { params: paramList.join(", ") } : {}
|
|
216
|
+
};
|
|
193
217
|
}
|
|
194
218
|
function generateFunctionForOperation(method, path, operation) {
|
|
195
219
|
if (!operation)
|
|
196
220
|
return "";
|
|
197
|
-
const
|
|
198
|
-
const
|
|
199
|
-
const formattedPath = formatPathWithParams(path);
|
|
221
|
+
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
222
|
+
const parameters = generateFunctionParameters(operation.parameters, isFileUpload);
|
|
200
223
|
const { requestBodyParam, requestBodyPayload } = generateRequestBody(operation.requestBody);
|
|
201
|
-
const allParams = combineAllParams(parameters, requestBodyParam);
|
|
224
|
+
const allParams = isFileUpload ? "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }" : combineAllParams(parameters, requestBodyParam);
|
|
225
|
+
const responseType = generateResponseType(operation.responses);
|
|
202
226
|
const responseTypeStr = responseType ? `: Promise<AxiosResponse<${responseType}>>` : "";
|
|
203
|
-
return generateAxiosFunction(
|
|
227
|
+
return generateAxiosFunction(
|
|
228
|
+
method,
|
|
229
|
+
formatPathWithParams(path),
|
|
230
|
+
allParams,
|
|
231
|
+
responseTypeStr,
|
|
232
|
+
parameters,
|
|
233
|
+
requestBodyPayload
|
|
234
|
+
);
|
|
204
235
|
}
|
|
205
236
|
const generateRandomString = () => Math.random().toString(36).slice(7);
|
|
206
237
|
function fileTemplate(tsString, typeForImport, baseURL) {
|
|
@@ -208,6 +239,12 @@ function fileTemplate(tsString, typeForImport, baseURL) {
|
|
|
208
239
|
import type { AxiosResponse } from 'axios';
|
|
209
240
|
import type {${typeForImport.join(", ")}} from './types.d';
|
|
210
241
|
|
|
242
|
+
interface UploadOptions {
|
|
243
|
+
onUploadProgress?: (progressEvent: any) => void
|
|
244
|
+
dirPath?: string
|
|
245
|
+
tags?: string[]
|
|
246
|
+
}
|
|
247
|
+
|
|
211
248
|
export const axios = ax.create({baseURL:${baseURL}});
|
|
212
249
|
${tsString}`;
|
|
213
250
|
const doubleQuoteRegex = /"([^"]+)":/g;
|
|
@@ -457,12 +494,14 @@ class BagelAuth {
|
|
|
457
494
|
}
|
|
458
495
|
}
|
|
459
496
|
class Bagel {
|
|
460
|
-
constructor({ host, onError }) {
|
|
497
|
+
constructor({ host, fileBaseUrl, onError }) {
|
|
461
498
|
__publicField(this, "host");
|
|
499
|
+
__publicField(this, "fileBaseUrl");
|
|
462
500
|
__publicField(this, "onError");
|
|
463
501
|
__publicField(this, "read_table");
|
|
464
502
|
__publicField(this, "auth", new BagelAuth(this));
|
|
465
503
|
this.host = host?.replace(/\/$/, "");
|
|
504
|
+
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
466
505
|
if (!this.host) {
|
|
467
506
|
return this;
|
|
468
507
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -71,8 +71,9 @@ declare class BagelAuth {
|
|
|
71
71
|
}
|
|
72
72
|
declare class Bagel {
|
|
73
73
|
host?: string;
|
|
74
|
+
fileBaseUrl?: string;
|
|
74
75
|
onError?: (err: any) => void;
|
|
75
|
-
constructor({ host, onError }: {
|
|
76
|
+
constructor({ host, fileBaseUrl, onError }: {
|
|
76
77
|
[key: string]: any;
|
|
77
78
|
});
|
|
78
79
|
read_table: Tables | undefined;
|
package/dist/index.d.mts
CHANGED
|
@@ -71,8 +71,9 @@ declare class BagelAuth {
|
|
|
71
71
|
}
|
|
72
72
|
declare class Bagel {
|
|
73
73
|
host?: string;
|
|
74
|
+
fileBaseUrl?: string;
|
|
74
75
|
onError?: (err: any) => void;
|
|
75
|
-
constructor({ host, onError }: {
|
|
76
|
+
constructor({ host, fileBaseUrl, onError }: {
|
|
76
77
|
[key: string]: any;
|
|
77
78
|
});
|
|
78
79
|
read_table: Tables | undefined;
|
package/dist/index.d.ts
CHANGED
|
@@ -71,8 +71,9 @@ declare class BagelAuth {
|
|
|
71
71
|
}
|
|
72
72
|
declare class Bagel {
|
|
73
73
|
host?: string;
|
|
74
|
+
fileBaseUrl?: string;
|
|
74
75
|
onError?: (err: any) => void;
|
|
75
|
-
constructor({ host, onError }: {
|
|
76
|
+
constructor({ host, fileBaseUrl, onError }: {
|
|
76
77
|
[key: string]: any;
|
|
77
78
|
});
|
|
78
79
|
read_table: Tables | undefined;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import axios$1 from 'axios';
|
|
2
2
|
|
|
3
|
-
const toCamelCase = (str) => str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toLowerCase()) || str;
|
|
4
|
-
const toPascalCase = (str) => str?.replaceAll(/[-_\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toUpperCase()) || str;
|
|
3
|
+
const toCamelCase = (str) => str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toLowerCase()) || str || "";
|
|
4
|
+
const toPascalCase = (str) => str?.replaceAll(/[-_\s]+(.)?/g, (_, c) => c?.toUpperCase() || "").replace(/^./, (str2) => str2.toUpperCase()) || str || "";
|
|
5
5
|
function formatType(typeName) {
|
|
6
6
|
typeName = typeName.replaceAll("-", "").replaceAll(/(post|put)$/gi, "").replaceAll(/^body_/gi, "");
|
|
7
7
|
return toPascalCase(typeName);
|
|
@@ -116,15 +116,25 @@ function generateResponseType(responses) {
|
|
|
116
116
|
return types.join(" | ");
|
|
117
117
|
}
|
|
118
118
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
119
|
+
if (allParams === "undefined")
|
|
120
|
+
allParams = "";
|
|
121
|
+
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
122
|
+
if (requestBodyPayload === "formData") {
|
|
123
|
+
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
124
|
+
axiosFunction += `
|
|
125
|
+
const formData = new FormData()
|
|
126
|
+
formData.append('upload', file)
|
|
127
|
+
return axios.${method}(${formattedPath}, formData, {
|
|
128
|
+
headers: { 'Content-Type': 'multipart/form-data' },
|
|
129
|
+
onUploadProgress: options?.onUploadProgress${paramStr ? `,
|
|
130
|
+
${paramStr}` : ""}
|
|
131
|
+
})`;
|
|
132
|
+
} else {
|
|
133
|
+
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : "";
|
|
124
134
|
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : "{}";
|
|
125
|
-
axiosFunction +=
|
|
135
|
+
axiosFunction += `return axios.${method}(${formattedPath}${["get", "delete"].includes(method) ? paramStr : `, ${bodyVar}${paramStr}`})`;
|
|
126
136
|
}
|
|
127
|
-
axiosFunction += "
|
|
137
|
+
axiosFunction += "}";
|
|
128
138
|
return axiosFunction;
|
|
129
139
|
}
|
|
130
140
|
const pathParamRegex = /\{([^}]+)\}/g;
|
|
@@ -138,14 +148,25 @@ function formatPathWithParams(path) {
|
|
|
138
148
|
return formattedPath;
|
|
139
149
|
}
|
|
140
150
|
function generateRequestBody(requestBody) {
|
|
141
|
-
|
|
142
|
-
if (!bodySchema)
|
|
151
|
+
if (!requestBody?.content)
|
|
143
152
|
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
153
|
+
const { content } = requestBody;
|
|
154
|
+
const multipartFormData = content["multipart/form-data"] || {};
|
|
155
|
+
if (multipartFormData.schema?.$ref?.includes("Body_upload_files")) {
|
|
156
|
+
return {
|
|
157
|
+
requestBodyParam: "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }",
|
|
158
|
+
requestBodyPayload: "formData"
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
const jsonContent = requestBody.content["application/json"];
|
|
162
|
+
if (!jsonContent || !jsonContent.schema) {
|
|
163
|
+
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
164
|
+
}
|
|
165
|
+
const bodySchema = jsonContent.schema;
|
|
144
166
|
const requestBodyType = schemaToType(bodySchema);
|
|
145
167
|
collectTypeForImportStatement(requestBodyType);
|
|
146
168
|
const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || "requestBody";
|
|
147
|
-
const
|
|
148
|
-
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, defaultValue);
|
|
169
|
+
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, bodySchema.default);
|
|
149
170
|
return { requestBodyParam, requestBodyPayload };
|
|
150
171
|
}
|
|
151
172
|
function combineAllParams(parameters, requestBodyParam) {
|
|
@@ -157,44 +178,54 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
157
178
|
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
158
179
|
return allParamsArray.join(", ");
|
|
159
180
|
}
|
|
160
|
-
function generateFunctionParameters(params) {
|
|
161
|
-
if (!params
|
|
181
|
+
function generateFunctionParameters(params, isFileUpload = false) {
|
|
182
|
+
if (!params?.length)
|
|
162
183
|
return {};
|
|
184
|
+
if (isFileUpload) {
|
|
185
|
+
return {
|
|
186
|
+
config: {
|
|
187
|
+
params: "dir_path: options?.dirPath, tags: options?.tags"
|
|
188
|
+
},
|
|
189
|
+
params: ""
|
|
190
|
+
// Empty string to ensure file is the first parameter
|
|
191
|
+
};
|
|
192
|
+
}
|
|
163
193
|
const functionParams = [];
|
|
164
194
|
const paramList = [];
|
|
165
195
|
for (const param of params) {
|
|
166
196
|
const paramType = schemaToType(param.schema);
|
|
167
197
|
collectTypeForImportStatement(paramType);
|
|
168
198
|
const paramName = param.name;
|
|
169
|
-
const varName = toCamelCase(param.name)
|
|
199
|
+
const varName = toCamelCase(param.name);
|
|
170
200
|
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
171
|
-
|
|
172
|
-
const varType = formatVarType(varName, param.schema, param.required, defaultValue);
|
|
173
|
-
functionParams.push(varType);
|
|
201
|
+
functionParams.push(formatVarType(varName, param.schema, param.required, param.schema.default));
|
|
174
202
|
}
|
|
175
203
|
if (param.in === "query" || param.in === "header") {
|
|
176
|
-
|
|
177
|
-
paramList.push(paramName);
|
|
178
|
-
else
|
|
179
|
-
paramList.push(`'${paramName}': ${varName}`);
|
|
204
|
+
paramList.push(paramName === varName ? paramName : `'${paramName}': ${varName}`);
|
|
180
205
|
}
|
|
181
206
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return { params: paramsString, config };
|
|
207
|
+
return {
|
|
208
|
+
params: functionParams.join(", "),
|
|
209
|
+
config: paramList.length ? { params: paramList.join(", ") } : {}
|
|
210
|
+
};
|
|
187
211
|
}
|
|
188
212
|
function generateFunctionForOperation(method, path, operation) {
|
|
189
213
|
if (!operation)
|
|
190
214
|
return "";
|
|
191
|
-
const
|
|
192
|
-
const
|
|
193
|
-
const formattedPath = formatPathWithParams(path);
|
|
215
|
+
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
216
|
+
const parameters = generateFunctionParameters(operation.parameters, isFileUpload);
|
|
194
217
|
const { requestBodyParam, requestBodyPayload } = generateRequestBody(operation.requestBody);
|
|
195
|
-
const allParams = combineAllParams(parameters, requestBodyParam);
|
|
218
|
+
const allParams = isFileUpload ? "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }" : combineAllParams(parameters, requestBodyParam);
|
|
219
|
+
const responseType = generateResponseType(operation.responses);
|
|
196
220
|
const responseTypeStr = responseType ? `: Promise<AxiosResponse<${responseType}>>` : "";
|
|
197
|
-
return generateAxiosFunction(
|
|
221
|
+
return generateAxiosFunction(
|
|
222
|
+
method,
|
|
223
|
+
formatPathWithParams(path),
|
|
224
|
+
allParams,
|
|
225
|
+
responseTypeStr,
|
|
226
|
+
parameters,
|
|
227
|
+
requestBodyPayload
|
|
228
|
+
);
|
|
198
229
|
}
|
|
199
230
|
const generateRandomString = () => Math.random().toString(36).slice(7);
|
|
200
231
|
function fileTemplate(tsString, typeForImport, baseURL) {
|
|
@@ -202,6 +233,12 @@ function fileTemplate(tsString, typeForImport, baseURL) {
|
|
|
202
233
|
import type { AxiosResponse } from 'axios';
|
|
203
234
|
import type {${typeForImport.join(", ")}} from './types.d';
|
|
204
235
|
|
|
236
|
+
interface UploadOptions {
|
|
237
|
+
onUploadProgress?: (progressEvent: any) => void
|
|
238
|
+
dirPath?: string
|
|
239
|
+
tags?: string[]
|
|
240
|
+
}
|
|
241
|
+
|
|
205
242
|
export const axios = ax.create({baseURL:${baseURL}});
|
|
206
243
|
${tsString}`;
|
|
207
244
|
const doubleQuoteRegex = /"([^"]+)":/g;
|
|
@@ -451,12 +488,14 @@ class BagelAuth {
|
|
|
451
488
|
}
|
|
452
489
|
}
|
|
453
490
|
class Bagel {
|
|
454
|
-
constructor({ host, onError }) {
|
|
491
|
+
constructor({ host, fileBaseUrl, onError }) {
|
|
455
492
|
__publicField(this, "host");
|
|
493
|
+
__publicField(this, "fileBaseUrl");
|
|
456
494
|
__publicField(this, "onError");
|
|
457
495
|
__publicField(this, "read_table");
|
|
458
496
|
__publicField(this, "auth", new BagelAuth(this));
|
|
459
497
|
this.host = host?.replace(/\/$/, "");
|
|
498
|
+
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
460
499
|
if (!this.host) {
|
|
461
500
|
return this;
|
|
462
501
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -189,11 +189,13 @@ class BagelAuth {
|
|
|
189
189
|
}
|
|
190
190
|
export class Bagel {
|
|
191
191
|
host?: string
|
|
192
|
+
fileBaseUrl?: string
|
|
192
193
|
|
|
193
194
|
onError?: (err: any) => void
|
|
194
195
|
|
|
195
|
-
constructor({ host, onError }: { [key: string]: any }) {
|
|
196
|
+
constructor({ host, fileBaseUrl, onError }: { [key: string]: any }) {
|
|
196
197
|
this.host = host?.replace(/\/$/, '')
|
|
198
|
+
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, '')
|
|
197
199
|
if (!this.host) {
|
|
198
200
|
return this
|
|
199
201
|
}
|
|
@@ -75,17 +75,29 @@ function generateAxiosFunction(
|
|
|
75
75
|
parameters: any,
|
|
76
76
|
requestBodyPayload: string,
|
|
77
77
|
): string {
|
|
78
|
-
|
|
79
|
-
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : ''
|
|
78
|
+
if (allParams === 'undefined') allParams = ''
|
|
80
79
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
80
|
+
let axiosFunction = `async (${allParams})${responseTypeStr} => {`
|
|
81
|
+
|
|
82
|
+
if (requestBodyPayload === 'formData') {
|
|
83
|
+
const paramStr = parameters?.config?.params
|
|
84
|
+
? `params: {${parameters.config.params}}`
|
|
85
|
+
: ''
|
|
85
86
|
|
|
86
|
-
axiosFunction +=
|
|
87
|
+
axiosFunction += `
|
|
88
|
+
const formData = new FormData()
|
|
89
|
+
formData.append('upload', file)
|
|
90
|
+
return axios.${method}(${formattedPath}, formData, {
|
|
91
|
+
headers: { 'Content-Type': 'multipart/form-data' },
|
|
92
|
+
onUploadProgress: options?.onUploadProgress${paramStr ? `,\n ${paramStr}` : ''}
|
|
93
|
+
})`
|
|
94
|
+
} else {
|
|
95
|
+
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : ''
|
|
96
|
+
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : '{}'
|
|
97
|
+
axiosFunction += `return axios.${method}(${formattedPath}${['get', 'delete'].includes(method) ? paramStr : `, ${bodyVar}${paramStr}`})`
|
|
87
98
|
}
|
|
88
|
-
|
|
99
|
+
|
|
100
|
+
axiosFunction += '}'
|
|
89
101
|
return axiosFunction
|
|
90
102
|
}
|
|
91
103
|
|
|
@@ -103,13 +115,28 @@ function formatPathWithParams(path: string) {
|
|
|
103
115
|
}
|
|
104
116
|
|
|
105
117
|
function generateRequestBody(requestBody?: RequestBodyObject): { [key: string]: string } {
|
|
106
|
-
|
|
107
|
-
|
|
118
|
+
if (!requestBody?.content) return { requestBodyParam: '', requestBodyPayload: '' }
|
|
119
|
+
|
|
120
|
+
const { content } = requestBody
|
|
121
|
+
const multipartFormData = content['multipart/form-data'] || {}
|
|
122
|
+
if (multipartFormData.schema?.$ref?.includes('Body_upload_files')) {
|
|
123
|
+
return {
|
|
124
|
+
requestBodyParam: 'file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }',
|
|
125
|
+
requestBodyPayload: 'formData'
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Handle regular JSON requests
|
|
130
|
+
const jsonContent = requestBody.content['application/json']
|
|
131
|
+
if (!jsonContent || !jsonContent.schema) {
|
|
132
|
+
return { requestBodyParam: '', requestBodyPayload: '' }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const bodySchema = jsonContent.schema
|
|
108
136
|
const requestBodyType = schemaToType(bodySchema)
|
|
109
137
|
collectTypeForImportStatement(requestBodyType)
|
|
110
138
|
const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || 'requestBody'
|
|
111
|
-
const
|
|
112
|
-
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, defaultValue)
|
|
139
|
+
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, bodySchema.default)
|
|
113
140
|
return { requestBodyParam, requestBodyPayload }
|
|
114
141
|
}
|
|
115
142
|
|
|
@@ -131,8 +158,19 @@ function combineAllParams(parameters: { params?: string }, requestBodyParam: str
|
|
|
131
158
|
return allParamsArray.join(', ')
|
|
132
159
|
}
|
|
133
160
|
|
|
134
|
-
function generateFunctionParameters(params?: ParameterObject[]) {
|
|
135
|
-
if (!params
|
|
161
|
+
function generateFunctionParameters(params?: ParameterObject[], isFileUpload = false) {
|
|
162
|
+
if (!params?.length) return {}
|
|
163
|
+
|
|
164
|
+
// For file uploads, we want to handle query parameters differently
|
|
165
|
+
if (isFileUpload) {
|
|
166
|
+
// Don't include these as function parameters, they'll be part of options
|
|
167
|
+
return {
|
|
168
|
+
config: {
|
|
169
|
+
params: 'dir_path: options?.dirPath, tags: options?.tags'
|
|
170
|
+
},
|
|
171
|
+
params: '' // Empty string to ensure file is the first parameter
|
|
172
|
+
}
|
|
173
|
+
}
|
|
136
174
|
|
|
137
175
|
const functionParams: string[] = []
|
|
138
176
|
const paramList: string[] = []
|
|
@@ -141,34 +179,47 @@ function generateFunctionParameters(params?: ParameterObject[]) {
|
|
|
141
179
|
const paramType = schemaToType(param.schema)
|
|
142
180
|
collectTypeForImportStatement(paramType)
|
|
143
181
|
const paramName = param.name
|
|
144
|
-
const varName = toCamelCase(param.name)
|
|
182
|
+
const varName = toCamelCase(param.name)
|
|
183
|
+
|
|
145
184
|
if (param.in === 'path' || param.in === 'query' || param.in === 'header') {
|
|
146
|
-
|
|
147
|
-
const varType = formatVarType(varName, param.schema, param.required, defaultValue)
|
|
148
|
-
functionParams.push(varType)
|
|
185
|
+
functionParams.push(formatVarType(varName, param.schema, param.required, param.schema.default))
|
|
149
186
|
}
|
|
187
|
+
|
|
150
188
|
if (param.in === 'query' || param.in === 'header') {
|
|
151
|
-
|
|
152
|
-
else paramList.push(`'${paramName}': ${varName}`)
|
|
189
|
+
paramList.push(paramName === varName ? paramName : `'${paramName}': ${varName}`)
|
|
153
190
|
}
|
|
154
191
|
}
|
|
155
192
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return { params: paramsString, config }
|
|
193
|
+
return {
|
|
194
|
+
params: functionParams.join(', '),
|
|
195
|
+
config: paramList.length ? { params: paramList.join(', ') } : {}
|
|
196
|
+
}
|
|
161
197
|
}
|
|
162
198
|
|
|
163
199
|
function generateFunctionForOperation(method: string, path: string, operation: OperationObject): string {
|
|
164
200
|
if (!operation) return ''
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
201
|
+
|
|
202
|
+
// Check if this is a file upload operation by looking at the schema reference
|
|
203
|
+
const isFileUpload = operation.requestBody?.content['multipart/form-data']?.schema?.$ref?.includes('Body_upload_files')
|
|
204
|
+
const parameters = generateFunctionParameters(operation.parameters, isFileUpload)
|
|
168
205
|
const { requestBodyParam, requestBodyPayload } = generateRequestBody(operation.requestBody)
|
|
169
|
-
|
|
206
|
+
|
|
207
|
+
// For file uploads, ignore the regular parameter generation
|
|
208
|
+
const allParams = isFileUpload
|
|
209
|
+
? 'file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }'
|
|
210
|
+
: combineAllParams(parameters, requestBodyParam)
|
|
211
|
+
|
|
212
|
+
const responseType = generateResponseType(operation.responses)
|
|
170
213
|
const responseTypeStr = responseType ? `: Promise<AxiosResponse<${responseType}>>` : ''
|
|
171
|
-
|
|
214
|
+
|
|
215
|
+
return generateAxiosFunction(
|
|
216
|
+
method,
|
|
217
|
+
formatPathWithParams(path),
|
|
218
|
+
allParams,
|
|
219
|
+
responseTypeStr,
|
|
220
|
+
parameters,
|
|
221
|
+
requestBodyPayload
|
|
222
|
+
)
|
|
172
223
|
}
|
|
173
224
|
|
|
174
225
|
const generateRandomString = () => Math.random().toString(36).slice(7)
|
|
@@ -179,6 +230,12 @@ function fileTemplate(tsString: string, typeForImport: string[], baseURL: string
|
|
|
179
230
|
import type { AxiosResponse } from 'axios';
|
|
180
231
|
import type {${typeForImport.join(', ')}} from './types.d';
|
|
181
232
|
|
|
233
|
+
interface UploadOptions {
|
|
234
|
+
onUploadProgress?: (progressEvent: any) => void
|
|
235
|
+
dirPath?: string
|
|
236
|
+
tags?: string[]
|
|
237
|
+
}
|
|
238
|
+
|
|
182
239
|
export const axios = ax.create({baseURL:${baseURL}});
|
|
183
240
|
${tsString}`
|
|
184
241
|
)
|
|
@@ -186,8 +243,13 @@ function fileTemplate(tsString: string, typeForImport: string[], baseURL: string
|
|
|
186
243
|
return templateCode.replace(doubleQuoteRegex, '$1:')
|
|
187
244
|
}
|
|
188
245
|
|
|
189
|
-
|
|
190
|
-
|
|
246
|
+
interface PathOperation {
|
|
247
|
+
path: string
|
|
248
|
+
method: string
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const functionsInventory: Record<string, string> = {}
|
|
252
|
+
const pathOperations: PathOperation[] = []
|
|
191
253
|
|
|
192
254
|
function hasConflict(path: string, method: string) {
|
|
193
255
|
const cleanPathName = path.split('/').filter(p => p && !(/\{|\}/).test(p)).join('/')
|
|
@@ -17,6 +17,9 @@ export interface PathItemObject {
|
|
|
17
17
|
put?: OperationObject
|
|
18
18
|
post?: OperationObject
|
|
19
19
|
delete?: OperationObject
|
|
20
|
+
patch?: OperationObject
|
|
21
|
+
options?: OperationObject
|
|
22
|
+
head?: OperationObject
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
export interface SchemasObject { [schema: string]: SchemaObject }
|
|
@@ -37,15 +40,17 @@ export interface ResponsesObject { [statusCode: string]: ResponseObject }
|
|
|
37
40
|
|
|
38
41
|
export interface SchemaObject {
|
|
39
42
|
type?: string
|
|
43
|
+
format?: string
|
|
40
44
|
properties?: { [property: string]: SchemaObject }
|
|
41
45
|
items?: SchemaObject
|
|
42
46
|
$ref?: string
|
|
43
|
-
enum
|
|
47
|
+
enum?: string[]
|
|
44
48
|
anyOf?: SchemaObject[]
|
|
45
49
|
allOf?: SchemaObject[]
|
|
46
50
|
title?: string
|
|
47
51
|
description?: string
|
|
48
|
-
default
|
|
52
|
+
default?: any
|
|
53
|
+
required?: string[]
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
export interface ParameterObject {
|
|
@@ -58,6 +63,7 @@ export interface ParameterObject {
|
|
|
58
63
|
export interface RequestBodyObject {
|
|
59
64
|
description?: string
|
|
60
65
|
content: { [mediaType: string]: MediaTypeObject }
|
|
66
|
+
required?: boolean
|
|
61
67
|
}
|
|
62
68
|
|
|
63
69
|
export interface ResponseObject {
|
|
@@ -67,4 +73,19 @@ export interface ResponseObject {
|
|
|
67
73
|
|
|
68
74
|
export interface MediaTypeObject {
|
|
69
75
|
schema?: SchemaObject
|
|
76
|
+
encoding?: { [key: string]: EncodingObject }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface EncodingObject {
|
|
80
|
+
contentType?: string
|
|
81
|
+
headers?: { [key: string]: HeaderObject }
|
|
82
|
+
style?: string
|
|
83
|
+
explode?: boolean
|
|
84
|
+
allowReserved?: boolean
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface HeaderObject {
|
|
88
|
+
description?: string
|
|
89
|
+
required?: boolean
|
|
90
|
+
schema?: SchemaObject
|
|
70
91
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SchemaObject } from './openApiTypes'
|
|
2
2
|
|
|
3
|
-
export const toCamelCase = (str?: string) => str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || '').replace(/^./, str => str.toLowerCase()) || str
|
|
4
|
-
export const toPascalCase = (str?: string) => str?.replaceAll(/[-_\s]+(.)?/g, (_, c) => c?.toUpperCase() || '').replace(/^./, str => str.toUpperCase()) || str
|
|
3
|
+
export const toCamelCase = (str?: string) => str?.replaceAll(/[-._\s]+(.)?/g, (_, c) => c?.toUpperCase() || '').replace(/^./, str => str.toLowerCase()) || str || ''
|
|
4
|
+
export const toPascalCase = (str?: string) => str?.replaceAll(/[-_\s]+(.)?/g, (_, c) => c?.toUpperCase() || '').replace(/^./, str => str.toUpperCase()) || str || ''
|
|
5
5
|
|
|
6
6
|
export function formatType(typeName: string): string {
|
|
7
7
|
// eslint-disable-next-line regexp/no-unused-capturing-group
|