@kravc/dos 1.9.0 → 1.9.2
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 +1 -1
- package/src/Document.js +12 -2
- package/src/Document.spec.js +3 -0
- package/src/Service.js +2 -2
- package/src/helpers/createContext.js +2 -1
- package/src/helpers/getOrFail.js +24 -0
- package/src/helpers/getOrFail.spec.js +17 -0
- package/src/helpers/handler.js +2 -2
- package/src/helpers/logRequest.js +2 -1
- package/src/index.js +1 -0
package/package.json
CHANGED
package/src/Document.js
CHANGED
|
@@ -150,8 +150,18 @@ class Document extends Component {
|
|
|
150
150
|
return { objects, ...rest }
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
-
static
|
|
154
|
-
|
|
153
|
+
static async indexAll(context, query, options) {
|
|
154
|
+
return this.index(context, query, options)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
static _index(query) {
|
|
158
|
+
const filter = item =>
|
|
159
|
+
Object.keys(query).every(key => item[key] === query[key])
|
|
160
|
+
|
|
161
|
+
const items = Object
|
|
162
|
+
.values(STORE[this.name] || {})
|
|
163
|
+
.filter(filter)
|
|
164
|
+
.map(cloneDeep)
|
|
155
165
|
|
|
156
166
|
return { items, count: items.length }
|
|
157
167
|
}
|
package/src/Document.spec.js
CHANGED
package/src/Service.js
CHANGED
|
@@ -92,8 +92,8 @@ class Service {
|
|
|
92
92
|
return get(this._spec.paths, `${httpPath}.${httpMethod}.operationId`, 'NONE')
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
handler(request) {
|
|
96
|
-
return handler(this)(request)
|
|
95
|
+
handler(request, logger) {
|
|
96
|
+
return handler(this)(request, logger)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async process(context) {
|
|
@@ -4,7 +4,7 @@ const { get, isString } = require('lodash')
|
|
|
4
4
|
const { parse } = require('url')
|
|
5
5
|
const { v4: uuid } = require('uuid')
|
|
6
6
|
|
|
7
|
-
const createContext = (service, request) => {
|
|
7
|
+
const createContext = (service, request, logger = console) => {
|
|
8
8
|
let httpPath
|
|
9
9
|
let httpMethod
|
|
10
10
|
|
|
@@ -33,6 +33,7 @@ const createContext = (service, request) => {
|
|
|
33
33
|
baseUrl: service.baseUrl,
|
|
34
34
|
validator: service.validator,
|
|
35
35
|
requestReceivedAt: new Date().toISOString(),
|
|
36
|
+
logger,
|
|
36
37
|
httpPath,
|
|
37
38
|
requestId,
|
|
38
39
|
httpMethod,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const lodash = require('lodash')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns value by path from the object, throws exception if value not defined.
|
|
7
|
+
* @param {object} object - The object to pull value by path
|
|
8
|
+
* @param {string} path - The path of the value within object
|
|
9
|
+
* @returns {any} A value
|
|
10
|
+
*/
|
|
11
|
+
const getOrFail = (object, path) => {
|
|
12
|
+
const value = lodash.get(object, path)
|
|
13
|
+
const isUndefined = value === undefined
|
|
14
|
+
|
|
15
|
+
/* istanbul ignore else: we should not get here in tests */
|
|
16
|
+
if (!isUndefined) {
|
|
17
|
+
return value
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* istanbul ignore next: should never be reached at runtime */
|
|
21
|
+
throw Error(`Value is undefined for "${path}"`)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = getOrFail
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const getOrFail = require('./getOrFail')
|
|
4
|
+
const { expect } = require('chai')
|
|
5
|
+
|
|
6
|
+
describe('getOrFail(object, path)', () => {
|
|
7
|
+
it('returns requested value', () => {
|
|
8
|
+
const value = getOrFail({ target: 1 }, 'target')
|
|
9
|
+
expect(value).to.exist
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('throw error if value is undefined', () => {
|
|
13
|
+
expect(
|
|
14
|
+
() => getOrFail({}, 'target')
|
|
15
|
+
).to.throw('Value is undefined for "target"')
|
|
16
|
+
})
|
|
17
|
+
})
|
package/src/helpers/handler.js
CHANGED
|
@@ -5,8 +5,8 @@ const createContext = require('./createContext')
|
|
|
5
5
|
const specMiddleware = require('./specMiddleware')
|
|
6
6
|
|
|
7
7
|
const handler = (service, _createContext = createContext, _middleware = specMiddleware) => {
|
|
8
|
-
return request => {
|
|
9
|
-
const context = _createContext(service, request)
|
|
8
|
+
return (request, logger) => {
|
|
9
|
+
const context = _createContext(service, request, logger)
|
|
10
10
|
|
|
11
11
|
const result = _middleware(service, context)
|
|
12
12
|
|
|
@@ -10,6 +10,7 @@ const logRequest = context => {
|
|
|
10
10
|
const {
|
|
11
11
|
operationId,
|
|
12
12
|
query,
|
|
13
|
+
logger,
|
|
13
14
|
mutation,
|
|
14
15
|
requestId,
|
|
15
16
|
headers,
|
|
@@ -29,7 +30,7 @@ const logRequest = context => {
|
|
|
29
30
|
metadata.mutation = mutation
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
logger.info(operationId, metadata)
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
module.exports = logRequest
|
package/src/index.js
CHANGED
|
@@ -14,6 +14,7 @@ module.exports = {
|
|
|
14
14
|
test: require('./test'),
|
|
15
15
|
handler: require('./helpers/handler'),
|
|
16
16
|
security: require('./security'),
|
|
17
|
+
getOrFail: require('./helpers/getOrFail'),
|
|
17
18
|
JwtAuthorization: require('./security/JwtAuthorization'),
|
|
18
19
|
SystemAuthorization: require('./security/SystemAuthorization')
|
|
19
20
|
}
|