Skip to content

Instantly share code, notes, and snippets.

@mmikhan
mmikhan / Form.ts
Last active October 25, 2025 21:40
Zod object from TypeScript interface
// @credit https://github.com/colinhacks/zod/issues/2807#issuecomment-2962935341
import z from 'zod'
export interface Product {
title: string;
description: string;
}
type ProductObjectShape<T extends Product> = {
@mmikhan
mmikhan / upstream-main-cherry-pick.md
Created October 11, 2025 19:58
A chat history between me and Copilot AI model to find out how to merge from the specific upstream main commit with the rest to the origin or local branch

mmikhan: I fetched the upstream branch first, git fetch upstream and then merged, git merge upstream/main --allow-unrelated-histories. It had some conflicts that I resolved and merged eventually but that brought all the upstream commit history. all I wanted was the last commit instead of pulling the entire upstream commit history. how could have I done better?

GitHub Copilot: You're right! Merging with --allow-unrelated-histories pulled in the entire upstream commit history, which is often not what you want. Here are better approaches:

Option 1: Cherry-pick the specific commit

git fetch upstream
git cherry-pick <commit-hash>

This applies only the specific commit you want without bringing the entire history.

@mmikhan
mmikhan / form-builder.ts
Created October 1, 2025 09:37
Payload CMS form builder plugin configuration
export default formBuilderPlugin({
fields: {
payment: true,
},
handlePayment: async ({ data, req }) => {
console.log('Payment handler called with hook data:', {
formId: data?.form,
submissionData: data?.submissionData,
payment: data?.payment,
})
@mmikhan
mmikhan / list-users.ts
Last active May 5, 2025 11:10
Infer the output type directly from the Better Auth API function
// Infer the output type directly from the Better Auth API function
type ListUsersOutputType = Awaited<ReturnType<typeof auth.api.listUsers>>;
// Implementation
// Use z.custom for non-Zod types or refine further if needed
.output(z.custom<ListUsersOutputType>())
@mmikhan
mmikhan / page.tsx
Created April 27, 2025 11:05
Manual dehydration on a T3 app through the `useAuthQuery` hook
import { auth } from "@/server/auth";
import { createQueryClient } from "@/trpc/query-client";
import { HydrationBoundary, dehydrate } from "@tanstack/react-query"; // Import dehydrate and HydrationBoundary
import { headers } from "next/headers";
import Link from "next/link";
import { cache } from "react";
import StateView from "./state";
// Cache the QueryClient per request in RSC
const getQueryClient = cache(() => createQueryClient());
@mmikhan
mmikhan / page.tsx
Created April 27, 2025 11:01
Manual dehydration through T3 `queryClient.fetchQuery` in a RSC
import { auth } from "@/server/auth";
import { createQueryClient } from "@/trpc/query-client";
import { HydrationBoundary, dehydrate } from "@tanstack/react-query"; // Import HydrationBoundary
import { headers } from "next/headers";
import Link from "next/link";
import { cache } from "react";
// Cache the QueryClient per request in RSC
const getQueryClient = cache(() => createQueryClient());
@mmikhan
mmikhan / schema.ts
Created March 12, 2025 18:27
Zod mail configuration schema that takes a default empty value for an email field but also can be an empty string
export const mailConfigurationSchema = z.object({
apiKey: z.string().default(""),
fromEmail: z
.string()
.email()
.optional()
.or(z.literal(""))
.default(""),
toName: z.string().default(""),
toEmail: z
@mmikhan
mmikhan / paypal.ts
Last active February 22, 2025 13:53
PayPal TypeScript SDK oAuth fetch token implementation
import { type User } from "@/server/auth/types";
import { type SelectProduct } from "@/server/db/schema/products-schema";
import {
ApiError,
CheckoutPaymentIntent,
Client,
ClientCredentialsAuthManager,
Environment,
LogLevel,
OrderApplicationContextShippingPreference,
@mmikhan
mmikhan / stripe-plan-upgrade-downgrade-dialog.tsx
Created February 21, 2025 19:29
Stripe plan upgrade downgrade dialog with Hook form implementation
"use client";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
@mmikhan
mmikhan / schema.ts
Created February 17, 2025 19:22
An example of Zod schema together with a React client component to submit an array of object through the input form
export const WEBHOOK_CONFIG_MODES = [
{ value: "auto", label: "Auto" },
{ value: "manual", label: "Manual" },
] as const;
export type WebhookConfigMode = (typeof WEBHOOK_CONFIG_MODES)[number];
export const WEBHOOK_CONFIG_MODE_VALUES = WEBHOOK_CONFIG_MODES.map(
(mode) => mode.value,
) as [string, ...string[]];