@bagelink/sdk 0.0.451 → 0.0.458
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
CHANGED
|
@@ -48,18 +48,20 @@ const schemaToType = (schema) => {
|
|
|
48
48
|
};
|
|
49
49
|
const isOptional = (schema) => {
|
|
50
50
|
const type = schemaToType(schema);
|
|
51
|
-
return type?.split(/\s+\|\s+/).includes("null");
|
|
51
|
+
return type?.split(/\s+\|\s+/).includes("null") || schema?.default !== void 0;
|
|
52
52
|
};
|
|
53
53
|
const cleanNulls = (str) => str.split(" | ").filter((t) => t !== "null").join(" | ");
|
|
54
54
|
function formatVarType(varName, schema, required = false, defaultValue = null) {
|
|
55
55
|
let type = schemaToType(schema);
|
|
56
|
-
const optionalStr = required || isOptional(schema) ? "?" : "";
|
|
57
56
|
type = cleanNulls(type);
|
|
58
57
|
let defaultStr = defaultValue ? ` = ${defaultValue}` : "";
|
|
59
58
|
if (defaultStr && type === "string")
|
|
60
59
|
defaultStr = ` = '${defaultValue}'`;
|
|
61
60
|
else if (defaultValue && (typeof defaultValue === "object" || defaultValue === "{}"))
|
|
62
61
|
defaultStr = " = {}";
|
|
62
|
+
let optionalStr = required || isOptional(schema) ? "?" : "";
|
|
63
|
+
if (defaultStr)
|
|
64
|
+
optionalStr = "";
|
|
63
65
|
return `${varName}${optionalStr}: ${type}${defaultStr}`;
|
|
64
66
|
}
|
|
65
67
|
function cleanPath(path) {
|
|
@@ -128,9 +130,9 @@ function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr
|
|
|
128
130
|
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : "";
|
|
129
131
|
if (["get", "delete"].includes(method)) {
|
|
130
132
|
axiosFunction += `${formattedPath}${paramStr}`;
|
|
131
|
-
} else if (["post", "put", "patch"].includes(method)) {
|
|
132
|
-
const bodyVar = requestBodyPayload ?
|
|
133
|
-
axiosFunction += `${formattedPath}${bodyVar}${paramStr}`;
|
|
133
|
+
} else if (["post", "put", "patch", "delete"].includes(method)) {
|
|
134
|
+
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : "{}";
|
|
135
|
+
axiosFunction += `${formattedPath}, ${bodyVar}${paramStr}`;
|
|
134
136
|
}
|
|
135
137
|
axiosFunction += ")";
|
|
136
138
|
return axiosFunction;
|
|
@@ -208,9 +210,9 @@ const generateRandomString = () => Math.random().toString(36).substring(7);
|
|
|
208
210
|
function fileTemplate(tsString, typeForImport, baseURL) {
|
|
209
211
|
const templateCode = `import axios, { type AxiosResponse } from 'axios';
|
|
210
212
|
import type {${typeForImport.join(", ")}} from './types.d';
|
|
211
|
-
|
|
213
|
+
|
|
212
214
|
axios.defaults.baseURL = ${baseURL};
|
|
213
|
-
|
|
215
|
+
|
|
214
216
|
${tsString}`;
|
|
215
217
|
const doubleQuoteRegex = /"([^"]+)":/g;
|
|
216
218
|
return templateCode.replace(doubleQuoteRegex, "$1:");
|
package/dist/index.mjs
CHANGED
|
@@ -42,18 +42,20 @@ const schemaToType = (schema) => {
|
|
|
42
42
|
};
|
|
43
43
|
const isOptional = (schema) => {
|
|
44
44
|
const type = schemaToType(schema);
|
|
45
|
-
return type?.split(/\s+\|\s+/).includes("null");
|
|
45
|
+
return type?.split(/\s+\|\s+/).includes("null") || schema?.default !== void 0;
|
|
46
46
|
};
|
|
47
47
|
const cleanNulls = (str) => str.split(" | ").filter((t) => t !== "null").join(" | ");
|
|
48
48
|
function formatVarType(varName, schema, required = false, defaultValue = null) {
|
|
49
49
|
let type = schemaToType(schema);
|
|
50
|
-
const optionalStr = required || isOptional(schema) ? "?" : "";
|
|
51
50
|
type = cleanNulls(type);
|
|
52
51
|
let defaultStr = defaultValue ? ` = ${defaultValue}` : "";
|
|
53
52
|
if (defaultStr && type === "string")
|
|
54
53
|
defaultStr = ` = '${defaultValue}'`;
|
|
55
54
|
else if (defaultValue && (typeof defaultValue === "object" || defaultValue === "{}"))
|
|
56
55
|
defaultStr = " = {}";
|
|
56
|
+
let optionalStr = required || isOptional(schema) ? "?" : "";
|
|
57
|
+
if (defaultStr)
|
|
58
|
+
optionalStr = "";
|
|
57
59
|
return `${varName}${optionalStr}: ${type}${defaultStr}`;
|
|
58
60
|
}
|
|
59
61
|
function cleanPath(path) {
|
|
@@ -122,9 +124,9 @@ function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr
|
|
|
122
124
|
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : "";
|
|
123
125
|
if (["get", "delete"].includes(method)) {
|
|
124
126
|
axiosFunction += `${formattedPath}${paramStr}`;
|
|
125
|
-
} else if (["post", "put", "patch"].includes(method)) {
|
|
126
|
-
const bodyVar = requestBodyPayload ?
|
|
127
|
-
axiosFunction += `${formattedPath}${bodyVar}${paramStr}`;
|
|
127
|
+
} else if (["post", "put", "patch", "delete"].includes(method)) {
|
|
128
|
+
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : "{}";
|
|
129
|
+
axiosFunction += `${formattedPath}, ${bodyVar}${paramStr}`;
|
|
128
130
|
}
|
|
129
131
|
axiosFunction += ")";
|
|
130
132
|
return axiosFunction;
|
|
@@ -202,9 +204,9 @@ const generateRandomString = () => Math.random().toString(36).substring(7);
|
|
|
202
204
|
function fileTemplate(tsString, typeForImport, baseURL) {
|
|
203
205
|
const templateCode = `import axios, { type AxiosResponse } from 'axios';
|
|
204
206
|
import type {${typeForImport.join(", ")}} from './types.d';
|
|
205
|
-
|
|
207
|
+
|
|
206
208
|
axios.defaults.baseURL = ${baseURL};
|
|
207
|
-
|
|
209
|
+
|
|
208
210
|
${tsString}`;
|
|
209
211
|
const doubleQuoteRegex = /"([^"]+)":/g;
|
|
210
212
|
return templateCode.replace(doubleQuoteRegex, "$1:");
|
package/package.json
CHANGED
|
@@ -56,6 +56,7 @@ function generateResponseType(responses?: ResponsesObject): string {
|
|
|
56
56
|
* @param parameters - Additional configuration parameters, if any.
|
|
57
57
|
* @returns A string representing the Axios function.
|
|
58
58
|
*/
|
|
59
|
+
|
|
59
60
|
function generateAxiosFunction(
|
|
60
61
|
method: string,
|
|
61
62
|
formattedPath: string,
|
|
@@ -65,14 +66,14 @@ function generateAxiosFunction(
|
|
|
65
66
|
requestBodyPayload: string,
|
|
66
67
|
): string {
|
|
67
68
|
let axiosFunction = `async (${allParams})${responseTypeStr} => axios.${method}(`;
|
|
68
|
-
// Different handling based on the HTTP method
|
|
69
69
|
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : '';
|
|
70
|
+
|
|
70
71
|
if (['get', 'delete'].includes(method)) {
|
|
71
72
|
axiosFunction += `${formattedPath}${paramStr}`;
|
|
72
|
-
} else if (['post', 'put', 'patch'].includes(method)) {
|
|
73
|
-
const bodyVar = requestBodyPayload ?
|
|
73
|
+
} else if (['post', 'put', 'patch', 'delete'].includes(method)) {
|
|
74
|
+
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : '{}';
|
|
74
75
|
|
|
75
|
-
axiosFunction += `${formattedPath}${bodyVar}${paramStr}`;
|
|
76
|
+
axiosFunction += `${formattedPath}, ${bodyVar}${paramStr}`;
|
|
76
77
|
}
|
|
77
78
|
axiosFunction += ')';
|
|
78
79
|
return axiosFunction;
|
|
@@ -164,9 +165,9 @@ function fileTemplate(tsString: string, typeForImport: string[], baseURL: string
|
|
|
164
165
|
const templateCode = (
|
|
165
166
|
`import axios, { type AxiosResponse } from 'axios';
|
|
166
167
|
import type {${typeForImport.join(', ')}} from './types.d';
|
|
167
|
-
|
|
168
|
+
|
|
168
169
|
axios.defaults.baseURL = ${baseURL};
|
|
169
|
-
|
|
170
|
+
|
|
170
171
|
${tsString}`
|
|
171
172
|
);
|
|
172
173
|
const doubleQuoteRegex = /"([^"]+)":/g;
|
|
@@ -42,18 +42,23 @@ export const schemaToType = (schema?: SchemaObject): string => {
|
|
|
42
42
|
export const isOptional = (schema: SchemaObject) => {
|
|
43
43
|
/// / !schema?.required?.includes(schema.title || '')
|
|
44
44
|
const type = schemaToType(schema);
|
|
45
|
-
return type?.split(/\s+\|\s+/).includes('null');
|
|
45
|
+
return type?.split(/\s+\|\s+/).includes('null') || schema?.default !== undefined;
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
export const cleanNulls = (str: string) => str.split(' | ').filter((t) => t !== 'null').join(' | ');
|
|
49
49
|
|
|
50
50
|
export function formatVarType(varName: string, schema: any, required = false, defaultValue: any = null) {
|
|
51
51
|
let type = schemaToType(schema);
|
|
52
|
-
const optionalStr = required || isOptional(schema) ? '?' : '';
|
|
53
52
|
type = cleanNulls(type);
|
|
53
|
+
|
|
54
54
|
let defaultStr = defaultValue ? ` = ${defaultValue}` : '';
|
|
55
|
+
|
|
55
56
|
if (defaultStr && type === 'string') defaultStr = ` = '${defaultValue}'`;
|
|
56
57
|
else if (defaultValue && (typeof defaultValue === 'object' || defaultValue === '{}')) defaultStr = ' = {}';
|
|
58
|
+
|
|
59
|
+
let optionalStr = required || isOptional(schema) ? '?' : '';
|
|
60
|
+
if (defaultStr) optionalStr = '';
|
|
61
|
+
|
|
57
62
|
return `${varName}${optionalStr}: ${type}${defaultStr}`;
|
|
58
63
|
}
|
|
59
64
|
|