import { HttpClient, HttpClientConfig } from './fetch' import { AuthError } from './errors' import { z } from 'zod' /** Response wrapper for validated API calls */ export interface FetchResponse { data: T | null error: AuthError | null } export class ValidatedApiClient extends HttpClient { constructor(config: HttpClientConfig) { super(config) } /** Override get to return FetchResponse */ // @ts-expect-error - Intentionally changing return type from AxiosResponse to FetchResponse async get(url: string, options: any = {}): Promise> { const response = await super.get(url, options).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data return { data: actualData as T, error: null } } /** Override post to return FetchResponse */ // @ts-expect-error - Intentionally changing return type from AxiosResponse to FetchResponse async post(url: string, body?: any, options: any = {}): Promise> { const response = await super.post(url, body, options).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data return { data: actualData as T, error: null } } /** Override put to return FetchResponse */ // @ts-expect-error - Intentionally changing return type from AxiosResponse to FetchResponse async put(url: string, body?: any, options: any = {}): Promise> { const response = await super.put(url, body, options).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data return { data: actualData as T, error: null } } /** Override patch to return FetchResponse */ // @ts-expect-error - Intentionally changing return type from AxiosResponse to FetchResponse async patch(url: string, body?: any, options: any = {}): Promise> { const response = await super.patch(url, body, options).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data return { data: actualData as T, error: null } } /** Override delete to return FetchResponse */ // @ts-expect-error - Intentionally changing return type from AxiosResponse to FetchResponse async delete(url: string, options: any = {}): Promise> { const response = await super.delete(url, options).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data return { data: actualData as T, error: null } } async postWithValidation( url: string, body: any, requestSchema?: z.ZodSchema, responseSchema?: z.ZodSchema, options?: any ): Promise> { // 1. Validate request body if (requestSchema && body) { const validation = requestSchema.safeParse(body) if (!validation.success) { return { data: null, error: { message: validation.error.issues[0].message, key: 'validation_error' } } } } // 2. Call HttpClient and catch HTTP errors const response = await super.post(url, body, options).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } // 3. Check for business errors in response if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } // 4. Extract data from Pin framework format const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data // 5. Validate response if (responseSchema && actualData) { const validation = responseSchema.safeParse(actualData) if (!validation.success) { console.error('Response validation failed:', { data: actualData, errors: validation.error.issues }) return { data: null, error: { message: `Invalid response format: ${validation.error.issues[0]?.message}`, key: 'response_validation_error' } } } } return { data: actualData as T, error: null } } async getWithValidation( url: string, responseSchema?: z.ZodSchema ): Promise> { // 1. Call HttpClient and catch HTTP errors const response = await super.get(url).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } // 2. Check for business errors in response if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } // 3. Extract data from Pin framework format const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data // 4. Validate response if (responseSchema && actualData) { const validation = responseSchema.safeParse(actualData) if (!validation.success) { return { data: null, error: { message: 'Invalid response format', key: 'response_validation_error' } } } } return { data: actualData as T, error: null } } async putWithValidation( url: string, body: any, requestSchema?: z.ZodSchema, responseSchema?: z.ZodSchema ): Promise> { // 1. Validate request body if (requestSchema && body) { const validation = requestSchema.safeParse(body) if (!validation.success) { return { data: null, error: { message: validation.error.issues[0].message, key: 'validation_error' } } } } // 2. Call HttpClient and catch HTTP errors const response = await super.put(url, body).catch(() => null) if (!response) { return { data: null, error: { message: 'Request failed', key: 'request_failed' } } } // 3. Check for business errors in response if (response.data && typeof response.data === 'object' && 'error' in response.data) { const errorData = (response.data as any).error if (errorData && errorData !== null) { return { data: null, error: errorData } } } // 4. Extract data from Pin framework format const actualData = response.data && typeof response.data === 'object' && 'data' in response.data ? (response.data as any).data : response.data // 5. Validate response if (responseSchema && actualData) { const validation = responseSchema.safeParse(actualData) if (!validation.success) { return { data: null, error: { message: 'Invalid response format', key: 'response_validation_error' } } } } return { data: actualData as T, error: null } } } export function createValidatedHttpClient(config: HttpClientConfig): ValidatedApiClient { return new ValidatedApiClient(config) }