Skip to content

Instantly share code, notes, and snippets.

View jeffijoe's full-sized avatar
:shipit:
Navigating a sea of slop

Jeff Hansen jeffijoe

:shipit:
Navigating a sea of slop
View GitHub Profile
@jeffijoe
jeffijoe / di-test-example.js
Last active June 16, 2018 10:39
Snippet for my Medium article
describe('Todos Service', function () {
beforeEach(() {
subject = makeTodosService({
db: testDatabaseSomehow
})
})
it('works', async function () {
const todos = await subject.getTodos()
expect(todos.length).to.equal(3)
@jeffijoe
jeffijoe / todosService.js
Last active September 6, 2016 10:14
Snippet for my Medium article
import assert from 'assert'
// Using object destructuring to make it look good.
export function makeTodosService({
// "repository" is a fancy term to descibe an object
// that is used to retrieve data from a datasource - the actual
// data source does not matter. Could be a database, a REST API,
// or some IoT things like sensors or whatever.
todosRepository,
// We also want info about the user that is using the service,
@jeffijoe
jeffijoe / todosRepository.js
Last active April 30, 2021 19:43
Snippet for my Medium article
// Let's do an in-memory implementation for now.
const _todos = []
export default class TodosRepository {
// Marking all methods async makes them return promises!
async find(query) {
const filtered = _todos.filter((todo) => {
// Check the user ID
if (todo.userId !== query.userId)
return false
@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: {
@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-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 / 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 / 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 / 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 / app.js
Last active April 6, 2025 09:18
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' }
})