51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
/**
|
|
* API Client for Mail Manager
|
|
* Provides a centralized way to make API calls with envelope wrapping/unwrapping
|
|
*/
|
|
|
|
import { createFetchWrapper } from '@KTXC/utils/helpers/fetch-wrapper-core';
|
|
import type { ApiRequest, ApiResponse } from '../types/common';
|
|
|
|
const fetchWrapper = createFetchWrapper();
|
|
const API_URL = '/m/mail_manager/v1';
|
|
const API_VERSION = 1;
|
|
|
|
/**
|
|
* Generate a unique transaction ID
|
|
*/
|
|
export function generateTransactionId(): string {
|
|
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
}
|
|
|
|
/**
|
|
* Make an API call with automatic envelope wrapping and unwrapping
|
|
*
|
|
* @param operation - Operation name (e.g., 'provider.list', 'service.autodiscover')
|
|
* @param data - Operation-specific request data
|
|
* @param user - Optional user identifier override
|
|
* @returns Promise with unwrapped response data
|
|
* @throws Error if the API returns an error status
|
|
*/
|
|
export async function transceivePost<TRequest, TResponse>(
|
|
operation: string,
|
|
data: TRequest,
|
|
user?: string
|
|
): Promise<TResponse> {
|
|
const request: ApiRequest<TRequest> = {
|
|
version: API_VERSION,
|
|
transaction: generateTransactionId(),
|
|
operation,
|
|
data,
|
|
user
|
|
};
|
|
|
|
const response: ApiResponse<TResponse> = await fetchWrapper.post(API_URL, request);
|
|
|
|
if (response.status === 'error') {
|
|
const errorMessage = `[${operation}] ${response.data.message}${response.data.code ? ` (code: ${response.data.code})` : ''}`;
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
return response.data;
|
|
}
|