This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ValidateType<T extends string> = | |
T extends 'bad' ? 'Error message' | |
: T extends 'another bad' ? 'Another error message' | |
: T extends 'Error message' | 'Another error message' ? `Can't use '${ T }'` | |
: T | |
function fn<T extends string> ( arg: ValidateType<T> ) { } | |
fn( 'this should work' ) // works | |
fn( 'bad' ) // Argument of type '"bad"' is not assignable to parameter of type '"Error message"'. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
CORS blocks this request from the browser, so you will need to run this code on the server. | |
*/ | |
async function checkIfYouTubeChannelIsLive ( channelId: string ) { | |
const liveCheckURL = `https://www.youtube.com/embed/live_stream?channel=${ channelId }` | |
const responseText = await fetch( liveCheckURL ).then( res => res.text() ) | |
const isLive = responseText.indexOf( 'This video is unavailable' ) < 0 | |
return isLive | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use client' | |
import { useEffect, useState } from 'react' | |
// https://www.youtube.com/watch?v=KAjemAivU24 | |
export function NoSSR ( { children }: { children?: React.ReactNode } ) { | |
const [ isClient, setIsClient ] = useState( false ) | |
useEffect( () => { setIsClient( true ) }, [] ) | |
return isClient ? <>{children}</> : null | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore } from 'zustand' | |
import { persist, StorageValue } from 'zustand/middleware' | |
const store = createStore( | |
persist( | |
() => ( {} ), | |
{ | |
name: 'store-state', | |
storage: { | |
async getItem ( name: string ): StorageValue<unknown> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useSyncExternalStore } from 'https://esm.sh/[email protected]/compat' | |
import { createStore, StoreApi } from 'https://raw.githubusercontent.com/pmndrs/zustand/v4.3.6/src/vanilla.ts' | |
function useStore<State> ( store: StoreApi<State> ) { | |
return useSyncExternalStore( store.subscribe, store.getState ) | |
} | |
const store = createStore( () => ( { count: 0, foo: 'bar' } ) ) | |
export default function ZustandStore () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod' | |
type ErrorCode = z.ZodIssueCode | 'required' | |
type Message = string | |
type MessageBuilder = ( data: unknown, options?: any[] ) => Message | |
type ErrorRecord = Partial<Record<ErrorCode, Message | MessageBuilder>> | |
function makeErrorMap ( errorRecord: ErrorRecord ): z.ZodErrorMap { | |
return ( issue, ctx ) => { | |
const options = issue.code === 'invalid_enum_value' | |
? issue.options : undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod' | |
function safeParseJSON ( string: string ): any { | |
try { return JSON.parse( string ) } | |
catch { return string } | |
} | |
function searchParamsToValues ( searchParams: URLSearchParams ): Record<string, any> { | |
return Array.from( searchParams.keys() ).reduce( ( record, key ) => { | |
const values = searchParams.getAll( key ).map( safeParseJSON ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ts-node-starter", | |
"main": "src/index.ts", | |
"author": "Jacob Weisenburger", | |
"type": "module", | |
"scripts": { | |
"start": "node --no-warnings --loader ts-node/esm --es-module-specifier-resolution=node src", | |
"dev": "nodemon", | |
"dev.cmd": "start cmd /k npm run dev" | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod' | |
import { mapValues, omit, pick } from 'lodash' | |
function partialSafeParse<Schema extends z.ZodObject<any>> ( schema: Schema, input: unknown ) { | |
const result = schema.safeParse( input ) | |
if ( result.success ) return result | |
const { fieldErrors, formErrors } = result.error.flatten() | |
if ( formErrors.length ) return result |
NewerOlder