Skip to content

Commit

Permalink
Merge pull request #5 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
[chore] update router
  • Loading branch information
scott-wyatt authored Jul 19, 2018
2 parents 9a17f43 + 33de2b8 commit 1586efa
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 27 deletions.
5 changes: 3 additions & 2 deletions lib/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { spool } from './spool'
import { web } from './web'
import { session } from './session'

import { router } from './router'
export {
spool,
web,
session
session,
router
}
3 changes: 3 additions & 0 deletions lib/config/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const router = {
sortOrder: 'asc'
}
4 changes: 2 additions & 2 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ export const Server = {
})
}

Object.keys(routes).forEach(r => {
const route = routes[r]
routes.forEach((route, r) => {

RouterUtils.methods.forEach(m => {
if (route[m]) {
this.serverRoutes[m.toLowerCase() + ' ' + r] = {
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-express",
"version": "1.1.2",
"version": "1.1.3",
"description": "Spool Express - Binds the routes compiled in spool-router to a Express 4 Server.",
"homepage": "https://fabrix.app",
"author": "Fabrix-app Team <[email protected]>",
Expand Down Expand Up @@ -56,12 +56,12 @@
"methods": "^1.1.2"
},
"devDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/fabrix": "^1.1.1",
"@fabrix/lint": "^1.0.0-alpha.3",
"@fabrix/spool-i18n": "^1.1.0",
"@fabrix/spool-router": "^1.1.1",
"@fabrix/spool-router": "^1.1.2",
"@fabrix/spool-sequelize": "^1.1.0",
"@fabrix/spool-tapestries": "^1.1.0",
"@fabrix/spool-tapestries": "^1.1.2",
"@types/body-parser": "^1.17.0",
"@types/compression": "0.0.36",
"@types/cookie-parser": "^1.4.1",
Expand Down Expand Up @@ -91,8 +91,8 @@
"typescript": "~2.8.1"
},
"peerDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/spool-router": "^1.1.1",
"@fabrix/fabrix": "^1.1.1",
"@fabrix/spool-router": "^1.1.2",
"@fabrix/spool-i18n": "^1.1.0"
},
"engines": {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/api/controllers/DefaultController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ module.exports = class DefaultController extends Controller {
const where = req.jsonCriteria(req.query.where)
res.json(where)
}
worldController(req, res) {
const where = req.jsonCriteria(req.query.where)
res.json({world: where})
}
planetController(req, res) {
const where = req.jsonCriteria(req.query.where)
res.json({[req.params.planet]: where})
}
}
10 changes: 10 additions & 0 deletions test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ const App = {
},
'/jsonCriteria': {
'GET': 'DefaultController.jsonCriteria'
},
// Routes that potentially could get out of order from the router.
'/test/earth': {
'GET': 'DefaultController.worldController'
},
'/test/:planet': {
'GET': 'DefaultController.planetController'
},
'/test/world': {
'GET': 'DefaultController.worldController'
}
},
web: {
Expand Down
35 changes: 35 additions & 0 deletions test/integration/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ describe('express controllers', () => {
done(err)
})
})

it('should test order', (done) => {
request
.get('/test/world')
.query({ where: {hello: 'world'}})
.expect(200)
.end((err, res) => {
assert.ok(res.body)
assert.deepEqual(res.body, { world: { hello: 'world' } })
done(err)
})
})
// TODO fix spool-router and come back to this
it.skip('should test order', (done) => {
request
.get('/test/earth')
.query({ where: {hello: 'world'}})
.expect(200)
.end((err, res) => {
assert.ok(res.body)
assert.deepEqual(res.body, { world: { hello: 'world' } })
done(err)
})
})
it('should test order', (done) => {
request
.get('/test/mars')
.query({ where: {hello: 'mars'}})
.expect(200)
.end((err, res) => {
assert.ok(res.body)
assert.deepEqual(res.body, {mars: { hello: 'mars' } })
done(err)
})
})
})
describe('ViewController', () => {
describe('helloWorld', () => {
Expand Down
135 changes: 128 additions & 7 deletions test/unit/utils/routeSortOrder.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict'
/* global describe, it */
const assert = require('assert')
const routeOrder = require('@fabrix/spool-router').Utils.createSpecificityComparator
const sort = require('@fabrix/spool-router').Utils.sortRoutes

describe('Utils Route Sort Order', () => {
it('should exist', () => {
assert(routeOrder)
assert(sort)
})
it('should sort the routes for express', () => {
it('should sort the routes for free variables (express style)', () => {
let routes = {
'/a': {},
'/a/:id': {},
Expand All @@ -23,9 +25,11 @@ describe('Utils Route Sort Order', () => {
'/b/*': {}
}

routes = sort(routes, {order: 'asc'})

assert.deepEqual(routes, {
routes = sort(routes, 'asc')
assert(routes)
console.log(routes)
const ordered = new Map()
const order = {
'/a': {},
'/a/:id': {},
'/a/*': {},
Expand All @@ -37,11 +41,15 @@ describe('Utils Route Sort Order', () => {
'/b/:id/:world': {},
'/b/:id/*': {},
'/': {},
'*': {}
'*': {},
}
Object.keys(order).forEach(k => {
ordered.set(k, order[k])
})
assert.deepEqual(routes, ordered)
})

it('should sort the routes for free variables', () => {
it('should sort the routes for free variables (hapi style)', () => {
let routes = {
'/a': {},
'/a/{id}': {},
Expand All @@ -58,8 +66,49 @@ describe('Utils Route Sort Order', () => {
}

routes = sort(routes, 'asc')
assert(routes)

const ordered = new Map()
const order = {
'/a': {},
'/a/{id}': {},
'/a/*': {},
'/a/{id}/{world}': {},
'/a/{id}/*': {},
'/b': {},
'/b/{id}': {},
'/b/*': {},
'/b/{id}/{world}': {},
'/b/{id}/*': {},
'/': {},
'*': {},
}
Object.keys(order).forEach(k => {
ordered.set(k, order[k])
})
assert.deepEqual(routes, ordered)
})

it('should sort the routes for free variables desc', () => {
let routes = {
'/a': {},
'/a/{id}': {},
'/a/*': {},
'/b': {},
'/a/{id}/{world}': {},
'/a/{id}/*': {},
'*': {},
'/b/{id}/{world}': {},
'/': {},
'/b/{id}/*': {},
'/b/{id}': {},
'/b/*': {},
}

assert.deepEqual(routes, {
routes = sort(routes, 'desc')
assert(routes)
const ordered = new Map()
const order = {
'/a': {},
'/a/{id}': {},
'/a/*': {},
Expand All @@ -72,6 +121,78 @@ describe('Utils Route Sort Order', () => {
'/b/{id}/*': {},
'/': {},
'*': {},
}
Object.keys(order).forEach(k => {
ordered.set(k, order[k])
})
assert.deepEqual(routes, ordered)
})

it('should sort the routes for free variables asc', () => {
let routes = {
'/a': {},
'/a/{id}': {},
'/a/*': {},
'/b': {},
'/a/{id}/{world}': {},
'/a/{id}/*': {},
'*': {},
'/b/{id}/{world}': {},
'/': {},
'/b/{id}/*': {},
'/b/{id}': {},
'/b/*': {},
}

const order = Object.keys(routes).sort(routeOrder({order: 'asc'}))
assert.deepEqual(order, [
'/a',
'/a/{id}',
'/a/*',
'/a/{id}/{world}',
'/a/{id}/*',
'/b',
'/b/{id}',
'/b/*',
'/b/{id}/{world}',
'/b/{id}/*',
'/',
'*',
])
})

// TODO
it.skip('should sort the routes for free variables desc', () => {
let routes = {
'/a': {},
'/a/{id}': {},
'/a/*': {},
'/b': {},
'/a/{id}/{world}': {},
'/a/{id}/*': {},
'*': {},
'/b/{id}/{world}': {},
'/': {},
'/b/{id}/*': {},
'/b/{id}': {},
'/b/*': {},
}

const order = Object.keys(routes).sort(routeOrder({order: 'desc'}))
console.log('BROKE', order)
assert.deepEqual(order, [
'*',
'/',
'/b/{id}/*',
'/b/{id}/{world}',
'/b/*',
'/b/{id}',
'/b',
'/a/{id}/*',
'/a/{id}/{world}',
'/a/*',
'/a/{id}',
'/a'
])
})
})

0 comments on commit 1586efa

Please sign in to comment.