@base44/sdk
Version:
JavaScript SDK for Base44 API
133 lines (132 loc) • 5.22 kB
JavaScript
import axios from "axios";
import { isInIFrame } from "./common.js";
import { v4 as uuidv4 } from "uuid";
export class Base44Error extends Error {
constructor(message, status, code, data, originalError) {
super(message);
this.name = "Base44Error";
this.status = status;
this.code = code;
this.data = data;
this.originalError = originalError;
}
// Add a method to safely serialize this error without circular references
toJSON() {
return {
name: this.name,
message: this.message,
status: this.status,
code: this.code,
data: this.data,
};
}
}
/**
* Safely logs error information without circular references
* @param {string} prefix - Prefix for the log message
* @param {Error} error - The error to log
*/
function safeErrorLog(prefix, error) {
if (error instanceof Base44Error) {
console.error(`${prefix} ${error.status}: ${error.message}`);
if (error.data) {
try {
console.error("Error data:", JSON.stringify(error.data, null, 2));
}
catch (e) {
console.error("Error data: [Cannot stringify error data]");
}
}
}
else {
console.error(`${prefix} ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Creates an axios client with default configuration and interceptors
* @param {Object} options - Client configuration options
* @param {string} options.baseURL - Base URL for all requests
* @param {Object} options.headers - Additional headers
* @param {string} options.token - Auth token
* @param {boolean} options.requiresAuth - Whether the application requires authentication
* @param {string|number} options.appId - Application ID (needed for login redirect)
* @param {string} options.serverUrl - Server URL (needed for login redirect)
* @returns {import('axios').AxiosInstance} Configured axios instance
*/
export function createAxiosClient({ baseURL, headers = {}, token, interceptResponses = true, onError, }) {
const client = axios.create({
baseURL,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
...headers,
},
});
// Add token to requests if available
if (token) {
client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
// Add origin URL in browser environment
client.interceptors.request.use((config) => {
if (typeof window !== "undefined") {
config.headers.set("X-Origin-URL", window.location.href);
}
const requestId = uuidv4();
config.requestId = requestId;
if (isInIFrame) {
try {
window.parent.postMessage({
type: "api-request-start",
requestId,
data: {
url: baseURL + config.url,
method: config.method,
body: config.data instanceof FormData
? "[FormData object]"
: config.data,
},
}, "*");
}
catch (_a) {
/* skip the logging */
}
}
return config;
});
// Handle responses
if (interceptResponses) {
client.interceptors.response.use((response) => {
var _a;
const requestId = (_a = response.config) === null || _a === void 0 ? void 0 : _a.requestId;
try {
if (isInIFrame && requestId) {
window.parent.postMessage({
type: "api-request-end",
requestId,
data: {
statusCode: response.status,
response: response.data,
},
}, "*");
}
}
catch (_b) {
/* do nothing */
}
return response.data;
}, (error) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
const message = ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ||
((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.detail) ||
error.message;
const base44Error = new Base44Error(message, (_e = error.response) === null || _e === void 0 ? void 0 : _e.status, (_g = (_f = error.response) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.code, (_h = error.response) === null || _h === void 0 ? void 0 : _h.data, error);
// Log errors in development
if (process.env.NODE_ENV !== "production") {
safeErrorLog("[Base44 SDK Error]", base44Error);
}
onError === null || onError === void 0 ? void 0 : onError(base44Error);
return Promise.reject(base44Error);
});
}
return client;
}