Skip to content

Instantly share code, notes, and snippets.

View sjelfull's full-sized avatar

Fred Carlsen sjelfull

View GitHub Profile
@CallumVass
CallumVass / anthropic.ts
Last active May 7, 2024 07:34
Haiku structured data - My own implementation of instructor for Haiku
import Anthropic from "@anthropic-ai/sdk";
import type { MessageParam } from "@anthropic-ai/sdk/resources";
import dotenv from "dotenv";
import { Instructor } from "./instructor";
import type { z } from "zod";
dotenv.config();
function sendToClaude<T>(message: MessageParam, schema: z.Schema<T>) {
const anthropicClient = new Anthropic({
@m1guelpf
m1guelpf / route.ts
Created May 26, 2023 04:18
Next.js App Route for getting the current playing song on Spotify (or a cached version)
import redis from '@/lib/redis'
import { tap } from '@/lib/utils'
import { NextResponse } from 'next/server'
import { NowPlaying } from '@/types/activities'
import { base64_encode, use } from '@/lib/utils'
type SpotifyResponse = {
is_playing: boolean
progress_ms: number
item: {
@snorrees
snorrees / collectionUpdate.ts
Created March 15, 2023 14:33
Sanity Connect custom handler. See requirements.md
import type {SanityClient} from '@sanity/client'
import {v5 as uuidv5} from 'uuid'
import {buildCollectionDocumentId, commitCollectionDocument} from './sanityOps'
import type {ShopifyDocumentCollection} from './storageTypes'
import {SHOPIFY_COLLECTION_DOCUMENT_TYPE, UUID_NAMESPACE_COLLECTIONS} from './constants'
import {DataSinkCollection} from './requestTypes'
import {idFromGid} from './requestHelpers'
import invariant from "tiny-invariant";
class AmalgoBox extends HTMLElement {
get input() {
return this.querySelector("input") as HTMLInputElement;
}
get button() {
return this.querySelector("button") as HTMLButtonElement;
}
@SimeonGriggs
SimeonGriggs / useListeningQuery.ts
Last active November 30, 2022 05:59
A custom hook for making and listening to document changes
import React, {useEffect, useState, useRef} from 'react'
import documentStore from 'part:@sanity/base/datastore/document'
import {catchError, distinctUntilChanged} from 'rxjs/operators'
import isEqual from 'react-fast-compare'
type Params = Record<string, string | number | boolean | string[]>
interface ListenQueryOptions {
tag?: string
apiVersion?: string
@merlinmann
merlinmann / wisdom.md
Last active April 17, 2024 21:52
Merlin's Wisdom Project (Draft)

Merlin's Wisdom Project

Or: “Everybody likes being given a glass of water.”

By Merlin Mann.

It's only advice for you because it had to be advice for me.

@SimeonGriggs
SimeonGriggs / desk-structure.js
Last active February 7, 2023 20:33
Sanity.io – Customize Desk Structure based on User Roles
import S from '@sanity/desk-tool/structure-builder'
import userStore from 'part:@sanity/base/user'
// Get the logged in user
const getCurrentUser = () => {
userStore.me.subscribe((user) => {
// Instead of a local variable, we use this window object to re-use it through the Studio
if (user) {
window._sanityUser = user ?? undefined
}
@steveruizok
steveruizok / getComponents.js
Last active July 16, 2023 22:37
Get components and styles (as full nodes) from a Figma file.
async function getComponents(fileKey, token) {
// Get file
const file = await fetch(`https://api.figma.com/v1/files/${fileKey}`, {
headers: { "X-Figma-Token": token }
}).then((r) => r.json())
if (file.err === undefined) {
// Get style ids
const styleIds = Object.keys(file.styles)
@kmelve
kmelve / create-community-newsletter.js
Created December 8, 2020 23:33
Create Mailchimp campaign document action proof of concept
const fetch = require('got');
const client = require('../client');
const HTML = require('../lib/newsletter-template');
const MAILCHIMP_URL = 'https://us3.api.mailchimp.com/3.0/campaigns';
const API_TOKEN = process.env.MAILCHIMP_TOKEN;
const QUERY = `//groq
*[
@steveruizok
steveruizok / render-state.js
Last active November 17, 2020 14:01
Render a State Designer state in the terminal.
import log from "ololog"
class Grid {
rows = []
width = 0
height = 0
chars = {
active: ["┌", "─", "┒", "┃", "┛", "━", "┕", "│"],
inactive: ["┌", "─", "┐", "│", "┘", "─", "└", "│"],