export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'; export interface HttpRequestOptions { url: string; method?: HttpMethod; data?: unknown; header?: Record; } export interface HttpResponse { statusCode: number; data: T; header: Record; } export class HttpStatusError extends Error { constructor( message: string, public readonly statusCode: number, public readonly data?: unknown ) { super(message); this.name = 'HttpStatusError'; } } export const httpRequest = (options: HttpRequestOptions) => { return new Promise>((resolve, reject) => { uni.request({ url: options.url, method: options.method || 'GET', data: options.data, header: options.header || {}, success: (response: any) => { resolve({ statusCode: Number(response.statusCode), data: response.data as T, header: response.header || {} }); }, fail: reject }); }); }; export const toFormUrlEncoded = (data: Record = {}) => { return Object.entries(data) .filter(([, value]) => value !== undefined && value !== null) .map(([key, value]) => { const normalizedValue = typeof value === 'object' ? JSON.stringify(value) : String(value); return `${encodeURIComponent(key)}=${encodeURIComponent(normalizedValue)}`; }) .join('&'); };