Skip to content

Instantly share code, notes, and snippets.

View jaredpalmer's full-sized avatar

Jared Palmer jaredpalmer

View GitHub Profile
@jaredpalmer
jaredpalmer / AuthModel.ts
Last active October 6, 2022 20:50
Express OAuth2 Provider example
'use strict';
import * as uuid from 'uuid';
import { Client, ClientDao } from '../dao/ClientDao';
import { Token, TokenDao } from '../dao/TokenDao';
import { User, UserDao } from '../dao/UserDao';
export const getAccessToken = async (bearerToken: string) => {
const token = await TokenDao.loadByToken(bearerToken);
@jaredpalmer
jaredpalmer / all-html-elements.html
Created September 17, 2018 16:13
All HTML Elements
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5 Test Page</title>
</head>
<body>
<div id="top" class="page" role="document">
<header role="banner">
import puppeteer from 'puppeteer'
import fs from 'fs'
async function buildPDF(htmlString) {
const browser = await puppeteer.launch({ headless: true })
const page = await browser.newPage();
await page.setContent(htmlString, { waitUntil: 'networkidle0' })
const pdf = await page.pdf({
format: 'A4',
displayHeaderFooter: false,
@jaredpalmer
jaredpalmer / doc-table.md
Created June 13, 2022 14:18 — forked from antfu/doc-table.md
Doc Table in Markdown

Example

Name

Description


@jaredpalmer
jaredpalmer / useQuery.ts
Created May 14, 2019 20:24
Suspense-ready useQuery
import { ApolloError } from 'apollo-boost';
import { print } from 'graphql/language/printer';
import React from 'react';
const queries = new Map();
const getCacheKey = ({ query, ...opts }) =>
`${print(query)}@@${JSON.stringify(opts)}`;
const invalidateCachedQuery = options =>
@jaredpalmer
jaredpalmer / oauth-device-flow.md
Last active September 1, 2021 09:35
OAuth 2.0 Device Flow

OAuth Device Flow

This is flow used by apps on Apple TV / Roku. However, it is also useful for CLIs.

Here is my rundown. Please correct me in comments if something is wrong or if there is a better way to do this.


Device pings the server to begin activation process

@jaredpalmer
jaredpalmer / forwardRefWithAs.tsx
Created February 26, 2020 14:56
forwardRefWithAs
import * as React from 'react';
/**
* React.Ref uses the readonly type `React.RefObject` instead of
* `React.MutableRefObject`, We pretty much always assume ref objects are
* mutable (at least when we create them), so this type is a workaround so some
* of the weird mechanics of using refs with TS.
*/
export type AssignableRef<ValueType> =
| {
@jaredpalmer
jaredpalmer / prefetch.js
Created October 19, 2017 14:27 — forked from acdlite/prefetch.js
Prefetching in React
function prefetch(getKey, getValue, getInitialValue, propName) {
const inFlight = new Set();
const cache = new Map();
return ChildComponent => {
return class extends React.Component {
state = {value: getInitialValue(this.props)};
componentWillReceiveProps(nextProps) {
const key = getKey(nextProps);
if (cache.has(key)) {
// Use cached value
@jaredpalmer
jaredpalmer / Video.js
Created July 8, 2020 13:30
Better <video> with intersection observer.
import { useRef, useCallback, useEffect } from 'react'
import { useInView } from 'react-intersection-observer'
import 'intersection-observer'
export default ({ src, caption, ratio }) => {
const [inViewRef, inView] = useInView({
threshold: 1,
})
const videoRef = useRef()