@kravc/dos 1.8.10 → 1.9.1

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.10",
3
+ "version": "1.9.1",
4
4
  "description": "Convention-based, easy-to-use library for building API-driven serverless services.",
5
5
  "keywords": [
6
6
  "Service",
package/src/Document.js CHANGED
@@ -81,10 +81,10 @@ class Document extends Component {
81
81
  const { validator } = context
82
82
  mutation = validator.normalize(mutation, this.id)
83
83
 
84
- const accountId = get(context, 'identity.accountId')
84
+ const identitySubjectId = get(context, 'identity.sub')
85
85
 
86
- if (accountId) {
87
- mutation.createdBy = accountId
86
+ if (identitySubjectId) {
87
+ mutation.createdBy = identitySubjectId
88
88
  }
89
89
 
90
90
  const timestamp = new Date().toJSON()
@@ -159,10 +159,10 @@ class Document extends Component {
159
159
  static async update(context, query, mutation, originalDocument = null) {
160
160
  mutation = omit(mutation, [ this.idKey, 'createdAt', 'createdBy' ])
161
161
 
162
- const accountId = get(context, 'identity.accountId')
162
+ const identitySubjectId = get(context, 'identity.sub')
163
163
 
164
- if (accountId) {
165
- mutation.updatedBy = accountId
164
+ if (identitySubjectId) {
165
+ mutation.updatedBy = identitySubjectId
166
166
  }
167
167
 
168
168
  const timestamp = new Date().toJSON()
@@ -22,7 +22,7 @@ Profile.schema = loadSync('examples/Profile.yaml')
22
22
 
23
23
  describe('Document', () => {
24
24
  const validator = new Validator([ Profile.schema ])
25
- const identity = { accountId: 'ACCOUNT_ID' }
25
+ const identity = { sub: 'USER_ID' }
26
26
  const getContext = () => ({ validator, identity })
27
27
 
28
28
  let id
@@ -19,7 +19,7 @@ class Profile extends Document {}
19
19
  Profile.schema = loadSync('examples/Profile.yaml')
20
20
 
21
21
  const validator = new Validator([ Profile.schema ])
22
- const identity = { accountId: 'ACCOUNT_ID' }
22
+ const identity = { sub: 'USER_ID' }
23
23
  const DEFAULT_CONTEXT = { validator, identity }
24
24
 
25
25
  describe('Operation', () => {
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
+ })
@@ -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
- console.log(operationId, metadata)
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
  }
@@ -44,7 +44,7 @@ class SystemAuthorization {
44
44
  return { isAuthorized: false, error }
45
45
  }
46
46
 
47
- return { isAuthorized: true, accountId: 'SYSTEM' }
47
+ return { isAuthorized: true, isSystem: true }
48
48
  }
49
49
  }
50
50
 
@@ -5,14 +5,13 @@ const { privateKey: PRIVATE_KEY } = require('./keys')
5
5
 
6
6
  const createAccessToken = (options, attributes) => {
7
7
  const {
8
- algorithm = 'RS256',
8
+ algorithm = 'RS256',
9
9
  privateKey = PRIVATE_KEY,
10
10
  ...jwtOptions
11
11
  } = options
12
12
 
13
13
  const payload = {
14
- sub: 'SESSION_ID',
15
- accountId: 'ACCOUNT_ID',
14
+ sub: 'USER_ID',
16
15
  ...attributes
17
16
  }
18
17