Skip to content

Commit 72ab262

Browse files
prettier changes and re-styling
1 parent 8a59c1a commit 72ab262

File tree

10 files changed

+378
-376
lines changed

10 files changed

+378
-376
lines changed

Diff for: index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require("dotenv").config();
2-
const app = require("./src/server");
1+
require('dotenv').config()
2+
const app = require('./src/server')
33

4-
const port = process.env.PORT || 3000;
5-
console.log(`Server listening on port ${port}`);
6-
app.listen(port);
4+
const port = process.env.PORT || 3000
5+
console.log(`Server listening on port ${port}`)
6+
app.listen(port)

Diff for: lib/db/mongoose.js

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
var mongoose = require("mongoose");
1+
var mongoose = require('mongoose')
22

33
// inspired from koa-mongoose
44
// uri: https://github.com/Jackong/koa-mongoose
55

6-
mongoose.Promise = global.Promise;
6+
mongoose.Promise = global.Promise
77

88
var middleware = (module.exports = options => {
9-
mongoose = options.mongoose ? options.mongoose : mongoose;
9+
mongoose = options.mongoose ? options.mongoose : mongoose
1010

1111
//mode: model
12-
var db = mongoose.connection;
13-
middleware.models = {};
14-
middleware.dbs = {};
15-
middleware.open(db, options);
12+
var db = mongoose.connection
13+
middleware.models = {}
14+
middleware.dbs = {}
15+
middleware.open(db, options)
1616
return async (ctx, next) => {
1717
var database =
18-
typeof options.database === "function"
18+
typeof options.database === 'function'
1919
? options.database(ctx)
2020
: options.database ||
21-
options.uri.match(/\/[^\/]+$/)[0].replace("/", "");
21+
options.uri.match(/\/[^\/]+$/)[0].replace('/', '')
2222

2323
if (!middleware.dbs.hasOwnProperty(database)) {
24-
middleware.dbs[database] = db.useDb(database);
24+
middleware.dbs[database] = db.useDb(database)
2525
}
2626

27-
await next();
28-
};
29-
});
27+
await next()
28+
}
29+
})
3030

31-
middleware.mongoose = mongoose;
31+
middleware.mongoose = mongoose
3232

