Skip to content

Instantly share code, notes, and snippets.

@jeffijoe
Last active December 11, 2022 10:09
Show Gist options
  • Select an option

  • Save jeffijoe/828b810d88c74449492bbc3bb9efa9cb to your computer and use it in GitHub Desktop.

Select an option

Save jeffijoe/828b810d88c74449492bbc3bb9efa9cb to your computer and use it in GitHub Desktop.

Revisions

  1. jeffijoe revised this gist Sep 6, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -27,12 +27,12 @@ app.use((ctx, next) => {
    const todosAPI = ({ todosService }) => {
    return {
    getTodos: async (ctx) => {
    const todos = todosService.getTodos(ctx.request.query)
    const todos = await todosService.getTodos(ctx.request.query)
    ctx.body = todos
    ctx.status = 200
    },
    createTodo: async (ctx) => {
    const todo = todosService.createTodo(ctx.request.body)
    const todo = await todosService.createTodo(ctx.request.body)
    ctx.body = todo
    ctx.status = 201
    },
  2. jeffijoe revised this gist Sep 6, 2016. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -27,22 +27,29 @@ app.use((ctx, next) => {
    const todosAPI = ({ todosService }) => {
    return {
    getTodos: async (ctx) => {
    return todosService.getTodos(ctx.request.query)
    const todos = todosService.getTodos(ctx.request.query)
    ctx.body = todos
    ctx.status = 200
    },
    createTodo: async (ctx) => {
    return todosService.createTodo(ctx.request.body)
    const todo = todosService.createTodo(ctx.request.body)
    ctx.body = todo
    ctx.status = 201
    },
    updateTodo: async (ctx) => {
    return todosService.updateTodo(
    const updated = await todosService.updateTodo(
    ctx.params.id,
    ctx.request.body
    )
    ctx.body = updated
    ctx.status = 200
    },
    deleteTodo: async (ctx) => {
    return todosService.deleteTodo(
    await todosService.deleteTodo(
    ctx.params.id,
    ctx.request.body
    )
    ctx.status = 204
    }
    }
    }
  3. jeffijoe created this gist Sep 6, 2016.
    60 changes: 60 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    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()

    // This installs a scoped container into our
    // context - we will use this to register our current user!
    app.use(scopePerRequest(container))
    // Let's do that now!
    app.use((ctx, next) => {
    ctx.state.container.registerValue({
    // Imagine some auth middleware somewhere...
    // This makes currentUser available to all services!
    currentUser: ctx.state.user
    })
    return next()
    })

    // Now our route handlers will be able to resolve a todos service..
    // using DEPENDENCY INJECTION! YEEEEHAW!
    // P.S: be a good dev and use multiple files. ;)
    const todosAPI = ({ todosService }) => {
    return {
    getTodos: async (ctx) => {
    return todosService.getTodos(ctx.request.query)
    },
    createTodo: async (ctx) => {
    return todosService.createTodo(ctx.request.body)
    },
    updateTodo: async (ctx) => {
    return todosService.updateTodo(
    ctx.params.id,
    ctx.request.body
    )
    },
    deleteTodo: async (ctx) => {
    return todosService.deleteTodo(
    ctx.params.id,
    ctx.request.body
    )
    }
    }
    }

    // Awilix magic will run the above function
    // every time a request comes in, so we have
    // a set of scoped services per request.
    const api = makeInvoker(todosAPI)
    router.get('/todos', api('getTodos'))
    router.post('/todos', api('createTodo'))
    router.patch('/todos/:id', api('updateTodo'))
    router.delete('/todos/:id', api('deleteTodo'))

    app.use(router.routes())
    app.listen(1337)