acq-sdk
Version:
SDK oficial para a API da ACQ
166 lines (159 loc) • 4.58 kB
JavaScript
import axios from 'axios';
/**
* Erro customizado para a API ACQ
*/
class AcqApiError extends Error {
constructor(error, status) {
super(error.message);
this.name = 'AcqApiError';
this.code = error.error;
this.details = error.details;
this.status = status;
}
}
/**
* Erro de validação de entrada
*/
class AcqValidationError extends Error {
constructor(message) {
super(message);
this.name = 'AcqValidationError';
}
}
/**
* Erro de configuração
*/
class AcqConfigError extends Error {
constructor(message) {
super(message);
this.name = 'AcqConfigError';
}
}
/**
* Cliente HTTP base para comunicação com a API
*/
class HttpClient {
constructor(config) {
if (!config.apiKey) {
throw new AcqConfigError('API key é obrigatória');
}
this.client = axios.create({
baseURL: config.baseUrl || 'https://api.acq.lat',
timeout: config.timeout || 30000,
headers: {
'Authorization': config.apiKey,
'Content-Type': 'application/json',
'User-Agent': 'acq-sdk/1.0.0'
}
});
// Interceptor para tratamento de erros
this.client.interceptors.response.use((response) => response, (error) => {
if (error.response?.data) {
const apiError = error.response.data;
throw new AcqApiError(apiError, error.response.status);
}
throw error;
});
}
/**
* Realiza uma requisição GET
*/
async get(url, params) {
const response = await this.client.get(url, { params });
return response.data;
}
/**
* Realiza uma requisição POST
*/
async post(url, data) {
const response = await this.client.post(url, data);
return response.data;
}
/**
* Realiza uma requisição POST que retorna um buffer (para imagens)
*/
async postBuffer(url, data) {
const response = await this.client.post(url, data, {
responseType: 'arraybuffer'
});
return Buffer.from(response.data);
}
/**
* Realiza uma requisição DELETE
*/
async delete(url, params) {
const response = await this.client.delete(url, { params });
return response.data;
}
}
/**
* Implementação do serviço de renderização
*/
class RenderServiceImpl {
constructor(httpClient) {
this.httpClient = httpClient;
}
async htmlToImage(options) {
if (!options.html || options.html.trim().length === 0) {
throw new AcqValidationError('HTML é obrigatório e não pode estar vazio');
}
return this.httpClient.postBuffer('/render', {
html: options.html
});
}
}
/**
* Implementação do serviço de emails
*/
class MailsServiceImpl {
constructor(httpClient) {
this.httpClient = httpClient;
}
async list() {
return this.httpClient.get('/mails');
}
async create(options) {
const data = options?.domain ? { domain: options.domain } : undefined;
return this.httpClient.post('/mail', data);
}
async delete(email) {
if (!email || !this.isValidEmail(email)) {
throw new AcqValidationError('Email inválido');
}
return this.httpClient.delete('/mail', { mail: email });
}
async getMessages(options) {
if (!options.mail || !this.isValidEmail(options.mail)) {
throw new AcqValidationError('Email inválido');
}
return this.httpClient.post('/mailbox', {
mail: options.mail
});
}
async deleteMessages(email) {
if (!email || !this.isValidEmail(email)) {
throw new AcqValidationError('Email inválido');
}
return this.httpClient.delete('/mailbox', { mail: email });
}
isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
}
/**
* Cliente principal do SDK ACQ
*/
class AcqClient {
/**
* Cria uma nova instância do cliente ACQ
* @param config Configuração do cliente
*/
constructor(config) {
this.httpClient = new HttpClient(config);
this.render = new RenderServiceImpl(this.httpClient);
this.mails = new MailsServiceImpl(this.httpClient);
}
}
export { AcqApiError, AcqClient, AcqConfigError, AcqValidationError };
//# sourceMappingURL=index.esm.js.map