Skip to content

Instantly share code, notes, and snippets.

@ciiqr
ciiqr / zod-type-guard-sendgrid.ts
Last active September 15, 2023 17:40
zod type guard for @sendgrid/mail's ResponseError
// NOTE: sendgrid doesn't properly export its ResponseError class, so this type
// (roughly) represents that type (with the type of body fixed)
const sendGridResponseErrorSchema = z.instanceof(Error).and(z.object({
code: z.number(),
response: z.object({
headers: z.record(z.string(), z.string()),
body: z.object({
errors: z.array(z.unknown())
})
})
@ciiqr
ciiqr / ts-function-param-unions.png
Last active April 6, 2023 16:35
typescript function parameter infer types between params
ts-function-param-unions.png
@ciiqr
ciiqr / zod-optional-null.ts
Last active May 8, 2024 15:11
zod optional/nullable/nullish differences
// zod schema
z.object({
// valid if string or:
optional: z.string().optional(), // field not provided, or explicitly `undefined`
nullable: z.string().nullable(), // field explicitly `null`
nullish: z.string().nullish(), // field not provided, explicitly `null`, or explicitly `undefined`
});
// type
{
@ciiqr
ciiqr / conflicts.ts
Last active February 12, 2023 18:59
find package conflicts in a monorepo's package-lock.json
import fs from "fs/promises";
import { minimatch } from "minimatch";
import { SemVer, parse, satisfies } from "semver";
import { z } from "zod";
type Package = (typeof packages)[number];
function isNonNullable<T>(val: T): val is NonNullable<T> {
return Boolean(val);
}
// preferred implementation
export type Result<T, E extends Error> =
| { readonly ok: false; readonly error: E }
| { readonly ok: true; readonly value: T };
export function Ok<T>(value: T) {
return { ok: true, value } as const;
}
export function Err<E extends Error>(error: E) {
@ciiqr
ciiqr / example.ts
Last active December 3, 2022 03:39
Alternate solution to @ThePrimeagen's typescript problem in AOC day 2, 2022 stream
interface FooEvent {
ack: { ack: string };
bash: { bash: string };
bar: { bar: string };
buzz: { buzz: string };
}
interface BarEvent {
ack: { ack: string };
bash: { bash: string };
@ciiqr
ciiqr / run-script-in-terminal.sh
Last active November 6, 2022 20:18
(WIP) hack to run some command in Terminal.app and get it's output (relevant in contexts where Terminal has permissions to run a script but bash doesn't, ie. inside github actions)
run_script_in_terminal() {
osascript - "$@" <<'EOF'
on FileExists(theFile) -- (String) as Boolean
try
do shell script "ls " & quoted form of theFile & " && ! lsof " & quoted form of theFile
return true
on error errMsg number errNum
return false
end try
end FileExists
@ciiqr
ciiqr / zStringToNumber.ts
Created October 19, 2022 21:35
zod numeric string to number
import { z } from "zod";
export function zStringToNumber() {
return z.preprocess((a) => {
const stringRes = z.string().safeParse(a);
if (!stringRes.success) {
return a;
}
const numberRes = Number.parseInt(stringRes.data);
@ciiqr
ciiqr / readme.md
Created October 17, 2022 02:19
nix install on m1 fails inexplicably
  • was failing on sudo chmod -R ugo-w "/nix/store/" (running this command manually it had an exit code of 1 but showed no errors...)
  • I manually edited the multi user install script to ignore failures from this line, and then of course had to fix a bunch of stuff from the first install attempt:
  • roughly following the steps I found here: https://iohk.zendesk.com/hc/en-us/articles/4415830650265-Uninstall-nix-on-MacOS
  • and then doing:
sudo rm /etc/*.backup-before-nix
sudo dscl . -rm /Groups/nixbld
for x in $(dscl . -list /Users|grep nix); do
    sudo dscl . -rm /Users/$x
done
@ciiqr
ciiqr / make-it-root.sh
Last active October 9, 2022 04:47
macos rerun script as root with (gui) user prompt
#!/usr/bin/env bash
# $ ./make-it-root.sh with params <<<"and stdin"
set -e
# if not run as root, prompt user to allow this to run as root
# NOTE: Must use `whoami`, $USER won't be set when run this way, which will cause this to trigger recursively
if [[ "$(whoami)" != 'root' ]]; then
exec osascript - "$0" "$@" 3<&0 <<APPLESCRIPT