forked from BrasilAPI/BrasilAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (37 loc) · 983 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import nc from 'next-connect';
import cors from 'cors';
import onError from './middlewares/errorHandler';
import cache from './middlewares/cache';
import logger from './middlewares/logger';
const corsDefaultConfiguration = {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
};
const cacheDefaultConfiguration = 86400;
const onNoMatch = (request, response) => {
return response.status(404).json({
message: 'Page not found.',
type: 'not_found',
name: 'NotFoundError',
});
};
export default (options = {}) => {
const corsOptions = options.cors || {};
const cacheOptions = options.cache || cacheDefaultConfiguration;
const configurations = {
cors: {
...corsDefaultConfiguration,
...corsOptions,
},
cache: cacheOptions,
};
return nc({
onError,
onNoMatch,
})
.use(cors(configurations.cors))
.use(logger)
.use(cache(configurations.cache));
};