magically-sdk
Version:
Official SDK for Magically - Build mobile apps with AI
94 lines (81 loc) • 2.7 kB
text/typescript
import { APIClient, RequestOptions } from './APIClient';
import { Logger } from './Logger';
import { SDKConfig } from './types';
import { MagicallyAuth } from './MagicallyAuth';
export interface FunctionInvokeOptions {
headers?: Record<string, string>;
body?: any;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; // Kept for backward compatibility, but ignored
}
export interface FunctionResponse<T = any> {
data?: T;
error?: {
message: string;
code?: string;
};
headers?: Record<string, string>;
status?: number;
}
export class MagicallyFunctions {
private config: SDKConfig;
private auth: MagicallyAuth;
private apiClient: APIClient;
private logger: Logger;
private projectId: string;
constructor(config: SDKConfig, auth: MagicallyAuth) {
this.config = config;
this.auth = auth;
this.apiClient = new APIClient(config, 'MagicallyFunctions');
this.logger = new 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<T = any>(
functionName: string,
options: FunctionInvokeOptions = {}
): Promise<FunctionResponse<T>> {
const startTime = Date.now();
const 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<T>(
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: any) {
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
};
}
}
}