Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View NuroDev's full-sized avatar
🌈

Ben NuroDev

🌈
View GitHub Profile
@NuroDev
NuroDev / createdTypedKv.ts
Last active September 29, 2023 14:13
💙 Create typed KV ─ A wrapper for a Deno KV instance to provide a more ORM style API
type Prettify<T> =
& { [K in keyof T]: T[K] }
& {};
type KvConsistencyOptions = Parameters<Deno.Kv['get']>[1];
interface MapTypedKv<K extends string, V extends unknown> {
delete(id: K): Promise<void>;
get(id: K, options?: KvConsistencyOptions): Promise<Deno.KvEntryMaybe<V>>;
getMany<T extends readonly unknown[]>(
@NuroDev
NuroDev / defineAppRouteHandler.ts
Last active March 30, 2023 12:12
🚦 Define app route handler ─ A basic function to define a new route handler for Next.js 13's app directory
type ByteFormatStr = "b" | "gb" | "kb" | "mb" | "pb" | "tb";
type ByteSize = `${number}${ByteFormatStr}`;
interface NextApiConfig {
api?: {
bodyParser?:
| false
| {
sizeLimit: ByteSize;
};
/**
* Creates a promise that is resolved with a disctionary of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
*
* This function is derived from the standard `Promise.all` function, but it works with a dictionary
* of promises instead of an array.
*
* @param values A dictionary of Promises.
*
* @example
@NuroDev
NuroDev / defineHead.tsx
Created November 19, 2022 07:51
🧠 Define head ─ Next.js 13 utility function, based on `next-seo` to generate head elements based on provided props
import { type ReactNode } from 'react';
interface NextHeadProps {
params?: Record<string, string | number | boolean>;
}
interface HeadProps {
description?: string;
title?: string;
titleTemplate?: `%s${string}`;
@NuroDev
NuroDev / page.tsx
Created November 8, 2022 15:07
❗️ Next.js inferred generateStaticParams ─ Infer the type of static router parameters by, optionally, passing in your `generateStaticParams` function
type GenerateStaticParamsFn<TParams extends Array<unknown>> = () => TParams | Promise<TParams>;
type ArrayElement<A> = A extends ReadonlyArray<infer T> ? T : never;
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
interface NextPageProps<
TGenerateStaticParams extends GenerateStaticParamsFn<TParams> | undefined = undefined,
TParams extends Array<unknown> = Array<unknown>,
> {
@NuroDev
NuroDev / flipEntries.ts
Created September 29, 2022 20:27
🔃 Flip Entries ─ Take the elements of an object & swap the keys with values
function flipEntries <TValue = unknown> (obj: Record<string, TValue>) {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => Array.isArray(value) ? [...value.map((v) => [v, key])] : [[value, key]]).flat())
}
@NuroDev
NuroDev / ComposeProviders.component.tsx
Created August 27, 2022 10:08
🤏 Compose Providers ─ Cleaner pattern for handling multiple context providers
// https://twitter.com/bidah/status/1563197138854588417
import reduceRight from 'lodash/reduceRight';
import type { ElementType, ReactNode } from 'react';
interface ComposeProvidersProps {
components: Array<ElementType>;
children: ReactNode;
}
@NuroDev
NuroDev / bytes.ts
Last active August 19, 2022 11:15
📏 Bytes ─ TypeScript ESM fork of the `bytes` NPM package
type ByteFormatStr = 'b' | 'gb' | 'kb' | 'mb' | 'pb' | 'tb';
interface ByteOptions {
decimalPlaces?: number;
fixedDecimals?: boolean;
thousandsSeparator?: string;
unit?: ByteFormatStr;
unitSeparator?: string;
}
@NuroDev
NuroDev / chunkify.ts
Created August 8, 2022 18:37
✂️ Chunkify - Splits a provided array into chunks of arrays
/**
* Chunkify
*
* @description Splits up an array into chnks of arrays of a specified size
*
* @param {Array} array - The input array that will be chunkified
* @param {number} [chunkSize] - The size of each chunk (**Default**: 10)
*/
export function chunkify<T>(array: Array<T>, chunkSize: number = 10) {
let results = new Array<Array<T>>();
@NuroDev
NuroDev / incidents.ts
Created July 19, 2022 15:29
🚨 Planetscale Incidents ─ TypeScript Planetscale status API proxy endpoint
// api/database/incidents.ts
interface Incident {
id: string;
url: string;
name: string;
}
interface UnresolvedIncidentsResponse {
page: {