71 lines
No EOL
1.9 KiB
TypeScript
71 lines
No EOL
1.9 KiB
TypeScript
/**
|
|
* Serviço de endereços via BFF/compatibilidade.
|
|
*/
|
|
|
|
const BFF_BASE_URL = process.env.NEXT_PUBLIC_BFF_API_URL || '';
|
|
|
|
export interface EnderecoData {
|
|
$id?: string;
|
|
id?: string;
|
|
titulo?: string;
|
|
cep: string;
|
|
logradouro: string;
|
|
bairro: string;
|
|
numero: string;
|
|
complemento?: string;
|
|
cidade: string;
|
|
estado: string;
|
|
pais?: string;
|
|
latitude?: number;
|
|
longitude?: number;
|
|
principal?: boolean;
|
|
$createdAt?: string;
|
|
$updatedAt?: string;
|
|
}
|
|
|
|
export type Endereco = EnderecoData;
|
|
|
|
interface ServiceResponse<T = unknown> {
|
|
success: boolean;
|
|
data?: T;
|
|
documents?: T[];
|
|
total?: number;
|
|
error?: string;
|
|
message?: string;
|
|
}
|
|
|
|
const emptyList = <T,>(): ServiceResponse<T> => ({ success: true, documents: [], total: 0 });
|
|
const removed = (message = 'Service removido'): ServiceResponse => ({ success: false, error: message, message });
|
|
|
|
export const enderecoService = {
|
|
buscarPorId: async (_enderecoId: string): Promise<Endereco | null> => null,
|
|
|
|
buscarPorCep: async (cep: string): Promise<Partial<Endereco> | null> => {
|
|
try {
|
|
const cleanCep = cep.replace(/\D/g, '');
|
|
if (cleanCep.length !== 8) return null;
|
|
|
|
const response = await fetch(`https://cep.awesomeapi.com.br/json/${cleanCep}`);
|
|
if (!response.ok) return null;
|
|
const data = await response.json();
|
|
|
|
return {
|
|
cep: data.cep,
|
|
logradouro: data.address,
|
|
bairro: data.district,
|
|
cidade: data.city,
|
|
estado: data.state,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
},
|
|
|
|
listar: async (_page = 1, _limit = 10) => emptyList<EnderecoData>(),
|
|
buscar: async (_termo: string, _page = 1, _limit = 10) => emptyList<EnderecoData>(),
|
|
buscarPorUserId: async () => emptyList<EnderecoData>(),
|
|
criar: async (_data?: EnderecoData) => removed(),
|
|
atualizar: async (_id?: string, _data?: Partial<EnderecoData>) => removed(),
|
|
excluir: async (_id?: string) => removed(),
|
|
deletar: async (_id?: string) => removed(),
|
|
}; |