@kravc/dos 1.8.1 → 1.8.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/dos",
3
- "version": "1.8.1",
3
+ "version": "1.8.3",
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",
@@ -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
 
@@ -7,32 +7,42 @@ const SWAGGER_UI_TEMPLATE_PATH = resolve(__dirname, '../../assets/index.html')
7
7
  const SWAGGER_UI_TEMPLATE = readFileSync(SWAGGER_UI_TEMPLATE_PATH, { encoding: 'utf8' })
8
8
 
9
9
  const ROOT_PATH = process.cwd()
10
- const { name: title } = require(`${ROOT_PATH}/package.json`)
10
+ 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 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 } }
19
+
14
20
  const specMiddleware = (service, context) => {
15
21
  const { httpPath, httpMethod } = context
16
22
 
17
23
  if (httpMethod !== 'get') { return null }
18
24
 
19
25
  if (httpPath === '/') {
26
+ const bodyText = _getHomeBody()
27
+
20
28
  return {
21
29
  headers: {
22
30
  'Content-Type': 'text/html; charset=UTF-8'
23
31
  },
24
32
  statusCode: 200,
25
- body: SWAGGER_UI_HTML
33
+ body: bodyText,
26
34
  }
27
35
  }
28
36
 
29
37
  if (httpPath === '/Spec') {
38
+ const bodyJson = JSON.stringify(_getSpecBody(service), null, 2)
39
+
30
40
  return {
31
41
  headers: {
32
42
  'Content-Type': 'application/json; charset=utf-8'
33
43
  },
34
44
  statusCode: 200,
35
- body: JSON.stringify(service.spec, null, 2)
45
+ body: bodyJson,
36
46
  }
37
47
  }
38
48