Skip to content

Instantly share code, notes, and snippets.

@sebastianjnuwu
Last active April 21, 2026 14:17
Show Gist options
  • Select an option

  • Save sebastianjnuwu/41d2209ee0104471b42acb635080d13d to your computer and use it in GitHub Desktop.

Select an option

Save sebastianjnuwu/41d2209ee0104471b42acb635080d13d to your computer and use it in GitHub Desktop.
/**
* Base da API pública caBRAPI
* @constant {string}
*/
const BASE_URL = "https://api.cabrapi.com.br";
/**
* Wrapper para requisições fetch
* @param {string} url
* @param {RequestInit} [options]
* @returns {Promise<any|string>} Retorna JSON da API ou mensagem de erro
*/
const request = (url, options = {}) =>
fetch(url, options)
.then(res => {
if (!res.ok) throw new Error(String(res.status));
return res.json();
})
.catch(err => err.message);
/* =========================
📂 CATEGORIAS
========================= */
/**
* Lista categorias da loja
* @param {string} storeId - ID da loja
* @param {number} [page=1] - Página atual
* @param {number} [limit=100] - Quantidade por página (máx: 100)
* @returns {Promise<{
* status: boolean,
* code: string,
* categories: Array<{
* id: string,
* name: string,
* description?: string|null,
* image?: string|null,
* createdAt: string,
* updatedAt: string
* }>,
* pagination: {
* page: number,
* limit: number,
* total: number,
* totalPages: number
* }
* }|string>}
*/
export const getCategories = (storeId, page = 1, limit = 100) =>
request(`${BASE_URL}/stores/${storeId}/categories?page=${page}&limit=${limit}`);
/* =========================
🛒 PRODUTOS
========================= */
/**
* Lista produtos da loja
* @param {string} storeId - ID da loja
* @param {number} [page=1] - Página atual
* @param {number} [limit=20] - Quantidade por página (máx: 100)
* @returns {Promise<{
* status: boolean,
* code: string,
* products: Array<{
* id: string,
* name: string,
* description?: string|null,
* price: number,
* image?: string|null,
* delivery: "DIGITAL"|"PHYSICAL",
* stock?: number,
* sold?: number,
* categories: Array<{ id: string, name: string }>,
* createdAt: string,
* updatedAt: string
* }>,
* pagination: {
* page: number,
* limit: number,
* total: number,
* totalPages: number
* }
* }|string>}
*/
export const getProducts = (storeId, page = 1, limit = 20) =>
request(`${BASE_URL}/stores/${storeId}/products?page=${page}&limit=${limit}`);
/**
* Busca um produto específico
* @param {string} storeId - ID da loja
* @param {string} productId - ID do produto
* @returns {Promise<{
* status: boolean,
* code: string,
* product: {
* id: string,
* name: string,
* description?: string|null,
* price: number,
* image?: string|null,
* delivery: "DIGITAL"|"PHYSICAL",
* stock?: number,
* sold?: number,
* categories: Array<{ id: string, name: string }>,
* createdAt: string,
* updatedAt: string
* }
* }|string>}
*/
export const getProductById = (storeId, productId) =>
request(`${BASE_URL}/stores/${storeId}/products/${productId}`);
/* =========================
🎟️ CUPOM
========================= */
/**
* Valida/busca cupom por código
* @param {string} storeId - ID da loja
* @param {string} code - Código do cupom
* @returns {Promise<{
* status: boolean,
* code: string,
* coupon: {
* id: string,
* code: string,
* discount: number,
* useLimit?: number|null,
* expiredAt?: string|null
* }
* }|string>}
*/
export const getCoupon = (storeId, code) =>
request(`${BASE_URL}/stores/${storeId}/coupons/${code}`);
/* =========================
💳 PAGAMENTO
========================= */
/**
* Cria um pagamento (checkout)
* @param {string} storeId - ID da loja
* @param {{
* name: string,
* email: string,
* cpf?: string,
* gateway: "MERCADOPAGO",
* coupon?: string,
* metadata?: object,
* items: Array<{
* productId: string,
* quantity: number
* }>
* }} data - Dados do pagamento
* @returns {Promise<{
* status: boolean,
* code: string,
* payment: object
* }|string>}
*/
export const createPayment = (storeId, data) =>
request(`${BASE_URL}/stores/${storeId}/payments`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});

Comments are disabled for this gist.