Skip to content

Instantly share code, notes, and snippets.

View vanaf1979's full-sized avatar

VA79 vanaf1979

View GitHub Profile
@vanaf1979
vanaf1979 / fetch-filtering.js
Created February 8, 2026 14:01
Fetch filtering
const getActiveUsers = async () => {
try {
const response = await fetch('https://api.since1979.dev/users');
if (!response.ok) throw new Error('Network response was not ok');
const users = await response.json();
// Now we use our Array Methods to process the data!
@vanaf1979
vanaf1979 / fetch-laravel-headers.js
Created February 8, 2026 13:59
Fetch laravel headers
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
}
@vanaf1979
vanaf1979 / fetch-post.js
Created February 8, 2026 13:58
Fetch post
const createNewUser = async (userData) => {
try {
const response = await fetch('https://api.since1979.dev/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(userData)
@vanaf1979
vanaf1979 / fetch-throw.js
Created February 8, 2026 13:57
Fetch throw
if (!response.ok) {
const errorBody = await response.json();
throw new Error(errorBody.message || 'Something went wrong on the server');
}
@vanaf1979
vanaf1979 / fetch-errors.js
Created February 8, 2026 13:55
Fetch errors
const getSecureData = async (url) => {
try {
const response = await fetch(url);
// This is the missing piece!
// If the server returns 404 or 500, we jump to the catch block manually.
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
@vanaf1979
vanaf1979 / Tetch-basic.js
Created February 8, 2026 13:54
Fetch basic
// 1. Mark the function as async
const fetchStarTrekData = async () => {
// 2. The first 'await' waits for the initial response (headers)
const response = await fetch('https://api.since1979.dev/characters');
// 3. The second 'await' waits for the full body to be parsed as JSON
const data = await response.json();
// Now the data is ready to be used
@vanaf1979
vanaf1979 / fetch-await.js
Created February 8, 2026 13:52
Fetch Await
const getUserData = async () => {
try {
const userResponse = await fetch('https://api.example.com/user/1');
const user = await userResponse.json();
const postsResponse = await fetch(`https://api.example.com/posts?userId=${user.id}`);
const posts = await postsResponse.json();
console.log(`User: ${user.name}, Posts: ${posts.length}`);
@vanaf1979
vanaf1979 / fetch-then.js
Created February 8, 2026 13:51
Fetch Tnen
/ This works, but it's hard to follow as it grows
fetch('https://api.example.com/user/1')
.then(response => response.json())
.then(user => {
console.log('User found:', user.name);
return fetch(`https://api.example.com/posts?userId=${user.id}`);
})
.then(response => response.json())
.then(posts => {
@vanaf1979
vanaf1979 / ga4-cf7-submissions.js
Created February 8, 2026 12:38
GA4 CF7 submissions
document.addEventListener('wpcf7mailsent', function(event) {
gtag('event', 'form_submission', {
'form_id': event.detail.contactFormId,
'event_category': 'Engagement'
});
}, false);
@vanaf1979
vanaf1979 / ga4-email-phone-clicks.js
Created February 8, 2026 12:36
GA4 email phone clicks
// Track Telephone Clicks
const phoneLinks = document.querySelectorAll('a[href^="tel:"]');
phoneLinks.forEach(link => {
link.addEventListener('click', () => {
gtag('event', 'contact_click', {
'type': 'phone',
'number': link.href
});
}, { once: true });