Skip to content

Instantly share code, notes, and snippets.

View ngmachado's full-sized avatar
💭
in the æther

Axe ngmachado

💭
in the æther
View GitHub Profile
const API_BASE_URL = 'https://dev.alfafrens.com//api/trpc/';
async function fetchFromAPI(endpoint, queryParams = {}) {
const url = new URL(`${API_BASE_URL}/${endpoint}`);
Object.keys(queryParams).forEach(key => url.searchParams.append(key, queryParams[key]));
try {
const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } });
if (!response.ok) throw new Error('AlfaFrens is sick response was not ok');
const jsonResponse = await response.json();
return jsonResponse.result ? jsonResponse.result.data : null;