/
Endpoint.js
105 lines (87 loc) · 3.09 KB
/
Endpoint.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { existsSync, readFileSync } from 'fs'
import { resolve } from 'path'
import OfflineEndpoint from './OfflineEndpoint.js'
import debugLog from '../../debugLog.js'
function readFile(filePath) {
return readFileSync(filePath, 'utf8')
}
// velocity template defaults
const defaultRequestTemplate = readFile(
resolve(__dirname, './templates/offline-default.req.vm'),
)
const defaultResponseTemplate = readFile(
resolve(__dirname, './templates/offline-default.res.vm'),
)
function getResponseContentType(fep) {
if (fep.response && fep.response.headers['Content-Type']) {
return fep.response.headers['Content-Type'].replace(/'/gm, '')
}
return 'application/json'
}
export default class Endpoint {
constructor(handlerPath, http) {
this._handlerPath = handlerPath
this._http = http
return this._generate()
}
// determine whether we have function level overrides for velocity templates
// if not we will use defaults
_setVmTemplates(fullEndpoint) {
// determine requestTemplate
// first check if requestTemplate is set through serverless
const fep = fullEndpoint
try {
// determine request template override
const reqFilename = `${this._handlerPath}.req.vm`
// check if serverless framework populates the object itself
if (
typeof this._http.request === 'object' &&
typeof this._http.request.template === 'object'
) {
const templatesConfig = this._http.request.template
Object.keys(templatesConfig).forEach((key) => {
fep.requestTemplates[key] = templatesConfig[key]
})
}
// load request template if exists if not use default from serverless offline
else if (existsSync(reqFilename)) {
fep.requestTemplates['application/json'] = readFile(reqFilename)
} else {
fep.requestTemplates['application/json'] = defaultRequestTemplate
}
// determine response template
const resFilename = `${this._handlerPath}.res.vm`
fep.responseContentType = getResponseContentType(fep)
debugLog('Response Content-Type ', fep.responseContentType)
// load response template from http response template, or load file if exists other use default
if (fep.response && fep.response.template) {
fep.responses.default.responseTemplates[fep.responseContentType] =
fep.response.template
} else if (existsSync(resFilename)) {
fep.responses.default.responseTemplates[
fep.responseContentType
] = readFile(resFilename)
} else {
fep.responses.default.responseTemplates[
fep.responseContentType
] = defaultResponseTemplate
}
} catch (err) {
debugLog(`Error: ${err}`)
}
return fep
}
// return fully generated Endpoint
_generate() {
const offlineEndpoint = new OfflineEndpoint()
const fullEndpoint = {
...offlineEndpoint,
...this._http,
}
if (this._http.integration === 'lambda') {
// determine request and response templates or use defaults
return this._setVmTemplates(fullEndpoint)
}
return fullEndpoint
}
}