@bagelink/sdk 0.0.1131 → 0.0.1135
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 +69 -41
- package/dist/index.mjs +69 -41
- package/package.json +1 -1
- package/src/openAPITools/typeGenerator.ts +6 -2
package/dist/index.cjs
CHANGED
|
@@ -18,20 +18,24 @@ function formatType(typeName) {
|
|
|
18
18
|
}
|
|
19
19
|
function resolveReference(ref) {
|
|
20
20
|
const t = ref.split("/").pop();
|
|
21
|
-
if (!t)
|
|
21
|
+
if (!t)
|
|
22
|
+
return "any";
|
|
22
23
|
return formatType(t);
|
|
23
24
|
}
|
|
24
25
|
function schemaToType(schema) {
|
|
25
|
-
if (!schema)
|
|
26
|
+
if (!schema)
|
|
27
|
+
return "any";
|
|
26
28
|
if (schema.anyOf) {
|
|
27
29
|
let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" | ");
|
|
28
|
-
if (_t === "" || _t === "null")
|
|
30
|
+
if (_t === "" || _t === "null")
|
|
31
|
+
_t = "any";
|
|
29
32
|
return _t;
|
|
30
33
|
}
|
|
31
34
|
if (schema.allOf) {
|
|
32
35
|
return schema.allOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" & ");
|
|
33
36
|
}
|
|
34
|
-
if (schema.$ref)
|
|
37
|
+
if (schema.$ref)
|
|
38
|
+
return resolveReference(schema.$ref);
|
|
35
39
|
switch (schema.type) {
|
|
36
40
|
case "object":
|
|
37
41
|
return "{ [key: string]: any }";
|
|
@@ -49,7 +53,7 @@ function schemaToType(schema) {
|
|
|
49
53
|
return "undefined";
|
|
50
54
|
case "null":
|
|
51
55
|
return "null";
|
|
52
|
-
case
|
|
56
|
+
case void 0:
|
|
53
57
|
return "any";
|
|
54
58
|
default:
|
|
55
59
|
console.log("Unknown type", schema.type);
|
|
@@ -61,7 +65,7 @@ function isOptional(schema) {
|
|
|
61
65
|
const splitType = type.split(/\s+\|\s+/);
|
|
62
66
|
const includesNull = splitType.includes("null");
|
|
63
67
|
const includesUndefined = splitType.includes("undefined");
|
|
64
|
-
return includesNull || includesUndefined || schema.default !==
|
|
68
|
+
return includesNull || includesUndefined || schema.default !== void 0;
|
|
65
69
|
}
|
|
66
70
|
function cleanOptionals(str) {
|
|
67
71
|
return str.split(" | ").filter((t) => t !== "null" && t !== "undefined").join(" | ");
|
|
@@ -83,7 +87,8 @@ function formatVarType({
|
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
let optionalStr = !required && isOptional(schema) ? "?" : "";
|
|
86
|
-
if (defaultStr)
|
|
90
|
+
if (defaultStr)
|
|
91
|
+
optionalStr = "";
|
|
87
92
|
return `${varName}${optionalStr}: ${type}${defaultStr}`;
|
|
88
93
|
}
|
|
89
94
|
function cleanPath(path) {
|
|
@@ -112,8 +117,10 @@ function collectTypeForImportStatement(typeName) {
|
|
|
112
117
|
}
|
|
113
118
|
const isPrimitive = primitiveTypes.includes(typeName);
|
|
114
119
|
typeName = formatType(typeName);
|
|
115
|
-
if (!typeName || isPrimitive)
|
|
116
|
-
|
|
120
|
+
if (!typeName || isPrimitive)
|
|
121
|
+
return;
|
|
122
|
+
if (!allTypes.includes(typeName))
|
|
123
|
+
allTypes.push(typeName);
|
|
117
124
|
}
|
|
118
125
|
function schemaToTypeWithCollection(schema) {
|
|
119
126
|
const type = schemaToType(schema);
|
|
@@ -122,12 +129,14 @@ function schemaToTypeWithCollection(schema) {
|
|
|
122
129
|
}
|
|
123
130
|
function getResponseType(response) {
|
|
124
131
|
const mediaTypeObject = response.content?.["application/json"];
|
|
125
|
-
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
132
|
+
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
133
|
+
return;
|
|
126
134
|
const responseType = schemaToTypeWithCollection(mediaTypeObject.schema);
|
|
127
135
|
return responseType;
|
|
128
136
|
}
|
|
129
137
|
function generateResponseType(responses) {
|
|
130
|
-
if (!responses)
|
|
138
|
+
if (!responses)
|
|
139
|
+
return "";
|
|
131
140
|
const types = [];
|
|
132
141
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
133
142
|
if (statusCode.startsWith("2")) {
|
|
@@ -140,7 +149,8 @@ function generateResponseType(responses) {
|
|
|
140
149
|
return types.join(" | ");
|
|
141
150
|
}
|
|
142
151
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
143
|
-
if (allParams === "undefined")
|
|
152
|
+
if (allParams === "undefined")
|
|
153
|
+
allParams = "";
|
|
144
154
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
145
155
|
if (requestBodyPayload === "formData") {
|
|
146
156
|
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
@@ -200,12 +210,14 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
200
210
|
let allParamsArray = [];
|
|
201
211
|
if (parameters && parameters.params)
|
|
202
212
|
allParamsArray = parameters.params.split(",").map((p) => p.trim());
|
|
203
|
-
if (requestBodyParam)
|
|
213
|
+
if (requestBodyParam)
|
|
214
|
+
allParamsArray.push(requestBodyParam.trim());
|
|
204
215
|
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
205
216
|
return allParamsArray.join(", ");
|
|
206
217
|
}
|
|
207
218
|
function generateFunctionParameters(params, isFileUpload = false) {
|
|
208
|
-
if (!params?.length)
|
|
219
|
+
if (!params?.length)
|
|
220
|
+
return {};
|
|
209
221
|
if (isFileUpload) {
|
|
210
222
|
return {
|
|
211
223
|
config: {
|
|
@@ -244,7 +256,8 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
244
256
|
};
|
|
245
257
|
}
|
|
246
258
|
function generateFunctionForOperation(method, path, operation) {
|
|
247
|
-
if (!operation)
|
|
259
|
+
if (!operation)
|
|
260
|
+
return "";
|
|
248
261
|
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
249
262
|
const parameters = generateFunctionParameters(
|
|
250
263
|
operation.parameters,
|
|
@@ -326,7 +339,8 @@ function generateFunctions(paths, baseUrl) {
|
|
|
326
339
|
const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
|
|
327
340
|
splitPath.reduce((acc, key, index, array) => {
|
|
328
341
|
const objFuncKey = toCamelCase(key);
|
|
329
|
-
if (!objFuncKey)
|
|
342
|
+
if (!objFuncKey)
|
|
343
|
+
return acc;
|
|
330
344
|
const methods = Object.keys(operation);
|
|
331
345
|
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === cleanPath(path)).length === 1) {
|
|
332
346
|
const method = methods[0];
|
|
@@ -341,7 +355,7 @@ function generateFunctions(paths, baseUrl) {
|
|
|
341
355
|
}, body);
|
|
342
356
|
}
|
|
343
357
|
for (const [parent, object] of Object.entries(body)) {
|
|
344
|
-
tsString += `export const ${parent} = ${JSON.stringify(object,
|
|
358
|
+
tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
|
|
345
359
|
`;
|
|
346
360
|
}
|
|
347
361
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
@@ -363,8 +377,6 @@ function generateTypes(schemas) {
|
|
|
363
377
|
`;
|
|
364
378
|
}
|
|
365
379
|
if (!schema.properties) {
|
|
366
|
-
console.log("No properties found for schema:", typeName);
|
|
367
|
-
console.log(JSON.stringify({ typeName, schema }, null, 2));
|
|
368
380
|
return "";
|
|
369
381
|
}
|
|
370
382
|
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
@@ -383,10 +395,12 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
383
395
|
try {
|
|
384
396
|
const { data: openApi } = await axios__default.get(openApiUrl, { headers: basicAuthHeader });
|
|
385
397
|
const schemas = openApi.components?.schemas;
|
|
386
|
-
if (!schemas)
|
|
398
|
+
if (!schemas)
|
|
399
|
+
throw new Error("No schemas found in OpenAPI document");
|
|
387
400
|
const types = generateTypes(schemas);
|
|
388
401
|
const { paths } = openApi;
|
|
389
|
-
if (!paths)
|
|
402
|
+
if (!paths)
|
|
403
|
+
throw new Error("No paths found in OpenAPI document");
|
|
390
404
|
const code = generateFunctions(paths, baseUrl);
|
|
391
405
|
return { types, code };
|
|
392
406
|
} catch (error) {
|
|
@@ -394,21 +408,28 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
394
408
|
}
|
|
395
409
|
};
|
|
396
410
|
|
|
411
|
+
var __defProp = Object.defineProperty;
|
|
412
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
413
|
+
var __publicField = (obj, key, value) => {
|
|
414
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
415
|
+
return value;
|
|
416
|
+
};
|
|
397
417
|
const axios = axios__default.create({
|
|
398
418
|
withCredentials: true
|
|
399
419
|
});
|
|
400
420
|
class DataRequest {
|
|
401
|
-
data_table;
|
|
402
|
-
bagel;
|
|
403
|
-
itemID;
|
|
404
|
-
_filter = {};
|
|
405
421
|
constructor(table, bagel) {
|
|
422
|
+
__publicField(this, "data_table");
|
|
423
|
+
__publicField(this, "bagel");
|
|
424
|
+
__publicField(this, "itemID");
|
|
425
|
+
__publicField(this, "_filter", {});
|
|
406
426
|
this.data_table = table;
|
|
407
427
|
this.bagel = bagel;
|
|
408
428
|
this.itemID = "";
|
|
409
429
|
}
|
|
410
430
|
async post(item) {
|
|
411
|
-
if (!this.data_table)
|
|
431
|
+
if (!this.data_table)
|
|
432
|
+
throw new Error("Data table not set");
|
|
412
433
|
const { data } = await axios.post(`/data/${this.data_table}`, item);
|
|
413
434
|
return data;
|
|
414
435
|
}
|
|
@@ -417,7 +438,8 @@ class DataRequest {
|
|
|
417
438
|
return this;
|
|
418
439
|
}
|
|
419
440
|
async get() {
|
|
420
|
-
if (!this.data_table)
|
|
441
|
+
if (!this.data_table)
|
|
442
|
+
throw new Error("Data table not set");
|
|
421
443
|
const filterStr = Object.keys(this._filter).length > 0 ? `?filter={${Object.entries(this._filter).map(([k, v]) => `${k}:${v}`).join(",")}}` : "";
|
|
422
444
|
const url = `/data/${this.data_table}${this.itemID ? `/${this.itemID}` : ""}${filterStr}`;
|
|
423
445
|
try {
|
|
@@ -426,7 +448,7 @@ class DataRequest {
|
|
|
426
448
|
} catch (err) {
|
|
427
449
|
console.log(err);
|
|
428
450
|
this.bagel.onError?.(err);
|
|
429
|
-
return
|
|
451
|
+
return void 0;
|
|
430
452
|
}
|
|
431
453
|
}
|
|
432
454
|
item(id) {
|
|
@@ -434,7 +456,8 @@ class DataRequest {
|
|
|
434
456
|
return this;
|
|
435
457
|
}
|
|
436
458
|
async delete() {
|
|
437
|
-
if (!this.data_table)
|
|
459
|
+
if (!this.data_table)
|
|
460
|
+
throw new Error("Data table not set");
|
|
438
461
|
const { data } = await axios.delete(
|
|
439
462
|
`/data/${this.data_table}/${this.itemID}`
|
|
440
463
|
);
|
|
@@ -442,8 +465,10 @@ class DataRequest {
|
|
|
442
465
|
}
|
|
443
466
|
async put(updatedItem) {
|
|
444
467
|
const { data_table, itemID } = this;
|
|
445
|
-
if (!data_table)
|
|
446
|
-
|
|
468
|
+
if (!data_table)
|
|
469
|
+
throw new Error("Data table not set");
|
|
470
|
+
if (!itemID)
|
|
471
|
+
throw new Error("Item ID not set");
|
|
447
472
|
const { data } = await axios.put(
|
|
448
473
|
`/data/${data_table}/${itemID}`,
|
|
449
474
|
updatedItem
|
|
@@ -461,9 +486,9 @@ function responses(key) {
|
|
|
461
486
|
class BagelAuth {
|
|
462
487
|
constructor(bagel) {
|
|
463
488
|
this.bagel = bagel;
|
|
489
|
+
__publicField(this, "user");
|
|
464
490
|
this.bagel = bagel;
|
|
465
491
|
}
|
|
466
|
-
user = undefined;
|
|
467
492
|
async validateUser() {
|
|
468
493
|
try {
|
|
469
494
|
const { data: usr } = await axios.get("/users/me", {
|
|
@@ -511,7 +536,7 @@ class BagelAuth {
|
|
|
511
536
|
this.bagel.onError?.(err);
|
|
512
537
|
console.log(err);
|
|
513
538
|
}
|
|
514
|
-
this.user =
|
|
539
|
+
this.user = void 0;
|
|
515
540
|
}
|
|
516
541
|
async acceptInvite(token, user) {
|
|
517
542
|
await axios.post(`/auth/accept-invite/${token}`, user);
|
|
@@ -529,10 +554,12 @@ class BagelAuth {
|
|
|
529
554
|
}
|
|
530
555
|
}
|
|
531
556
|
class Bagel {
|
|
532
|
-
host;
|
|
533
|
-
fileBaseUrl;
|
|
534
|
-
onError;
|
|
535
557
|
constructor({ host, fileBaseUrl, onError }) {
|
|
558
|
+
__publicField(this, "host");
|
|
559
|
+
__publicField(this, "fileBaseUrl");
|
|
560
|
+
__publicField(this, "onError");
|
|
561
|
+
__publicField(this, "read_table");
|
|
562
|
+
__publicField(this, "auth", new BagelAuth(this));
|
|
536
563
|
this.host = host?.replace(/\/$/, "");
|
|
537
564
|
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
538
565
|
if (!this.host) {
|
|
@@ -541,11 +568,9 @@ class Bagel {
|
|
|
541
568
|
axios.defaults.baseURL = this.host;
|
|
542
569
|
this.onError = onError;
|
|
543
570
|
}
|
|
544
|
-
read_table = undefined;
|
|
545
571
|
data(table) {
|
|
546
572
|
return new DataRequest(table, this);
|
|
547
573
|
}
|
|
548
|
-
auth = new BagelAuth(this);
|
|
549
574
|
_endpointCleaner(endpoint) {
|
|
550
575
|
const url = `${endpoint.replace(/^\//, "").replaceAll(/\/$/g, "")}`;
|
|
551
576
|
return url;
|
|
@@ -560,10 +585,12 @@ class Bagel {
|
|
|
560
585
|
async get(endpoint, query) {
|
|
561
586
|
this._setAuthorization();
|
|
562
587
|
endpoint = this._endpointCleaner(endpoint);
|
|
563
|
-
if (/undefined|null/.test(endpoint))
|
|
588
|
+
if (/undefined|null/.test(endpoint))
|
|
589
|
+
throw new Error(`Invalid endpoint: ${endpoint}`);
|
|
564
590
|
if (query) {
|
|
565
591
|
const queryParams = Object.entries(query).filter(([_, value]) => !!value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
566
|
-
if (queryParams)
|
|
592
|
+
if (queryParams)
|
|
593
|
+
endpoint = `${endpoint}?${queryParams}`;
|
|
567
594
|
}
|
|
568
595
|
const url = `/${endpoint}`;
|
|
569
596
|
return axios.get(url).then(({ data }) => data).catch((err) => {
|
|
@@ -613,7 +640,8 @@ class Bagel {
|
|
|
613
640
|
const formData = new FormData();
|
|
614
641
|
formData.append("file", file);
|
|
615
642
|
let url = "/static_files/upload";
|
|
616
|
-
if (options?.topic)
|
|
643
|
+
if (options?.topic)
|
|
644
|
+
url = `/static_files/upload?topic=${options.topic}`;
|
|
617
645
|
const { data } = await axios.post(url, formData, {
|
|
618
646
|
headers: {
|
|
619
647
|
"Content-Type": "multipart/form-data"
|
package/dist/index.mjs
CHANGED
|
@@ -12,20 +12,24 @@ function formatType(typeName) {
|
|
|
12
12
|
}
|
|
13
13
|
function resolveReference(ref) {
|
|
14
14
|
const t = ref.split("/").pop();
|
|
15
|
-
if (!t)
|
|
15
|
+
if (!t)
|
|
16
|
+
return "any";
|
|
16
17
|
return formatType(t);
|
|
17
18
|
}
|
|
18
19
|
function schemaToType(schema) {
|
|
19
|
-
if (!schema)
|
|
20
|
+
if (!schema)
|
|
21
|
+
return "any";
|
|
20
22
|
if (schema.anyOf) {
|
|
21
23
|
let _t = schema.anyOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" | ");
|
|
22
|
-
if (_t === "" || _t === "null")
|
|
24
|
+
if (_t === "" || _t === "null")
|
|
25
|
+
_t = "any";
|
|
23
26
|
return _t;
|
|
24
27
|
}
|
|
25
28
|
if (schema.allOf) {
|
|
26
29
|
return schema.allOf.map((s) => schemaToType(s)).filter((p) => p !== "any").join(" & ");
|
|
27
30
|
}
|
|
28
|
-
if (schema.$ref)
|
|
31
|
+
if (schema.$ref)
|
|
32
|
+
return resolveReference(schema.$ref);
|
|
29
33
|
switch (schema.type) {
|
|
30
34
|
case "object":
|
|
31
35
|
return "{ [key: string]: any }";
|
|
@@ -43,7 +47,7 @@ function schemaToType(schema) {
|
|
|
43
47
|
return "undefined";
|
|
44
48
|
case "null":
|
|
45
49
|
return "null";
|
|
46
|
-
case
|
|
50
|
+
case void 0:
|
|
47
51
|
return "any";
|
|
48
52
|
default:
|
|
49
53
|
console.log("Unknown type", schema.type);
|
|
@@ -55,7 +59,7 @@ function isOptional(schema) {
|
|
|
55
59
|
const splitType = type.split(/\s+\|\s+/);
|
|
56
60
|
const includesNull = splitType.includes("null");
|
|
57
61
|
const includesUndefined = splitType.includes("undefined");
|
|
58
|
-
return includesNull || includesUndefined || schema.default !==
|
|
62
|
+
return includesNull || includesUndefined || schema.default !== void 0;
|
|
59
63
|
}
|
|
60
64
|
function cleanOptionals(str) {
|
|
61
65
|
return str.split(" | ").filter((t) => t !== "null" && t !== "undefined").join(" | ");
|
|
@@ -77,7 +81,8 @@ function formatVarType({
|
|
|
77
81
|
}
|
|
78
82
|
}
|
|
79
83
|
let optionalStr = !required && isOptional(schema) ? "?" : "";
|
|
80
|
-
if (defaultStr)
|
|
84
|
+
if (defaultStr)
|
|
85
|
+
optionalStr = "";
|
|
81
86
|
return `${varName}${optionalStr}: ${type}${defaultStr}`;
|
|
82
87
|
}
|
|
83
88
|
function cleanPath(path) {
|
|
@@ -106,8 +111,10 @@ function collectTypeForImportStatement(typeName) {
|
|
|
106
111
|
}
|
|
107
112
|
const isPrimitive = primitiveTypes.includes(typeName);
|
|
108
113
|
typeName = formatType(typeName);
|
|
109
|
-
if (!typeName || isPrimitive)
|
|
110
|
-
|
|
114
|
+
if (!typeName || isPrimitive)
|
|
115
|
+
return;
|
|
116
|
+
if (!allTypes.includes(typeName))
|
|
117
|
+
allTypes.push(typeName);
|
|
111
118
|
}
|
|
112
119
|
function schemaToTypeWithCollection(schema) {
|
|
113
120
|
const type = schemaToType(schema);
|
|
@@ -116,12 +123,14 @@ function schemaToTypeWithCollection(schema) {
|
|
|
116
123
|
}
|
|
117
124
|
function getResponseType(response) {
|
|
118
125
|
const mediaTypeObject = response.content?.["application/json"];
|
|
119
|
-
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
126
|
+
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
127
|
+
return;
|
|
120
128
|
const responseType = schemaToTypeWithCollection(mediaTypeObject.schema);
|
|
121
129
|
return responseType;
|
|
122
130
|
}
|
|
123
131
|
function generateResponseType(responses) {
|
|
124
|
-
if (!responses)
|
|
132
|
+
if (!responses)
|
|
133
|
+
return "";
|
|
125
134
|
const types = [];
|
|
126
135
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
127
136
|
if (statusCode.startsWith("2")) {
|
|
@@ -134,7 +143,8 @@ function generateResponseType(responses) {
|
|
|
134
143
|
return types.join(" | ");
|
|
135
144
|
}
|
|
136
145
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
137
|
-
if (allParams === "undefined")
|
|
146
|
+
if (allParams === "undefined")
|
|
147
|
+
allParams = "";
|
|
138
148
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
139
149
|
if (requestBodyPayload === "formData") {
|
|
140
150
|
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
@@ -194,12 +204,14 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
194
204
|
let allParamsArray = [];
|
|
195
205
|
if (parameters && parameters.params)
|
|
196
206
|
allParamsArray = parameters.params.split(",").map((p) => p.trim());
|
|
197
|
-
if (requestBodyParam)
|
|
207
|
+
if (requestBodyParam)
|
|
208
|
+
allParamsArray.push(requestBodyParam.trim());
|
|
198
209
|
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
199
210
|
return allParamsArray.join(", ");
|
|
200
211
|
}
|
|
201
212
|
function generateFunctionParameters(params, isFileUpload = false) {
|
|
202
|
-
if (!params?.length)
|
|
213
|
+
if (!params?.length)
|
|
214
|
+
return {};
|
|
203
215
|
if (isFileUpload) {
|
|
204
216
|
return {
|
|
205
217
|
config: {
|
|
@@ -238,7 +250,8 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
238
250
|
};
|
|
239
251
|
}
|
|
240
252
|
function generateFunctionForOperation(method, path, operation) {
|
|
241
|
-
if (!operation)
|
|
253
|
+
if (!operation)
|
|
254
|
+
return "";
|
|
242
255
|
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
243
256
|
const parameters = generateFunctionParameters(
|
|
244
257
|
operation.parameters,
|
|
@@ -320,7 +333,8 @@ function generateFunctions(paths, baseUrl) {
|
|
|
320
333
|
const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
|
|
321
334
|
splitPath.reduce((acc, key, index, array) => {
|
|
322
335
|
const objFuncKey = toCamelCase(key);
|
|
323
|
-
if (!objFuncKey)
|
|
336
|
+
if (!objFuncKey)
|
|
337
|
+
return acc;
|
|
324
338
|
const methods = Object.keys(operation);
|
|
325
339
|
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === cleanPath(path)).length === 1) {
|
|
326
340
|
const method = methods[0];
|
|
@@ -335,7 +349,7 @@ function generateFunctions(paths, baseUrl) {
|
|
|
335
349
|
}, body);
|
|
336
350
|
}
|
|
337
351
|
for (const [parent, object] of Object.entries(body)) {
|
|
338
|
-
tsString += `export const ${parent} = ${JSON.stringify(object,
|
|
352
|
+
tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
|
|
339
353
|
`;
|
|
340
354
|
}
|
|
341
355
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
@@ -357,8 +371,6 @@ function generateTypes(schemas) {
|
|
|
357
371
|
`;
|
|
358
372
|
}
|
|
359
373
|
if (!schema.properties) {
|
|
360
|
-
console.log("No properties found for schema:", typeName);
|
|
361
|
-
console.log(JSON.stringify({ typeName, schema }, null, 2));
|
|
362
374
|
return "";
|
|
363
375
|
}
|
|
364
376
|
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
@@ -377,10 +389,12 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
377
389
|
try {
|
|
378
390
|
const { data: openApi } = await axios$1.get(openApiUrl, { headers: basicAuthHeader });
|
|
379
391
|
const schemas = openApi.components?.schemas;
|
|
380
|
-
if (!schemas)
|
|
392
|
+
if (!schemas)
|
|
393
|
+
throw new Error("No schemas found in OpenAPI document");
|
|
381
394
|
const types = generateTypes(schemas);
|
|
382
395
|
const { paths } = openApi;
|
|
383
|
-
if (!paths)
|
|
396
|
+
if (!paths)
|
|
397
|
+
throw new Error("No paths found in OpenAPI document");
|
|
384
398
|
const code = generateFunctions(paths, baseUrl);
|
|
385
399
|
return { types, code };
|
|
386
400
|
} catch (error) {
|
|
@@ -388,21 +402,28 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
388
402
|
}
|
|
389
403
|
};
|
|
390
404
|
|
|
405
|
+
var __defProp = Object.defineProperty;
|
|
406
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
407
|
+
var __publicField = (obj, key, value) => {
|
|
408
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
409
|
+
return value;
|
|
410
|
+
};
|
|
391
411
|
const axios = axios$1.create({
|
|
392
412
|
withCredentials: true
|
|
393
413
|
});
|
|
394
414
|
class DataRequest {
|
|
395
|
-
data_table;
|
|
396
|
-
bagel;
|
|
397
|
-
itemID;
|
|
398
|
-
_filter = {};
|
|
399
415
|
constructor(table, bagel) {
|
|
416
|
+
__publicField(this, "data_table");
|
|
417
|
+
__publicField(this, "bagel");
|
|
418
|
+
__publicField(this, "itemID");
|
|
419
|
+
__publicField(this, "_filter", {});
|
|
400
420
|
this.data_table = table;
|
|
401
421
|
this.bagel = bagel;
|
|
402
422
|
this.itemID = "";
|
|
403
423
|
}
|
|
404
424
|
async post(item) {
|
|
405
|
-
if (!this.data_table)
|
|
425
|
+
if (!this.data_table)
|
|
426
|
+
throw new Error("Data table not set");
|
|
406
427
|
const { data } = await axios.post(`/data/${this.data_table}`, item);
|
|
407
428
|
return data;
|
|
408
429
|
}
|
|
@@ -411,7 +432,8 @@ class DataRequest {
|
|
|
411
432
|
return this;
|
|
412
433
|
}
|
|
413
434
|
async get() {
|
|
414
|
-
if (!this.data_table)
|
|
435
|
+
if (!this.data_table)
|
|
436
|
+
throw new Error("Data table not set");
|
|
415
437
|
const filterStr = Object.keys(this._filter).length > 0 ? `?filter={${Object.entries(this._filter).map(([k, v]) => `${k}:${v}`).join(",")}}` : "";
|
|
416
438
|
const url = `/data/${this.data_table}${this.itemID ? `/${this.itemID}` : ""}${filterStr}`;
|
|
417
439
|
try {
|
|
@@ -420,7 +442,7 @@ class DataRequest {
|
|
|
420
442
|
} catch (err) {
|
|
421
443
|
console.log(err);
|
|
422
444
|
this.bagel.onError?.(err);
|
|
423
|
-
return
|
|
445
|
+
return void 0;
|
|
424
446
|
}
|
|
425
447
|
}
|
|
426
448
|
item(id) {
|
|
@@ -428,7 +450,8 @@ class DataRequest {
|
|
|
428
450
|
return this;
|
|
429
451
|
}
|
|
430
452
|
async delete() {
|
|
431
|
-
if (!this.data_table)
|
|
453
|
+
if (!this.data_table)
|
|
454
|
+
throw new Error("Data table not set");
|
|
432
455
|
const { data } = await axios.delete(
|
|
433
456
|
`/data/${this.data_table}/${this.itemID}`
|
|
434
457
|
);
|
|
@@ -436,8 +459,10 @@ class DataRequest {
|
|
|
436
459
|
}
|
|
437
460
|
async put(updatedItem) {
|
|
438
461
|
const { data_table, itemID } = this;
|
|
439
|
-
if (!data_table)
|
|
440
|
-
|
|
462
|
+
if (!data_table)
|
|
463
|
+
throw new Error("Data table not set");
|
|
464
|
+
if (!itemID)
|
|
465
|
+
throw new Error("Item ID not set");
|
|
441
466
|
const { data } = await axios.put(
|
|
442
467
|
`/data/${data_table}/${itemID}`,
|
|
443
468
|
updatedItem
|
|
@@ -455,9 +480,9 @@ function responses(key) {
|
|
|
455
480
|
class BagelAuth {
|
|
456
481
|
constructor(bagel) {
|
|
457
482
|
this.bagel = bagel;
|
|
483
|
+
__publicField(this, "user");
|
|
458
484
|
this.bagel = bagel;
|
|
459
485
|
}
|
|
460
|
-
user = undefined;
|
|
461
486
|
async validateUser() {
|
|
462
487
|
try {
|
|
463
488
|
const { data: usr } = await axios.get("/users/me", {
|
|
@@ -505,7 +530,7 @@ class BagelAuth {
|
|
|
505
530
|
this.bagel.onError?.(err);
|
|
506
531
|
console.log(err);
|
|
507
532
|
}
|
|
508
|
-
this.user =
|
|
533
|
+
this.user = void 0;
|
|
509
534
|
}
|
|
510
535
|
async acceptInvite(token, user) {
|
|
511
536
|
await axios.post(`/auth/accept-invite/${token}`, user);
|
|
@@ -523,10 +548,12 @@ class BagelAuth {
|
|
|
523
548
|
}
|
|
524
549
|
}
|
|
525
550
|
class Bagel {
|
|
526
|
-
host;
|
|
527
|
-
fileBaseUrl;
|
|
528
|
-
onError;
|
|
529
551
|
constructor({ host, fileBaseUrl, onError }) {
|
|
552
|
+
__publicField(this, "host");
|
|
553
|
+
__publicField(this, "fileBaseUrl");
|
|
554
|
+
__publicField(this, "onError");
|
|
555
|
+
__publicField(this, "read_table");
|
|
556
|
+
__publicField(this, "auth", new BagelAuth(this));
|
|
530
557
|
this.host = host?.replace(/\/$/, "");
|
|
531
558
|
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
532
559
|
if (!this.host) {
|
|
@@ -535,11 +562,9 @@ class Bagel {
|
|
|
535
562
|
axios.defaults.baseURL = this.host;
|
|
536
563
|
this.onError = onError;
|
|
537
564
|
}
|
|
538
|
-
read_table = undefined;
|
|
539
565
|
data(table) {
|
|
540
566
|
return new DataRequest(table, this);
|
|
541
567
|
}
|
|
542
|
-
auth = new BagelAuth(this);
|
|
543
568
|
_endpointCleaner(endpoint) {
|
|
544
569
|
const url = `${endpoint.replace(/^\//, "").replaceAll(/\/$/g, "")}`;
|
|
545
570
|
return url;
|
|
@@ -554,10 +579,12 @@ class Bagel {
|
|
|
554
579
|
async get(endpoint, query) {
|
|
555
580
|
this._setAuthorization();
|
|
556
581
|
endpoint = this._endpointCleaner(endpoint);
|
|
557
|
-
if (/undefined|null/.test(endpoint))
|
|
582
|
+
if (/undefined|null/.test(endpoint))
|
|
583
|
+
throw new Error(`Invalid endpoint: ${endpoint}`);
|
|
558
584
|
if (query) {
|
|
559
585
|
const queryParams = Object.entries(query).filter(([_, value]) => !!value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
560
|
-
if (queryParams)
|
|
586
|
+
if (queryParams)
|
|
587
|
+
endpoint = `${endpoint}?${queryParams}`;
|
|
561
588
|
}
|
|
562
589
|
const url = `/${endpoint}`;
|
|
563
590
|
return axios.get(url).then(({ data }) => data).catch((err) => {
|
|
@@ -607,7 +634,8 @@ class Bagel {
|
|
|
607
634
|
const formData = new FormData();
|
|
608
635
|
formData.append("file", file);
|
|
609
636
|
let url = "/static_files/upload";
|
|
610
|
-
if (options?.topic)
|
|
637
|
+
if (options?.topic)
|
|
638
|
+
url = `/static_files/upload?topic=${options.topic}`;
|
|
611
639
|
const { data } = await axios.post(url, formData, {
|
|
612
640
|
headers: {
|
|
613
641
|
"Content-Type": "multipart/form-data"
|
package/package.json
CHANGED
|
@@ -14,11 +14,15 @@ export function generateTypes(schemas: SchemasObject): string {
|
|
|
14
14
|
return `export type ${typeName} = ${schemaToType(schema.items)}[];\n`
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
// if (typeName === 'LocalizedJsonInt') {
|
|
18
|
+
// console.log(JSON.stringify({ typeName, schema }, null, 2))
|
|
19
|
+
// }
|
|
20
|
+
|
|
17
21
|
if (!schema.properties) {
|
|
18
22
|
// #region Debug
|
|
19
|
-
console.log('No properties found for schema:', typeName)
|
|
23
|
+
// console.log('No properties found for schema:', typeName)
|
|
20
24
|
|
|
21
|
-
console.log(JSON.stringify({ typeName, schema }, null, 2))
|
|
25
|
+
// console.log(JSON.stringify({ typeName, schema }, null, 2))
|
|
22
26
|
// #endregion Debug
|
|
23
27
|
|
|
24
28
|
return ''
|