Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
DEV-147 added basic routes and a paw file to test with
Browse files Browse the repository at this point in the history
  • Loading branch information
davesag committed Mar 1, 2018
1 parent 620e795 commit ab2a4c1
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 32 deletions.
Binary file added Crowdsale API.paw
Binary file not shown.
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,7 @@ Returns a list of API versions.
}
]

#### `GET /api/v1`

Returns a list of V1 API routes

[
{
method: 'POST',
path: '/api/v1/login',
params: {
body: {
username: 'string',
password: 'string'
}
}
},
{
method: 'POST',
path: '/api/v1/logout',
}

]

#### `POST /api/v1/login`
#### `POST /api/v1/login` (_not implemented_)

Logs a user in via simple credentials (can be enhanced later to support 2fa)

Expand All @@ -84,7 +62,7 @@ Error Response

401 Unauthorised

#### `POST /api/v1/logout`
#### `POST /api/v1/logout` (_not implemented_)

Logs a user out

Expand Down
8 changes: 8 additions & 0 deletions src/api/getPing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getPing = (req, res) => {
res.json({
response: 'Okay',
uptime: process.uptime()
})
}

module.exports = getPing
10 changes: 10 additions & 0 deletions src/api/getVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const getPing = (req, res) => {
res.json([
{
version: 1,
path: '/api/v1'
}
])
}

module.exports = getPing
5 changes: 5 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const path = require('path')

const traversal = require('../utils/traversal')

module.exports = traversal(__dirname, path.basename(module.filename))()
5 changes: 5 additions & 0 deletions src/api/v1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const path = require('path')

const traversal = require('../../utils/traversal')

module.exports = traversal(__dirname, path.basename(module.filename))()
7 changes: 7 additions & 0 deletions src/api/v1/postLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const postLogin = (req, res) => {
res.json({
test: 'test'
})
}

module.exports = postLogin
7 changes: 7 additions & 0 deletions src/api/v1/postLogout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const postLogout = (req, res) => {
res.json({
test: 'test'
})
}

module.exports = postLogout
13 changes: 13 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable camelcase */
const { getPing, getVersions, v1_postLogin, v1_postLogout } = require('./api')

module.exports = {
get: {
'/': getVersions,
'/ping': getPing
},
post: {
'/api/v1/login': v1_postLogin,
'/api/v1/logout': v1_postLogout
}
}
12 changes: 5 additions & 7 deletions src/utils/appMaker.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const express = require('express')
const cors = require('cors')

const rest = require('./rest')

// TODO: In the real app we'll want to pay more attention to site security
// but for now I'm choosing to ignore this.

const makeApp = () => {
const app = express()
app.use(cors())
app.get('/ping', (req, res) => {
res.json({
response: 'Okay'
})
})
// API routes
// app.get('/api/v1', getAPI)
// add any other middlewares here

rest(app) // apply the routes
return app
}

Expand Down
17 changes: 17 additions & 0 deletions src/utils/rest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const routes = require('../routes')

const METHODS = ['get', 'post', 'patch', 'put', 'delete']

const rest = app => {
const mapRoute = method => {
if (routes[method]) {
Object.keys(routes[method]).forEach(route => {
app[method](route, routes[method][route])
})
}
}

METHODS.forEach(mapRoute)
}

module.exports = rest
33 changes: 33 additions & 0 deletions src/utils/traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs')
const path = require('path')

const traversal = (base, basename) => {
const jsFiles = file =>
file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'

const folders = file => file.indexOf('.') === -1

const requireJs = (acc, elem) => {
const controller = require(path.join(base, elem))
acc[elem.slice(0, -3)] = controller
return acc
}

const requireFolderIndex = (acc, elem) => {
const controllers = require(path.join(base, elem, basename))
Object.keys(controllers).forEach(controller => {
acc[`${elem}_${controller}`] = controllers[controller]
})
return acc
}

const getApis = () => {
const contents = fs.readdirSync(base)
const api = contents.filter(jsFiles).reduce(requireJs, {})
return contents.filter(folders).reduce(requireFolderIndex, api)
}

return getApis
}

module.exports = traversal
45 changes: 44 additions & 1 deletion test/server/Routes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@ describe('Routes', () => {
db.close()
})

describe('Ping', () => {
describe('/', () => {
it('returns a list of versions and status code 200', done => {
request(server)
.get('/')
.end((err, res) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(res.body[0].version).to.equal(1)
expect(res.body[0].path).to.equal('/api/v1')
done()
})
})
})

describe('/ping', () => {
it('returns an Okay result and status code 200', done => {
request(server)
.get('/ping')
Expand All @@ -27,4 +41,33 @@ describe('Routes', () => {
})
})
})

describe('/api/v1/login', () => {
it('returns a test result and status code 200', done => {
request(server)
.post('/api/v1/login', {
username: '[email protected]',
password: 'Password1'
})
.end((err, res) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(res.body.test).to.equal('test')
done()
})
})
})

describe('/api/v1/logout', () => {
it('returns a test result and status code 200', done => {
request(server)
.post('/api/v1/logout')
.end((err, res) => {
expect(err).to.not.exist
expect(res.statusCode).to.equal(200)
expect(res.body.test).to.equal('test')
done()
})
})
})
})

0 comments on commit ab2a4c1

Please sign in to comment.