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