-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
72 lines (64 loc) · 1.77 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict"
const fs = require("fs").promises
const path = require("path")
const bodyParser = require("body-parser")
const express = require("express")
const cors = require("cors")
// @ts-ignore
const morganBody = require("morgan-body")
// @ts-ignore
const oasTools = require("oas-tools")
const jsyaml = require("js-yaml")
const pins = require("./service/pins")
/**
* @typedef {import('./service/pins').State} State
*
* @param {object} [options]
* @param {string|null} [options.token] - Access token to validate against.
* @param {State} [options.state] - initial state to start with.
* @param {boolean} [options.validator]
* @param {boolean} [options.strict]
* @param {"error"|"info"|"debug"} [options.loglevel]
* @param {string|null} [options.delegates]
*/
const setup = async ({
validator = true,
strict = false,
loglevel = "error",
delegates = null,
token = null,
state = pins.init({
accessToken: token,
delegates: delegates != null ? delegates.split(",") : undefined,
}),
} = {}) => {
const spec = await fs.readFile(
path.join(__dirname, "/api/oas-doc.yaml"),
"utf8"
)
const oasDoc = jsyaml.load(spec)
const app = express()
app.use(cors())
app.use(bodyParser.json({ strict: false }))
app.locals = { state }
oasTools.configure({
controllers: path.join(__dirname, "./controllers"),
loglevel,
strict,
router: true,
validator,
})
if (loglevel === 'info') {
// print JSON for each request and response
// ('debug' will print them via oasTools, so only info here)
// @ts-ignore
morganBody(app, {
logAllReqHeader: true,
logAllResHeader: true,
maxBodyLength: 100000
})
}
await new Promise((resolve) => oasTools.initialize(oasDoc, app, resolve))
return app
}
module.exports = { setup }