Skip to content

Commit

Permalink
Merge 8041ebf into 3a3d481
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Aug 23, 2018
2 parents 3a3d481 + 8041ebf commit 1bea605
Show file tree
Hide file tree
Showing 40 changed files with 1,883 additions and 332 deletions.
9 changes: 4 additions & 5 deletions .gitignore
@@ -1,6 +1,5 @@
node_modules

.DS_Store
.env
docker-compose.override.yml
*.log
.DS_Store
.nyc_output
.yarn*
node_modules
5 changes: 2 additions & 3 deletions .npmignore
Expand Up @@ -5,8 +5,7 @@ test

.DS_Store
.env
.jshintrc
.jshintignore
.eslintrc.js
.travis.yml
*.log
yarn.lock
yarn*
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,5 +1,6 @@
language: node_js
node_js:
- '10'
- '8'
- '7'
- '6'
Expand All @@ -8,5 +9,4 @@ before_install:
install:
- yarn install --pure-lockfile
script:
- yarn run lint
- yarn test
- yarn run test:ci
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -5,21 +5,23 @@
Lighter-than-air node.js server framework.
</p>
<p align="center">
<a href="https://www.npmjs.com/package/paperplane"><img src="https://img.shields.io/npm/v/paperplane.svg" alt="npm version" style="max-width:100%;"></a> <a href="https://www.npmjs.com/package/paperplane"><img src="https://img.shields.io/npm/dm/paperplane.svg" alt="npm downloads" style="max-width:100%;"></a> <a href="https://travis-ci.org/articulate/paperplane"><img src="https://travis-ci.org/articulate/paperplane.svg?branch=master" alt="Build Status" style="max-width:100%;"></a> <a href="https://nodesecurity.io/orgs/articulate/projects/103be316-79be-4dd0-9d9c-73674b8ad85b"><img src="https://nodesecurity.io/orgs/articulate/projects/103be316-79be-4dd0-9d9c-73674b8ad85b/badge" alt="NSP Vuln Status"></a>
<a href="https://www.npmjs.com/package/paperplane"><img src="https://img.shields.io/npm/v/paperplane.svg" alt="npm version" style="max-width:100%;"></a> <a href="https://www.npmjs.com/package/paperplane"><img src="https://img.shields.io/npm/dm/paperplane.svg" alt="npm downloads" style="max-width:100%;"></a> <a href="https://travis-ci.org/articulate/paperplane"><img src="https://travis-ci.org/articulate/paperplane.svg?branch=master" alt="Build Status" style="max-width:100%;"></a> <a href='https://coveralls.io/github/articulate/paperplane?branch=v2'><img src='https://coveralls.io/repos/github/articulate/paperplane/badge.svg?branch=v2' alt='Coverage Status' /></a> <a href="https://nodesecurity.io/orgs/articulate/projects/103be316-79be-4dd0-9d9c-73674b8ad85b"><img src="https://nodesecurity.io/orgs/articulate/projects/103be316-79be-4dd0-9d9c-73674b8ad85b/badge" alt="NSP Vuln Status"></a>
</p>

## Documentation

- [Getting started guide](https://github.com/articulate/paperplane/blob/master/docs/getting-started.md)
- [API docs](https://github.com/articulate/paperplane/blob/master/docs/API.md)
- [Migration guide](https://github.com/articulate/paperplane/blob/master/docs/migration-guide.md)

## Introduction

The main goal of `paperplane` is to make building a `node.js` server easy, without all of the configuration or imperative boilerplate required for other server frameworks. If you prefer to build apps with function composition or even a point-free style, then `paperplane` is for you.

With `paperplane` you get all of these out-of-the-box:

- pure, functional, Promise-based [route handlers](https://github.com/articulate/paperplane/blob/master/docs/getting-started.md#basic-concepts)
- pure, functional, Promise-based [request handlers](https://github.com/articulate/paperplane/blob/master/docs/getting-started.md#basic-concepts)
- support for request handlers that return [Algebraic Data Types](https://github.com/articulate/paperplane/blob/master/docs/getting-started.md#what-are-algebraic-data-types) - **new in v2!**
- composeable json [body parsing](https://github.com/articulate/paperplane/blob/master/docs/API.md#parsejson)
- dead-simple [routing functions](https://github.com/articulate/paperplane/blob/master/docs/API.md#routes)
- several common [response helpers](https://github.com/articulate/paperplane/blob/master/docs/getting-started.md#response-object)
Expand All @@ -44,9 +46,7 @@ const app = routes({
})
})

const opts = { errLogger: logger, logger }

http.createServer(mount(app, opts)).listen(3000, logger)
http.createServer(mount({ app })).listen(3000, logger)
```

So simple and functional, with an easily readable routing table and pure functions for the route handler. If that sounds like fun to you, then read the [Getting started guide](https://github.com/articulate/paperplane/blob/master/docs/getting-started.md) or the [API docs](https://github.com/articulate/paperplane/blob/master/docs/API.md) to learn more.
53 changes: 10 additions & 43 deletions demo/index.js
@@ -1,52 +1,19 @@
require('./lib/seed')()
const { compose, prop } = require('ramda')
const http = require('http')

const {
json, logger, methods, mount, parseJson,
redirect, routes, static: renderStatic
} = require('..')

const {
createUser,
fetchUser,
fetchUsers,
updateUser
} = require('./api/users')
const { compose } = require('ramda')
const future = require('redux-future').default
const http = require('http')
const { mount, parseJson } = require('..')

const { home } = require('./api/pages')
const logger = require('./lib/logger')
const routes = require('./routes')

const port = process.env.PORT || 3000

const listening = err =>
err ? console.error(err) : console.info(`Listening on port: ${port}`)

const endpoints = routes({
'/': methods({
GET: home
}),

'/cookies': compose(json, prop('cookies')),

'/public/:path+': renderStatic({ root: 'demo/public' }),

'/users': methods({
GET: fetchUsers,
POST: createUser
}),

'/users/:id': methods({
GET: fetchUser,
PATCH: updateUser,
PUT: updateUser
}),

'/error': () => { throw new Error('this code is broken') },
const app = compose(routes, parseJson)

'/old-users': () => redirect('/users')
})
const middleware = [ future ]

const app = compose(endpoints, parseJson)
const opts = { errLogger: logger, logger }
const server = http.createServer(mount({ app, logger, middleware }))

http.createServer(mount(app, opts)).listen(port, listening)
if (require.main === module) server.listen(port, logger)
4 changes: 4 additions & 0 deletions demo/lib/logger.js
@@ -0,0 +1,4 @@
const { compose, dissocPath } = require('ramda')
const { logger } = require('../..')

module.exports = compose(logger, dissocPath(['req', 'headers']))
38 changes: 38 additions & 0 deletions demo/routes/index.js
@@ -0,0 +1,38 @@
const { always, compose, prop } = require('ramda')
const { json, methods, routes, send, serve } = require('../..')

const pages = require('./pages')
const users = require('./users')

const error = () => {
throw new Error('this code is broken')
}

module.exports = routes({
'/api/old-users': methods({
GET: users.oldUsers
}),

'/api/users': methods({
GET: users.fetchUsers,
POST: users.createUser
}),

'/api/users/:id': methods({
GET: users.fetchUser,
PATCH: users.updateUser,
PUT: users.updateUser
}),

'/cookies': compose(json, prop('cookies')),

'/error': error,

'/health': always(send()),

'/public/:path+': serve({ root: 'demo/public' }),

'/': methods({
GET: pages.home
})
})
11 changes: 7 additions & 4 deletions demo/api/pages.js → demo/routes/pages.js
@@ -1,12 +1,15 @@
require('pug/register')
const { Async } = require('crocks')
const { objOf } = require('ramda')
const { html } = require('../..')

const db = require('../lib/db')('users')
const home = require('../views/home')

const where = Async.fromPromise(db.where)

exports.home = () =>
db.where({ keys: false })
.then(objOf('users'))
.then(home)
.then(html)
where({ keys: false })
.map(objOf('users'))
.map(home)
.map(html)
5 changes: 4 additions & 1 deletion demo/api/users.js → demo/routes/users.js
@@ -1,5 +1,5 @@
const { assoc } = require('ramda')
const { json } = require('../..')
const { json, redirect } = require('../..')

const db = require('../lib/db')('users')

Expand All @@ -16,6 +16,9 @@ exports.fetchUsers = () =>
db.where({ keys: false })
.then(json)

exports.oldUsers = () =>
redirect('/api/users')

exports.updateUser = req =>
db.patch(assoc('id', req.params.id, req.body))
.then(json)
2 changes: 1 addition & 1 deletion demo/views/home.pug
Expand Up @@ -9,4 +9,4 @@ html(lang='en')
body
img(src='/public/paperplane.jpg')
p
pre= JSON.stringify(courses, null, 2)
pre= JSON.stringify(users, null, 2)

0 comments on commit 1bea605

Please sign in to comment.