@kravc/dos 1.8.7 → 1.8.9

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/examples/index.js CHANGED
@@ -22,6 +22,8 @@ const modules = [
22
22
  IndexProfiles
23
23
  ]
24
24
 
25
- const service = new Service(modules, 'http://localhost:3000/', '/examples')
25
+ const ROOT_PATH = process.cwd()
26
+
27
+ const service = new Service(modules, 'http://localhost:3000/', `${ROOT_PATH}/examples`)
26
28
 
27
29
  exports.handler = handler(service)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/dos",
3
- "version": "1.8.7",
3
+ "version": "1.8.9",
4
4
  "description": "Convention-based, easy-to-use library for building API-driven serverless services.",
5
5
  "keywords": [
6
6
  "Service",
@@ -33,7 +33,8 @@
33
33
  "pluralize": "^8.0.0",
34
34
  "ulid": "^2.3.0",
35
35
  "uuid": "^9.0.1",
36
- "z-schema": "^6.0.1"
36
+ "z-schema": "^6.0.1",
37
+ "read-yaml-file": "^2.1.0"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@eslint/eslintrc": "^3.2.0",
@@ -0,0 +1 @@
1
+ []
@@ -0,0 +1 @@
1
+ []
@@ -0,0 +1 @@
1
+ []
@@ -0,0 +1 @@
1
+ []
@@ -0,0 +1 @@
1
+ []
@@ -0,0 +1 @@
1
+ []
package/src/Service.js CHANGED
@@ -1,18 +1,20 @@
1
1
  'use strict'
2
2
 
3
- const { get, uniq, compact } = require('lodash')
4
- const handler = require('./helpers/handler')
5
- const authorize = require('./helpers/authorize')
6
- const createSpec = require('./helpers/createSpec')
7
- const { Validator } = require('@kravc/schema')
8
- const OperationError = require('./errors/OperationError')
9
- const createSchemasMap = require('./helpers/createSchemasMap')
10
- const InvalidInputError = require('./errors/InvalidInputError')
3
+ const handler = require('./helpers/handler')
4
+ const authorize = require('./helpers/authorize')
5
+ const createSpec = require('./helpers/createSpec')
6
+ const { Validator } = require('@kravc/schema')
7
+ const OperationError = require('./errors/OperationError')
8
+ const createSchemasMap = require('./helpers/createSchemasMap')
9
+ const InvalidInputError = require('./errors/InvalidInputError')
11
10
  const InvalidOutputError = require('./errors/InvalidOutputError')
12
11
  const OperationNotFoundError = require('./errors/OperationNotFoundError')
12
+ const { get, uniq, compact } = require('lodash')
13
+
14
+ const ROOT_PATH = process.cwd()
13
15
 
14
16
  class Service {
15
- constructor(modules, url = 'http://localhost:3000/', path = '/src') {
17
+ constructor(modules, url = 'http://localhost:3000/', path = `${ROOT_PATH}/src`) {
16
18
  if (!url.endsWith('/')) { url = url + '/' }
17
19
 
18
20
  const schemasMap = createSchemasMap(path)
@@ -25,6 +25,8 @@ const modules = [
25
25
  IndexProfiles
26
26
  ]
27
27
 
28
+ const ROOT_PATH = process.cwd()
29
+
28
30
  describe('Service', () => {
29
31
  before(() => {
30
32
  const { Component: Profile } = CreateProfile
@@ -33,7 +35,7 @@ describe('Service', () => {
33
35
 
34
36
  describe('Service.constructor(modules, url, path = \'/src\')', () => {
35
37
  it('initializes service', () => {
36
- const service = new Service(modules, 'https://example.com/api', '/examples')
38
+ const service = new Service(modules, 'https://example.com/api', `${ROOT_PATH}/examples`)
37
39
  expect(service).to.exist
38
40
  })
39
41
 
@@ -69,7 +71,7 @@ describe('Service', () => {
69
71
  })
70
72
 
71
73
  describe('.handler(request)', () => {
72
- const service = new Service(modules, 'https://example.com/api/', '/examples')
74
+ const service = new Service(modules, 'https://example.com/api/', `${ROOT_PATH}/examples`)
73
75
  const exec = test.execute(service)
74
76
 
75
77
  const authorization = test.createAccessToken({}, { group: 'Administrators' })
@@ -287,6 +289,14 @@ describe('Service', () => {
287
289
  const body = JSON.parse(response.body)
288
290
  expect(body.swagger).to.exist
289
291
 
292
+ response = await lambdaFunction({ path: '/Documents.yaml', httpMethod: 'GET' })
293
+ response = await lambdaFunction({ path: '/Enums.yaml', httpMethod: 'GET' })
294
+ response = await lambdaFunction({ path: '/Operations.yaml', httpMethod: 'GET' })
295
+ response = await lambdaFunction({ path: '/Parameters.yaml', httpMethod: 'GET' })
296
+ response = await lambdaFunction({ path: '/Schenarios.yaml', httpMethod: 'GET' })
297
+ response = await lambdaFunction({ path: '/Schemas.yaml', httpMethod: 'GET' })
298
+ expect(response.statusCode).to.eql(200)
299
+
290
300
  process.env.NODE_APP_INSTANCE = undefined
291
301
  })
292
302
 
@@ -5,8 +5,6 @@ const { keyBy } = require('lodash')
5
5
  const loadSync = require('./loadSync')
6
6
  const { readdirSync, statSync } = require('fs')
7
7
 
8
- const ROOT_PATH = process.cwd()
9
-
10
8
  const listFilesSync = dir =>
11
9
  readdirSync(dir)
12
10
  .reduce((files, file) =>
@@ -16,7 +14,7 @@ const listFilesSync = dir =>
16
14
  , [])
17
15
 
18
16
  const readSchemasSync = path =>
19
- listFilesSync(ROOT_PATH + path)
17
+ listFilesSync(path)
20
18
  .filter(fileName => fileName.endsWith('.yaml'))
21
19
  .map(schemaPath => loadSync(schemaPath))
22
20
 
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const { resolve } = require('path')
3
+ const { resolve } = require('path')
4
+ const readYamlFile = require('read-yaml-file')
4
5
  const { readFileSync } = require('fs')
5
6
 
6
7
  const SWAGGER_UI_TEMPLATE_PATH = resolve(__dirname, '../../assets/index.html')
@@ -46,6 +47,34 @@ const specMiddleware = (service, context) => {
46
47
  }
47
48
  }
48
49
 
50
+ const readFileJson = httpPath => {
51
+ const fileName = httpPath.replace('/', '')
52
+ const source = readYamlFile.sync(`${ROOT_PATH}/specs/${fileName}`)
53
+
54
+ return JSON.stringify(source, null, 2)
55
+ }
56
+
57
+ const isComposer = [
58
+ '/Enums.yaml',
59
+ '/Schemas.yaml',
60
+ '/Documents.yaml',
61
+ '/Scenarios.yaml',
62
+ '/Operations.yaml',
63
+ '/Parameters.yaml'
64
+ ].includes(httpPath)
65
+
66
+ const shouldReturnComposerSpecs = isComposer && isDevelopment()
67
+
68
+ if (shouldReturnComposerSpecs) {
69
+ return {
70
+ headers: {
71
+ 'Content-Type': 'application/json; charset=utf-8'
72
+ },
73
+ statusCode: 200,
74
+ body: readFileJson(httpPath),
75
+ }
76
+ }
77
+
49
78
  return null
50
79
  }
51
80