Skip to content

Instantly share code, notes, and snippets.

View imaginamundo's full-sized avatar

Diogo Fernandes imaginamundo

View GitHub Profile
@imaginamundo
imaginamundo / request.ts
Created August 19, 2025 22:05
Fetch wrapper with timeout with tuple response
async function fetchWithTimeout(
resource: RequestInfo | URL,
options: RequestInit & { timeout?: number } = {}
): Promise<Response> {
const { timeout = 3000 } = options;
try {
const response = await fetch(resource, {
...options,
signal: AbortSignal.timeout(timeout),
@imaginamundo
imaginamundo / Form.svelte
Last active September 20, 2024 15:38
Using Astro Actions result on Svelte
<script lang="ts">
import InputError from './InputError.svelte';
import { actions, type ActionReturnType } from 'astro:actions';
const { result }: {
result: ActionReturnType<typeof actions.getGreeting>
} = $props();
let name = $state(result?.data?.fields.name || '');
let surname = $state(result?.data?.fields.surname || '');
function _init()
offsetx=240
offsety=135
x=0+offsetx
y=0+offsety
radius=50
theta=0
end
@imaginamundo
imaginamundo / html.js
Created February 20, 2024 05:15
HTML string test
// Faster
function html() {
const base = arguments[0];
if (!arguments[1]) return base[0];
let html = base[0];
for (let i = 1; i < arguments.length; i++) {
html += arguments[i];
if (base[i]) html += base[i];
}
return html;
@imaginamundo
imaginamundo / style.css
Created November 9, 2023 23:12
A grid for content
.content-grid {
--padding: 1rem;
--content-max-width: 700px;
--breakout-max-width: 900px;
justify-items: center;
display: grid;
grid-auto-flow: row;
}
.content-grid > * {
box-sizing: border-box;
@imaginamundo
imaginamundo / random.ts
Created September 2, 2021 19:03
Function to generate a random number with start and end as arguments
function random(from = 0, to = 1) {
return Math.random() * (to - from) + from;
}
export default random;
@imaginamundo
imaginamundo / .zshrc
Last active August 29, 2023 20:29
zshrc ❤️
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }
precmd_functions+=( precmd_vcs_info )
# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats ' · %b'
# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
@imaginamundo
imaginamundo / api-upload.js
Created May 24, 2021 20:48
Next.js get CSV and parse
import fs from 'fs';
import { IncomingForm } from 'formidable';
import csvParser from 'csv-parser';
export default async (req, res) => {
const data = await new Promise((resolve, reject) => {
const form = new IncomingForm()
form.parse(req, (err, fields, files) => {
if (err) return reject(err);
import React, { createContext, useContext, useState } from "react";
const Context = createContext({});
export function AuthContextProvider({ children }) {
const [ auth, setAuth ] = useState(null);
return (
<Context.Provider value={ { auth, setAuth } }>
{ children }
@imaginamundo
imaginamundo / timeoutFetch.js
Last active August 7, 2025 17:28
A wrapper for fetch with timeout using abort controller and a specific timeout error with url and status.
const DEFAULT_TIMEOUT = 3000;
function request(resource, options = {}) {
const { timeout = DEFAULT_TIMEOUT } = options;
delete options.timeout;
return fetch(resource, { ...options, signal: AbortSignal.timeout(timeout) });
}
export default request;