Skip to content

Instantly share code, notes, and snippets.

View tpjnorton's full-sized avatar

Tom Norton tpjnorton

View GitHub Profile
@tpjnorton
tpjnorton / decoratorsRoute.ts
Last active November 9, 2021 14:23
A simple API route using next-api-decorators
import { createHandler, Post, Delete, Get } from '@storyofams/next-api-decorators';
import { requiresAuth } from './requires-auth.ts'
class ItemHandler {
@requiresAuth()
@Post()
async create() {
doSomething();
return { name: 'John Doe' };
}
@tpjnorton
tpjnorton / cat.controller.ts
Created November 9, 2021 12:55
Simple NestJS controller taken from docs
import { Controller, Get } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats';
}
}
@tpjnorton
tpjnorton / complexRoute.ts
Created November 9, 2021 12:25
A more complicated Next API route handler
export default function handler(req, res) {
if (req.method === 'POST') {
checkAuth();
doSomething();
res.status(200).json({ name: 'John Doe' })
} else if (req.method === 'DELETE') {
checkAuth();
doSomethingElse();
res.status(200).json({ status: 'Deleted' })
} else if (req.method === 'GET') { // no auth
@tpjnorton
tpjnorton / simpleroute.ts
Created November 9, 2021 12:15
Basic API Route for Next.js (From Docs)
export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}
@tpjnorton
tpjnorton / newproject.sh
Created November 1, 2021 15:15
Set up new next project with ts
yarn create next-app --typescript
@tpjnorton
tpjnorton / rebase.sh
Created July 5, 2021 21:28
A quick bash function for updating Gitflow branches.
# usage: rbs [-m] -b base_branch_name
function rbs() {
OPTIND=1
BASE_BRANCH=develop
UPDATE_ACTION=rebase
while getopts "b:m" flag; do
case "${flag}" in
@tpjnorton
tpjnorton / tsButton.tsx
Last active November 22, 2020 16:22
TS Button
// TS Version
import React from 'react'
interface ButtonProps extends React.HTMLProps<HTMLButtonElement> {
onClicked?: () => void
}
const Button = ({onClicked, ...rest}:) => {
return <button onClick={onClicked} {...rest}/>
}
@tpjnorton
tpjnorton / jsbutton.jsx
Last active November 22, 2020 16:22
JS Button
// JS Version
import React from 'react'
import PropTypes from 'prop-types'
const Button = ({onClicked, ...rest}) => {
return <button onClick={onClicked} {...rest}/>
}
Button.propTypes = {
onClicked: PropTypes.func
const Button = ({onClick, ...rest}) => {
const [loading, setLoading] = useState(false);
const onClicked = async () => {
setLoading(true);
await onClick();
setLoading(false);
}
return <button onClick={onClicked} {...rest}/>
}
const Button = ({onClick, ...rest}) => {
const [loading, setLoading] = useState(false);
const isMounted = useMounted();
const onClicked = async (e) => {
setLoading(true);
await onClick(e);
isMounted.current && setLoading(false);
}
return <button onClick={onClicked} {...rest}/>
}