3333
middleware.open = (db, options) => {
3434
if (!options && (!options.host || !options.port) && !options.uri) {
35-
throw new Error("options not found");
35+
throw new Error('options not found')
3636
}
3737

3838
var database =
39-
typeof options.database === "function" ? undefined : options.database;
39+
typeof options.database === 'function' ? undefined : options.database
4040

4141
var uri =
4242
options.uri ||
4343
`mongodb://${options.user
44-
? options.user + ":" + options.pass + "@"
45-
: ""}${options.host}:${options.port}${database
46-
? "/" + database
47-
: ""}`;
44+
? options.user + ':' + options.pass + '@'
45+
: ''}${options.host}:${options.port}${database
46+
? '/' + database
47+
: ''}`
4848

49-
process.on("SIGINT", function() {
49+
process.on('SIGINT', function() {
5050
db.close(function() {
51-
process.exit(1);
52-
});
53-
});
54-
db.on("error", err => {
55-
db.close();
56-
console.error(err);
57-
process.exit(1);
58-
});
51+
process.exit(1)
52+
})
53+
})
54+
db.on('error', err => {
55+
db.close()
56+
console.error(err)
57+
process.exit(1)
58+
})
5959

6060
if (options.events) {
6161
for (var evt in options.events) {
62-
db.on(evt, options.events[evt]);
62+
db.on(evt, options.events[evt])
6363
}
6464
}
6565

66-
db.openUri(uri, options.mongodbOptions);
66+
db.openUri(uri, options.mongodbOptions)
6767

68-
return db;
69-
};
68+
return db
69+
}

Diff for: package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"supertest": "^3.0.0"
3838
},
3939
"prettier": {
40-
"tabWidth": 4
40+
"tabWidth": 4,
41+
"semi": false,
42+
"singleQuote": true,
43+
"bracketSpacing": true
4144
}
4245
}

Diff for: src/api/server.test.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
const mongoose = require("../../lib/db/mongoose");
2-
const request = require("supertest");
3-
const app = require("../server");
1+
const mongoose = require('../../lib/db/mongoose')
2+
const request = require('supertest')
3+
const app = require('../server')
44

5-
const User = require("../model/user");
5+
const User = require('../model/user')
66

77
beforeAll(async () => {
88
// await User.remove({}).exec();
9-
});
10-
test("root route", async () => {
9+
})
10+
test('root route', async () => {
1111
const response2 = await request(app.callback())
12-
.post("/users")
12+
.post('/users')
1313
.send({
1414
id: 5,
15-
text: "anon"
16-
});
15+
text: 'anon'
16+
})
1717

18-
expect(response2.body).toMatchSnapshot();
19-
const response = await request(app.callback()).get("/users/5");
20-
expect(response.body).toMatchSnapshot();
21-
console.log(response.body);
22-
});
18+
expect(response2.body).toMatchSnapshot()
19+
const response = await request(app.callback()).get('/users/5')
20+
expect(response.body).toMatchSnapshot()
21+
console.log(response.body)
22+
})
2323

2424
afterAll(async () => {
25-
await User.remove({}).exec();
26-
mongoose.mongoose.connection.close();
27-
});
25+
await User.remove({}).exec()
26+
mongoose.mongoose.connection.close()
27+
})

Diff for: src/api/server2.test.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
const mongoose = require("../../lib/db/mongoose");
2-
const request = require("supertest");
3-
const app = require("../server");
1+
const mongoose = require('../../lib/db/mongoose')
2+
const request = require('supertest')
3+
const app = require('../server')
44

5-
const User = require("../model/user");
5+
const User = require('../model/user')
66

77
// beforeAll(async () => {
88
// console.log("Prepare objects");
99
// });
1010
afterAll(async () => {
11-
await User.remove({}).exec();
12-
mongoose.mongoose.connection.close();
13-
});
11+
await User.remove({}).exec()
12+
mongoose.mongoose.connection.close()
13+
})
1414

15-
test("root route", async () => {
15+
test('root route', async () => {
1616
const response2 = await request(app.callback())
17-
.post("/users")
17+
.post('/users')
1818
.send({
19-
text: "anon"
20-
});
21-
expect(response2.body.text).toMatchSnapshot();
22-
const response = await request(app.callback()).get("/users/5");
23-
expect(response.body).toMatchSnapshot();
24-
});
19+
text: 'anon'
20+
})
21+
expect(response2.body.text).toMatchSnapshot()
22+
const response = await request(app.callback()).get('/users/5')
23+
expect(response.body).toMatchSnapshot()
24+
})

Diff for: src/middleware/co-body.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const body = require("co-body");
1+
const body = require('co-body')
22

33
module.exports = async (ctx, next) => {
4-
// remove if you want to have undefined if body is empty. and change fallback to false for co-body
5-
ctx.request.body = {};
4+
// remove if you want to have undefined if body is empty. and change fallback to false for co-body
5+
ctx.request.body = {}
66

7-
// add this file to only methods which you want to parse json to. POST,PATCH,PUT
7+
// add this file to only methods which you want to parse json to. POST,PATCH,PUT
88

9-
// if (ctx.method === "GET" || ctx.method === "DELETE") {
10-
// return next();
11-
// }
9+
// if (ctx.method === "GET" || ctx.method === "DELETE") {
10+
// return next();
11+
// }
1212

13-
try {
14-
ctx.request.body = await body.json(ctx.req, {
15-
strict: true,
16-
fallback: true
17-
});
18-
return next();
19-
} catch (err) {
20-
return next(err);
21-
}
22-
};
13+
try {
14+
ctx.request.body = await body.json(ctx.req, {
15+
strict: true,
16+
fallback: true
17+
})
18+
return next()
19+
} catch (err) {
20+
return next(err)
21+
}
22+
}

Diff for: src/middleware/error-handler.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module.exports = async (ctx, next) => {
2-
try {
3-
await next();
4-
} catch (err) {
5-
ctx.status = err.statusCode || err.status || 500;
6-
ctx.body = {
7-
message: err.message
8-
};
9-
}
10-
};
2+
try {
3+
await next()
4+
} catch (err) {
5+
ctx.status = err.statusCode || err.status || 500
6+
ctx.body = {
7+
message: err.message
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)