-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
54 lines (46 loc) · 1.25 KB
/
api.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
45
46
47
48
49
50
51
52
53
54
const sharedConfig = require('@ladjs/shared-config');
const jwt = require('koa-jwt');
const sse = require('koa-sse-stream');
const logger = require('../helpers/logger');
const routes = require('../routes');
const config = require('../config');
module.exports = (opts = {}) => {
const sseMiddleware = sse({ ...config.sse, ...opts.sse });
const jwtMiddleware = jwt({
...config.jwt,
...opts.jwt,
getToken(ctx, _) {
// pull token off of url if it is the sse endpoint
if (ctx.url.indexOf('/v1/sse') === 0) {
const splitUrl = ctx.url.split('/');
if (splitUrl.length === 4) {
return splitUrl[3];
}
}
return null;
}
});
return {
...sharedConfig('API'),
port: config.port,
...opts,
routes: routes.api,
logger,
hookBeforeRoutes(app) {
app.use((ctx, next) => {
// return early if jwt is set to false
if (!opts.jwt && typeof opts.jwt === 'boolean') {
return next();
}
return jwtMiddleware(ctx, next);
});
app.use((ctx, next) => {
// only do this on sse route
if (ctx.url.indexOf('/v1/sse') === 0) {
return sseMiddleware(ctx, next);
}
return next();
});
}
};
};