sc-cobre-facil
Version:
Biblioteca TypeScript para consumir a API da Cobre Fácil
151 lines • 4.96 kB
JavaScript
export class CobreFacil {
constructor({ baseUrl = "https://api.sandbox.cobrefacil.com.br/v1", app_id, secret, }) {
this.token = null;
this.baseUrl = baseUrl;
this.app_id = app_id;
this.secret = secret;
}
async auth() {
try {
const response = await fetch(`${this.baseUrl}/authenticate`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
app_id: this.app_id,
secret: this.secret,
}),
});
const data = await response.json();
if (data.success) {
this.token = data.data.token;
return "ok";
}
else {
this.token = null;
return "error";
}
}
catch (error) {
return "error";
}
}
async getCustomer(ein) {
try {
const response = await fetch(`${this.baseUrl}/customers?ein=${encodeURIComponent(ein)}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
},
});
const data = await response.json();
if (data.success) {
return data;
}
else {
if (data.errors) {
console.error("Errors:", data.errors);
}
throw new Error(data.message || "Failed to get customer");
}
}
catch (error) {
console.error("Error getting customer:", error);
throw error;
}
}
async createCustomer(customerData) {
try {
const response = await fetch(`${this.baseUrl}/customers`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
},
body: JSON.stringify(customerData),
});
const data = await response.json();
if (data.success) {
return data;
}
else {
if (data.errors) {
console.error("Errors:", data.errors);
}
throw new Error(data.message || "Failed to create customer");
}
}
catch (error) {
console.error("Error creating customer:", error);
throw error;
}
}
async updateCustomer({ customerId, updateData, }) {
try {
const response = await fetch(`${this.baseUrl}/customers/${customerId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
},
body: JSON.stringify(updateData),
});
const data = await response.json();
if (data.success) {
return data;
}
else {
throw new Error(data.message || "Failed to update customer");
}
}
catch (error) {
console.error("Error updating customer:", error);
throw error;
}
}
async deleteCustomer(customerId) {
try {
const response = await fetch(`${this.baseUrl}/customers/${customerId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
},
});
const data = await response.json();
if (data.success) {
return data;
}
else {
throw new Error(data.message || "Failed to delete customer");
}
}
catch (error) {
console.error("Error deleting customer:", error);
throw error;
}
}
async createOrGetCustomer(customerData) {
try {
const existingCustomer = await this.getCustomer(customerData.ein);
if (existingCustomer.data.length > 0) {
return {
success: true,
data: existingCustomer.data[0],
};
}
const newCustomer = await this.createCustomer(customerData);
return {
success: newCustomer.success,
data: newCustomer.data,
message: newCustomer.message,
errors: newCustomer.errors,
};
}
catch (error) {
console.error("Error creating customer:", error);
throw error;
}
}
}
//# sourceMappingURL=index.js.map