Skip to content

Instantly share code, notes, and snippets.

View NuroDev's full-sized avatar
🌈

Ben NuroDev

🌈
View GitHub Profile
@NuroDev
NuroDev / mod.ts
Created April 16, 2025 00:50
💎 Zod - `X` or `X_FILE` transformer
import { z } from 'zod';
function transformFileProperty<T extends string>(key: T) {
return (data: Record<string, string>) => {
if (data[key]) {
return {
[key]: data[key],
} as {
[P in T]: string;
};
@NuroDev
NuroDev / bunfig.toml
Last active March 27, 2025 09:57
🔌 Bun HTTP import plugin
preload = ['./path/to/plugin.ts']
@NuroDev
NuroDev / latench.sh
Last active March 7, 2025 11:11
⏱️ Measure latency to a provided URL & show the min, max & average results
latency() {
# Check if URL is provided
if [[ -z "$1" ]]; then
echo "Error: URL is required"
echo "Usage: latency <url> [number_of_requests]"
return 1
fi
local url="$1"
local times=${2:-10}
@NuroDev
NuroDev / cloud-config.yml
Created December 15, 2024 00:43
☁️ A `cloud-config` script for configuring a fresh Ubuntu server to be secure
#cloud-config
# Variables:
# - `ssh_authorized_keys`: A YAML encoded list of SSH public keys to add to the user's `authorized_keys` file
# - `timezone`: The timezone to set the system to
# - `username`: The username of the user to create
disable_root: true
manage_resolv_conf: true
package_reboot_if_required: true
@NuroDev
NuroDev / deno.ts
Last active November 21, 2024 23:22
🔒 Remap environment variables ending in `_FILE` with the contents of the file
import { existsSync } from "@std/fs/exists";
interface LoadOptions {
/**
* Whether to overwrite existing env vars.
*
* @default false
*/
overwrite?: boolean;
/**
const ChuckNorrisFact = z.object({
icon_url: z.string().url(),
id: z.string().min(1),
url: z.string(),
value: z.string(),
});
const UserOptions = z
.object({
category: z.enum([
@NuroDev
NuroDev / defineLoader.ts
Created August 16, 2024 10:15
A small helper utility to create a new Astro collections loader
import { AstroError } from "astro/errors";
import { z } from "astro/zod";
import type { Loader, LoaderContext } from "astro/loaders";
interface CustomLoader<T extends z.ZodTypeAny> extends Omit<Loader, "load"> {
load: (ctx: LoaderContext, options: z.infer<T>) => ReturnType<Loader["load"]>;
options?: T;
}
@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