Skip to content

Commit

Permalink
Merge pull request #22 from GiovanniCardamone/dev
Browse files Browse the repository at this point in the history
release 1.0.9
  • Loading branch information
GiovanniCardamone authored Aug 10, 2020
2 parents c0fb334 + 5a0c4cd commit a36949c
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 117 deletions.
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
README.md

tests/*.test.*
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# fastify-autoroutes

<div align="center">

![Logo](./logo.png)

![JavaScript](https://img.shields.io/badge/ES6-Supported-yellow.svg?style=for-the-badge&logo=JavaScript) &nbsp; ![TypeScript](https://img.shields.io/badge/TypeScript-Supported-blue.svg?style=for-the-badge)

[![GitHub forks](https://img.shields.io/github/forks/GiovanniCardamone/fastify-autoroutes.svg)](https://github.com/GiovanniCardamone/fastify-autoroutes/network)
Expand All @@ -11,9 +15,12 @@
![CI Test](https://github.com/GiovanniCardamone/fastify-autoroutes/workflows/CI%20Test/badge.svg)
[![codecov](https://codecov.io/gh/GiovanniCardamone/fastify-autoroutes/branch/master/graph/badge.svg)](https://codecov.io/gh/GiovanniCardamone/fastify-autoroutes)

[![NPM version](https://img.shields.io/npm/v/fastify-autoroutes.svg?style=flat)](https://www.npmjs.com/package/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)

</div>

> :star: Thanks everyone who want to star the project, it means a lot!
Automatic add routes based on file system hierarchy.
Expand Down
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-autoroutes",
"version": "1.0.8",
"version": "1.0.9",
"description": "Map directory structure to routes",
"main": "dist/index.js",
"scripts": {
Expand Down
26 changes: 10 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function isAcceptableFile(file: string, stat: fs.Stats): boolean {
!file.startsWith('_') &&
!file.endsWith('.test.js') &&
!file.endsWith('.test.ts') &&
(stat.isFile() || stat.isSymbolicLink())
stat.isFile()
)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ function autoload(

if (typeof module !== 'function') {
throw new Error(
`${errorLabel} module ${fullPath} must export default function`
`${errorLabel} module ${fullPath} must be valid js/ts module and should export route methods definitions`
)
}

Expand All @@ -136,23 +136,17 @@ function autoload(
}

function loadModule(path: string, log: boolean) {
try {
const module = require(path)
const module = require(path)

if (typeof module === 'function') {
return module
}

if (typeof module === 'object' && 'default' in module) {
return module.default
}

return
} catch (error) {
log && console.error(`${errorLabel} unable to load module ${path}`)
if (typeof module === 'function') {
return module
}

throw error
if (typeof module === 'object' && 'default' in module) {
return module.default
}

return
}

export default fastifyPlugin<FastifyAutoroutesOptions>(
Expand Down

This file was deleted.

87 changes: 72 additions & 15 deletions tests/invalid-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,104 @@ const path = require('path')
const fastify = require('fastify')
const autoroutes = require('../dist')

const exampleErrorModule = `
thisSyntaxIsInvalid :(
`

const exampleInvalidModule = `
var whateverIdontCare = 3
`

const errorLabel = autoroutes.errorLabel

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

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

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

server.inject({
method: 'GET',
url: '/',
}, (err, res) => {
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
'index.js': '', // empty
})

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

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

tap.test('modules with error', { saveFixture: false }, (t) => {
const server = fastify()

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

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

server.inject(
{
method: 'GET',
url: '/',
},
(err) => {
t.type(err, SyntaxError)
t.end()
}
)
})

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

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

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

server.inject(
{
method: 'GET',
url: '/',
},
(err, res) => {
t.assert(err.message.startsWith(errorLabel))
t.end()
}
)
})
57 changes: 32 additions & 25 deletions tests/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,54 @@ const errorLabel = autoroutes.errorLabel
tap.test('no dir parameters', (t) => {
const server = fastify()

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

server.inject({
method: 'GET',
url: '/does-not-really-matter'
}, (error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
})
server.register(autoroutes)

server.inject(
{
method: 'GET',
url: '/does-not-really-matter',
},
(error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
}
)
})

tap.test('ivalid dir parameters', (t) => {
const server = fastify()

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

server.inject({
method: 'GET',
url: '/does-not-really-matter'
}, (error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
})
server.inject(
{
method: 'GET',
url: '/does-not-really-matter',
},
(error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
}
)
})

tap.test('dir does not exists', (t) => {
const server = fastify()

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

server.inject({
method: 'GET',
url: '/does-not-really-matter'
}, (error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
})
server.inject(
{
method: 'GET',
url: '/does-not-really-matter',
},
(error) => {
t.assert(error.message.startsWith(errorLabel))
t.end()
}
)
})
Loading

0 comments on commit a36949c

Please sign in to comment.