@bagelink/sdk 0.0.1274 → 0.0.1286
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/bin/index.ts +2 -0
- package/bin/utils.ts +66 -0
- package/dist/chunks/acorn.cjs +20 -0
- package/dist/chunks/acorn.mjs +17 -0
- package/dist/chunks/angular.cjs +6 -0
- package/dist/chunks/angular.mjs +3 -0
- package/dist/chunks/babel.cjs +20 -0
- package/dist/chunks/babel.mjs +17 -0
- package/dist/chunks/estree.cjs +43 -0
- package/dist/chunks/estree.mjs +38 -0
- package/dist/chunks/flow.cjs +24 -0
- package/dist/chunks/flow.mjs +21 -0
- package/dist/chunks/glimmer.cjs +37 -0
- package/dist/chunks/glimmer.mjs +32 -0
- package/dist/chunks/graphql.cjs +37 -0
- package/dist/chunks/graphql.mjs +31 -0
- package/dist/chunks/html.cjs +30 -0
- package/dist/chunks/html.mjs +24 -0
- package/dist/chunks/index.cjs +1451 -0
- package/dist/chunks/index.mjs +1441 -0
- package/dist/chunks/jiti.cjs +371 -0
- package/dist/chunks/jiti.mjs +342 -0
- package/dist/chunks/markdown.cjs +71 -0
- package/dist/chunks/markdown.mjs +65 -0
- package/dist/chunks/meriyah.cjs +9 -0
- package/dist/chunks/meriyah.mjs +6 -0
- package/dist/chunks/postcss.cjs +62 -0
- package/dist/chunks/postcss.mjs +56 -0
- package/dist/chunks/typescript.cjs +25 -0
- package/dist/chunks/typescript.mjs +22 -0
- package/dist/chunks/yaml.cjs +169 -0
- package/dist/chunks/yaml.mjs +163 -0
- package/dist/index.cjs +52 -626
- package/dist/index.d.cts +3 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.mjs +46 -621
- package/dist/shared/sdk.BJ23wlaP.mjs +182492 -0
- package/dist/shared/sdk.UmZR6apR.cjs +182547 -0
- package/package.json +2 -2
- package/src/index.ts +2 -0
package/dist/index.d.mts
CHANGED
|
@@ -4,6 +4,8 @@ interface OpenAPIResponse {
|
|
|
4
4
|
}
|
|
5
5
|
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
6
6
|
|
|
7
|
+
declare function formatAPIErrorMessage(error: any): any;
|
|
8
|
+
|
|
7
9
|
type Tables = '';
|
|
8
10
|
type TableToTypeMapping = Record<Tables, any>;
|
|
9
11
|
|
|
@@ -106,4 +108,4 @@ declare class Bagel {
|
|
|
106
108
|
uploadFile<T = any>(file: File, options?: UploadOptions): Promise<T>;
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
export { Bagel, type TableToTypeMapping, type Tables, type User, _default as openAPI };
|
|
111
|
+
export { Bagel, type TableToTypeMapping, type Tables, type User, formatAPIErrorMessage, _default as openAPI };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ interface OpenAPIResponse {
|
|
|
4
4
|
}
|
|
5
5
|
declare const _default: (openApiUrl: string, baseUrl: string) => Promise<OpenAPIResponse>;
|
|
6
6
|
|
|
7
|
+
declare function formatAPIErrorMessage(error: any): any;
|
|
8
|
+
|
|
7
9
|
type Tables = '';
|
|
8
10
|
type TableToTypeMapping = Record<Tables, any>;
|
|
9
11
|
|
|
@@ -106,4 +108,4 @@ declare class Bagel {
|
|
|
106
108
|
uploadFile<T = any>(file: File, options?: UploadOptions): Promise<T>;
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
export { Bagel, type TableToTypeMapping, type Tables, type User, _default as openAPI };
|
|
111
|
+
export { Bagel, type TableToTypeMapping, type Tables, type User, formatAPIErrorMessage, _default as openAPI };
|
package/dist/index.mjs
CHANGED
|
@@ -1,621 +1,46 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return "any";
|
|
48
|
-
default:
|
|
49
|
-
console.log("Unknown type", schema.type);
|
|
50
|
-
return "any";
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function isOptional(schema) {
|
|
54
|
-
const type = schemaToType(schema);
|
|
55
|
-
const splitType = type.split(/\s+\|\s+/);
|
|
56
|
-
const includesNull = splitType.includes("null");
|
|
57
|
-
const includesUndefined = splitType.includes("undefined");
|
|
58
|
-
return includesNull || includesUndefined || schema.default !== undefined;
|
|
59
|
-
}
|
|
60
|
-
function cleanOptionals(str) {
|
|
61
|
-
return str.split(" | ").filter((t) => t !== "null" && t !== "undefined").join(" | ");
|
|
62
|
-
}
|
|
63
|
-
function formatVarType({
|
|
64
|
-
varName,
|
|
65
|
-
schema,
|
|
66
|
-
required = false,
|
|
67
|
-
defaultValue
|
|
68
|
-
}) {
|
|
69
|
-
let type = schemaToType(schema);
|
|
70
|
-
type = cleanOptionals(type);
|
|
71
|
-
let defaultStr = "";
|
|
72
|
-
if (defaultValue) {
|
|
73
|
-
if (typeof defaultValue === "string") {
|
|
74
|
-
defaultStr = ` = '${defaultValue}'`;
|
|
75
|
-
} else if (typeof defaultValue === "object") {
|
|
76
|
-
defaultStr = ` = ${JSON.stringify(defaultValue)}`;
|
|
77
|
-
} else {
|
|
78
|
-
defaultStr = ` = ${defaultValue}`;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
let optionalStr = !required && isOptional(schema) ? "?" : "";
|
|
82
|
-
if (defaultStr) optionalStr = "";
|
|
83
|
-
return `${varName}${optionalStr}: ${type}${defaultStr}`;
|
|
84
|
-
}
|
|
85
|
-
function cleanPath(path) {
|
|
86
|
-
return path.split("/").filter((p) => p && !/\{|\}/.test(p)).join("/");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const allTypes = [];
|
|
90
|
-
const primitiveTypes = [
|
|
91
|
-
"string",
|
|
92
|
-
"number",
|
|
93
|
-
"boolean",
|
|
94
|
-
"null",
|
|
95
|
-
"void",
|
|
96
|
-
"any",
|
|
97
|
-
"Record<string, any>",
|
|
98
|
-
"undefined",
|
|
99
|
-
"{ [key: string]: any }"
|
|
100
|
-
];
|
|
101
|
-
function collectTypeForImportStatement(typeName) {
|
|
102
|
-
typeName = typeName.trim().replace("[]", "");
|
|
103
|
-
if (typeName.includes("|")) {
|
|
104
|
-
typeName.split("|").forEach((singleType) => {
|
|
105
|
-
collectTypeForImportStatement(singleType);
|
|
106
|
-
});
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
const isPrimitive = primitiveTypes.includes(typeName);
|
|
110
|
-
typeName = formatType(typeName);
|
|
111
|
-
if (!typeName || isPrimitive) return;
|
|
112
|
-
if (!allTypes.includes(typeName)) allTypes.push(typeName);
|
|
113
|
-
}
|
|
114
|
-
function schemaToTypeWithCollection(schema) {
|
|
115
|
-
const type = schemaToType(schema);
|
|
116
|
-
collectTypeForImportStatement(type);
|
|
117
|
-
return type;
|
|
118
|
-
}
|
|
119
|
-
function getResponseType(response) {
|
|
120
|
-
const mediaTypeObject = response.content?.["application/json"];
|
|
121
|
-
if (!mediaTypeObject || !mediaTypeObject.schema) return;
|
|
122
|
-
const responseType = schemaToTypeWithCollection(mediaTypeObject.schema);
|
|
123
|
-
return responseType;
|
|
124
|
-
}
|
|
125
|
-
function generateResponseType(responses) {
|
|
126
|
-
if (!responses) return "";
|
|
127
|
-
const types = [];
|
|
128
|
-
for (const [statusCode, response] of Object.entries(responses)) {
|
|
129
|
-
if (statusCode.startsWith("2")) {
|
|
130
|
-
const responseType = getResponseType(response);
|
|
131
|
-
if (responseType && responseType !== "any") {
|
|
132
|
-
types.push(responseType);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return types.join(" | ");
|
|
137
|
-
}
|
|
138
|
-
function generateAxiosFunction(method, formattedPath, allParams, responseTypeStr, parameters, requestBodyPayload) {
|
|
139
|
-
if (allParams === "undefined") allParams = "";
|
|
140
|
-
let axiosFunction = `async (${allParams})${responseTypeStr} => {`;
|
|
141
|
-
if (requestBodyPayload === "formData") {
|
|
142
|
-
const paramStr = parameters?.config?.params ? `params: {${parameters.config.params}}` : "";
|
|
143
|
-
axiosFunction += `
|
|
144
|
-
const formData = new FormData()
|
|
145
|
-
formData.append('upload', file)
|
|
146
|
-
return axios.${method}(${formattedPath}, formData, {
|
|
147
|
-
headers: { 'Content-Type': 'multipart/form-data' },
|
|
148
|
-
onUploadProgress: options?.onUploadProgress${paramStr ? `,
|
|
149
|
-
${paramStr}` : ""}
|
|
150
|
-
})`;
|
|
151
|
-
} else {
|
|
152
|
-
const paramStr = parameters?.config?.params ? `, { params: {${parameters.config.params}} }` : "";
|
|
153
|
-
const bodyVar = requestBodyPayload ? `${requestBodyPayload}` : "{}";
|
|
154
|
-
axiosFunction += `return axios.${method}(${formattedPath}${["get", "delete"].includes(method) ? paramStr : `, ${bodyVar}${paramStr}`})`;
|
|
155
|
-
}
|
|
156
|
-
axiosFunction += "}";
|
|
157
|
-
return axiosFunction;
|
|
158
|
-
}
|
|
159
|
-
const pathParamRegex = /\{([^}]+)\}/g;
|
|
160
|
-
function getParamsFromPath(path) {
|
|
161
|
-
const params = path.match(pathParamRegex)?.map((p) => p.slice(1, -1));
|
|
162
|
-
return params;
|
|
163
|
-
}
|
|
164
|
-
function formatPathWithParams(path) {
|
|
165
|
-
const params = getParamsFromPath(path);
|
|
166
|
-
const formattedPath = params ? `\`${path.replace(pathParamRegex, (v) => `$${toCamelCase(v)}`)}\`` : `'${path}'`;
|
|
167
|
-
return formattedPath;
|
|
168
|
-
}
|
|
169
|
-
function generateRequestBody(requestBody) {
|
|
170
|
-
if (!requestBody?.content)
|
|
171
|
-
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
172
|
-
const { content } = requestBody;
|
|
173
|
-
const multipartFormData = content["multipart/form-data"] || {};
|
|
174
|
-
if (multipartFormData.schema?.$ref?.includes("Body_upload_files")) {
|
|
175
|
-
return {
|
|
176
|
-
requestBodyParam: "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }",
|
|
177
|
-
requestBodyPayload: "formData"
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
const jsonContent = requestBody.content["application/json"];
|
|
181
|
-
if (!jsonContent || !jsonContent.schema) {
|
|
182
|
-
return { requestBodyParam: "", requestBodyPayload: "" };
|
|
183
|
-
}
|
|
184
|
-
const bodySchema = jsonContent.schema;
|
|
185
|
-
const requestBodyType = schemaToType(bodySchema);
|
|
186
|
-
collectTypeForImportStatement(requestBodyType);
|
|
187
|
-
const requestBodyPayload = toCamelCase(bodySchema.title) || toCamelCase(requestBodyType) || "requestBody";
|
|
188
|
-
const requestBodyParam = formatVarType({
|
|
189
|
-
varName: requestBodyPayload,
|
|
190
|
-
schema: bodySchema,
|
|
191
|
-
defaultValue: bodySchema.default
|
|
192
|
-
});
|
|
193
|
-
return { requestBodyParam, requestBodyPayload };
|
|
194
|
-
}
|
|
195
|
-
function combineAllParams(parameters, requestBodyParam) {
|
|
196
|
-
let allParamsArray = [];
|
|
197
|
-
if (parameters && parameters.params)
|
|
198
|
-
allParamsArray = parameters.params.split(",").map((p) => p.trim());
|
|
199
|
-
if (requestBodyParam) allParamsArray.push(requestBodyParam.trim());
|
|
200
|
-
allParamsArray = allParamsArray.filter((p) => p).sort((a, b) => (a.includes("?") ? 1 : -1) - (b.includes("?") ? 1 : -1));
|
|
201
|
-
return allParamsArray.join(", ");
|
|
202
|
-
}
|
|
203
|
-
function generateFunctionParameters(params, isFileUpload = false) {
|
|
204
|
-
if (!params?.length) return {};
|
|
205
|
-
if (isFileUpload) {
|
|
206
|
-
return {
|
|
207
|
-
config: {
|
|
208
|
-
params: "dir_path: options?.dirPath, tags: options?.tags"
|
|
209
|
-
},
|
|
210
|
-
params: ""
|
|
211
|
-
// Empty string to ensure file is the first parameter
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
const functionParams = [];
|
|
215
|
-
const paramList = [];
|
|
216
|
-
for (const param of params) {
|
|
217
|
-
const paramType = schemaToType(param.schema);
|
|
218
|
-
collectTypeForImportStatement(paramType);
|
|
219
|
-
const paramName = param.name;
|
|
220
|
-
const varName = toCamelCase(param.name);
|
|
221
|
-
if (param.in === "path" || param.in === "query" || param.in === "header") {
|
|
222
|
-
functionParams.push(
|
|
223
|
-
formatVarType({
|
|
224
|
-
varName,
|
|
225
|
-
schema: param.schema,
|
|
226
|
-
required: param.required,
|
|
227
|
-
defaultValue: param.schema.default
|
|
228
|
-
})
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
if (param.in === "query" || param.in === "header") {
|
|
232
|
-
paramList.push(
|
|
233
|
-
paramName === varName ? paramName : `'${paramName}': ${varName}`
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return {
|
|
238
|
-
params: functionParams.join(", "),
|
|
239
|
-
config: paramList.length ? { params: paramList.join(", ") } : {}
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
function generateFunctionForOperation(method, path, operation) {
|
|
243
|
-
if (!operation) return "";
|
|
244
|
-
const isFileUpload = operation.requestBody?.content["multipart/form-data"]?.schema?.$ref?.includes("Body_upload_files");
|
|
245
|
-
const parameters = generateFunctionParameters(
|
|
246
|
-
operation.parameters,
|
|
247
|
-
isFileUpload
|
|
248
|
-
);
|
|
249
|
-
const { requestBodyParam, requestBodyPayload } = generateRequestBody(
|
|
250
|
-
operation.requestBody
|
|
251
|
-
);
|
|
252
|
-
const allParams = isFileUpload ? "file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }" : combineAllParams(parameters, requestBodyParam);
|
|
253
|
-
const responseType = generateResponseType(operation.responses);
|
|
254
|
-
const responseTypeStr = responseType ? `: Promise<AxiosResponse<${responseType}>>` : "";
|
|
255
|
-
return generateAxiosFunction(
|
|
256
|
-
method,
|
|
257
|
-
formatPathWithParams(path),
|
|
258
|
-
allParams,
|
|
259
|
-
responseTypeStr,
|
|
260
|
-
parameters,
|
|
261
|
-
requestBodyPayload
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
const generateRandomString = () => Math.random().toString(36).slice(7);
|
|
265
|
-
const DOUBLE_QUOTE_REGEX = /"([^"]+)":/g;
|
|
266
|
-
function fileTemplate(tsString, typeForImport, baseURL) {
|
|
267
|
-
const templateCode = `import ax from 'axios';
|
|
268
|
-
import type { AxiosResponse } from 'axios';
|
|
269
|
-
import type {${typeForImport.join(", ")}} from './types.d';
|
|
270
|
-
|
|
271
|
-
interface UploadOptions {
|
|
272
|
-
onUploadProgress?: (progressEvent: any) => void
|
|
273
|
-
dirPath?: string
|
|
274
|
-
tags?: string[]
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export const axios = ax.create({baseURL:${baseURL}});
|
|
278
|
-
${tsString}`;
|
|
279
|
-
return templateCode.replace(DOUBLE_QUOTE_REGEX, "$1:");
|
|
280
|
-
}
|
|
281
|
-
const functionsInventory = {};
|
|
282
|
-
const pathOperations = [];
|
|
283
|
-
function hasConflict(path, method) {
|
|
284
|
-
const cleanPathName = path.split("/").filter((p) => p && !/\{|\}/.test(p)).join("/");
|
|
285
|
-
const matchingPaths = pathOperations.filter(
|
|
286
|
-
(p) => p.path === cleanPathName && p.method === method
|
|
287
|
-
);
|
|
288
|
-
pathOperations.push({ path: cleanPathName, method });
|
|
289
|
-
return matchingPaths.length > 0;
|
|
290
|
-
}
|
|
291
|
-
function createFunctionPlaceholder(path, method, operation) {
|
|
292
|
-
const funcID = generateRandomString();
|
|
293
|
-
functionsInventory[funcID] = generateFunctionForOperation(
|
|
294
|
-
method,
|
|
295
|
-
path,
|
|
296
|
-
operation
|
|
297
|
-
);
|
|
298
|
-
return funcID;
|
|
299
|
-
}
|
|
300
|
-
function handlePathSegment(path, operation, existingObj = {}) {
|
|
301
|
-
const methods = Object.keys(operation);
|
|
302
|
-
const obj = {};
|
|
303
|
-
for (const method of methods) {
|
|
304
|
-
let functionName = method.toLowerCase();
|
|
305
|
-
if (hasConflict(path, method)) {
|
|
306
|
-
const params = getParamsFromPath(path);
|
|
307
|
-
functionName += params ? `By${toPascalCase(params.pop() || "")}` : "All";
|
|
308
|
-
}
|
|
309
|
-
obj[functionName] = createFunctionPlaceholder(
|
|
310
|
-
path,
|
|
311
|
-
method,
|
|
312
|
-
operation[method]
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
return { ...obj, ...existingObj };
|
|
316
|
-
}
|
|
317
|
-
function generateFunctions(paths, baseUrl) {
|
|
318
|
-
let tsString = "";
|
|
319
|
-
const body = {};
|
|
320
|
-
const allPathsClean = Object.keys(paths).map(cleanPath);
|
|
321
|
-
for (const [path, operation] of Object.entries(paths)) {
|
|
322
|
-
const splitPath = path.split("/").filter((p) => p && !/\{|\}/.test(p));
|
|
323
|
-
splitPath.reduce((acc, key, index, array) => {
|
|
324
|
-
const objFuncKey = toCamelCase(key);
|
|
325
|
-
if (!objFuncKey) return acc;
|
|
326
|
-
const methods = Object.keys(operation);
|
|
327
|
-
if (index === array.length - 1 && methods.length === 1 && allPathsClean.filter((p) => p === cleanPath(path)).length === 1) {
|
|
328
|
-
const method = methods[0];
|
|
329
|
-
const opp = { ...operation }[method];
|
|
330
|
-
acc[objFuncKey] = createFunctionPlaceholder(path, methods[0], opp);
|
|
331
|
-
} else if (index === array.length - 1) {
|
|
332
|
-
acc[objFuncKey] = handlePathSegment(path, operation, acc[objFuncKey]);
|
|
333
|
-
} else if (!acc[objFuncKey] || typeof acc[objFuncKey] !== "object") {
|
|
334
|
-
acc[objFuncKey] = {};
|
|
335
|
-
}
|
|
336
|
-
return acc[objFuncKey];
|
|
337
|
-
}, body);
|
|
338
|
-
}
|
|
339
|
-
for (const [parent, object] of Object.entries(body)) {
|
|
340
|
-
tsString += `export const ${parent} = ${JSON.stringify(object, undefined, 2)};
|
|
341
|
-
`;
|
|
342
|
-
}
|
|
343
|
-
Object.entries(functionsInventory).forEach(([key, value]) => {
|
|
344
|
-
tsString = tsString.replace(`"${key}"`, value);
|
|
345
|
-
});
|
|
346
|
-
tsString = fileTemplate(tsString, allTypes, baseUrl);
|
|
347
|
-
return tsString;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
function generateTypes(schemas) {
|
|
351
|
-
return Object.entries(schemas).map(([typeName, schema]) => {
|
|
352
|
-
typeName = formatType(typeName);
|
|
353
|
-
if (schema.enum) {
|
|
354
|
-
return `export type ${typeName} = ${schema.enum.map((item) => `'${item}'`).join(" | ")};
|
|
355
|
-
`;
|
|
356
|
-
}
|
|
357
|
-
if (schema.type === "array" && schema.items) {
|
|
358
|
-
return `export type ${typeName} = ${schemaToType(schema.items)}[];
|
|
359
|
-
`;
|
|
360
|
-
}
|
|
361
|
-
if (!schema.properties) {
|
|
362
|
-
return "";
|
|
363
|
-
}
|
|
364
|
-
const properties = Object.entries(schema.properties).map(([key, value]) => {
|
|
365
|
-
const varType = formatVarType({ varName: key, schema: value });
|
|
366
|
-
return ` ${varType}`;
|
|
367
|
-
}).join(";\n ");
|
|
368
|
-
return `export type ${typeName} = {
|
|
369
|
-
${properties};
|
|
370
|
-
};
|
|
371
|
-
`;
|
|
372
|
-
}).join("\n");
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
const basicAuthHeader = { Authorization: "Basic YmFnZWxfdXNlcm5hbWU6Tm90U2VjdXJlQGJhZ2Vs" };
|
|
376
|
-
const index = async (openApiUrl, baseUrl) => {
|
|
377
|
-
try {
|
|
378
|
-
const { data: openApi } = await axios$1.get(openApiUrl, { headers: basicAuthHeader });
|
|
379
|
-
const schemas = openApi.components?.schemas;
|
|
380
|
-
if (!schemas) throw new Error("No schemas found in OpenAPI document");
|
|
381
|
-
const types = generateTypes(schemas);
|
|
382
|
-
const { paths } = openApi;
|
|
383
|
-
if (!paths) throw new Error("No paths found in OpenAPI document");
|
|
384
|
-
const code = generateFunctions(paths, baseUrl);
|
|
385
|
-
return { types, code };
|
|
386
|
-
} catch (error) {
|
|
387
|
-
throw new Error(error);
|
|
388
|
-
}
|
|
389
|
-
};
|
|
390
|
-
|
|
391
|
-
const axios = axios$1.create({
|
|
392
|
-
withCredentials: true
|
|
393
|
-
});
|
|
394
|
-
class DataRequest {
|
|
395
|
-
data_table;
|
|
396
|
-
bagel;
|
|
397
|
-
itemID;
|
|
398
|
-
_filter = {};
|
|
399
|
-
constructor(table, bagel) {
|
|
400
|
-
this.data_table = table;
|
|
401
|
-
this.bagel = bagel;
|
|
402
|
-
this.itemID = "";
|
|
403
|
-
}
|
|
404
|
-
async post(item) {
|
|
405
|
-
if (!this.data_table) throw new Error("Data table not set");
|
|
406
|
-
const { data } = await axios.post(`/data/${this.data_table}`, item);
|
|
407
|
-
return data;
|
|
408
|
-
}
|
|
409
|
-
filter(filter) {
|
|
410
|
-
this._filter = filter;
|
|
411
|
-
return this;
|
|
412
|
-
}
|
|
413
|
-
async get() {
|
|
414
|
-
if (!this.data_table) throw new Error("Data table not set");
|
|
415
|
-
const filterStr = Object.keys(this._filter).length > 0 ? `?filter={${Object.entries(this._filter).map(([k, v]) => `${k}:${v}`).join(",")}}` : "";
|
|
416
|
-
const url = `/data/${this.data_table}${this.itemID ? `/${this.itemID}` : ""}${filterStr}`;
|
|
417
|
-
try {
|
|
418
|
-
const { data } = await axios.get(url);
|
|
419
|
-
return data;
|
|
420
|
-
} catch (err) {
|
|
421
|
-
console.log(err);
|
|
422
|
-
this.bagel.onError?.(err);
|
|
423
|
-
return undefined;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
item(id) {
|
|
427
|
-
this.itemID = id;
|
|
428
|
-
return this;
|
|
429
|
-
}
|
|
430
|
-
async delete() {
|
|
431
|
-
if (!this.data_table) throw new Error("Data table not set");
|
|
432
|
-
const { data } = await axios.delete(
|
|
433
|
-
`/data/${this.data_table}/${this.itemID}`
|
|
434
|
-
);
|
|
435
|
-
return data;
|
|
436
|
-
}
|
|
437
|
-
async put(updatedItem) {
|
|
438
|
-
const { data_table, itemID } = this;
|
|
439
|
-
if (!data_table) throw new Error("Data table not set");
|
|
440
|
-
if (!itemID) throw new Error("Item ID not set");
|
|
441
|
-
const { data } = await axios.put(
|
|
442
|
-
`/data/${data_table}/${itemID}`,
|
|
443
|
-
updatedItem
|
|
444
|
-
);
|
|
445
|
-
return data;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
function responses(key) {
|
|
449
|
-
const res = {
|
|
450
|
-
LOGIN_BAD_CREDENTIALS: "Invalid username or password",
|
|
451
|
-
RESET_PASSWORD_BAD_TOKEN: "This reset password link is invalid or expired."
|
|
452
|
-
};
|
|
453
|
-
return res[key] || key;
|
|
454
|
-
}
|
|
455
|
-
class BagelAuth {
|
|
456
|
-
constructor(bagel) {
|
|
457
|
-
this.bagel = bagel;
|
|
458
|
-
this.bagel = bagel;
|
|
459
|
-
}
|
|
460
|
-
user = undefined;
|
|
461
|
-
async validateUser() {
|
|
462
|
-
try {
|
|
463
|
-
const { data: usr } = await axios.get("/users/me", {
|
|
464
|
-
withCredentials: true
|
|
465
|
-
});
|
|
466
|
-
this.user = usr;
|
|
467
|
-
return usr;
|
|
468
|
-
} catch {
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
async resetPassword(ctx) {
|
|
472
|
-
return this.bagel.post("auth/reset-password", ctx).catch((err) => {
|
|
473
|
-
throw responses(err.response?.data?.detail);
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
async forgotPassword(email) {
|
|
477
|
-
try {
|
|
478
|
-
await this.bagel.post("auth/forgot-password", { email });
|
|
479
|
-
} catch (err) {
|
|
480
|
-
throw responses(err?.response?.data?.detail);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
async login(user) {
|
|
484
|
-
const formData = new FormData();
|
|
485
|
-
formData.append("username", user.email || "");
|
|
486
|
-
formData.append("password", user.password || "");
|
|
487
|
-
console.log("Logging in");
|
|
488
|
-
try {
|
|
489
|
-
await axios.post("/auth/cookie/login", formData, {
|
|
490
|
-
headers: {
|
|
491
|
-
"withCredentials": true,
|
|
492
|
-
"Content-Type": "multipart/form-data"
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
axios.defaults.withCredentials = true;
|
|
496
|
-
return this.validateUser();
|
|
497
|
-
} catch (err) {
|
|
498
|
-
throw responses(err.response?.data?.detail || "LOGIN_BAD_CREDENTIALS");
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
async logout() {
|
|
502
|
-
try {
|
|
503
|
-
await axios.post("/auth/cookie/logout");
|
|
504
|
-
} catch (err) {
|
|
505
|
-
this.bagel.onError?.(err);
|
|
506
|
-
console.log(err);
|
|
507
|
-
}
|
|
508
|
-
this.user = undefined;
|
|
509
|
-
}
|
|
510
|
-
async acceptInvite(token, user) {
|
|
511
|
-
await axios.post(`/auth/accept-invite/${token}`, user);
|
|
512
|
-
await this.login(user);
|
|
513
|
-
}
|
|
514
|
-
async register(user, errors) {
|
|
515
|
-
try {
|
|
516
|
-
await axios.post("/auth/register", user);
|
|
517
|
-
return this.login(user);
|
|
518
|
-
} catch (err) {
|
|
519
|
-
console.error(err);
|
|
520
|
-
errors.email = err.response.data.detail;
|
|
521
|
-
throw err;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
class Bagel {
|
|
526
|
-
host;
|
|
527
|
-
fileBaseUrl;
|
|
528
|
-
onError;
|
|
529
|
-
constructor({ host, fileBaseUrl, onError }) {
|
|
530
|
-
this.host = host?.replace(/\/$/, "");
|
|
531
|
-
this.fileBaseUrl = fileBaseUrl?.replace(/\/$/, "");
|
|
532
|
-
if (!this.host) {
|
|
533
|
-
return this;
|
|
534
|
-
}
|
|
535
|
-
axios.defaults.baseURL = this.host;
|
|
536
|
-
this.onError = onError;
|
|
537
|
-
}
|
|
538
|
-
read_table = undefined;
|
|
539
|
-
data(table) {
|
|
540
|
-
return new DataRequest(table, this);
|
|
541
|
-
}
|
|
542
|
-
auth = new BagelAuth(this);
|
|
543
|
-
_endpointCleaner(endpoint) {
|
|
544
|
-
const url = `${endpoint.replace(/^\//, "").replaceAll(/\/$/g, "")}`;
|
|
545
|
-
return url;
|
|
546
|
-
}
|
|
547
|
-
async api(endpoint, query) {
|
|
548
|
-
if (query) {
|
|
549
|
-
const queryParams = Object.entries(query).map(([key, value]) => `${key}=${value}`).join("&");
|
|
550
|
-
endpoint = `${endpoint}?${queryParams}`;
|
|
551
|
-
}
|
|
552
|
-
return axios.get(`/api/${endpoint}`).then(({ data }) => data);
|
|
553
|
-
}
|
|
554
|
-
async get(endpoint, query) {
|
|
555
|
-
this._setAuthorization();
|
|
556
|
-
endpoint = this._endpointCleaner(endpoint);
|
|
557
|
-
if (/undefined|null/.test(endpoint)) throw new Error(`Invalid endpoint: ${endpoint}`);
|
|
558
|
-
if (query) {
|
|
559
|
-
const queryParams = Object.entries(query).filter(([_, value]) => !!value).map(([key, value]) => `${key}=${value}`).join("&");
|
|
560
|
-
if (queryParams) endpoint = `${endpoint}?${queryParams}`;
|
|
561
|
-
}
|
|
562
|
-
const url = `/${endpoint}`;
|
|
563
|
-
return axios.get(url).then(({ data }) => data).catch((err) => {
|
|
564
|
-
this.onError?.(err);
|
|
565
|
-
throw err;
|
|
566
|
-
});
|
|
567
|
-
}
|
|
568
|
-
async delete(endpoint) {
|
|
569
|
-
this._setAuthorization();
|
|
570
|
-
endpoint = this._endpointCleaner(endpoint);
|
|
571
|
-
return axios.delete(`/${endpoint}`).then(({ data }) => data).catch((err) => {
|
|
572
|
-
this.onError?.(err);
|
|
573
|
-
throw err;
|
|
574
|
-
});
|
|
575
|
-
}
|
|
576
|
-
async put(endpoint, payload) {
|
|
577
|
-
endpoint = this._endpointCleaner(endpoint);
|
|
578
|
-
this._setAuthorization();
|
|
579
|
-
return axios.put(`/${endpoint}`, payload).then(({ data }) => data).catch((err) => {
|
|
580
|
-
this.onError?.(err);
|
|
581
|
-
throw err;
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
async patch(endpoint, payload = {}) {
|
|
585
|
-
endpoint = this._endpointCleaner(endpoint);
|
|
586
|
-
return axios.patch(`/${endpoint}`, payload).then(({ data }) => data).catch((err) => {
|
|
587
|
-
this.onError?.(err);
|
|
588
|
-
throw err;
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
_setAuthorization() {
|
|
592
|
-
const Authorization = localStorage.getItem("access_token");
|
|
593
|
-
if (Authorization) {
|
|
594
|
-
axios.defaults.headers.common.Authorization = `Bearer ${Authorization}`;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
async post(endpoint, payload = {}) {
|
|
598
|
-
this._setAuthorization();
|
|
599
|
-
endpoint = this._endpointCleaner(endpoint);
|
|
600
|
-
return axios.post(`/${endpoint}`, payload).then(({ data }) => data).catch((err) => {
|
|
601
|
-
this.onError?.(err);
|
|
602
|
-
throw err;
|
|
603
|
-
});
|
|
604
|
-
}
|
|
605
|
-
async uploadFile(file, options) {
|
|
606
|
-
this._setAuthorization();
|
|
607
|
-
const formData = new FormData();
|
|
608
|
-
formData.append("file", file);
|
|
609
|
-
let url = "/static_files/upload";
|
|
610
|
-
if (options?.topic) url = `/static_files/upload?topic=${options.topic}`;
|
|
611
|
-
const { data } = await axios.post(url, formData, {
|
|
612
|
-
headers: {
|
|
613
|
-
"Content-Type": "multipart/form-data"
|
|
614
|
-
},
|
|
615
|
-
onUploadProgress: options?.onUploadProgress
|
|
616
|
-
});
|
|
617
|
-
return data;
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
export { Bagel, index as openAPI };
|
|
1
|
+
import 'axios';
|
|
2
|
+
export { B as Bagel, f as formatAPIErrorMessage, i as openAPI } from './shared/sdk.BJ23wlaP.mjs';
|
|
3
|
+
import 'node:fs';
|
|
4
|
+
import 'node:path';
|
|
5
|
+
import 'node:process';
|
|
6
|
+
import 'node:fs/promises';
|
|
7
|
+
import 'util';
|
|
8
|
+
import 'path';
|
|
9
|
+
import 'tty';
|
|
10
|
+
import 'os';
|
|
11
|
+
import 'fs';
|
|
12
|
+
import 'module';
|
|
13
|
+
import 'assert';
|
|
14
|
+
import 'url';
|
|
15
|
+
import 'node:url';
|
|
16
|
+
import 'crypto';
|
|
17
|
+
import 'events';
|
|
18
|
+
import 'node:util';
|
|
19
|
+
import 'node:assert';
|
|
20
|
+
import 'process';
|
|
21
|
+
import 'fs/promises';
|
|
22
|
+
import 'v8';
|
|
23
|
+
import 'node:perf_hooks';
|
|
24
|
+
import 'node:module';
|
|
25
|
+
import 'node:crypto';
|
|
26
|
+
import 'child_process';
|
|
27
|
+
import 'node:child_process';
|
|
28
|
+
import 'node:http';
|
|
29
|
+
import 'node:https';
|
|
30
|
+
import 'net';
|
|
31
|
+
import 'http';
|
|
32
|
+
import 'stream';
|
|
33
|
+
import 'node:os';
|
|
34
|
+
import 'node:net';
|
|
35
|
+
import 'node:dns';
|
|
36
|
+
import 'node:readline';
|
|
37
|
+
import 'node:buffer';
|
|
38
|
+
import 'node:events';
|
|
39
|
+
import 'node:v8';
|
|
40
|
+
import 'node:worker_threads';
|
|
41
|
+
import 'zlib';
|
|
42
|
+
import 'buffer';
|
|
43
|
+
import 'https';
|
|
44
|
+
import 'tls';
|
|
45
|
+
import 'node:querystring';
|
|
46
|
+
import 'node:zlib';
|