@bagelink/sdk 0.0.466 → 0.0.471
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 +43 -39
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +43 -39
- package/package.json +2 -2
- package/src/index.ts +130 -131
- package/src/openAPITools/functionGenerator.ts +132 -122
- package/src/openAPITools/index.ts +17 -17
- package/src/openAPITools/openApiTypes.ts +38 -38
- package/src/openAPITools/typeGenerator.ts +16 -14
- package/src/openAPITools/utils.ts +46 -46
package/dist/index.cjs
CHANGED
|
@@ -12,13 +12,13 @@ function formatType(typeName) {
|
|
|
12
12
|
typeName = typeName.replace(/-/g, "").replace(/(post|put)$/gi, "").replace(/^body_/gi, "");
|
|
13
13
|
return toPascalCase(typeName);
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
function resolveReference(ref) {
|
|
16
16
|
const t = ref.split("/").pop();
|
|
17
17
|
if (!t)
|
|
18
18
|
return "any";
|
|
19
19
|
return formatType(t);
|
|
20
|
-
}
|
|
21
|
-
|
|
20
|
+
}
|
|
21
|
+
function schemaToType(schema) {
|
|
22
22
|
if (!schema)
|
|
23
23
|
return "any";
|
|
24
24
|
if (schema.anyOf)
|
|
@@ -45,11 +45,11 @@ const schemaToType = (schema) => {
|
|
|
45
45
|
default:
|
|
46
46
|
return "any";
|
|
47
47
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
48
|
+
}
|
|
49
|
+
function isOptional(schema) {
|
|
50
50
|
const type = schemaToType(schema);
|
|
51
|
-
return type
|
|
52
|
-
}
|
|
51
|
+
return type.split(/\s+\|\s+/).includes("null") || schema.default !== void 0;
|
|
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);
|
|
@@ -68,30 +68,34 @@ function cleanPath(path) {
|
|
|
68
68
|
return path.split("/").filter((p) => p && !p.match(/\{|\}/)).join("/");
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
function generateTypes(schemas) {
|
|
72
|
+
return Object.entries(schemas).map(([typeName, schema]) => {
|
|
73
|
+
typeName = formatType(typeName);
|
|
74
|
+
if (schema.enum) {
|
|
75
|
+
return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
|
|
75
76
|
`;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
}
|
|
78
|
+
if (!schema.properties)
|
|
79
|
+
return "";
|
|
80
|
+
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
81
|
+
const varType = formatVarType(key, value);
|
|
82
|
+
return ` ${varType}`;
|
|
83
|
+
}).join(";\n ");
|
|
84
|
+
return `export type ${typeName} = {
|
|
84
85
|
${properties};
|
|
85
86
|
};
|
|
86
87
|
`;
|
|
87
|
-
}).join("\n");
|
|
88
|
+
}).join("\n");
|
|
89
|
+
}
|
|
88
90
|
|
|
89
91
|
const allTypes = [];
|
|
90
92
|
function collectTypeForImportStatement(typeName) {
|
|
91
93
|
typeName = typeName.trim().replace("[]", "");
|
|
92
94
|
if (typeName.includes("|")) {
|
|
93
95
|
typeName.split("|").forEach(
|
|
94
|
-
(singleType) =>
|
|
96
|
+
(singleType) => {
|
|
97
|
+
collectTypeForImportStatement(singleType);
|
|
98
|
+
}
|
|
95
99
|
);
|
|
96
100
|
return;
|
|
97
101
|
}
|
|
@@ -123,7 +127,7 @@ function generateResponseType(responses) {
|
|
|
123
127
|
}
|
|
124
128
|
}
|
|
125
129
|
}
|
|
126
|
-
return types
|
|
130
|
+
return types.join(" | ");
|
|
127
131
|
}
|
|
128
132
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
129
133
|
let axiosFunction = `async (${allParams})${responseTypeStr} => axios.${method}(`;
|
|
@@ -138,23 +142,23 @@ function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr
|
|
|
138
142
|
return axiosFunction;
|
|
139
143
|
}
|
|
140
144
|
const pathParamRegex = /\{([^}]+)\}/g;
|
|
141
|
-
|
|
145
|
+
function getParamsFromPath(path) {
|
|
142
146
|
const params = path.match(pathParamRegex)?.map((p) => p.slice(1, -1));
|
|
143
147
|
return params;
|
|
144
|
-
}
|
|
148
|
+
}
|
|
145
149
|
function formatPathWithParams(path) {
|
|
146
150
|
const params = getParamsFromPath(path);
|
|
147
151
|
const formattedPath = params ? `\`${path.replace(pathParamRegex, (v) => `$${toCamelCase(v)}`)}\`` : `'${path}'`;
|
|
148
152
|
return formattedPath;
|
|
149
153
|
}
|
|
150
154
|
function generateRequestBody(requestBody) {
|
|
151
|
-
const bodySchema = requestBody?.content
|
|
155
|
+
const bodySchema = requestBody?.content["application/json"]?.schema;
|
|
152
156
|
if (!bodySchema)
|
|
153
157
|
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
154
158
|
const requestBodyType = schemaToType(bodySchema);
|
|
155
159
|
collectTypeForImportStatement(requestBodyType);
|
|
156
|
-
const requestBodyPayload = toCamelCase(bodySchema
|
|
157
|
-
const defaultValue = requestBody
|
|
160
|
+
const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || "requestBody";
|
|
161
|
+
const defaultValue = requestBody.content["application/json"].schema?.default;
|
|
158
162
|
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, defaultValue);
|
|
159
163
|
return { requestBodyParam, requestBodyPayload };
|
|
160
164
|
}
|
|
@@ -178,7 +182,7 @@ function generateFunctionParameters(params) {
|
|
|
178
182
|
const paramName = param.name;
|
|
179
183
|
const varName = toCamelCase(param.name) || "param";
|
|
180
184
|
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
181
|
-
const defaultValue = param
|
|
185
|
+
const defaultValue = param.schema.default;
|
|
182
186
|
const varType = formatVarType(varName, param.schema, param.required, defaultValue);
|
|
183
187
|
functionParams.push(varType);
|
|
184
188
|
}
|
|
@@ -219,17 +223,17 @@ function fileTemplate(tsString, typeForImport, baseURL) {
|
|
|
219
223
|
}
|
|
220
224
|
const functionsInventory = {};
|
|
221
225
|
const pathOperations = [];
|
|
222
|
-
|
|
226
|
+
function hasConflict(path, method) {
|
|
223
227
|
const cleanPathName = path.split("/").filter((p) => p && !p.match(/\{|\}/)).join("/");
|
|
224
228
|
const matchingPaths = pathOperations.filter((p) => p.path === cleanPathName && p.method === method);
|
|
225
229
|
pathOperations.push({ path: cleanPathName, method });
|
|
226
230
|
return matchingPaths.length > 0;
|
|
227
|
-
}
|
|
228
|
-
|
|
231
|
+
}
|
|
232
|
+
function createFunctionPlaceholder(path, method, operation) {
|
|
229
233
|
const funcID = generateRandomString();
|
|
230
234
|
functionsInventory[funcID] = generateFunctionForOperation(method, path, operation);
|
|
231
235
|
return funcID;
|
|
232
|
-
}
|
|
236
|
+
}
|
|
233
237
|
function handlePathSegment(path, operation, existingObj = null) {
|
|
234
238
|
const methods = Object.keys(operation);
|
|
235
239
|
const obj = {};
|
|
@@ -237,7 +241,7 @@ function handlePathSegment(path, operation, existingObj = null) {
|
|
|
237
241
|
let functionName = method.toLowerCase();
|
|
238
242
|
if (hasConflict(path, method)) {
|
|
239
243
|
const params = getParamsFromPath(path);
|
|
240
|
-
functionName += params ? `By${toPascalCase(params
|
|
244
|
+
functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
|
|
241
245
|
}
|
|
242
246
|
obj[functionName] = createFunctionPlaceholder(path, method, operation[method]);
|
|
243
247
|
}
|
|
@@ -258,10 +262,11 @@ function generateFunctions(paths, baseURL) {
|
|
|
258
262
|
const method = methods[0];
|
|
259
263
|
const opp = { ...operation }[method];
|
|
260
264
|
acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
|
|
261
|
-
} else if (index === array.length - 1)
|
|
265
|
+
} else if (index === array.length - 1) {
|
|
262
266
|
acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
|
|
263
|
-
else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object")
|
|
267
|
+
} else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
|
|
264
268
|
acc[objFuncKey] = {};
|
|
269
|
+
}
|
|
265
270
|
return acc[objFuncKey];
|
|
266
271
|
}, body);
|
|
267
272
|
}
|
|
@@ -399,13 +404,13 @@ class BagelAuth {
|
|
|
399
404
|
}
|
|
400
405
|
async login(user) {
|
|
401
406
|
const formData = new FormData();
|
|
402
|
-
formData.append("username", user
|
|
403
|
-
formData.append("password", user
|
|
407
|
+
formData.append("username", user.email || "");
|
|
408
|
+
formData.append("password", user.password || "");
|
|
404
409
|
console.log("Logging in");
|
|
405
410
|
try {
|
|
406
411
|
await axios.post("/auth/cookie/login", formData, {
|
|
407
412
|
headers: {
|
|
408
|
-
withCredentials: true,
|
|
413
|
+
"withCredentials": true,
|
|
409
414
|
"Content-Type": "multipart/form-data"
|
|
410
415
|
}
|
|
411
416
|
});
|
|
@@ -442,7 +447,6 @@ class BagelAuth {
|
|
|
442
447
|
class Bagel {
|
|
443
448
|
constructor({ host, onError }) {
|
|
444
449
|
__publicField(this, "host");
|
|
445
|
-
// eslint-disable-next-line no-unused-vars
|
|
446
450
|
__publicField(this, "onError");
|
|
447
451
|
__publicField(this, "read_table", null);
|
|
448
452
|
__publicField(this, "auth", new BagelAuth(this));
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -6,13 +6,13 @@ function formatType(typeName) {
|
|
|
6
6
|
typeName = typeName.replace(/-/g, "").replace(/(post|put)$/gi, "").replace(/^body_/gi, "");
|
|
7
7
|
return toPascalCase(typeName);
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
function resolveReference(ref) {
|
|
10
10
|
const t = ref.split("/").pop();
|
|
11
11
|
if (!t)
|
|
12
12
|
return "any";
|
|
13
13
|
return formatType(t);
|
|
14
|
-
}
|
|
15
|
-
|
|
14
|
+
}
|
|
15
|
+
function schemaToType(schema) {
|
|
16
16
|
if (!schema)
|
|
17
17
|
return "any";
|
|
18
18
|
if (schema.anyOf)
|
|
@@ -39,11 +39,11 @@ const schemaToType = (schema) => {
|
|
|
39
39
|
default:
|
|
40
40
|
return "any";
|
|
41
41
|
}
|
|
42
|
-
}
|
|
43
|
-
|
|
42
|
+
}
|
|
43
|
+
function isOptional(schema) {
|
|
44
44
|
const type = schemaToType(schema);
|
|
45
|
-
return type
|
|
46
|
-
}
|
|
45
|
+
return type.split(/\s+\|\s+/).includes("null") || schema.default !== void 0;
|
|
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);
|
|
@@ -62,30 +62,34 @@ function cleanPath(path) {
|
|
|
62
62
|
return path.split("/").filter((p) => p && !p.match(/\{|\}/)).join("/");
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
function generateTypes(schemas) {
|
|
66
|
+
return Object.entries(schemas).map(([typeName, schema]) => {
|
|
67
|
+
typeName = formatType(typeName);
|
|
68
|
+
if (schema.enum) {
|
|
69
|
+
return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
|
|
69
70
|
`;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
}
|
|
72
|
+
if (!schema.properties)
|
|
73
|
+
return "";
|
|
74
|
+
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
75
|
+
const varType = formatVarType(key, value);
|
|
76
|
+
return ` ${varType}`;
|
|
77
|
+
}).join(";\n ");
|
|
78
|
+
return `export type ${typeName} = {
|
|
78
79
|
${properties};
|
|
79
80
|
};
|
|
80
81
|
`;
|
|
81
|
-
}).join("\n");
|
|
82
|
+
}).join("\n");
|
|
83
|
+
}
|
|
82
84
|
|
|
83
85
|
const allTypes = [];
|
|
84
86
|
function collectTypeForImportStatement(typeName) {
|
|
85
87
|
typeName = typeName.trim().replace("[]", "");
|
|
86
88
|
if (typeName.includes("|")) {
|
|
87
89
|
typeName.split("|").forEach(
|
|
88
|
-
(singleType) =>
|
|
90
|
+
(singleType) => {
|
|
91
|
+
collectTypeForImportStatement(singleType);
|
|
92
|
+
}
|
|
89
93
|
);
|
|
90
94
|
return;
|
|
91
95
|
}
|
|
@@ -117,7 +121,7 @@ function generateResponseType(responses) {
|
|
|
117
121
|
}
|
|
118
122
|
}
|
|
119
123
|
}
|
|
120
|
-
return types
|
|
124
|
+
return types.join(" | ");
|
|
121
125
|
}
|
|
122
126
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
123
127
|
let axiosFunction = `async (${allParams})${responseTypeStr} => axios.${method}(`;
|
|
@@ -132,23 +136,23 @@ function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr
|
|
|
132
136
|
return axiosFunction;
|
|
133
137
|
}
|
|
134
138
|
const pathParamRegex = /\{([^}]+)\}/g;
|
|
135
|
-
|
|
139
|
+
function getParamsFromPath(path) {
|
|
136
140
|
const params = path.match(pathParamRegex)?.map((p) => p.slice(1, -1));
|
|
137
141
|
return params;
|
|
138
|
-
}
|
|
142
|
+
}
|
|
139
143
|
function formatPathWithParams(path) {
|
|
140
144
|
const params = getParamsFromPath(path);
|
|
141
145
|
const formattedPath = params ? `\`${path.replace(pathParamRegex, (v) => `$${toCamelCase(v)}`)}\`` : `'${path}'`;
|
|
142
146
|
return formattedPath;
|
|
143
147
|
}
|
|
144
148
|
function generateRequestBody(requestBody) {
|
|
145
|
-
const bodySchema = requestBody?.content
|
|
149
|
+
const bodySchema = requestBody?.content["application/json"]?.schema;
|
|
146
150
|
if (!bodySchema)
|
|
147
151
|
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
148
152
|
const requestBodyType = schemaToType(bodySchema);
|
|
149
153
|
collectTypeForImportStatement(requestBodyType);
|
|
150
|
-
const requestBodyPayload = toCamelCase(bodySchema
|
|
151
|
-
const defaultValue = requestBody
|
|
154
|
+
const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || "requestBody";
|
|
155
|
+
const defaultValue = requestBody.content["application/json"].schema?.default;
|
|
152
156
|
const requestBodyParam = formatVarType(requestBodyPayload, bodySchema, defaultValue);
|
|
153
157
|
return { requestBodyParam, requestBodyPayload };
|
|
154
158
|
}
|
|
@@ -172,7 +176,7 @@ function generateFunctionParameters(params) {
|
|
|
172
176
|
const paramName = param.name;
|
|
173
177
|
const varName = toCamelCase(param.name) || "param";
|
|
174
178
|
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
175
|
-
const defaultValue = param
|
|
179
|
+
const defaultValue = param.schema.default;
|
|
176
180
|
const varType = formatVarType(varName, param.schema, param.required, defaultValue);
|
|
177
181
|
functionParams.push(varType);
|
|
178
182
|
}
|
|
@@ -213,17 +217,17 @@ function fileTemplate(tsString, typeForImport, baseURL) {
|
|
|
213
217
|
}
|
|
214
218
|
const functionsInventory = {};
|
|
215
219
|
const pathOperations = [];
|
|
216
|
-
|
|
220
|
+
function hasConflict(path, method) {
|
|
217
221
|
const cleanPathName = path.split("/").filter((p) => p && !p.match(/\{|\}/)).join("/");
|
|
218
222
|
const matchingPaths = pathOperations.filter((p) => p.path === cleanPathName && p.method === method);
|
|
219
223
|
pathOperations.push({ path: cleanPathName, method });
|
|
220
224
|
return matchingPaths.length > 0;
|
|
221
|
-
}
|
|
222
|
-
|
|
225
|
+
}
|
|
226
|
+
function createFunctionPlaceholder(path, method, operation) {
|
|
223
227
|
const funcID = generateRandomString();
|
|
224
228
|
functionsInventory[funcID] = generateFunctionForOperation(method, path, operation);
|
|
225
229
|
return funcID;
|
|
226
|
-
}
|
|
230
|
+
}
|
|
227
231
|
function handlePathSegment(path, operation, existingObj = null) {
|
|
228
232
|
const methods = Object.keys(operation);
|
|
229
233
|
const obj = {};
|
|
@@ -231,7 +235,7 @@ function handlePathSegment(path, operation, existingObj = null) {
|
|
|
231
235
|
let functionName = method.toLowerCase();
|
|
232
236
|
if (hasConflict(path, method)) {
|
|
233
237
|
const params = getParamsFromPath(path);
|
|
234
|
-
functionName += params ? `By${toPascalCase(params
|
|
238
|
+
functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
|
|
235
239
|
}
|
|
236
240
|
obj[functionName] = createFunctionPlaceholder(path, method, operation[method]);
|
|
237
241
|
}
|
|
@@ -252,10 +256,11 @@ function generateFunctions(paths, baseURL) {
|
|
|
252
256
|
const method = methods[0];
|
|
253
257
|
const opp = { ...operation }[method];
|
|
254
258
|
acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
|
|
255
|
-
} else if (index === array.length - 1)
|
|
259
|
+
} else if (index === array.length - 1) {
|
|
256
260
|
acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
|
|
257
|
-
else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object")
|
|
261
|
+
} else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
|
|
258
262
|
acc[objFuncKey] = {};
|
|
263
|
+
}
|
|
259
264
|
return acc[objFuncKey];
|
|
260
265
|
}, body);
|
|
261
266
|
}
|
|
@@ -393,13 +398,13 @@ class BagelAuth {
|
|
|
393
398
|
}
|
|
394
399
|
async login(user) {
|
|
395
400
|
const formData = new FormData();
|
|
396
|
-
formData.append("username", user
|
|
397
|
-
formData.append("password", user
|
|
401
|
+
formData.append("username", user.email || "");
|
|
402
|
+
formData.append("password", user.password || "");
|
|
398
403
|
console.log("Logging in");
|
|
399
404
|
try {
|
|
400
405
|
await axios.post("/auth/cookie/login", formData, {
|
|
401
406
|
headers: {
|
|
402
|
-
withCredentials: true,
|
|
407
|
+
"withCredentials": true,
|
|
403
408
|
"Content-Type": "multipart/form-data"
|
|
404
409
|
}
|
|
405
410
|
});
|
|
@@ -436,7 +441,6 @@ class BagelAuth {
|
|
|
436
441
|
class Bagel {
|
|
437
442
|
constructor({ host, onError }) {
|
|
438
443
|
__publicField(this, "host");
|
|
439
|
-
// eslint-disable-next-line no-unused-vars
|
|
440
444
|
__publicField(this, "onError");
|
|
441
445
|
__publicField(this, "read_table", null);
|
|
442
446
|
__publicField(this, "auth", new BagelAuth(this));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bagelink/sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.471",
|
|
5
5
|
"description": "Bagel core sdk packages",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Neveh Allon",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"axios": "^1.
|
|
48
|
+
"axios": "^1.7.2"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"dev": "unbuild --stub",
|