asaasjs
Version:
An AsaasJS SDK for NodeJS
252 lines (245 loc) • 6.19 kB
JavaScript
;
// src/constants.ts
var BASE_URL = "https://api.asaas.com/v3";
var SANDBOX_BASE_URL = "https://api-sandbox.asaas.com/v3";
var ASAAS_DOCS = "https://docs.asaas.com/reference";
function DEFAULT_HEADERS(apiKey) {
return {
"content-type": "application/json",
accept: "application/json",
access_token: apiKey,
"user-agent": `NodeJS SDK`
};
}
// src/exceptions.ts
var AsaasError = class extends Error {
constructor(message) {
super(
`Asaas Error: ${message}
Please, refer to the documentation at: ${ASAAS_DOCS}`
);
this.name = "AsaasError";
}
toJSON() {
return {
name: this.name,
message: this.message
};
}
};
var ApiErrors = {
401: {
code: "unauthorized",
description: "Unauthorized request"
},
403: {
code: "forbidden",
description: "Forbidden request"
},
404: {
code: "not_found",
description: "Resource not found"
}
};
// src/requests.ts
function createRequest(apiKey, sandbox) {
const url = sandbox ? SANDBOX_BASE_URL : BASE_URL;
const defaultHeaders = DEFAULT_HEADERS(apiKey);
return async (path, options) => {
try {
const response = await fetch(`${url}${path}`, {
...options,
headers: { ...defaultHeaders, ...options?.headers }
});
try {
const data = await response.json();
if (!response.ok) {
return { errors: data.errors, data: null };
}
return { data, errors: null };
} catch (error) {
if (!response.ok && response.status in ApiErrors) {
return {
data: null,
errors: [ApiErrors[response.status]]
};
}
return {
data: null,
errors: [
{
code: "sdk_error",
description: error.message
}
]
};
}
} catch (error) {
return {
errors: {
code: "sdk_error",
description: error.message
},
data: null
};
}
};
}
// src/utils.ts
function objectToQueryString(obj) {
if (obj === null || obj === void 0) {
return "";
}
return Object.entries(obj).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => {
if (value === null || value === void 0) {
return;
}
if (typeof value === "object") {
return `${encodeURIComponent(key)}=${encodeURIComponent(
JSON.stringify(value)
)}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(
value.toString()
)}`;
}).join("&");
}
// src/index.ts
function asaasjs({
apiKey,
sandbox = false
}) {
if (!apiKey) throw new AsaasError("API key is required!");
const request = createRequest(apiKey, sandbox);
return {
/**
* Gerencie seus clientes.
*/
customers: {
/**
* Permite que você recupere uma lista dos clientes criados.
*
* @returns Lista de clientes criados ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.customers.list();
* /* ... * /
*/
list(data = {}) {
return request(`/customers?${objectToQueryString(data)}`, {
method: "GET"
});
},
/**
* Permite que você cadastre um novo cliente
*
* @returns Cliente criado ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.customers.create(data);
* /* ... * /
*/
create(data) {
return request(`/customers`, {
method: "POST",
body: JSON.stringify(data)
});
}
},
/**
* Gerencie suas cobranças.
*/
payments: {
/**
* Permite que você crie uma nova cobrança
*
* @returns Cobrança criada ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.payments.create(data);
* /* ... * /
*/
create(data) {
return request(`/payments`, {
method: "POST",
body: JSON.stringify(data)
});
},
/**
* Permite que você crie uma nova cobrança pagando com cartão de crédito
*
* @returns Cobrança criada ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.payments.create(data);
* /* ... * /
*/
createWithCreditCard(data) {
return request(`/payments`, {
method: "POST",
body: JSON.stringify(data)
});
},
/**
* Permite que você recupere uma única cobrança
*
* @returns Cobrança ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.payments.show(cobrancaId);
* /* ... * /
*/
show(id) {
return request(`/payments/${id}`, {
method: "GET"
});
},
/**
* Permite que você recupere a linha digitável de uma cobrança única
*
* @returns Linha digitável ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.payments.showIdentificationField(cobrancaId);
* /* ... * /
*/
showIdentificationField(id) {
return request(`/payments/${id}/identificationField`, {
method: "GET"
});
},
/**
* Permite que você recupere o QR Code Pix de uma cobrança única
*
* @returns QRCode ou erro
* @example
* ```ts
* const asaasjs = asaasjs('apiKey');
*
* const response = await asaasjs.payments.showPixQrCode(cobrancaId);
* /* ... * /
*/
showPixQrCode(id) {
return request(`/payments/${id}/pixQrCode`, {
method: "GET"
});
}
}
};
}
exports.AsaasError = AsaasError;
exports.asaasjs = asaasjs;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map