import { HttpClient , HttpStatusCode } from '@/data/protocols/http' ; import { UnexpectedError , AccessDeniedError } from '@/domain/errors' ; import { LoadSurveyList } from '@/domain/usecases' ; export class RemoteLoadSurveyList implements LoadSurveyList { constructor ( private readonly url , private readonly httpClient ) { } async loadAll ( ) { const httpResponse = await this . httpClient . request ( { url : this . url , method : 'get' } ) ; const remoteSurveys = httpResponse . body || [ ] ; switch ( httpResponse . statusCode ) { case HttpStatusCode . ok : return remoteSurveys . map ( remoteSurvey => ( { ... remoteSurvey , date : new Date ( remoteSurvey . date ) } ) ) ; case HttpStatusCode . noContent : return [ ] ; case HttpStatusCode . forbidden : throw new AccessDeniedError ( ) ; default : throw new UnexpectedError ( ) ; } } } export namespace RemoteLoadSurveyList { export type Model = { id : string ; question : string ; date : string ; didAnswer : boolean ; } ; }