Skip to content

Instantly share code, notes, and snippets.

View raynirola's full-sized avatar
🎯
Focusing

Ray Nirola raynirola

🎯
Focusing
View GitHub Profile
@raynirola
raynirola / relativetime.ts
Created June 27, 2022 05:32
Quick code snippet for calculating "X days ago" given a specific date, with localized language.
const rtf = new Intl.RelativeTimeFormat ("en", {
localeMatcher: "best fit",
numeric: "always",
style: "long",
});
function getDifferenceInDays(fromDate, toDate) {
const diff = Math.floor( (fromDate - toDate) / (1000 * 60 * 60 * 24));
return rtf.format (diff, "day");
}
@raynirola
raynirola / headlesslink.tsx
Last active July 4, 2022 18:58
Wrapper around Nextjs `Link` component to make it work with HeadlessUI
@raynirola
raynirola / prisma.ts
Created July 12, 2022 23:33
Instantiating Prisma with NextJS using TypeScript
import { PrismaClient } from '@prisma/client'
const globalWithPrisma = global as typeof globalThis & { prisma: PrismaClient }
let prisma: PrismaClient
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!globalWithPrisma.prisma) globalWithPrisma.prisma = new PrismaClient()
import sendgrid from '@sendgrid/mail';
sendgrid.setApiKey(process.env.SENDGRID_API_KEY);
export default async (req, res) => {
try {
await sendgrid.send({
to: '[email protected]',
from: '[email protected]',
subject: 'Serverless Functions',

How to protect yourself as a website provider?

Until Instagram resolves this issue (if ever), you can quite easily trick the Instagram and Facebook app to believe the tracking code is already installed. Just add the following to your HTML code:

<span id="iab-pcm-sdk"></span>
<span id="iab-autofill-sdk"></span>

Additionally, to prevent Instagram from tracking the user’s text selections on your website:

@raynirola
raynirola / withZod.ts
Last active December 28, 2023 11:52
Higher Order Function that wraps Next Api handler with Zod validation library, handles errors as well.
import { ZodError, ZodTypeAny, z } from 'zod'
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
interface WithZodArgs<T> {
schema: T
method?: NextApiRequest['method']
}
interface ZodRequest<T> extends NextApiRequest {
body: T
type Params<T = Record<string, string>> = T
type SearchParams<T = Record<string, string | string[] | undefined>> = T
interface PageProps<P, S> {
params: Params<P>
searchParams: SearchParams<S>
}
interface LayoutProps<P> {
export function IsolateCSS(props: { children: React.ReactNode }) {
const onceRef = useRef(false);
const [shadowRoot, setShadowRoot] = useState<ShadowRoot>();
const ref = useCallback((ref: HTMLDivElement | null) => {
if (ref && onceRef.current === false) {
onceRef.current = true;
setShadowRoot(ref.attachShadow({ mode: 'open' }));
}
}, []);
import { createCipheriv, createDecipheriv, randomBytes, createHash } from 'node:crypto';
type Preprocessor = (input: string) => string;
type Transformer = (input: string) => string;
class EncryptionError extends Error {
constructor(message: string) {
super(message);
this.name = 'EncryptionError';
}
function extractDomainFromURL(url: string, onlyDomain?: boolean): string | undefined {
const hostname = new URL(url).hostname
const parts = hostname.split('.').slice(-3)
const tld = parts.at(1)
if(tld && tld === "com") return onlyDomain ? parts.shift() : parts.join('.')
if(tld && tld.length > 2) {