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