@aligent/nx-openapi 2.1.1 → 2.1.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/README.md +9 -6
- package/package.json +1 -1
- package/src/generators/client/client-specific-files/client.ts.template +2 -0
- package/src/generators/client/generator.js +1 -1
- package/src/generators/client/helpers/auth-configurations.js +3 -2
- package/src/generators/client/schema.d.ts +1 -1
- package/src/generators/client/schema.json +11 -2
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ Run `nx g @aligent/nx-openapi:client` with optional flags:
|
|
|
24
24
|
- `--configPath` path to the redocly config file responsible for authentication when fetching a schema remotely. For more information: https://openapi-ts.dev/cli#auth.
|
|
25
25
|
- `--skipValidate` If passed, this will skip schema pre-validation. Only do this if you have good reason to - not validating the schema beforehand may produce unpredictable results (either things may not generate at all or they may generate something that is incorrect).
|
|
26
26
|
- `--override` Override the schema (and the generated type) of an existing client.
|
|
27
|
-
- `--authMethod` Authentication method for the generated client middleware.
|
|
27
|
+
- `--authMethod` **(required)** Authentication method for the generated client middleware. Available options:
|
|
28
28
|
- `api-key` - API key authentication using SSM Parameter Store
|
|
29
29
|
- `oauth1.0a` - OAuth 1.0a authentication
|
|
30
30
|
- `basic` - Basic authentication (username/password)
|
|
@@ -78,9 +78,12 @@ A `types` file that will contain several typescript interfaces depending on the
|
|
|
78
78
|
- `operations`: The defined operations that can be performed on a given path. [More information.](https://swagger.io/docs/specification/v3_0/paths-and-operations/#operations)
|
|
79
79
|
- `components`: A way of avoid duplication when paths or operations contain the same structure in their responses. Components define a structure that is used multiple time throughout the API. [More information.](https://swagger.io/docs/specification/v3_0/components/)
|
|
80
80
|
|
|
81
|
-
A `client` file that will contain boilerplate code to help you get started. The client includes
|
|
81
|
+
A `client` file that will contain boilerplate code to help you get started. The client includes:
|
|
82
82
|
|
|
83
|
-
-
|
|
84
|
-
-
|
|
85
|
-
-
|
|
86
|
-
- **
|
|
83
|
+
- `logMiddleware` for request/response logging
|
|
84
|
+
- `retryMiddleware` for automatic retry with exponential backoff
|
|
85
|
+
- Authentication middleware configured based on the `--authMethod` flag:
|
|
86
|
+
- **api-key**: Configured to fetch credentials from AWS SSM Parameter Store
|
|
87
|
+
- **oauth1.0a**: Configured with HMAC-SHA256 algorithm and placeholder credentials
|
|
88
|
+
- **basic**: Configured with placeholder username/password credentials
|
|
89
|
+
- **oauth2.0**: Configured with placeholder token and Bearer token type
|
package/package.json
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* - For more information about openapi-fetch middlewares: https://openapi-ts.dev/openapi-fetch/middleware-auth#middleware
|
|
7
7
|
*/
|
|
8
8
|
import {
|
|
9
|
+
logMiddleware,
|
|
9
10
|
retryMiddleware,
|
|
10
11
|
} from '@aligent/microservice-util-lib';
|
|
11
12
|
import createClient, { Client, ClientOptions } from 'openapi-fetch';
|
|
@@ -24,6 +25,7 @@ export class <%= className %> {
|
|
|
24
25
|
*/
|
|
25
26
|
|
|
26
27
|
this.client.use(
|
|
28
|
+
logMiddleware('<%= className %>'),
|
|
27
29
|
retryMiddleware({
|
|
28
30
|
onRetry: ({ attempt, error }) =>
|
|
29
31
|
console.log(`Retrying...${attempt} due to ${String(error)}`),
|
|
@@ -9,7 +9,7 @@ const VALID_EXTENSIONS = ['yaml', 'yml', 'json'];
|
|
|
9
9
|
// We also use this as the project root for all generated clients
|
|
10
10
|
const PROJECT_NAME = 'clients';
|
|
11
11
|
async function clientGenerator(tree, options) {
|
|
12
|
-
const { name, schemaPath, importPath = `@clients`, skipValidate, override, authMethod
|
|
12
|
+
const { name, schemaPath, importPath = `@clients`, skipValidate, override, authMethod, } = options;
|
|
13
13
|
const ext = schemaPath.split('.').pop() || '';
|
|
14
14
|
if (!VALID_EXTENSIONS.includes(ext)) {
|
|
15
15
|
throw new Error(`Invalid schema file extension: ${ext}`);
|
|
@@ -141,9 +141,10 @@ function applyAuthMethodConfiguration(tree, filePath, authMethod, className) {
|
|
|
141
141
|
${config.middlewareConfig.properties}
|
|
142
142
|
})
|
|
143
143
|
);`;
|
|
144
|
-
// Find the
|
|
144
|
+
// Find the middleware use statement and insert auth before it
|
|
145
145
|
const statements = constructor.getStatements();
|
|
146
|
-
const retryStatementIndex = statements.findIndex(statement => /this\.client\.use\(
|
|
146
|
+
const retryStatementIndex = statements.findIndex(statement => /this\.client\.use\(/.test(statement.getText()) &&
|
|
147
|
+
/retryMiddleware/.test(statement.getText()));
|
|
147
148
|
if (retryStatementIndex !== -1) {
|
|
148
149
|
constructor.insertStatements(retryStatementIndex, middlewareCall);
|
|
149
150
|
}
|
|
@@ -45,8 +45,17 @@
|
|
|
45
45
|
"type": "string",
|
|
46
46
|
"description": "Authentication method to use for the generated client middleware",
|
|
47
47
|
"enum": ["api-key", "oauth1.0a", "basic", "oauth2.0"],
|
|
48
|
-
"
|
|
48
|
+
"x-prompt": {
|
|
49
|
+
"message": "Which authentication method should the client use?",
|
|
50
|
+
"type": "list",
|
|
51
|
+
"items": [
|
|
52
|
+
{ "value": "api-key", "label": "API Key" },
|
|
53
|
+
{ "value": "oauth1.0a", "label": "OAuth 1.0a" },
|
|
54
|
+
{ "value": "basic", "label": "Basic Auth" },
|
|
55
|
+
{ "value": "oauth2.0", "label": "OAuth 2.0" }
|
|
56
|
+
]
|
|
57
|
+
}
|
|
49
58
|
}
|
|
50
59
|
},
|
|
51
|
-
"required": ["name", "schemaPath"]
|
|
60
|
+
"required": ["name", "schemaPath", "authMethod"]
|
|
52
61
|
}
|