Skip to content

Instantly share code, notes, and snippets.

View jeffijoe's full-sized avatar
:shipit:
Netting them dots

Jeff Hansen jeffijoe

:shipit:
Netting them dots
View GitHub Profile
@jeffijoe
jeffijoe / GenericTypeResolver.cs
Last active October 2, 2018 20:31
JsonConverter based on type
// SkyClip
// - GenericTypeResolver.cs
// --------------------------------------------------------------------
// Author: Jeff Hansen <jeff@jeffijoe.com>
// Copyright (C) Jeff Hansen 2015. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@jeffijoe
jeffijoe / ScrollManager.jsx
Last active November 1, 2023 18:51
Save and restore scroll position in React
/*
The MIT License
Copyright (c) Jeff Hansen 2018 to present.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION W
@jeffijoe
jeffijoe / docker-nuke.sh
Created October 9, 2017 10:15
Clearing space for Docker on macOS - the Nuclear Option
# This script will completely remove the Docker virtual disk (I guess?). Relaunch Docker for Mac to rebuild it.
# Why do you need this? If you're building Node apps in Docker, eventually you might get the ´no space left on device`
# error. You can use ` docker rm $(docker ps -q -f 'status=exited'); docker rmi $(docker images -q -f "dangling=true")`
# but that only works a few times. The command below is a complete reset, and will nuke your containers and images.
rm -rf ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2
@jeffijoe
jeffijoe / app.js
Last active September 1, 2023 07:34
Streaming uploads through Koa.
import Koa from 'koa'
import parse from './busboy'
import AWS from 'aws-sdk'
const app = new Koa()
const s3 = new AWS.S3({
params: { Bucket: 'myBucket' }
})
@jeffijoe
jeffijoe / auth.js
Last active December 21, 2022 07:06
Awilix example for @garbagemule
// middleware/auth.js
export default async function authenticate (ctx, next) {
// Do your auth stuff here.
const decoded = extractTokenSomehow(ctx)
const teamId = decoded.teamId
const userId = decoded.userId
// Use the User Repository and Team Repository (or a cache?) to fetch
// our entities.
// Resolve instances of these using Awilix's cradle proxy.
@jeffijoe
jeffijoe / server.js
Last active December 11, 2022 10:09
Snippet for my Medium article
import Koa from 'koa'
import KoaRouter from 'koa-router'
import { asValue } from 'awilix'
import { scopePerRequest, makeInvoker } from 'awilix-koa'
import configureContainer from './configureContainer'
const app = new Koa()
const router = new KoaRouter()
const container = configureContainer()
@jeffijoe
jeffijoe / configureContainer.js
Created September 6, 2016 09:55
Snippet for my Medium article
import { createContainer, asClass, asFunction } from 'awilix'
import makeTodosService from './todosService'
import TodosRepository from './todosRepository'
export default function configureContainer () {
const container = createContainer()
// Ordering does not matter.
container.register({
// Notice the scoped() at the end - this signals
@jeffijoe
jeffijoe / poor-mans-web.js
Created September 6, 2016 09:52
Snippet for my Medium article
const router = new KoaRouter()
router.get('/todos', async (ctx) => {
const todosService = makeTodosService({
todosRepository: new TodosRepository(),
// Our Koa request knows about the current user
currentUser: ctx.state.user
})
ctx.body = await todosService.getTodos(ctx.request.query)
@jeffijoe
jeffijoe / compositionRoot.js
Created September 6, 2016 09:51
Snippet for my Medium article
const currentUser = {
id: 123,
name: 'Jeff'
}
const todosRepository = new TodosRepository()
const todosService = makeTodosService({
todosRepository,
currentUser
@jeffijoe
jeffijoe / poor-mans-test.js
Last active September 6, 2016 10:16
Snippet for my Medium article
import makeTodosService from './todosService'
import TodosRepository from './todosRepository'
describe('Todos System', function () {
it('works', async function () {
// This is how DI is done manually
const todosService = makeTodosService({
todosRepository: new TodosRepository(),
// Let's fake it til we make it!
currentUser: {