0.0.3 • Published 10 years ago

fantasy-express-resource v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

express-fantasy-resource

This module allows you to write your route handlers as expressions and it maps Promises, Options, and Eithers to obvious HTTP status code.

Types to HTTP Status Codes

  • Left(a) -> 400; res.body = a
  • Right(a) -> 200; res.body = a
  • None -> 404
  • Some(a) -> 200; res.body = a

Example

import ExpressResource from 'fantasy-express-resource'
import express from 'express'
import expressPromise from 'express-promise'
import bodyParser from 'body-parser'
import {None, Some} from 'fantasy-options'
import {Left, Right} from 'fantasy-eithers'

// This is our in-memory 'database'
const db = {
  _data: {
    'dummy': {
      'key': 'dummy',
      'name': 'Dummy McDumb'
    }
  },

  get: key => new Promise(resolve => {
    if(db._data[key]) resolve(Some(db._data[key]))
    else resolve(None)
  }),

  put: (key, data) => new Promise(resolve => {
    db._data[key] = data
    data['key'] = key
    resolve(data)
  })
}

// Require the .name field
const validateData = form => (
  !form.name
    ? Left({'error': '.name is required'})
    : Right(form)
)

const validateBody = form => (
  !form
    ? Left({'error': 'no request body sent'})
    : Right(form)
)

// map a record to a response
const recordToResponse = record => (
  {
    "@id": "/" + record.key,
    "name": record.name,
  }
)

// Get access
export const app = express()
app.use(expressPromise())
app.use(bodyParser.json())

app.get('/:key', ExpressResource(
  (req, res) => db.get(req.params.key).then(
     option => option.map(recordToResponse)
  )
))

const trace = msg => x => {
  console.log(msg, x)
  return x
}

const NullPromise = () => new Promise(resolve => resolve(undefined))

app.put('/:key', ExpressResource(
  (req, res) =>
    validateBody(req.body)
    .chain(validateData)
    .map(
      form => db
        .put(
          req.params.key, form
        ).then(
          _ => {
            res.status(204)
            return NullPromise
          }
        )
    )
))
0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago