Skip to content

Commit

Permalink
updated fpx dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mitranim committed Jan 9, 2018
1 parent f090a91 commit 6642299
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
24 changes: 10 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const {testBy, slice, isFunction, isString, isList, isFinite, isObject,
const {testBy, drop, isFunction, isString, isList, isFinite, isObject,
validate} = require('fpx')

/**
Expand All @@ -9,8 +9,8 @@ const {testBy, slice, isFunction, isString, isList, isFinite, isObject,

exports.toKoaMiddleware = toKoaMiddleware
function toKoaMiddleware(handler) {
validate(isFunction, handler)
return async function koaMiddleware(ctx, next) {
validate(handler, isFunction)
return async function koaRingMiddleware(ctx, next) {
const request = toPlainRequest(ctx)
request.koaNext = next
const response = await handler(request)
Expand All @@ -20,24 +20,24 @@ function toKoaMiddleware(handler) {

exports.match = match
function match(pattern, handler) {
validate(isFunction, handler)
return function matchHandler(request) {
return testBy(pattern, request) ? handler(request) : undefined
validate(handler, isFunction)
return function matchingHandler(request) {
return testBy(request, pattern) ? handler(request) : undefined
}
}

exports.mount = mount
function mount(path, handler) {
const segmentsTest = isList(path) ? path : splitPath(path)
const segments = isList(path) ? path : splitPath(path)

return function mountedHandler(request) {
if (!isString(request.url)) {
throw Error(`Expected request URL to be a string, got: ${request.url}`)
}
const urlSegments = splitPath(request.url)

return testBy(segmentsTest, urlSegments)
? handler(patch(request, {url: drop(segmentsTest.length, urlSegments).join('/')}))
return testBy(urlSegments, segments)
? handler(patch(request, {url: drop(urlSegments, segments.length).join('/')}))
: undefined
}
}
Expand Down Expand Up @@ -102,10 +102,6 @@ function isAwaitingResponse(ctx) {
*/

function splitPath(path) {
validate(isString, path)
validate(path, isString)
return path.split('/').filter(Boolean)
}

function drop(count, value) {
return isList(value) ? slice(value, count) : []
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-ring",
"version": "0.2.0",
"version": "0.2.1",
"description": "Write Koa handlers as ƒ(request) -> response, like in Clojure/Ring. Supports automatic work cancelation via Posterus futures and coroutines. Comes with routing/mounting utilities out of the box.",
"license": "MIT",
"keywords": [
Expand All @@ -14,7 +14,7 @@
],
"dependencies": {
"posterus": "^0.3.0",
"fpx": "^0.3.0"
"fpx": "^0.4.0"
},
"devDependencies": {
"babel-eslint": "7.2.3",
Expand Down
2 changes: 1 addition & 1 deletion posterus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {toPlainRequest, toPlainResponse, isAwaitingResponse, updateKoaContext} =

exports.toKoaMiddleware = toKoaMiddleware
function toKoaMiddleware(handler) {
return async function koaMiddleware(ctx, next) {
return async function koaRingMiddleware(ctx, next) {
const request = toPlainRequest(ctx)
request.koaNext = next
const future = toFuture(handler(request))
Expand Down

0 comments on commit 6642299

Please sign in to comment.