import { request } from '@/utils/request';

export const postJson = async <T extends API.Result = API.Result>(
    url: string,
    params?: { [key: string]: any },
    options?: { [key: string]: any }
) => {
    const data = { ...(params || {}) };

    console.log('postJson request:', data);

    const promise = request<T>(url, {
        method: 'POST',
        credentials: 'include',
        requestType: 'json',
        responseType: 'json',
        data: data,
        ...(options || {}),
    }).then((resp) => {
        console.log('postJson response:', resp);
        return resp;
    });

    if (options && options.skipErrorHandler) {
        return promise;
    }

    return promise.catch((err) => {
        console.error('postJson error: ', err);
        return {
            success: false,
            errorMessage: err.message,
        } as T;
    });
};
