Skip to content

Instantly share code, notes, and snippets.

View tomfa's full-sized avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / check-cors.sh
Last active May 21, 2025 10:01
Debugging CORS errors with curl
curl -I -XOPTIONS \
-H "Access-Control-Request-Method: POST" \
-H "Origin: https://frontend.app.com" \
https://api.app.com/messages | grep access-control-allow-origin
@tomfa
tomfa / autocomplete-ts-type.ts
Created May 16, 2025 18:11
TS generic type, but with autocomplete suggestions
// (string & {}) defers collapsing of type, showing your
// suggested values as autocompletable.
// https://www.youtube.com/watch?v=lraHlXpuhKs&t=481s
const Brand = 'Audi' | 'BMW' | 'Volvo' | (string & {});
const drive = (brand: Brand) => {
console.log(`${brand} goes wroom wroom!`);
}
@tomfa
tomfa / ts-clean-props.ts
Created May 16, 2025 18:06
Prettify Typescript Props
// Util type to simplify how types are rendered in editor
// https://www.youtube.com/watch?v=lraHlXpuhKs&t=420s
type Prettify<T> = {
[K in keyof T]: T[K];
};
type ComplexType = { a: boolean } & Omit<{ b: number, c: number}, 'c'>;
type FlattenedType = Prettify<ComplexType>
@tomfa
tomfa / get_data.sql
Created October 23, 2024 18:45
PSQL to JSON: creating data for tests based on database data
select json_agg(row_to_json(t)) from (select * from "my_table" LIMIT 1) as t;
-- This ⬆️ will return a json of the results, which you could use for test data/mock data, you name it.
@tomfa
tomfa / understand-unknown-db.sql
Created September 29, 2024 06:18
MSSQL find all referencing FKs
SELECT
referencing_schema_name, referencing_entity_name, referencing_id,
referencing_class_desc, is_caller_dependent
FROM
sys.dm_sql_referencing_entities ('REPLACE_WITH_TABLE_NAME', 'OBJECT');
GO
@tomfa
tomfa / Component.tsx
Created August 15, 2024 12:49
React Query: async fetch from cache or api
import { useQueryClient } from '@tanstack/react-query';
/*
* Demonstration of how to fetch data from API in a promise,
* but utilizing already cached data from React Query
* or (if not present) populate the cache after fetch.
*
* Motivation: reduce unnecessary data fetching.
*/
@tomfa
tomfa / manifest.json
Last active June 16, 2024 11:25
iOS push notifications
{
"name": "Notification app",
"short_name": "Notifs",
"start_url": ".",
"display": "standalone",
"background_color": "#1f2937",
"orientation": "portrait",
"description": "Notifications made easy.",
"icons": [{
"src": "img/logo_144.png",
@tomfa
tomfa / auth.ts
Last active June 15, 2024 13:33
Next Auth Email OTP Provider
export const authOptions: NextAuthOptions = {
// [...other options]
providers: [
env.NODE_ENV === "development"
? ConsoleOtpProvider()
: EmailOtpProvider({ from: env.EMAIL_FROM, server: env.EMAIL_URL }),
],
};
@tomfa
tomfa / auth.ts
Last active June 15, 2024 10:39
NextAuth: link in console for development
import { type NextAuthOptions } from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { ConsoleEmailLink } from "./dev.auth.ts";
/**
* ConsoleEmailLink will print a login link to the console.
* Enables you to login while developing without internet,
* and makes local setup easier.
*/
@tomfa
tomfa / urls.test.ts
Last active December 15, 2024 18:51
NextJS route vs pathname matcher
describe('matchesPath', () => {
const matches = [
['/cake', '/cake'],
['/cake', '/cake/'],
['/cake', '/cake?frige=warm'],
['/cake', '/cake?frige=warm&freezer=cold'],
['/[id]', '/cake'],
['/[anything-goes]', '/cake'],
['/c/[id]/practitioner/[pid]/[anything-goes]', '/c/1/practitioner/2/3'],
['/[...rest]', '/cake'],