Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
"bugs": {
"url": "https://github.com/base44/sdk/issues"
},
"homepage": "https://github.com/base44/sdk#readme"
"homepage": "https://github.com/base44/sdk#readme",
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
85 changes: 35 additions & 50 deletions src/modules/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,44 @@ import { AxiosInstance } from "axios";
*/
export function createFunctionsModule(axios: AxiosInstance, appId: string) {
// Using nested Proxy objects to handle dynamic function names
return new Proxy(
{},
{
get(_, functionName) {
// Skip internal properties
if (
typeof functionName !== "string" ||
functionName === "then" ||
functionName.startsWith("_")
) {
return undefined;
}
return {
async invoke(functionName: string, data: Record<string, any>) {
// Validate input
if (typeof data === "string") {
throw new Error(
`Function ${functionName} must receive an object with named parameters, received: ${data}`
);
}

// Return a function that calls the function endpoint
return async (data: Record<string, any>) => {
// Validate input
if (typeof data === "string") {
throw new Error(
`Function ${functionName} must receive an object with named parameters, received: ${data}`
);
}

let formData: FormData | Record<string, any>;
let contentType: string;
let formData: FormData | Record<string, any>;
let contentType: string;

// Handle file uploads with FormData
if (
data instanceof FormData ||
(data && Object.values(data).some((value) => value instanceof File))
) {
formData = new FormData();
Object.keys(data).forEach((key) => {
if (data[key] instanceof File) {
formData.append(key, data[key], data[key].name);
} else if (typeof data[key] === "object" && data[key] !== null) {
formData.append(key, JSON.stringify(data[key]));
} else {
formData.append(key, data[key]);
}
});
contentType = "multipart/form-data";
// Handle file uploads with FormData
if (
data instanceof FormData ||
(data && Object.values(data).some((value) => value instanceof File))
) {
formData = new FormData();
Object.keys(data).forEach((key) => {
if (data[key] instanceof File) {
formData.append(key, data[key], data[key].name);
} else if (typeof data[key] === "object" && data[key] !== null) {
formData.append(key, JSON.stringify(data[key]));
} else {
formData = data;
contentType = "application/json";
formData.append(key, data[key]);
}
});
contentType = "multipart/form-data";
} else {
formData = data;
contentType = "application/json";
}

return axios.post(
`/apps/${appId}/functions/${functionName}`,
formData || data,
{ headers: { "Content-Type": contentType } }
);
};
},
}
);
return axios.post(
`/apps/${appId}/functions/${functionName}`,
formData || data,
{ headers: { "Content-Type": contentType } }
);
},
};
}
Loading