@mimik/api-helper 2.0.6 → 2.0.8
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/.claude/settings.local.json +9 -0
- package/.husky/pre-commit +2 -0
- package/.husky/pre-push +2 -0
- package/README.md +57 -63
- package/eslint.config.js +30 -11
- package/index.js +113 -117
- package/lib/ajvHelpers.js +1 -1
- package/lib/baseHandlers.js +2 -3
- package/lib/common.js +1 -1
- package/lib/oauthValidation-helper.js +1 -1
- package/lib/securityHandlers.js +1 -1
- package/package.json +24 -23
- package/test/ajvHelpers.test.js +159 -0
- package/test/baseHandlers.test.js +150 -0
- package/test/extract-helper.test.js +100 -0
- package/test/index-async.test.js +599 -0
- package/test/index-sync.test.js +282 -0
- package/test/oauthValidation-helper.test.js +136 -0
- package/test/securityHandlers.test.js +557 -0
- package/.nycrc +0 -4
package/.husky/pre-push
ADDED
package/README.md
CHANGED
|
@@ -4,91 +4,65 @@
|
|
|
4
4
|
**Example**
|
|
5
5
|
```js
|
|
6
6
|
import apiHelper from '@mimik/api-helper';
|
|
7
|
-
or
|
|
7
|
+
// or
|
|
8
8
|
import { apiSetup, securityLib, getAPIFile, validateSecuritySchemes, extractProperties, setupServerFiles } from '@mimik/api-helper';
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
* [api-helper](#module_api-helper)
|
|
12
12
|
* _async_
|
|
13
|
-
* [~
|
|
14
|
-
* [~
|
|
15
|
-
* [~
|
|
16
|
-
* [~setupServerFiles(apiFilename, controllersDirectory, buildDirectory, correlationId, options)](#module_api-helper..setupServerFiles) ⇒ <code>Promise</code>
|
|
13
|
+
* [~apiSetup(setup, registeredOperations, securityHandlers, extraFormats, config, correlationId)](#module_api-helper..apiSetup) ⇒ <code>Promise.<object></code>
|
|
14
|
+
* [~getAPIFile(apiFilename, correlationId, options)](#module_api-helper..getAPIFile) ⇒ <code>Promise.<object></code>
|
|
15
|
+
* [~setupServerFiles(apiFilename, controllersDirectory, buildDirectory, correlationId, options)](#module_api-helper..setupServerFiles) ⇒ <code>Promise.<object></code>
|
|
17
16
|
* _sync_
|
|
18
|
-
* [~
|
|
19
|
-
* [~
|
|
20
|
-
|
|
21
|
-
<a name="module_api-helper..securityLib"></a>
|
|
22
|
-
|
|
23
|
-
### api-helper~securityLib(config)
|
|
24
|
-
Implement the security flows for the API.
|
|
25
|
-
|
|
26
|
-
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
27
|
-
**Category**: async
|
|
28
|
-
**Throws**:
|
|
29
|
-
|
|
30
|
-
- <code>Promise</code> An error is thrown if the initiatilization failed.
|
|
31
|
-
|
|
32
|
-
This function is used to setup the following security handlers for the API:
|
|
33
|
-
- `SystemSecurity` - used for the system operations, like /system, /onbehalf
|
|
34
|
-
- `AdminSecurity` - used for the admin operations, like /admin,
|
|
35
|
-
- `UserSecurity` - used for the user operations, like /user,
|
|
36
|
-
- `ApiKeySecurity` - used for the API key operations, like /apikey,
|
|
37
|
-
The security handlers are used to validate the tokens and scopes for the API operations.
|
|
38
|
-
|
|
39
|
-
**Requires**: <code>module:@mimik/swagger-helper</code>, <code>module:jsonwebtoken</code>, <code>module:lodash</code>
|
|
40
|
-
|
|
41
|
-
| Param | Type | Description |
|
|
42
|
-
| --- | --- | --- |
|
|
43
|
-
| config | <code>object</code> | Configuration of the service. &fulfil {object} The API file itself. |
|
|
17
|
+
* [~securityLib(config)](#module_api-helper..securityLib) ⇒ <code>object</code>
|
|
18
|
+
* [~validateSecuritySchemes(apiDefinition, correlationId)](#module_api-helper..validateSecuritySchemes) ⇒ <code>Array.<string></code>
|
|
19
|
+
* [~extractProperties(apiDefinition, controllersDirectory, buildDirectory, correlationId)](#module_api-helper..extractProperties) ⇒ <code>void</code>
|
|
44
20
|
|
|
45
21
|
<a name="module_api-helper..apiSetup"></a>
|
|
46
22
|
|
|
47
|
-
### api-helper~apiSetup(setup, registeredOperations, securityHandlers, extraFormats, config, correlationId) ⇒ <code>Promise
|
|
48
|
-
Setup the API to be
|
|
23
|
+
### api-helper~apiSetup(setup, registeredOperations, securityHandlers, extraFormats, config, correlationId) ⇒ <code>Promise.<object></code>
|
|
24
|
+
Setup the API to be used for a service
|
|
49
25
|
|
|
50
26
|
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
51
|
-
**Returns**: <code>Promise
|
|
52
|
-
&fulfil {object} The API file itself.
|
|
27
|
+
**Returns**: <code>Promise.<object></code> - The API file itself.
|
|
53
28
|
**Category**: async
|
|
54
29
|
**Throws**:
|
|
55
30
|
|
|
56
|
-
- <code>Promise</code> An error is thrown if the
|
|
31
|
+
- <code>Promise</code> An error is thrown if the initialization failed.
|
|
57
32
|
|
|
58
33
|
The following scheme names are reserved: `SystemSecurity`, `AdminSecurity`, `UserSecurity`, `PeerSecurity`, `ApiKeySecurity`.
|
|
59
34
|
The following security schemes can be defaulted: `SystemSecurity`, `AdminSecurity`, `UserSecurity`, `ApiKeySecurity`.
|
|
60
35
|
The secOptions in the options property passed when using `init` allows the following operations:
|
|
61
36
|
- introduce a customer security scheme, in this case secOptions contains: { newSecurityScheme: {function}newSecurityHandler },
|
|
62
37
|
- disable a security scheme that is defined in the swagger API, in this case secOptions contains: { securitySchemeToDisable: { {boolean}notEnabled: true } },
|
|
63
|
-
-
|
|
38
|
+
- overwrite an existing security scheme, in this case secOptions contains: { securitySchemeToOverwrite: {function}newSecurityHandler }.
|
|
64
39
|
If the secOptions is not present either to introduce, disable or overwrite a security scheme that is present in the swagger API file an error is generated.
|
|
65
40
|
If the secOptions contains unused security schemes, an error is generated.
|
|
66
41
|
|
|
67
42
|
The default formats for validation are: `date`, `time`, `date-time`, `byte`, `uuid`, `uri`, `email`, `ipv4`, `ipv6`, `semver`, `ip`.
|
|
68
43
|
|
|
69
|
-
**Requires**: <code>module:@mimik/response-helper</code>, <code>module:@mimik/sumologic-winston-logger</code>, <code>module
|
|
44
|
+
**Requires**: <code>module:@mimik/response-helper</code>, <code>module:@mimik/sumologic-winston-logger</code>, <code>module:lodash.difference</code>, <code>module:openapi-backend</code>
|
|
70
45
|
|
|
71
46
|
| Param | Type | Description |
|
|
72
47
|
| --- | --- | --- |
|
|
73
|
-
| setup | <code>object</code> | Object containing the apiFilename and the
|
|
48
|
+
| setup | <code>object</code> | Object containing the apiFilename and the existing security schemes in the API definition. |
|
|
74
49
|
| registeredOperations | <code>object</code> | List of the operation to register for the API. |
|
|
75
50
|
| securityHandlers | <code>object</code> | List of the security handlers to add for the service. |
|
|
76
|
-
| extraFormats | <code>object</code> | list of the formats to add for
|
|
51
|
+
| extraFormats | <code>object</code> | list of the formats to add for validating properties. |
|
|
77
52
|
| config | <code>object</code> | Configuration of the service. |
|
|
78
|
-
| correlationId | <code>UUID.<string></code> | CorrelationId when logging
|
|
53
|
+
| correlationId | <code>UUID.<string></code> | CorrelationId when logging activities. |
|
|
79
54
|
|
|
80
55
|
<a name="module_api-helper..getAPIFile"></a>
|
|
81
56
|
|
|
82
|
-
### api-helper~getAPIFile(apiFilename, correlationId, options) ⇒ <code>Promise
|
|
83
|
-
Gets the API file from swaggerhub and store it in the
|
|
57
|
+
### api-helper~getAPIFile(apiFilename, correlationId, options) ⇒ <code>Promise.<object></code>
|
|
58
|
+
Gets the API file from swaggerhub and store it in the given PATH location.
|
|
84
59
|
|
|
85
60
|
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
86
|
-
**Returns**: <code>Promise
|
|
87
|
-
&fulfil {object} The API file itself.
|
|
61
|
+
**Returns**: <code>Promise.<object></code> - The API file itself.
|
|
88
62
|
**Category**: async
|
|
89
63
|
**Throws**:
|
|
90
64
|
|
|
91
|
-
- <code>Promise</code> An error is
|
|
65
|
+
- <code>Promise</code> An error is thrown if the apiFilename resolution generates an error or the request to the API provider fails or the file cannot be saved.
|
|
92
66
|
|
|
93
67
|
`apiInfo` options has the following format:
|
|
94
68
|
``` javascript
|
|
@@ -98,29 +72,29 @@ Gets the API file from swaggerhub and store it in the give PATH location.
|
|
|
98
72
|
"username": "username for bitbucket",
|
|
99
73
|
"password": "password for bitbucket"
|
|
100
74
|
},
|
|
101
|
-
"apiApiKey": "apiKey for access private API on swaggerhub, can be optional if the API is accessible
|
|
75
|
+
"apiApiKey": "apiKey for access private API on swaggerhub, can be optional if the API is accessible publicly"
|
|
102
76
|
}
|
|
77
|
+
```
|
|
103
78
|
|
|
104
79
|
**Requires**: <code>module:@mimik/request-retry</code>, <code>module:@mimik/response-helper</code>, <code>module:@mimik/sumologic-winston-logger</code>, <code>module:fs</code>, <code>module:js-yaml</code>, <code>module:path</code>
|
|
105
80
|
|
|
106
81
|
| Param | Type | Description |
|
|
107
82
|
| --- | --- | --- |
|
|
108
83
|
| apiFilename | <code>PATH.<string></code> | Name of the file where the API file will be stored. |
|
|
109
|
-
| correlationId | <code>UUID.<string></code> | CorrelationId when logging
|
|
84
|
+
| correlationId | <code>UUID.<string></code> | CorrelationId when logging activities. |
|
|
110
85
|
| options | <code>object</code> | Options associated with the call. Use to pass `metrics` to `rpRetry` and `apiInfo` to access the api file in the api provider. |
|
|
111
86
|
|
|
112
87
|
<a name="module_api-helper..setupServerFiles"></a>
|
|
113
88
|
|
|
114
|
-
### api-helper~setupServerFiles(apiFilename, controllersDirectory, buildDirectory, correlationId, options) ⇒ <code>Promise
|
|
89
|
+
### api-helper~setupServerFiles(apiFilename, controllersDirectory, buildDirectory, correlationId, options) ⇒ <code>Promise.<object></code>
|
|
115
90
|
Setup and validates files for the server
|
|
116
91
|
|
|
117
92
|
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
118
|
-
**Returns**: <code>Promise
|
|
119
|
-
&fulfil {object} The API file, the API filename, the existing known security schemes and the defined security schemes.
|
|
93
|
+
**Returns**: <code>Promise.<object></code> - The API file, the API filename, the existing known security schemes and the defined security schemes.
|
|
120
94
|
**Category**: async
|
|
121
95
|
**Throws**:
|
|
122
96
|
|
|
123
|
-
- <code>Promise</code> An error is thrown for many reasons
|
|
97
|
+
- <code>Promise</code> An error is thrown for many reasons associated with getAPIFile or validateSecuritySchemes or extractProperties.
|
|
124
98
|
|
|
125
99
|
**Requires**: <code>module:@mimik/request-retry</code>, <code>module:@mimik/response-helper</code>, <code>module:@mimik/sumologic-winston-logger</code>, <code>module:fs</code>, <code>module:js-yaml</code>, <code>module:path</code>
|
|
126
100
|
|
|
@@ -129,39 +103,59 @@ Setup and validates files for the server
|
|
|
129
103
|
| apiFilename | <code>PATH.<string></code> | Name of the file where the API file will be stored. |
|
|
130
104
|
| controllersDirectory | <code>PATH.<string></code> | Directory to find the controller files. |
|
|
131
105
|
| buildDirectory | <code>PATH.<string></code> | Directory where the register file will be stored. |
|
|
132
|
-
| correlationId | <code>UUID.<string></code> | CorrelationId when logging
|
|
133
|
-
| options | <code>object</code> | Options associated with the call. Use to pass `metrics` to `rpRetry` and `apiKey
|
|
106
|
+
| correlationId | <code>UUID.<string></code> | CorrelationId when logging activities. |
|
|
107
|
+
| options | <code>object</code> | Options associated with the call. Use to pass `metrics` to `rpRetry` and `apiKey` to access private API. |
|
|
108
|
+
|
|
109
|
+
<a name="module_api-helper..securityLib"></a>
|
|
110
|
+
|
|
111
|
+
### api-helper~securityLib(config) ⇒ <code>object</code>
|
|
112
|
+
Implement the security flows for the API.
|
|
113
|
+
|
|
114
|
+
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
115
|
+
**Returns**: <code>object</code> - An object containing `SystemSecurity`, `AdminSecurity`, `UserSecurity`, and `ApiKeySecurity` handlers.
|
|
116
|
+
|
|
117
|
+
This function is used to setup the following security handlers for the API:
|
|
118
|
+
- `SystemSecurity` - used for the system operations, like /system, /onbehalf
|
|
119
|
+
- `AdminSecurity` - used for the admin operations, like /admin,
|
|
120
|
+
- `UserSecurity` - used for the user operations, like /user,
|
|
121
|
+
- `ApiKeySecurity` - used for the API key operations, like /apikey,
|
|
122
|
+
The security handlers are used to validate the tokens and scopes for the API operations.
|
|
123
|
+
**Category**: sync
|
|
124
|
+
**Requires**: <code>module:@mimik/swagger-helper</code>, <code>module:jsonwebtoken</code>, <code>module:lodash</code>
|
|
125
|
+
|
|
126
|
+
| Param | Type | Description |
|
|
127
|
+
| --- | --- | --- |
|
|
128
|
+
| config | <code>object</code> | Configuration of the service. |
|
|
134
129
|
|
|
135
130
|
<a name="module_api-helper..validateSecuritySchemes"></a>
|
|
136
131
|
|
|
137
|
-
### api-helper~validateSecuritySchemes(apiDefinition, correlationId) ⇒
|
|
132
|
+
### api-helper~validateSecuritySchemes(apiDefinition, correlationId) ⇒ <code>Array.<string></code>
|
|
138
133
|
Validates the known SecuritySchemes: `SystemSecurity`, `AdminSecurity`, `UserSecurity`, `PeerSecurity`, `ApiKeySecurity`.
|
|
139
134
|
|
|
140
135
|
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
141
|
-
**Returns**: An array of the known securitySchemes that are in the API definition.
|
|
136
|
+
**Returns**: <code>Array.<string></code> - An array of the known securitySchemes that are in the API definition.
|
|
142
137
|
**Category**: sync
|
|
143
138
|
**Throws**:
|
|
144
139
|
|
|
145
|
-
- An error is thrown
|
|
140
|
+
- An error is thrown if a validation fails.
|
|
146
141
|
|
|
147
142
|
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>, <code>module:@mimik/response-helper</code>
|
|
148
143
|
|
|
149
144
|
| Param | Type | Description |
|
|
150
145
|
| --- | --- | --- |
|
|
151
146
|
| apiDefinition | <code>object</code> | JSON object containing the API definition. |
|
|
152
|
-
| correlationId | <code>UUID.<string></code> | CorrelationId when logging
|
|
147
|
+
| correlationId | <code>UUID.<string></code> | CorrelationId when logging activities. |
|
|
153
148
|
|
|
154
149
|
<a name="module_api-helper..extractProperties"></a>
|
|
155
150
|
|
|
156
|
-
### api-helper~extractProperties(apiDefinition, controllersDirectory, buildDirectory, correlationId) ⇒
|
|
157
|
-
Extracts the properties from API
|
|
151
|
+
### api-helper~extractProperties(apiDefinition, controllersDirectory, buildDirectory, correlationId) ⇒ <code>void</code>
|
|
152
|
+
Extracts the properties from API definition and creates a file binding the handler with the controller operations.
|
|
158
153
|
|
|
159
154
|
**Kind**: inner method of [<code>api-helper</code>](#module_api-helper)
|
|
160
|
-
**Returns**: null
|
|
161
155
|
**Category**: sync
|
|
162
156
|
**Throws**:
|
|
163
157
|
|
|
164
|
-
- An error is thrown for many reasons, like operationId does not exist in controllers, controller
|
|
158
|
+
- An error is thrown for many reasons, like operationId does not exist in controllers, controller does not exist...
|
|
165
159
|
|
|
166
160
|
**Requires**: <code>module:@mimik/response-helper</code>, <code>module:@mimik/sumologic-winston-logger</code>, <code>module:fs</code>
|
|
167
161
|
|
|
@@ -170,5 +164,5 @@ Extracts the properties from API definiton and creates a file binding the handle
|
|
|
170
164
|
| apiDefinition | <code>object</code> | JSON object containing the API definition. |
|
|
171
165
|
| controllersDirectory | <code>PATH.<string></code> | Directory to find the controller files. |
|
|
172
166
|
| buildDirectory | <code>PATH.<string></code> | Directory where the register file will be stored. |
|
|
173
|
-
| correlationId | <code>UUID.<string></code> | CorrelationId when logging
|
|
167
|
+
| correlationId | <code>UUID.<string></code> | CorrelationId when logging activities. |
|
|
174
168
|
|
package/eslint.config.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
1
2
|
import importPlugin from 'eslint-plugin-import';
|
|
2
3
|
import js from '@eslint/js';
|
|
3
4
|
import processDoc from '@mimik/eslint-plugin-document-env';
|
|
@@ -10,6 +11,9 @@ const MAX_LINES_IN_FUNCTION = 150;
|
|
|
10
11
|
const MAX_STATEMENTS_IN_FUNCTION = 45;
|
|
11
12
|
const MIN_KEYS_IN_OBJECT = 10;
|
|
12
13
|
const MAX_COMPLEXITY = 30;
|
|
14
|
+
const ECMA_VERSION = 'latest';
|
|
15
|
+
const MAX_DEPTH = 6;
|
|
16
|
+
const ALLOWED_CONSTANTS = [0, 1, -1];
|
|
13
17
|
|
|
14
18
|
export default [
|
|
15
19
|
{
|
|
@@ -23,18 +27,18 @@ export default [
|
|
|
23
27
|
processDoc,
|
|
24
28
|
},
|
|
25
29
|
languageOptions: {
|
|
26
|
-
ecmaVersion:
|
|
30
|
+
ecmaVersion: ECMA_VERSION,
|
|
27
31
|
globals: {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
it: 'readonly',
|
|
31
|
-
require: 'readonly',
|
|
32
|
+
...globals.mocha,
|
|
33
|
+
...globals.nodeBuiltin,
|
|
32
34
|
},
|
|
33
35
|
sourceType: 'module',
|
|
34
36
|
},
|
|
35
37
|
rules: {
|
|
36
38
|
'@stylistic/brace-style': ['warn', 'stroustrup', { allowSingleLine: true }],
|
|
37
39
|
'@stylistic/line-comment-position': ['off'],
|
|
40
|
+
'@stylistic/max-len': ['warn', MAX_LENGTH_LINE, { ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
|
|
41
|
+
'@stylistic/quotes': ['warn', 'single'],
|
|
38
42
|
'@stylistic/semi': ['error', 'always'],
|
|
39
43
|
'capitalized-comments': ['off'],
|
|
40
44
|
'complexity': ['error', MAX_COMPLEXITY],
|
|
@@ -44,20 +48,35 @@ export default [
|
|
|
44
48
|
'import/no-unresolved': ['error', { amd: true, caseSensitiveStrict: true, commonjs: true }],
|
|
45
49
|
'init-declarations': ['off'],
|
|
46
50
|
'linebreak-style': ['off'],
|
|
47
|
-
'max-
|
|
48
|
-
'max-
|
|
49
|
-
'max-lines
|
|
51
|
+
'max-depth': ['error', MAX_DEPTH],
|
|
52
|
+
'max-len': ['off'],
|
|
53
|
+
'max-lines': ['warn', { max: MAX_LINES_IN_FILES, skipComments: true, skipBlankLines: true }],
|
|
54
|
+
'max-lines-per-function': ['warn', { max: MAX_LINES_IN_FUNCTION, skipComments: true, skipBlankLines: true }],
|
|
50
55
|
'max-params': ['error', MAX_FUNCTION_PARAMETERS],
|
|
51
56
|
'max-statements': ['warn', MAX_STATEMENTS_IN_FUNCTION],
|
|
52
|
-
'no-confusing-arrow': ['off'],
|
|
57
|
+
'no-confusing-arrow': ['off'],
|
|
53
58
|
'no-inline-comments': ['off'],
|
|
59
|
+
'no-magic-numbers': ['error', { ignore: ALLOWED_CONSTANTS, enforceConst: true, detectObjects: true }],
|
|
54
60
|
'no-process-env': ['error'],
|
|
55
61
|
'no-ternary': ['off'],
|
|
56
62
|
'no-undefined': ['off'],
|
|
57
63
|
'one-var': ['error', 'never'],
|
|
58
64
|
'processDoc/validate-document-env': ['error'],
|
|
59
|
-
'quotes': ['
|
|
60
|
-
'sort-
|
|
65
|
+
'quotes': ['off'],
|
|
66
|
+
'sort-imports': ['error', { allowSeparatedGroups: true }],
|
|
67
|
+
'sort-keys': ['error', 'asc', { caseSensitive: true, minKeys: MIN_KEYS_IN_OBJECT, natural: false, allowLineSeparatedGroups: true }],
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
files: ['test/**/*.js'],
|
|
72
|
+
rules: {
|
|
73
|
+
'class-methods-use-this': ['off'],
|
|
74
|
+
'max-classes-per-file': ['off'],
|
|
75
|
+
'max-lines': ['off'],
|
|
76
|
+
'max-lines-per-function': ['off'],
|
|
77
|
+
'max-statements': ['off'],
|
|
78
|
+
'no-empty-function': ['off'],
|
|
79
|
+
'no-magic-numbers': ['off'],
|
|
61
80
|
},
|
|
62
81
|
},
|
|
63
82
|
];
|