Skip to content

Instantly share code, notes, and snippets.

View Fredkiss3's full-sized avatar
🏠
Working from home

Adrien KISSIE Fredkiss3

🏠
Working from home
View GitHub Profile
@Fredkiss3
Fredkiss3 / bun-sse.ts
Created July 19, 2023 08:30 — forked from gtrabanco/bun-sse.ts
Bun Server Sent Events
// bun --hot sse.ts
import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
const sseEvents = new EventEmitter();
export const sse = (data) => {
sseEvents.emit(
"sse",
`id: ${randomUUID()}\ndata: ${JSON.stringify(data)}\n\n`
@Fredkiss3
Fredkiss3 / wait.ts
Created April 25, 2023 16:47
a simple utility function for waiting a certain amount of time, in TS
export function wait(ms: number): Promise<void> {
// Wait for the specified amount of time
return new Promise(resolve => setTimeout(resolve, ms));
}
@Fredkiss3
Fredkiss3 / cache.ts
Last active January 3, 2023 14:51
Redis Cache Lib
import * as Redis from 'redis';
import * as superjson from 'superjson';
export class Cache {
private static client: Redis.RedisClient;
private static readonly prefix = process.env.CACHE_PREFIX || '';
private static readonly bypassCache = process.env.CACHE_BYPASS === 'true';
static async get<T>(key: (string | number | Date)[], getValue: () => Promise<T>): Promise<T> {
if (this.bypassCache) {
@Fredkiss3
Fredkiss3 / clsx.ts
Last active January 3, 2023 14:54
A custom utility for classnames
/**
* Concatenate the specified classNames automatically with spaces.
* - If the className is undefined, it is ignored.
* - If the className is passed as a record of {class: condition }, it returns the class only if the condition is true.
*
* @example
* clsx('class1', 'class2', { class: true }) // 'class1 class2 class'
* clsx('class1', 'class2', { class: false }) // 'class1 class2'
* clsx('class1', undefined) // 'class1'
*
@Fredkiss3
Fredkiss3 / cloudfare-worker-post-to-notion.js
Created July 2, 2022 02:38
Script for cloudfare worker to post to a notion database
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Allow-Credentials': true,
};
const ALLOWED_HOSTS = [
// Put your allowed cors hosts here
// By default localhost is allowed
// Your fetch should have a `mode: cors` enabled for it to work
@Fredkiss3
Fredkiss3 / custom-range-with-iterator.ts
Created July 2, 2022 02:21
Custom Range implementation in TypeScript with iterator
class MyRange implements Iterable<number> {
public readonly [Symbol.iterator]: () => Iterator<number> = function* () { }
constructor(private readonly start: number,private readonly end: number,private readonly step: number = 1) {
if (step === 0) return
if (step < 0) step = -step
if (start > end) this[Symbol.iterator] = function* () {
for (let i = start; i >= end; i -= step) yield i
@Fredkiss3
Fredkiss3 / jsonfetch.ts
Last active August 28, 2022 04:42
JSON FETCH
export async function jsonFetch<T>(
url: string,
options: RequestInit = {}
): Promise<T> {
// Set the default headers correctly
const headers: HeadersInit = new Headers(options.headers);
headers.set('Accept', 'application/json');
headers.set('Content-Type', 'application/json');
if (process.env.NODE_ENV === 'development') {