Skip to content

Instantly share code, notes, and snippets.

View magicspon's full-sized avatar
🤘

Dave Stockley magicspon

🤘
View GitHub Profile
@magicspon
magicspon / helper.ts
Last active January 22, 2026 16:16
Payload DangerouslyExpandRelations
type HasObject<T> = T extends object ? (T extends null ? false : true) : false
// Remove string from union if it contains both string and object types
type RemoveStringIfHasObject<T> = string extends T
? true extends HasObject<T>
? Exclude<T, string>
: T
: T
// Recursive helper that handles arrays / readonly arrays and objects
@magicspon
magicspon / create-page.tsx
Last active February 18, 2025 19:38 — forked from Pagebakers/create-page.tsx
Next.js createPage helper with loader pattern
// https://saas-ui.dev/blog/nextjs-create-page-helper-with-loader-pattern
// source: https://gist.github.com/magicspon/c1647cf91909808be0a1eed5448bb56d
// updated by @magicspon
import type { Metadata, ResolvingMetadata } from 'next'
import { unstable_cache as cache } from 'next/cache'
import { draftMode } from 'next/headers'
import type { AnyZodObject, z } from 'zod'
const DEFAULT_REVALIDATE_TIME = 60 * 60 * 24 // 1 DAY
@magicspon
magicspon / parseRichText.ts
Last active October 31, 2021 15:51
Next/Image from html string
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import Image from 'next/image'
import Parse, {
HTMLReactParserOptions,
DOMNode,
Element,
} from 'html-react-parser'
const isElement = (domNode: DOMNode): domNode is Element => {
@magicspon
magicspon / index.tsx
Last active September 14, 2021 12:32
nextAuth/Craft/codegen usage
import * as React from 'react'
import { GetServerSideProps } from 'next'
import {
providers,
getSession,
csrfToken,
ClientSafeProvider,
} from 'next-auth/client'
import { getSdk } from '@schema/graphql'
import cmsClient from '@healthwave/utils/cms/graphqlClient'
@magicspon
magicspon / sso-login.ts
Last active April 20, 2023 09:19 — forked from jorgemasta/sso-login.ts
SSO Login to BigCommerce using a (Next) API Route
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
import jwt from 'jsonwebtoken';
import {v4 as uuidv4} from 'uuid';
import concatHeader from '../utils/concat-cookie'
import getConfig from '../utils/get-config'
function getSsoLoginUrl(customerId: number, storeHash: string, storeUrl: string, clientId: string, clientSecret: string) {
const dateCreated = Math.round((new Date()). getTime() / 1000);
const payload = {
"iss": clientId,
@magicspon
magicspon / machine.js
Last active April 26, 2020 21:24
Generated by XState Viz: https://xstate.js.org/viz
const playerMachine = {
id: "player",
initial: "waiting",
states: {
waiting: {
on: {
PLAY: "playing",
},
},
@magicspon
magicspon / machine.js
Created April 26, 2020 20:58
Generated by XState Viz: https://xstate.js.org/viz
const waiting = {
on: {
BET: "playing",
RAISE: "betting",
},
}
const playing = {
on: {
FOLD: "folded",
CHECK: "waiting",
@magicspon
magicspon / server.js
Created November 7, 2018 09:48
using https with next
const https = require('https')
const { parse } = require('url')
const next = require('next')
const fs = require('fs')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const options = {
@magicspon
magicspon / animationEnd.js
Last active March 21, 2018 00:27
animation/transition end event prop
export default (type = 'transition') => {
let types =
type === 'transition'
? {
OTransition: 'oTransitionEnd',
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
transition: 'transitionend'
}
: {
@magicspon
magicspon / webpack.config.js
Created January 19, 2018 10:44
Webpack config - common chunks outputting arrow functions in generated js
/* global */
const webpack = require('webpack')
const path = require('path')
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
const querystring = require('querystring')
const { removeEmpty } = require('webpack-config-utils')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
let BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin