@travetto/openapi 3.0.0-rc.9 → 3.0.0
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
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
2
|
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/openapi/DOC.ts and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# OpenAPI Specification
|
|
4
|
-
## OpenAPI integration support for the
|
|
4
|
+
## OpenAPI integration support for the Travetto framework
|
|
5
5
|
|
|
6
6
|
**Install: @travetto/openapi**
|
|
7
7
|
```bash
|
|
8
8
|
npm install @travetto/openapi
|
|
9
|
+
|
|
10
|
+
# or
|
|
11
|
+
|
|
12
|
+
yarn add @travetto/openapi
|
|
9
13
|
```
|
|
10
14
|
|
|
11
15
|
In the [RESTful API](https://github.com/travetto/travetto/tree/main/module/rest#readme "Declarative api for RESTful APIs with support for the dependency injection module.") module, the controllers and endpoints can be described via decorators, comments, or typings. This only provides the general metadata internally. This is not sufficient to generate a usable API doc, and so this module exists to bridge that gap.
|
|
@@ -19,9 +23,9 @@ All of the high level configurations can be found in the following structure:
|
|
|
19
23
|
|
|
20
24
|
**Code: Config: OpenAPI Configuration**
|
|
21
25
|
```typescript
|
|
22
|
-
import { ServerObject, ContactObject, LicenseObject } from 'openapi3-ts/src/model/OpenApi';
|
|
26
|
+
import type { ServerObject, ContactObject, LicenseObject } from 'openapi3-ts/src/model/OpenApi';
|
|
23
27
|
|
|
24
|
-
import { Config } from '@travetto/config';
|
|
28
|
+
import { Config, EnvVar } from '@travetto/config';
|
|
25
29
|
import { path, RootIndex } from '@travetto/manifest';
|
|
26
30
|
import { GlobalEnv } from '@travetto/base';
|
|
27
31
|
import { Required } from '@travetto/schema';
|
|
@@ -77,10 +81,12 @@ export class ApiSpecConfig {
|
|
|
77
81
|
/**
|
|
78
82
|
* Where to output file to
|
|
79
83
|
*/
|
|
84
|
+
@EnvVar('TRV_OPENAPI_OUTPUT')
|
|
80
85
|
output: string = 'openapi.yml';
|
|
81
86
|
/**
|
|
82
87
|
* Should file be generated at runtime
|
|
83
88
|
*/
|
|
89
|
+
@EnvVar('TRV_OPENAPI_PERSIST')
|
|
84
90
|
persist?: boolean;
|
|
85
91
|
/**
|
|
86
92
|
* Skip emitting all routes
|
|
@@ -112,7 +118,7 @@ The framework, when in watch mode, will generate the [OpenAPI](https://github.co
|
|
|
112
118
|
|
|
113
119
|
## CLI - openapi:spec
|
|
114
120
|
|
|
115
|
-
The module provides a command for the [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for
|
|
121
|
+
The module provides a command for the [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework") to allow scripting file generation.
|
|
116
122
|
|
|
117
123
|
**Terminal: OpenAPI usage**
|
|
118
124
|
```bash
|
|
@@ -123,8 +129,6 @@ Usage: openapi:spec [options]
|
|
|
123
129
|
Options:
|
|
124
130
|
-o, --output <output> Output files (default: "./openapi.yml")
|
|
125
131
|
-h, --help display help for command
|
|
126
|
-
|
|
127
|
-
[s[r[u
|
|
128
132
|
```
|
|
129
133
|
|
|
130
134
|
The command will run your application, in non-server mode, to collect all the routes and model information, to produce the `openapi.yml`. Once produced, the code will store the output in the specified location.
|
|
@@ -133,13 +137,32 @@ The command will run your application, in non-server mode, to collect all the ro
|
|
|
133
137
|
|
|
134
138
|
## CLI - openapi:client
|
|
135
139
|
|
|
136
|
-
The module provides a command for the [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for
|
|
140
|
+
The module provides a command for the [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework") to allow client generation from the API structure.
|
|
137
141
|
|
|
138
142
|
**Terminal: OpenAPI usage**
|
|
139
143
|
```bash
|
|
140
144
|
$ trv openapi:client --help
|
|
141
145
|
|
|
142
|
-
|
|
146
|
+
Usage: openapi:client [options] <format-or-preset>
|
|
147
|
+
|
|
148
|
+
Options:
|
|
149
|
+
-x, --extended-help Show Extended Help
|
|
150
|
+
-a, --additional-properties <additional-properties> Additional Properties (default: [])
|
|
151
|
+
-i, --input <input> Input file (default:
|
|
152
|
+
"./openapi.yml")
|
|
153
|
+
-o, --output <output> Output folder (default:
|
|
154
|
+
"./api-client")
|
|
155
|
+
-d, --docker-image <docker-image> Docker Image to use (default:
|
|
156
|
+
"arcsine/openapi-generator:latest")
|
|
157
|
+
-w, --watch Watch for file changes
|
|
158
|
+
-h, --help display help for command
|
|
159
|
+
|
|
160
|
+
Available Presets
|
|
161
|
+
----------------------------------
|
|
162
|
+
* @travetto/angular10 -- typescript-angular supportsES6=true,ngVersion=10.0
|
|
163
|
+
* @travetto/angular11 -- typescript-angular supportsES6=true,ngVersion=11.0
|
|
164
|
+
* @travetto/angular12 -- typescript-angular supportsES6=true,ngVersion=11.0
|
|
165
|
+
* @travetto/fetch -- typescript-fetch
|
|
143
166
|
```
|
|
144
167
|
|
|
145
168
|
This tool relies upon a custom build of [OpenAPI client generation tools](https://github.com/OpenAPITools/openapi-generator), which supports watching. This allows for fast responsive client generation as the shape of the API changes.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/openapi",
|
|
3
|
-
"version": "3.0.0
|
|
4
|
-
"description": "OpenAPI integration support for the
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "OpenAPI integration support for the Travetto framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rest",
|
|
7
7
|
"travetto",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"directory": "module/openapi"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/config": "^3.0.0
|
|
30
|
-
"@travetto/rest": "^3.0.0
|
|
31
|
-
"@travetto/schema": "^3.0.0
|
|
32
|
-
"@travetto/yaml": "^3.0.0
|
|
33
|
-
"openapi3-ts": "^
|
|
29
|
+
"@travetto/config": "^3.0.0",
|
|
30
|
+
"@travetto/rest": "^3.0.0",
|
|
31
|
+
"@travetto/schema": "^3.0.0",
|
|
32
|
+
"@travetto/yaml": "^3.0.0",
|
|
33
|
+
"openapi3-ts": "^3.1.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@travetto/cli": "^3.0.0
|
|
36
|
+
"@travetto/cli": "^3.0.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependenciesMeta": {
|
|
39
39
|
"@travetto/cli": {
|
package/src/config.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ServerObject, ContactObject, LicenseObject } from 'openapi3-ts/src/model/OpenApi';
|
|
1
|
+
import type { ServerObject, ContactObject, LicenseObject } from 'openapi3-ts/src/model/OpenApi';
|
|
2
2
|
|
|
3
|
-
import { Config } from '@travetto/config';
|
|
3
|
+
import { Config, EnvVar } from '@travetto/config';
|
|
4
4
|
import { path, RootIndex } from '@travetto/manifest';
|
|
5
5
|
import { GlobalEnv } from '@travetto/base';
|
|
6
6
|
import { Required } from '@travetto/schema';
|
|
@@ -57,10 +57,12 @@ export class ApiSpecConfig {
|
|
|
57
57
|
/**
|
|
58
58
|
* Where to output file to
|
|
59
59
|
*/
|
|
60
|
+
@EnvVar('TRV_OPENAPI_OUTPUT')
|
|
60
61
|
output: string = 'openapi.yml';
|
|
61
62
|
/**
|
|
62
63
|
* Should file be generated at runtime
|
|
63
64
|
*/
|
|
65
|
+
@EnvVar('TRV_OPENAPI_PERSIST')
|
|
64
66
|
persist?: boolean;
|
|
65
67
|
/**
|
|
66
68
|
* Skip emitting all routes
|
package/src/service.ts
CHANGED
package/src/spec-generate.ts
CHANGED
|
@@ -85,8 +85,7 @@ export class SpecGenerator {
|
|
|
85
85
|
...this.#getType(sub)
|
|
86
86
|
} : this.#getType(sub),
|
|
87
87
|
required: !!(rootField?.required?.active && sub.required?.active),
|
|
88
|
-
in: location
|
|
89
|
-
extract: undefined
|
|
88
|
+
in: location
|
|
90
89
|
});
|
|
91
90
|
}
|
|
92
91
|
}
|
|
@@ -245,7 +244,7 @@ export class SpecGenerator {
|
|
|
245
244
|
} else if (body.type === Readable || body.type === Buffer) {
|
|
246
245
|
return {
|
|
247
246
|
content: {
|
|
248
|
-
[mime ?? 'application/octet-stream']: { type: 'string', format: 'binary' }
|
|
247
|
+
[mime ?? 'application/octet-stream']: { schema: { type: 'string', format: 'binary' } }
|
|
249
248
|
},
|
|
250
249
|
description: ''
|
|
251
250
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DependencyRegistry } from '@travetto/di';
|
|
2
2
|
import { RootRegistry } from '@travetto/registry';
|
|
3
3
|
|
|
4
|
-
import { OpenApiService } from '
|
|
4
|
+
import { OpenApiService } from '../../src/service';
|
|
5
5
|
|
|
6
6
|
export async function main(): Promise<unknown> {
|
|
7
7
|
await RootRegistry.init();
|
|
@@ -2,7 +2,7 @@ import fs from 'fs/promises';
|
|
|
2
2
|
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
3
3
|
import cp from 'child_process';
|
|
4
4
|
|
|
5
|
-
import { path } from '@travetto/manifest';
|
|
5
|
+
import { path, RootIndex } from '@travetto/manifest';
|
|
6
6
|
import { ExecUtil, FileResourceProvider } from '@travetto/base';
|
|
7
7
|
import { CliCommand, cliTpl, OptionConfig, ListOptionConfig } from '@travetto/cli';
|
|
8
8
|
|
|
@@ -20,7 +20,7 @@ type Options = {
|
|
|
20
20
|
export class OpenApiClientCommand extends CliCommand<Options> {
|
|
21
21
|
#presets: Record<string, [string, object] | [string]>;
|
|
22
22
|
name = 'openapi:client';
|
|
23
|
-
#resources = new FileResourceProvider(['@travetto/openapi']);
|
|
23
|
+
#resources = new FileResourceProvider(['@travetto/openapi#support/resources']);
|
|
24
24
|
|
|
25
25
|
async getPresets(): Promise<Record<string, [string, object] | [string]>> {
|
|
26
26
|
if (!this.#presets) {
|
|
@@ -35,7 +35,7 @@ export class OpenApiClientCommand extends CliCommand<Options> {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
getListOfFormats(): string[] {
|
|
38
|
-
const formatCache = path.resolve('
|
|
38
|
+
const formatCache = path.resolve(RootIndex.manifest.workspacePath, RootIndex.manifest.outputFolder, 'trv-openapi-formats.json');
|
|
39
39
|
if (!existsSync(formatCache)) {
|
|
40
40
|
const stdout = cp.execSync(`docker run --rm ${this.cmd.dockerImage} list`, { stdio: ['pipe', 'pipe'], encoding: 'utf8' }).trim();
|
|
41
41
|
const lines = stdout
|
|
@@ -84,12 +84,12 @@ ${this.getListOfFormats().map(x => cliTpl`* ${{ input: x }}`).join('\n')} `;
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
getArgs(): string {
|
|
87
|
-
return '
|
|
87
|
+
return '<format-or-preset>';
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
async action(format: string): Promise<void> {
|
|
91
91
|
if (!format) {
|
|
92
|
-
return this.showHelp(
|
|
92
|
+
return this.showHelp('Format is required');
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
// Ensure its there
|
|
@@ -24,7 +24,11 @@ export class OpenApiSpecCommand extends CliCommand<Options> {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
async action(): Promise<void> {
|
|
27
|
-
const result = await ExecUtil.worker(
|
|
27
|
+
const result = await ExecUtil.worker(
|
|
28
|
+
RootIndex.resolveFileImport('@travetto/cli/support/cli.ts'),
|
|
29
|
+
['main', '@travetto/openapi/support/bin/generate.ts'],
|
|
30
|
+
{ env: { TRV_OPENAPI_OUTPUT: this.cmd.output, TRV_OPENAPI_PERSIST: '1' } }
|
|
31
|
+
).message;
|
|
28
32
|
|
|
29
33
|
if (this.cmd.output === '-' || !this.cmd.output) {
|
|
30
34
|
console.log!(result);
|