@bagelink/sdk 0.0.1125 → 0.0.1129
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 +71 -40
- package/dist/index.mjs +71 -40
- package/package.json +1 -1
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,18 +117,22 @@ 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 getResponseType(response) {
|
|
119
126
|
const mediaTypeObject = response.content?.["application/json"];
|
|
120
|
-
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
127
|
+
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
128
|
+
return;
|
|
121
129
|
const responseType = schemaToType(mediaTypeObject.schema);
|
|
122
130
|
collectTypeForImportStatement(responseType);
|
|
123
131
|
return responseType;
|
|
124
132
|
}
|
|
125
133
|
function generateResponseType(responses) {
|
|
126
|
-
if (!responses)
|
|
134
|
+
if (!responses)
|
|
135
|
+
return "";
|
|
127
136
|
const types = [];
|
|
128
137
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
129
138
|
if (statusCode.startsWith("2")) {
|
|
@@ -136,7 +145,8 @@ function generateResponseType(responses) {
|
|
|
136
145
|
return types.join(" | ");
|
|
137
146
|
}
|
|
138
147
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
139
|
-
if (allParams === "undefined")
|
|
148
|
+
if (allParams === "undefined")
|
|
149
|
+
allParams = "";
|
|
140
150
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
141
151
|
if (requestBodyPayload === "formData") {
|
|
142
152
|
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
@@ -196,12 +206,14 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
196
206
|
let allParamsArray = [];
|
|
197
207
|
if (parameters && parameters.params)
|
|
198
208
|
allParamsArray = parameters.params.split(",").map((p) => p.trim());
|
|
199
|
-
if (requestBodyParam)
|
|
209
|
+
if (requestBodyParam)
|
|
210
|
+
allParamsArray.push(requestBodyParam.trim());
|
|
200
211
|
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
201
212
|
return allParamsArray.join(", ");
|
|
202
213
|
}
|
|
203
214
|
function generateFunctionParameters(params, isFileUpload = false) {
|
|
204
|
-
if (!params?.length)
|
|
215
|
+
if (!params?.length)
|
|
216
|
+
return {};
|
|
205
217
|
if (isFileUpload) {
|
|
206
218
|
return {
|
|
207
219
|
config: {
|
|
@@ -240,7 +252,8 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
240
252
|
};
|
|
241
253
|
}
|
|
242
254
|
function generateFunctionForOperation(method, path, operation) {
|
|
243
|
-
if (!operation)
|
|
255
|
+
if (!operation)
|
|
256
|
+
return "";
|
|
244
257
|
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
245
258
|
const parameters = generateFunctionParameters(
|
|
246
259
|
operation.parameters,
|
|
@@ -322,7 +335,8 @@ function generateFunctions(paths, baseUrl) {
|
|
|
322
335
|
const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
|
|
323
336
|
splitPath.reduce((acc, key, index, array) => {
|
|
324
337
|
const objFuncKey = toCamelCase(key);
|
|
325
|
-
if (!objFuncKey)
|
|
338
|
+
if (!objFuncKey)
|
|
339
|
+
return acc;
|
|
326
340
|
const methods = Object.keys(operation);
|
|
327
341
|
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === cleanPath(path)).length === 1) {
|
|
328
342
|
const method = methods[0];
|
|
@@ -337,7 +351,7 @@ function generateFunctions(paths, baseUrl) {
|
|
|
337
351
|
}, body);
|
|
338
352
|
}
|
|
339
353
|
for (const [parent, object] of Object.entries(body)) {
|
|
340
|
-
tsString += `export const ${parent} = ${JSON.stringify(object,
|
|
354
|
+
tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
|
|
341
355
|
`;
|
|
342
356
|
}
|
|
343
357
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
@@ -354,7 +368,8 @@ function generateTypes(schemas) {
|
|
|
354
368
|
return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
|
|
355
369
|
`;
|
|
356
370
|
}
|
|
357
|
-
if (!schema.properties)
|
|
371
|
+
if (!schema.properties)
|
|
372
|
+
return "";
|
|
358
373
|
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
359
374
|
const varType = formatVarType({ varName: key, schema: value });
|
|
360
375
|
return ` ${varType}`;
|
|
@@ -371,10 +386,12 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
371
386
|
try {
|
|
372
387
|
const { data: openApi } = await axios__default.get(openApiUrl, { headers: basicAuthHeader });
|
|
373
388
|
const schemas = openApi.components?.schemas;
|
|
374
|
-
if (!schemas)
|
|
389
|
+
if (!schemas)
|
|
390
|
+
throw new Error("No schemas found in OpenAPI document");
|
|
375
391
|
const types = generateTypes(schemas);
|
|
376
392
|
const { paths } = openApi;
|
|
377
|
-
if (!paths)
|
|
393
|
+
if (!paths)
|
|
394
|
+
throw new Error("No paths found in OpenAPI document");
|
|
378
395
|
const code = generateFunctions(paths, baseUrl);
|
|
379
396
|
return { types, code };
|
|
380
397
|
} catch (error) {
|
|
@@ -382,21 +399,28 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
382
399
|
}
|
|
383
400
|
};
|
|
384
401
|
|
|
402
|
+
var __defProp = Object.defineProperty;
|
|
403
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
404
|
+
var __publicField = (obj, key, value) => {
|
|
405
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
406
|
+
return value;
|
|
407
|
+
};
|
|
385
408
|
const axios = axios__default.create({
|
|
386
409
|
withCredentials: true
|
|
387
410
|
});
|
|
388
411
|
class DataRequest {
|
|
389
|
-
data_table;
|
|
390
|
-
bagel;
|
|
391
|
-
itemID;
|
|
392
|
-
_filter = {};
|
|
393
412
|
constructor(table, bagel) {
|
|
413
|
+
__publicField(this, "data_table");
|
|
414
|
+
__publicField(this, "bagel");
|
|
415
|
+
__publicField(this, "itemID");
|
|
416
|
+
__publicField(this, "_filter", {});
|
|
394
417
|
this.data_table = table;
|
|
395
418
|
this.bagel = bagel;
|
|
396
419
|
this.itemID = "";
|
|
397
420
|
}
|
|
398
421
|
async post(item) {
|
|
399
|
-
if (!this.data_table)
|
|
422
|
+
if (!this.data_table)
|
|
423
|
+
throw new Error("Data table not set");
|
|
400
424
|
const { data } = await axios.post(`/data/${this.data_table}`, item);
|
|
401
425
|
return data;
|
|
402
426
|
}
|
|
@@ -405,7 +429,8 @@ class DataRequest {
|
|
|
405
429
|
return this;
|
|
406
430
|
}
|
|
407
431
|
async get() {
|
|
408
|
-
if (!this.data_table)
|
|
432
|
+
if (!this.data_table)
|
|
433
|
+
throw new Error("Data table not set");
|
|
409
434
|
const filterStr = Object.keys(this._filter).length > 0 ? `?filter={${Object.entries(this._filter).map(([k, v]) => `${k}:${v}`).join(",")}}` : "";
|
|
410
435
|
const url = `/data/${this.data_table}${this.itemID ? `/${this.itemID}` : ""}${filterStr}`;
|
|
411
436
|
try {
|
|
@@ -414,7 +439,7 @@ class DataRequest {
|
|
|
414
439
|
} catch (err) {
|
|
415
440
|
console.log(err);
|
|
416
441
|
this.bagel.onError?.(err);
|
|
417
|
-
return
|
|
442
|
+
return void 0;
|
|
418
443
|
}
|
|
419
444
|
}
|
|
420
445
|
item(id) {
|
|
@@ -422,7 +447,8 @@ class DataRequest {
|
|
|
422
447
|
return this;
|
|
423
448
|
}
|
|
424
449
|
async delete() {
|
|
425
|
-
if (!this.data_table)
|
|
450
|
+
if (!this.data_table)
|
|
451
|
+
throw new Error("Data table not set");
|
|
426
452
|
const { data } = await axios.delete(
|
|
427
453
|
`/data/${this.data_table}/${this.itemID}`
|
|
428
454
|
);
|
|
@@ -430,8 +456,10 @@ class DataRequest {
|
|
|
430
456
|
}
|
|
431
457
|
async put(updatedItem) {
|
|
432
458
|
const { data_table, itemID } = this;
|
|
433
|
-
if (!data_table)
|
|
434
|
-
|
|
459
|
+
if (!data_table)
|
|
460
|
+
throw new Error("Data table not set");
|
|
461
|
+
if (!itemID)
|
|
462
|
+
throw new Error("Item ID not set");
|
|
435
463
|
const { data } = await axios.put(
|
|
436
464
|
`/data/${data_table}/${itemID}`,
|
|
437
465
|
updatedItem
|
|
@@ -449,9 +477,9 @@ function responses(key) {
|
|
|
449
477
|
class BagelAuth {
|
|
450
478
|
constructor(bagel) {
|
|
451
479
|
this.bagel = bagel;
|
|
480
|
+
__publicField(this, "user");
|
|
452
481
|
this.bagel = bagel;
|
|
453
482
|
}
|
|
454
|
-
user = undefined;
|
|
455
483
|
async validateUser() {
|
|
456
484
|
try {
|
|
457
485
|
const { data: usr } = await axios.get("/users/me", {
|
|
@@ -499,7 +527,7 @@ class BagelAuth {
|
|
|
499
527
|
this.bagel.onError?.(err);
|
|
500
528
|
console.log(err);
|
|
501
529
|
}
|
|
502
|
-
this.user =
|
|
530
|
+
this.user = void 0;
|
|
503
531
|
}
|
|
504
532
|
async acceptInvite(token, user) {
|
|
505
533
|
await axios.post(`/auth/accept-invite/${token}`, user);
|
|
@@ -517,10 +545,12 @@ class BagelAuth {
|
|
|
517
545
|
}
|
|
518
546
|
}
|
|
519
547
|
class Bagel {
|
|
520
|
-
host;
|
|
521
|
-
fileBaseUrl;
|
|
522
|
-
onError;
|
|
523
548
|
constructor({ host, fileBaseUrl, onError }) {
|
|
549
|
+
__publicField(this, "host");
|
|
550
|
+
__publicField(this, "fileBaseUrl");
|
|
551
|
+
__publicField(this, "onError");
|
|
552
|
+
__publicField(this, "read_table");
|
|
553
|
+
__publicField(this, "auth", new BagelAuth(this));
|
|
524
554
|
this.host = host?.replace(/\/$/, "");
|
|
525
555
|
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
526
556
|
if (!this.host) {
|
|
@@ -529,11 +559,9 @@ class Bagel {
|
|
|
529
559
|
axios.defaults.baseURL = this.host;
|
|
530
560
|
this.onError = onError;
|
|
531
561
|
}
|
|
532
|
-
read_table = undefined;
|
|
533
562
|
data(table) {
|
|
534
563
|
return new DataRequest(table, this);
|
|
535
564
|
}
|
|
536
|
-
auth = new BagelAuth(this);
|
|
537
565
|
_endpointCleaner(endpoint) {
|
|
538
566
|
const url = `${endpoint.replace(/^\//, "").replaceAll(/\/$/g, "")}`;
|
|
539
567
|
return url;
|
|
@@ -548,10 +576,12 @@ class Bagel {
|
|
|
548
576
|
async get(endpoint, query) {
|
|
549
577
|
this._setAuthorization();
|
|
550
578
|
endpoint = this._endpointCleaner(endpoint);
|
|
551
|
-
if (/undefined|null/.test(endpoint))
|
|
579
|
+
if (/undefined|null/.test(endpoint))
|
|
580
|
+
throw new Error(`Invalid endpoint: ${endpoint}`);
|
|
552
581
|
if (query) {
|
|
553
582
|
const queryParams = Object.entries(query).filter(([_, value]) => !!value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
554
|
-
if (queryParams)
|
|
583
|
+
if (queryParams)
|
|
584
|
+
endpoint = `${endpoint}?${queryParams}`;
|
|
555
585
|
}
|
|
556
586
|
const url = `/${endpoint}`;
|
|
557
587
|
return axios.get(url).then(({ data }) => data).catch((err) => {
|
|
@@ -601,7 +631,8 @@ class Bagel {
|
|
|
601
631
|
const formData = new FormData();
|
|
602
632
|
formData.append("file", file);
|
|
603
633
|
let url = "/static_files/upload";
|
|
604
|
-
if (options?.topic)
|
|
634
|
+
if (options?.topic)
|
|
635
|
+
url = `/static_files/upload?topic=${options.topic}`;
|
|
605
636
|
const { data } = await axios.post(url, formData, {
|
|
606
637
|
headers: {
|
|
607
638
|
"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,18 +111,22 @@ 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 getResponseType(response) {
|
|
113
120
|
const mediaTypeObject = response.content?.["application/json"];
|
|
114
|
-
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
121
|
+
if (!mediaTypeObject || !mediaTypeObject.schema)
|
|
122
|
+
return;
|
|
115
123
|
const responseType = schemaToType(mediaTypeObject.schema);
|
|
116
124
|
collectTypeForImportStatement(responseType);
|
|
117
125
|
return responseType;
|
|
118
126
|
}
|
|
119
127
|
function generateResponseType(responses) {
|
|
120
|
-
if (!responses)
|
|
128
|
+
if (!responses)
|
|
129
|
+
return "";
|
|
121
130
|
const types = [];
|
|
122
131
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
123
132
|
if (statusCode.startsWith("2")) {
|
|
@@ -130,7 +139,8 @@ function generateResponseType(responses) {
|
|
|
130
139
|
return types.join(" | ");
|
|
131
140
|
}
|
|
132
141
|
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
133
|
-
if (allParams === "undefined")
|
|
142
|
+
if (allParams === "undefined")
|
|
143
|
+
allParams = "";
|
|
134
144
|
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
135
145
|
if (requestBodyPayload === "formData") {
|
|
136
146
|
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
@@ -190,12 +200,14 @@ function combineAllParams(parameters, requestBodyParam) {
|
|
|
190
200
|
let allParamsArray = [];
|
|
191
201
|
if (parameters && parameters.params)
|
|
192
202
|
allParamsArray = parameters.params.split(",").map((p) => p.trim());
|
|
193
|
-
if (requestBodyParam)
|
|
203
|
+
if (requestBodyParam)
|
|
204
|
+
allParamsArray.push(requestBodyParam.trim());
|
|
194
205
|
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
195
206
|
return allParamsArray.join(", ");
|
|
196
207
|
}
|
|
197
208
|
function generateFunctionParameters(params, isFileUpload = false) {
|
|
198
|
-
if (!params?.length)
|
|
209
|
+
if (!params?.length)
|
|
210
|
+
return {};
|
|
199
211
|
if (isFileUpload) {
|
|
200
212
|
return {
|
|
201
213
|
config: {
|
|
@@ -234,7 +246,8 @@ function generateFunctionParameters(params, isFileUpload = false) {
|
|
|
234
246
|
};
|
|
235
247
|
}
|
|
236
248
|
function generateFunctionForOperation(method, path, operation) {
|
|
237
|
-
if (!operation)
|
|
249
|
+
if (!operation)
|
|
250
|
+
return "";
|
|
238
251
|
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
239
252
|
const parameters = generateFunctionParameters(
|
|
240
253
|
operation.parameters,
|
|
@@ -316,7 +329,8 @@ function generateFunctions(paths, baseUrl) {
|
|
|
316
329
|
const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
|
|
317
330
|
splitPath.reduce((acc, key, index, array) => {
|
|
318
331
|
const objFuncKey = toCamelCase(key);
|
|
319
|
-
if (!objFuncKey)
|
|
332
|
+
if (!objFuncKey)
|
|
333
|
+
return acc;
|
|
320
334
|
const methods = Object.keys(operation);
|
|
321
335
|
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === cleanPath(path)).length === 1) {
|
|
322
336
|
const method = methods[0];
|
|
@@ -331,7 +345,7 @@ function generateFunctions(paths, baseUrl) {
|
|
|
331
345
|
}, body);
|
|
332
346
|
}
|
|
333
347
|
for (const [parent, object] of Object.entries(body)) {
|
|
334
|
-
tsString += `export const ${parent} = ${JSON.stringify(object,
|
|
348
|
+
tsString += `export const ${parent} = ${JSON.stringify(object, void 0, 2)};
|
|
335
349
|
`;
|
|
336
350
|
}
|
|
337
351
|
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
@@ -348,7 +362,8 @@ function generateTypes(schemas) {
|
|
|
348
362
|
return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
|
|
349
363
|
`;
|
|
350
364
|
}
|
|
351
|
-
if (!schema.properties)
|
|
365
|
+
if (!schema.properties)
|
|
366
|
+
return "";
|
|
352
367
|
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
353
368
|
const varType = formatVarType({ varName: key, schema: value });
|
|
354
369
|
return ` ${varType}`;
|
|
@@ -365,10 +380,12 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
365
380
|
try {
|
|
366
381
|
const { data: openApi } = await axios$1.get(openApiUrl, { headers: basicAuthHeader });
|
|
367
382
|
const schemas = openApi.components?.schemas;
|
|
368
|
-
if (!schemas)
|
|
383
|
+
if (!schemas)
|
|
384
|
+
throw new Error("No schemas found in OpenAPI document");
|
|
369
385
|
const types = generateTypes(schemas);
|
|
370
386
|
const { paths } = openApi;
|
|
371
|
-
if (!paths)
|
|
387
|
+
if (!paths)
|
|
388
|
+
throw new Error("No paths found in OpenAPI document");
|
|
372
389
|
const code = generateFunctions(paths, baseUrl);
|
|
373
390
|
return { types, code };
|
|
374
391
|
} catch (error) {
|
|
@@ -376,21 +393,28 @@ const index = async (openApiUrl, baseUrl) => {
|
|
|
376
393
|
}
|
|
377
394
|
};
|
|
378
395
|
|
|
396
|
+
var __defProp = Object.defineProperty;
|
|
397
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
398
|
+
var __publicField = (obj, key, value) => {
|
|
399
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
400
|
+
return value;
|
|
401
|
+
};
|
|
379
402
|
const axios = axios$1.create({
|
|
380
403
|
withCredentials: true
|
|
381
404
|
});
|
|
382
405
|
class DataRequest {
|
|
383
|
-
data_table;
|
|
384
|
-
bagel;
|
|
385
|
-
itemID;
|
|
386
|
-
_filter = {};
|
|
387
406
|
constructor(table, bagel) {
|
|
407
|
+
__publicField(this, "data_table");
|
|
408
|
+
__publicField(this, "bagel");
|
|
409
|
+
__publicField(this, "itemID");
|
|
410
|
+
__publicField(this, "_filter", {});
|
|
388
411
|
this.data_table = table;
|
|
389
412
|
this.bagel = bagel;
|
|
390
413
|
this.itemID = "";
|
|
391
414
|
}
|
|
392
415
|
async post(item) {
|
|
393
|
-
if (!this.data_table)
|
|
416
|
+
if (!this.data_table)
|
|
417
|
+
throw new Error("Data table not set");
|
|
394
418
|
const { data } = await axios.post(`/data/${this.data_table}`, item);
|
|
395
419
|
return data;
|
|
396
420
|
}
|
|
@@ -399,7 +423,8 @@ class DataRequest {
|
|
|
399
423
|
return this;
|
|
400
424
|
}
|
|
401
425
|
async get() {
|
|
402
|
-
if (!this.data_table)
|
|
426
|
+
if (!this.data_table)
|
|
427
|
+
throw new Error("Data table not set");
|
|
403
428
|
const filterStr = Object.keys(this._filter).length > 0 ? `?filter={${Object.entries(this._filter).map(([k, v]) => `${k}:${v}`).join(",")}}` : "";
|
|
404
429
|
const url = `/data/${this.data_table}${this.itemID ? `/${this.itemID}` : ""}${filterStr}`;
|
|
405
430
|
try {
|
|
@@ -408,7 +433,7 @@ class DataRequest {
|
|
|
408
433
|
} catch (err) {
|
|
409
434
|
console.log(err);
|
|
410
435
|
this.bagel.onError?.(err);
|
|
411
|
-
return
|
|
436
|
+
return void 0;
|
|
412
437
|
}
|
|
413
438
|
}
|
|
414
439
|
item(id) {
|
|
@@ -416,7 +441,8 @@ class DataRequest {
|
|
|
416
441
|
return this;
|
|
417
442
|
}
|
|
418
443
|
async delete() {
|
|
419
|
-
if (!this.data_table)
|
|
444
|
+
if (!this.data_table)
|
|
445
|
+
throw new Error("Data table not set");
|
|
420
446
|
const { data } = await axios.delete(
|
|
421
447
|
`/data/${this.data_table}/${this.itemID}`
|
|
422
448
|
);
|
|
@@ -424,8 +450,10 @@ class DataRequest {
|
|
|
424
450
|
}
|
|
425
451
|
async put(updatedItem) {
|
|
426
452
|
const { data_table, itemID } = this;
|
|
427
|
-
if (!data_table)
|
|
428
|
-
|
|
453
|
+
if (!data_table)
|
|
454
|
+
throw new Error("Data table not set");
|
|
455
|
+
if (!itemID)
|
|
456
|
+
throw new Error("Item ID not set");
|
|
429
457
|
const { data } = await axios.put(
|
|
430
458
|
`/data/${data_table}/${itemID}`,
|
|
431
459
|
updatedItem
|
|
@@ -443,9 +471,9 @@ function responses(key) {
|
|
|
443
471
|
class BagelAuth {
|
|
444
472
|
constructor(bagel) {
|
|
445
473
|
this.bagel = bagel;
|
|
474
|
+
__publicField(this, "user");
|
|
446
475
|
this.bagel = bagel;
|
|
447
476
|
}
|
|
448
|
-
user = undefined;
|
|
449
477
|
async validateUser() {
|
|
450
478
|
try {
|
|
451
479
|
const { data: usr } = await axios.get("/users/me", {
|
|
@@ -493,7 +521,7 @@ class BagelAuth {
|
|
|
493
521
|
this.bagel.onError?.(err);
|
|
494
522
|
console.log(err);
|
|
495
523
|
}
|
|
496
|
-
this.user =
|
|
524
|
+
this.user = void 0;
|
|
497
525
|
}
|
|
498
526
|
async acceptInvite(token, user) {
|
|
499
527
|
await axios.post(`/auth/accept-invite/${token}`, user);
|
|
@@ -511,10 +539,12 @@ class BagelAuth {
|
|
|
511
539
|
}
|
|
512
540
|
}
|
|
513
541
|
class Bagel {
|
|
514
|
-
host;
|
|
515
|
-
fileBaseUrl;
|
|
516
|
-
onError;
|
|
517
542
|
constructor({ host, fileBaseUrl, onError }) {
|
|
543
|
+
__publicField(this, "host");
|
|
544
|
+
__publicField(this, "fileBaseUrl");
|
|
545
|
+
__publicField(this, "onError");
|
|
546
|
+
__publicField(this, "read_table");
|
|
547
|
+
__publicField(this, "auth", new BagelAuth(this));
|
|
518
548
|
this.host = host?.replace(/\/$/, "");
|
|
519
549
|
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
520
550
|
if (!this.host) {
|
|
@@ -523,11 +553,9 @@ class Bagel {
|
|
|
523
553
|
axios.defaults.baseURL = this.host;
|
|
524
554
|
this.onError = onError;
|
|
525
555
|
}
|
|
526
|
-
read_table = undefined;
|
|
527
556
|
data(table) {
|
|
528
557
|
return new DataRequest(table, this);
|
|
529
558
|
}
|
|
530
|
-
auth = new BagelAuth(this);
|
|
531
559
|
_endpointCleaner(endpoint) {
|
|
532
560
|
const url = `${endpoint.replace(/^\//, "").replaceAll(/\/$/g, "")}`;
|
|
533
561
|
return url;
|
|
@@ -542,10 +570,12 @@ class Bagel {
|
|
|
542
570
|
async get(endpoint, query) {
|
|
543
571
|
this._setAuthorization();
|
|
544
572
|
endpoint = this._endpointCleaner(endpoint);
|
|
545
|
-
if (/undefined|null/.test(endpoint))
|
|
573
|
+
if (/undefined|null/.test(endpoint))
|
|
574
|
+
throw new Error(`Invalid endpoint: ${endpoint}`);
|
|
546
575
|
if (query) {
|
|
547
576
|
const queryParams = Object.entries(query).filter(([_, value]) => !!value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
548
|
-
if (queryParams)
|
|
577
|
+
if (queryParams)
|
|
578
|
+
endpoint = `${endpoint}?${queryParams}`;
|
|
549
579
|
}
|
|
550
580
|
const url = `/${endpoint}`;
|
|
551
581
|
return axios.get(url).then(({ data }) => data).catch((err) => {
|
|
@@ -595,7 +625,8 @@ class Bagel {
|
|
|
595
625
|
const formData = new FormData();
|
|
596
626
|
formData.append("file", file);
|
|
597
627
|
let url = "/static_files/upload";
|
|
598
|
-
if (options?.topic)
|
|
628
|
+
if (options?.topic)
|
|
629
|
+
url = `/static_files/upload?topic=${options.topic}`;
|
|
599
630
|
const { data } = await axios.post(url, formData, {
|
|
600
631
|
headers: {
|
|
601
632
|
"Content-Type": "multipart/form-data"
|