Skip to content

Commit 50b8f65

Browse files
feat: Throw type error for incorrect middleware options (#38)
1 parent ddf91b2 commit 50b8f65

10 files changed

+80
-0
lines changed

src/middleware/Cli.js

+6
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ export default class Cli {
3939
* commander.
4040
* @param {!string} options.name - The name of the Cli program.
4141
* @param {!string} options.version - The version of the Cli program.
42+
* @throw {TypeError} - 'name' and 'version' are required options for the Cli
43+
* middleware!
4244
*/
4345
constructor(PopApi: any, {argv, name, version}: Object): void {
46+
if (!name || !version) {
47+
throw new TypeError('\'name\' and \'version\' are required options for the Cli middleware!')
48+
}
49+
4450
/**
4551
* The command line parser to process the Cli inputs.
4652
* @type {Command}

src/middleware/Database.js

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export default class Database {
6060
* connection.
6161
* @param {?string} [options.password] - The password for the database
6262
* connection.
63+
* @throws {TypeError} - 'database' is a required option for the Database
64+
* middleware!
6365
*/
6466
constructor(PopApi: any, {
6567
database,
@@ -68,6 +70,10 @@ export default class Database {
6870
username,
6971
password
7072
}: Object): void {
73+
if (!database) {
74+
throw new TypeError('\'database\' is a required option for the Database middleware!')
75+
}
76+
7177
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
7278

7379
const {

src/middleware/HttpServer.js

+6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ export default class HttpServer {
4141
* @param {!number} [options.serverPort=process.env.PORT] - The port the API
4242
* will run on.
4343
* @param {!number} [options.workers=2] - The amount of workers to fork.
44+
* @throws {TypeError} - 'app' is a required option for the HttpServer
45+
* middleware!
4446
*/
4547
constructor(PopApi: any, {
4648
app,
4749
serverPort = process.env.PORT,
4850
workers = 2
4951
}: Object): void {
52+
if (!app) {
53+
throw new TypeError('\'app\' is a required option for the HttpServer middleware!')
54+
}
55+
5056
/**
5157
* The amount of workers on the cluster.
5258
* @type {number}

src/middleware/Logger.js

+6
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ export default class Logger {
6161
* @param {!string} options.name - The name of the log file.
6262
* @param {?boolean} [options.pretty] - Pretty mode for output with colors.
6363
* @param {?boolean} [options.quiet] - No output.
64+
* @throws {TypeError} - 'name' and 'logDir' are required options for the
65+
* Logger middleware!
6466
*/
6567
constructor(PopApi: any, {name, logDir, pretty, quiet}: Object): void {
68+
if (!name || !logDir) {
69+
throw new TypeError('\'name\' and \'logDir\' are required options for the Logger middleware!')
70+
}
71+
6672
/**
6773
* The log levels the logger middleware will be using.
6874
* @type {Object}

src/middleware/Routes.js

+6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ export default class Routes {
3030
* @param {!Express} options.app - The application instance to add middleware
3131
* and bind the routes to.
3232
* @param {?Array<Object>} options.controllers - The controllers to register.
33+
* @throws {TypeError} - 'app' are required options for the routes
34+
* middleware!
3335
*/
3436
constructor(PopApi: any, {app, controllers}: Object): void {
37+
if (!app) {
38+
throw new TypeError('\'app\' is a required option for the Routes middleware!')
39+
}
40+
3541
this.setupRoutes(app, PopApi, controllers)
3642
}
3743

test/middleware/Cli.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ describe('Cli', () => {
5959
version
6060
})
6161
expect(cli).to.be.an('object')
62+
63+
try {
64+
new Cli({}, {}) // eslint-disable-line no-new
65+
expect(true).to.be.false
66+
} catch (err) {
67+
expect(err).to.be.an('Error')
68+
expect(err.message).to.equal(
69+
'\'name\' and \'version\' are required options for the Cli middleware!'
70+
)
71+
}
6272
})
6373

6474
/** @test {Cli#constructor} */

test/middleware/Database.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ describe('Database', () => {
7676
database: name
7777
})
7878
process.env.MONGO_PORT_27017_TCP_ADDR = stub
79+
80+
try {
81+
new Database({}, {}) // eslint-disable-line no-new
82+
expect(true).to.be.false
83+
} catch (err) {
84+
expect(err).to.be.an('Error')
85+
expect(err.message).to.equal(
86+
'\'database\' is a required option for the Database middleware!'
87+
)
88+
}
7989
})
8090

8191
/** @test {Database#constructor} */

test/middleware/HttpServer.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ describe('HttpServer', () => {
6262
app,
6363
workers: 0
6464
})
65+
66+
try {
67+
new HttpServer({}, {}) // eslint-disable-line no-new
68+
expect(true).to.be.false
69+
} catch (err) {
70+
expect(err).to.be.an('Error')
71+
expect(err.message).to.equal(
72+
'\'app\' is a required option for the HttpServer middleware!'
73+
)
74+
}
6575
})
6676

6777
/** @test {HttpServer#constructor} */

test/middleware/Logger.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ describe('Logger', () => {
6161
})
6262
expect(logger).to.be.an('object')
6363

64+
try {
65+
new Logger({}, {}) // eslint-disable-line no-new
66+
expect(true).to.be.false
67+
} catch (err) {
68+
expect(err).to.be.an('Error')
69+
expect(err.message).to.equal(
70+
'\'name\' and \'logDir\' are required options for the Logger middleware!'
71+
)
72+
}
73+
6474
processStub.restore()
6575
padStartStub.restore()
6676
})

test/middleware/Routes.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ describe('Routes', () => {
7979
/** @test {Routes#constructor} */
8080
it('should register the routes when creating a new Routes object', () => {
8181
new Routes({}, { app }) // eslint-disable-line no-new
82+
83+
try {
84+
new Routes({}, {}) // eslint-disable-line no-new
85+
expect(true).to.be.false
86+
} catch (err) {
87+
expect(err).to.be.an('Error')
88+
expect(err.message).to.equal(
89+
'\'app\' is a required option for the Routes middleware!'
90+
)
91+
}
8292
})
8393

8494
/** @test {Routes#registerControllers} */

0 commit comments

Comments
 (0)