Skip to content

Commit

Permalink
Merge pull request #21 from GiovanniCardamone/dev
Browse files Browse the repository at this point in the history
Release 1.0.8
  • Loading branch information
GiovanniCardamone authored Aug 9, 2020
2 parents 23f10b7 + 639c5eb commit c0fb334
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 139 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci-coverage.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: CI Coverage
on: [push, pull_request]
on:
push:
branches:
- master
jobs:
test:
coverage:
name: ${{ matrix.node-version }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![GitHub license](https://img.shields.io/github/license/GiovanniCardamone/fastify-autoroutes.svg)](https://github.com/GiovanniCardamone/fastify-autoroutes/blob/master/LICENSE)

![CI Test](https://github.com/GiovanniCardamone/fastify-autoroutes/workflows/CI%20Test/badge.svg)
[![codecov](https://codecov.io/gh/GiovanniCardamone/fastify-autoroutes/branch/dev/graph/badge.svg)](https://codecov.io/gh/GiovanniCardamone/fastify-autoroutes)
[![codecov](https://codecov.io/gh/GiovanniCardamone/fastify-autoroutes/branch/master/graph/badge.svg)](https://codecov.io/gh/GiovanniCardamone/fastify-autoroutes)

[![NPM downloads](https://img.shields.io/npm/dm/fastify-autoroutes.svg?style=flat)](https://www.npmjs.com/package/fastify-autoroutes)
[![Known Vulnerabilities](https://snyk.io/test/github/GiovanniCardamone/fastify-autoroutes/badge.svg)](https://snyk.io/test/github/GiovanniCardamone/fastify-autoroutes)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-autoroutes",
"version": "1.0.6",
"version": "1.0.8",
"description": "Map directory structure to routes",
"main": "dist/index.js",
"scripts": {
Expand All @@ -11,6 +11,7 @@
"test": "npm run build && npm run tap",
"test:watch": "nodemon --exec 'npm run tap'",
"coverage": "tap --coverage-report=text-lcov | codecov -t $CODECOV_TOKEN",
"coverage:show": "tap --cov --coverage-report=html tests",
"deploy": "npm run test && npm publish"
},
"repository": {
Expand Down
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import process from 'process'
import path from 'path'
import fs from 'fs'

export const errorLabel = '[ERROR] fastify-autoload:'

export type ValidMethods =
| 'DELETE'
| 'GET'
Expand Down Expand Up @@ -112,7 +114,9 @@ function autoload(
const module = loadModule(fullPath, log)

if (typeof module !== 'function') {
throw new Error(`module ${fullPath} must export default function`)
throw new Error(
`${errorLabel} module ${fullPath} must export default function`
)
}

const routes = module(fastify)
Expand Down Expand Up @@ -143,10 +147,9 @@ function loadModule(path: string, log: boolean) {
return module.default
}

throw new Error('unable to find any entrypoint for module')
return
} catch (error) {
log &&
console.error(`[ERROR] fastify-autoload: unable to load module ${path}`)
log && console.error(`${errorLabel} unable to load module ${path}`)

throw error
}
Expand All @@ -157,15 +160,15 @@ export default fastifyPlugin<FastifyAutoroutesOptions>(
const log = options.log ?? true

if (!options.dir) {
const message = 'dir must be specified'
log && console.error(`[ERROR] fastify-autoload: ${message}`)
const message = `${errorLabel} dir must be specified`
log && console.error(message)

return next(new Error(message))
}

if (typeof options.dir !== 'string') {
const message = 'dir must be the path of autoroutes-directory'
log && console.error(`[ERROR] fastify-autoload: ${message}`)
const message = `${errorLabel} dir must be the path of autoroutes-directory`
log && console.error(message)

return next(new Error(message))
}
Expand All @@ -181,24 +184,23 @@ export default fastifyPlugin<FastifyAutoroutesOptions>(
}

if (!fs.existsSync(dirPath)) {
const message = `dir ${dirPath} does not exists`
log && console.error(`[ERROR] fastify-autoload: ${message}`)
const message = `${errorLabel} dir ${dirPath} does not exists`
log && console.error(message)

return next(new Error(message))
}

if (!fs.statSync(dirPath).isDirectory()) {
const message = `dir ${dirPath} must be a directory`
log && console.error(`[ERROR] fastify-autoload: ${message}`)
const message = `${errorLabel} dir ${dirPath} must be a directory`
log && console.error(message)

return next(new Error(message))
}

try {
scan(fastify, dirPath, '', options.log)
} catch (error) {
const message = error.message
log && console.error(`[ERROR] fastify-autoload: ${message}`)
log && console.error(error.message)

return next(error)
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
routes
50 changes: 50 additions & 0 deletions tests/invalid-routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const tap = require('tap')
const path = require('path')

const fastify = require('fastify')
const autoroutes = require('../dist')

const errorLabel = autoroutes.errorLabel

tap.test('invalid type routes directory', { saveFixture: true }, (t) => {
const server = fastify()

const invalidDir = t.testdir({
'dirAsFile': t.fixture('file', 'routes')
})

server.register(autoroutes, {
dir: path.join(invalidDir, 'dirAsFile'),
log: false
})

server.inject({
method: 'GET',
url: '/',
}, (err, res) => {
t.assert(err.message.startsWith(errorLabel))
t.end()
})

})

tap.test('empty routes module', { saveFixture: false }, (t) => {
const server = fastify()

const dir = t.testdir({
'index.js': '' // empty
})

server.register(autoroutes, {
dir: dir,
log: false
})

server.inject({
method: 'GET',
url: '/',
}, (err, res) => {
t.assert(err.message.startsWith(errorLabel))
t.end()
})
})
140 changes: 19 additions & 121 deletions tests/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,154 +3,52 @@ const tap = require('tap')
const fastify = require('fastify')
const autoroutes = require('../dist')

const exampleGetRoute =
`module.exports = function (server) {
return {
get: {
handler: function (request, reply) {
reply.send('get')
}
}
}
}
`
const errorLabel = autoroutes.errorLabel

const exampleGetRouteUrlParam =
`module.exports = function (server) {
return {
get: {
handler: function (request, reply) {
reply.send(request.params.PARAM)
}
}
}
}
`

tap.test('simple index', { saveFixture: false }, (t) => {
const server = fastify()

const dir = t.testdir({
'index.js': exampleGetRoute
})

server.register(autoroutes, {
dir: dir,
log: false
})

server.inject({
method: 'GET',
url: '/',
}, (err, res) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
})
})

tap.test('nested routes', { saveFixture: false }, (t) => {
const server = fastify()

const dir = t.testdir({
'users': {
'foo.js': exampleGetRoute
}
})

server.register(autoroutes, {
dir: dir,
log: false
})

server.inject({
method: 'GET',
url: '/users/foo',
}, (err, res) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
})
})

tap.test('nested routes with trailing slashes', { saveFixture: false }, (t) => {
tap.test('no dir parameters', (t) => {
const server = fastify()

const dir = t.testdir({
'users': {
'foo': {
'index.js': exampleGetRoute
}
}
})

server.register(autoroutes, {
dir: dir,
log: false
})
server.register(autoroutes, { log: false })

server.inject({
method: 'GET',
url: '/users/foo/',
}, (err, res) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
url: '/does-not-really-matter'
}, (error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
})
})

tap.test('nested routes with url parameter', { saveFixture: false }, (t) => {
tap.test('ivalid dir parameters', (t) => {
const server = fastify()

const dir = t.testdir({
'users': {
'{PARAM}.js': exampleGetRouteUrlParam
}
})

server.register(autoroutes, {
dir: dir,
dir: 33,
log: false
})

const userId = 'foo'

server.inject({
method: 'GET',
url: `/users/${userId}`,
}, (err, res) => {
t.is(err, null)
t.is(res.payload, userId)
t.end()
url: '/does-not-really-matter'
}, (error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
})

})

tap.test('nested routes with url parameter with trailing slashes', { saveFixture: false }, (t) => {
tap.test('dir does not exists', (t) => {
const server = fastify()

const dir = t.testdir({
'users': {
'{PARAM}': {
'index.js': exampleGetRouteUrlParam
}
}
})

server.register(autoroutes, {
dir: dir,
dir: './this-directory-does-not-exists',
log: false
})

const userId = 'foo'

server.inject({
method: 'GET',
url: `/users/${userId}/`,
}, (err, res) => {
t.is(err, null)
t.is(res.payload, userId)
t.end()
url: '/does-not-really-matter'
}, (error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
})

})
Loading

0 comments on commit c0fb334

Please sign in to comment.