magically-sdk
Version:
Official SDK for Magically - Build mobile apps with AI
111 lines (110 loc) • 4.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagicallyFunctions = void 0;
const APIClient_1 = require("./APIClient");
const Logger_1 = require("./Logger");
class MagicallyFunctions {
constructor(config, auth) {
this.config = config;
this.auth = auth;
this.apiClient = new APIClient_1.APIClient(config, 'MagicallyFunctions');
this.logger = new Logger_1.Logger(config.debug || false, 'MagicallyFunctions');
this.projectId = config.projectId;
}
/**
* Invoke a Cloudflare Worker function
* @param functionName - The name of the function to invoke
* @param options - Options for the function invocation
*/
async invoke(functionName, options = {}) {
const startTime = Date.now();
const method = options.method || 'POST';
this.logger.info(`Invoking function: ${functionName} via ${method}`);
// Build the endpoint path
const endpoint = `/api/functions/v1/${this.projectId}/${functionName}`;
try {
// Get token following MagicallyData pattern
const token = this.apiClient.isEdgeEnvironment()
? null
: await this.auth.getValidToken();
// Make the request through APIClient for proper auth and logging
const response = await this.apiClient.request(endpoint, {
method,
headers: options.headers,
body: options.body,
operation: `invoke_function_${functionName}`
}, token);
const duration = Date.now() - startTime;
this.logger.info(`Function ${functionName} completed in ${duration}ms`);
return {
data: response,
status: 200
};
}
catch (error) {
const duration = Date.now() - startTime;
this.logger.error(`Function ${functionName} failed after ${duration}ms:`, error);
// Simple error handling - just pass through the error
return {
error: {
message: error.message || 'An unknown error occurred',
code: 'FUNCTION_INVOCATION_ERROR'
},
status: 500
};
}
}
/**
* Invoke a function with a specific path
* @param functionName - The name of the function to invoke
* @param path - The path to append to the function URL
* @param options - Options for the function invocation
*/
async invokeWithPath(functionName, path, options = {}) {
const startTime = Date.now();
const method = options.method || 'GET';
// Clean the path (remove leading slash if present)
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
this.logger.info(`Invoking function: ${functionName}/${cleanPath} via ${method}`);
// Build the endpoint path with the additional path segments
const endpoint = `/api/functions/v1/${this.projectId}/${functionName}/${cleanPath}`;
try {
// Get token following MagicallyData pattern
const token = this.apiClient.isEdgeEnvironment()
? null
: await this.auth.getValidToken();
const response = await this.apiClient.request(endpoint, {
method,
headers: options.headers,
body: options.body,
operation: `invoke_function_${functionName}_${cleanPath.replace(/\//g, '_')}`
}, token);
const duration = Date.now() - startTime;
this.logger.info(`Function ${functionName}/${cleanPath} completed in ${duration}ms`);
return {
data: response,
status: 200
};
}
catch (error) {
const duration = Date.now() - startTime;
this.logger.error(`Function ${functionName}/${cleanPath} failed after ${duration}ms:`, error);
// Simple error handling - just pass through the error
return {
error: {
message: error.message || 'An unknown error occurred',
code: 'FUNCTION_INVOCATION_ERROR'
},
status: 500
};
}
}
/**
* Health check for a function
* @param functionName - The name of the function to check
*/
async health(functionName) {
return this.invokeWithPath(functionName, 'health', { method: 'GET' });
}
}
exports.MagicallyFunctions = MagicallyFunctions;