@kravc/dos 1.8.2 → 1.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/Document.js +1 -1
- package/src/Document.spec.js +1 -1
- package/src/Service.spec.js +54 -1
- package/src/helpers/logRequest.js +2 -2
- package/src/helpers/specMiddleware.js +11 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kravc/dos",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.4",
|
|
4
4
|
"description": "Convention-based, easy-to-use library for building API-driven serverless services.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Service",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"src": "src"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"test": "eslint src/ examples/ && NODE_APP_INSTANCE=test NODE_PATH=./ nyc mocha \"./src/**/*.spec.js\""
|
|
23
|
+
"test": "eslint src/ examples/ && NODE_APP_INSTANCE=test NODE_PATH=./ nyc --skip-full mocha \"./src/**/*.spec.js\""
|
|
24
24
|
},
|
|
25
25
|
"author": "Alexander Kravets <a@kra.vc>",
|
|
26
26
|
"license": "ISC",
|
package/src/Document.js
CHANGED
|
@@ -265,7 +265,7 @@ class Document extends Component {
|
|
|
265
265
|
|
|
266
266
|
get originalDocument() {
|
|
267
267
|
if (!this._originalDocument) {
|
|
268
|
-
throw new Error('
|
|
268
|
+
throw new Error('Original document is undefined')
|
|
269
269
|
}
|
|
270
270
|
|
|
271
271
|
return this._originalDocument
|
package/src/Document.spec.js
CHANGED
package/src/Service.spec.js
CHANGED
|
@@ -259,7 +259,9 @@ describe('Service', () => {
|
|
|
259
259
|
expect(response.statusCode).to.eql(200)
|
|
260
260
|
})
|
|
261
261
|
|
|
262
|
-
it('supports spec middleware', async () => {
|
|
262
|
+
it('supports spec middleware in dev environment', async () => {
|
|
263
|
+
process.env.NODE_APP_INSTANCE = 'dev'
|
|
264
|
+
|
|
263
265
|
const lambdaFunction = request => service.handler(request)
|
|
264
266
|
|
|
265
267
|
let request
|
|
@@ -272,6 +274,7 @@ describe('Service', () => {
|
|
|
272
274
|
|
|
273
275
|
response = await lambdaFunction(request)
|
|
274
276
|
expect(response.statusCode).to.eql(200)
|
|
277
|
+
expect(response.body.startsWith('<!DOCTYPE')).to.be.true
|
|
275
278
|
|
|
276
279
|
request = {
|
|
277
280
|
path: '/Spec',
|
|
@@ -280,6 +283,56 @@ describe('Service', () => {
|
|
|
280
283
|
|
|
281
284
|
response = await lambdaFunction(request)
|
|
282
285
|
expect(response.statusCode).to.eql(200)
|
|
286
|
+
|
|
287
|
+
const body = JSON.parse(response.body)
|
|
288
|
+
expect(body.swagger).to.exist
|
|
289
|
+
|
|
290
|
+
process.env.NODE_APP_INSTANCE = undefined
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it('does not expose spec middleware in non-dev environment', async () => {
|
|
294
|
+
const lambdaFunction = request => service.handler(request)
|
|
295
|
+
|
|
296
|
+
let request
|
|
297
|
+
let response
|
|
298
|
+
|
|
299
|
+
request = {
|
|
300
|
+
url: 'http://localhost:3000/',
|
|
301
|
+
httpMethod: 'GET'
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
response = await lambdaFunction(request)
|
|
305
|
+
expect(response.statusCode).to.eql(200)
|
|
306
|
+
expect(response.body).to.eql('healthy')
|
|
307
|
+
|
|
308
|
+
request = {
|
|
309
|
+
path: '/Spec',
|
|
310
|
+
httpMethod: 'GET'
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
response = await lambdaFunction(request)
|
|
314
|
+
expect(response.statusCode).to.eql(200)
|
|
315
|
+
|
|
316
|
+
const body = JSON.parse(response.body)
|
|
317
|
+
expect(body.swagger).to.not.exist
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it('logs operation input in non-test environment', async () => {
|
|
321
|
+
process.env.NODE_APP_INSTANCE = 'dev'
|
|
322
|
+
|
|
323
|
+
const lambdaFunction = request => service.handler(request)
|
|
324
|
+
|
|
325
|
+
await lambdaFunction({
|
|
326
|
+
url: 'http://localhost:3000/api/Health',
|
|
327
|
+
httpMethod: 'GET'
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
const id = 'HELLO_WORLD'
|
|
331
|
+
await exec('CreateProfile', {
|
|
332
|
+
mutation: { id, name: 'Hello, world!' }
|
|
333
|
+
}, { authorization })
|
|
334
|
+
|
|
335
|
+
process.env.NODE_APP_INSTANCE = undefined
|
|
283
336
|
})
|
|
284
337
|
})
|
|
285
338
|
})
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const isTestEnvironment = process.env.NODE_APP_INSTANCE === 'test'
|
|
3
|
+
const isTestEnvironment = () => process.env.NODE_APP_INSTANCE === 'test'
|
|
4
4
|
|
|
5
5
|
const logRequest = context => {
|
|
6
|
-
if (isTestEnvironment) {
|
|
6
|
+
if (isTestEnvironment()) {
|
|
7
7
|
return
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -11,7 +11,11 @@ const { name: title, version } = require(`${ROOT_PATH}/package.json`)
|
|
|
11
11
|
|
|
12
12
|
const SWAGGER_UI_HTML = SWAGGER_UI_TEMPLATE.replace('$TITLE', title)
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const isDevelopment = () => process.env.NODE_APP_INSTANCE === 'dev' || !process.env.NODE_APP_INSTANCE
|
|
15
|
+
|
|
16
|
+
const _getHomeBody = () => isDevelopment() ? SWAGGER_UI_HTML : 'healthy'
|
|
17
|
+
|
|
18
|
+
const _getSpecBody = (service) => isDevelopment() ? service.spec : { info: { title, version } }
|
|
15
19
|
|
|
16
20
|
const specMiddleware = (service, context) => {
|
|
17
21
|
const { httpPath, httpMethod } = context
|
|
@@ -19,22 +23,26 @@ const specMiddleware = (service, context) => {
|
|
|
19
23
|
if (httpMethod !== 'get') { return null }
|
|
20
24
|
|
|
21
25
|
if (httpPath === '/') {
|
|
26
|
+
const bodyText = _getHomeBody()
|
|
27
|
+
|
|
22
28
|
return {
|
|
23
29
|
headers: {
|
|
24
30
|
'Content-Type': 'text/html; charset=UTF-8'
|
|
25
31
|
},
|
|
26
32
|
statusCode: 200,
|
|
27
|
-
body:
|
|
33
|
+
body: bodyText,
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
if (httpPath === '/Spec') {
|
|
38
|
+
const bodyJson = JSON.stringify(_getSpecBody(service), null, 2)
|
|
39
|
+
|
|
32
40
|
return {
|
|
33
41
|
headers: {
|
|
34
42
|
'Content-Type': 'application/json; charset=utf-8'
|
|
35
43
|
},
|
|
36
44
|
statusCode: 200,
|
|
37
|
-
body:
|
|
45
|
+
body: bodyJson,
|
|
38
46
|
}
|
|
39
47
|
}
|
|
40
48
|
|