import { OpenApiHttpError } from '@/services/openApiFetch';

/**
 * Missing content is an upstream 404, or the empty 200 body the Java API currently
 * sends for an unknown identifier. All other errors propagate.
 */
export async function getApiResource<T>(request: () => Promise<{ data: T }>): Promise<T | null> {
  try {
    return (await request()).data ?? null;
  } catch (error) {
    if (error instanceof OpenApiHttpError && error.status === 404) return null;
    throw error;
  }
}
