Skip to content

Commit

Permalink
Upgrade typegen
Browse files Browse the repository at this point in the history
  • Loading branch information
anttiviljami committed Sep 10, 2024
1 parent 7dc53ee commit 9ff8b71
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ EXAMPLES
$ openapi auth --security BasicAuth --username admin --password password
```

_See code: [src/commands/auth.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/auth.ts)_
_See code: [src/commands/auth.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/auth.ts)_

## `openapi call [DEFINITION]`

Expand Down Expand Up @@ -149,7 +149,7 @@ EXAMPLES
$ openapi call -o createPet -d '{ "name": "Garfield" }'
```

_See code: [src/commands/call.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/call.ts)_
_See code: [src/commands/call.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/call.ts)_

## `openapi help [COMMAND]`

Expand Down Expand Up @@ -213,7 +213,7 @@ EXAMPLES
$ openapi info ./openapi.yml
```

_See code: [src/commands/info.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/info.ts)_
_See code: [src/commands/info.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/info.ts)_

## `openapi init`

Expand Down Expand Up @@ -246,7 +246,7 @@ EXAMPLES
$ openapi init --title 'My API' > openapi.yml
```

_See code: [src/commands/init.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/init.ts)_
_See code: [src/commands/init.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/init.ts)_

## `openapi load DEFINITION`

Expand All @@ -273,7 +273,7 @@ EXAMPLES
$ openapi load https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml
```

_See code: [src/commands/load.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/load.ts)_
_See code: [src/commands/load.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/load.ts)_

## `openapi mock [DEFINITION]`

Expand Down Expand Up @@ -312,7 +312,7 @@ EXAMPLES
$ openapi mock https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml
```

_See code: [src/commands/mock.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/mock.ts)_
_See code: [src/commands/mock.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/mock.ts)_

## `openapi read [DEFINITION]`

Expand Down Expand Up @@ -357,7 +357,7 @@ EXAMPLES
$ openapi read ./openapi.yml -f json > openapi.json
```

_See code: [src/commands/read.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/read.ts)_
_See code: [src/commands/read.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/read.ts)_

## `openapi redoc [DEFINITION]`

Expand Down Expand Up @@ -397,7 +397,7 @@ EXAMPLES
$ openapi redoc ./openapi.yml --bundle outDir
```

_See code: [src/commands/redoc.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/redoc.ts)_
_See code: [src/commands/redoc.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/redoc.ts)_

## `openapi swagger-editor [DEFINITION]`

Expand Down Expand Up @@ -425,7 +425,7 @@ EXAMPLES
$ openapi swagger-editor ./openapi.yml
```

_See code: [src/commands/swagger-editor.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/swagger-editor.ts)_
_See code: [src/commands/swagger-editor.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/swagger-editor.ts)_

## `openapi swagger-ui [DEFINITION]`

Expand Down Expand Up @@ -476,7 +476,7 @@ EXAMPLES
$ openapi swagger-ui ./openapi.yml --bundle outDir
```

_See code: [src/commands/swagger-ui.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/swagger-ui.ts)_
_See code: [src/commands/swagger-ui.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/swagger-ui.ts)_

## `openapi swagger2openapi [DEFINITION]`

Expand Down Expand Up @@ -519,7 +519,7 @@ EXAMPLES
$ openapi swagger2openapi --yaml ./swagger.json > openapi.yml
```

_See code: [src/commands/swagger2openapi.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/swagger2openapi.ts)_
_See code: [src/commands/swagger2openapi.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/swagger2openapi.ts)_

## `openapi test`

Expand Down Expand Up @@ -565,7 +565,7 @@ EXAMPLES
$ openapi test -o getPets
```

_See code: [src/commands/test/index.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/test/index.ts)_
_See code: [src/commands/test/index.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/test/index.ts)_

## `openapi test add [DEFINITION]`

Expand Down Expand Up @@ -620,7 +620,7 @@ EXAMPLES
$ openapi test add -o getPet --checks all
```

_See code: [src/commands/test/add.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/test/add.ts)_
_See code: [src/commands/test/add.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/test/add.ts)_

## `openapi typegen [DEFINITION]`

Expand All @@ -629,12 +629,15 @@ Generate types from openapi definition
```
USAGE
$ openapi typegen [DEFINITION] [-h] [-D] [-B] [-R /] [-H <value>...] [-V] [-S http://localhost:9000...] [-I
{"info":{"version":"1.0.0"}}...] [-E x-internal] [-C default|all|openapi_client_axios|openapi_backend] [-U]
{"info":{"version":"1.0.0"}}...] [-E x-internal] [-C default|all|openapi_client_axios|openapi_backend] [-U] [-b
<value>] [-A]
ARGUMENTS
DEFINITION input definition file
FLAGS
-A, --[no-]type-aliases Generate module level type aliases for schema components
defined in spec
-B, --bundle resolve remote $ref pointers
-C, --strip=default|all|openapi_client_axios|openapi_backend Strip optional metadata such as examples and
descriptions from definition
Expand All @@ -649,6 +652,8 @@ FLAGS
component being removed by setting x-openapicmd-keep to
true
-V, --validate validate against openapi schema
-b, --banner=<value> include a banner comment at the top of the generated
file
-h, --help Show CLI help.
DESCRIPTION
Expand All @@ -658,7 +663,7 @@ EXAMPLES
$ openapi typegen ./openapi.yml > openapi.d.ts
```

_See code: [src/commands/typegen.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/typegen.ts)_
_See code: [src/commands/typegen.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/typegen.ts)_

## `openapi unload`

Expand All @@ -678,7 +683,7 @@ EXAMPLES
$ openapi unload
```

_See code: [src/commands/unload.ts](https://github.com/openapistack/openapicmd/blob/v2.4.0/src/commands/unload.ts)_
_See code: [src/commands/unload.ts](https://github.com/openapistack/openapicmd/blob/v2.5.0/src/commands/unload.ts)_
<!-- commandsstop -->

## Commercial support
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"koa-static": "^5.0.0",
"openapi-backend": "^5.6.2",
"openapi-client-axios": "^7.5.5",
"openapi-client-axios-typegen": "^7.6.2",
"openapi-client-axios-typegen": "^7.7.0",
"swagger-editor-dist": "^4.11.2",
"swagger-ui-dist": "^5.9.0",
"swagger2openapi": "^7.0.8",
Expand Down
7 changes: 7 additions & 0 deletions src/commands/typegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ describe('typegen', () => {
expect(ctx.stdout).to.match(/getPetById/);
});

test
.stdout()
.command(['typegen', resourcePath('openapi.json'), '-A'])
.it('generates module level schema aliases', (ctx) => {
expect(ctx.stdout).to.match(/export type Pet = Components.Schemas.Pet/);
});

test
.stdout()
.command(['typegen', resourcePath('openapi.json')])
Expand Down
26 changes: 23 additions & 3 deletions src/commands/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Flags } from '@oclif/core';
import { Command, Args } from '@oclif/core';
import { parseDefinition, resolveDefinition } from '../common/definition';
import * as commonFlags from '../common/flags';
Expand All @@ -14,6 +15,16 @@ export class Typegen extends Command {
public static flags = {
...commonFlags.help(),
...commonFlags.parseOpts(),
banner: Flags.string({
char: 'b',
description: 'include a banner comment at the top of the generated file'
}),
['type-aliases']: Flags.boolean({
char: 'A',
description: 'Generate module level type aliases for schema components defined in spec',
default: true,
allowNo: true,
}),
};

public static args = {
Expand Down Expand Up @@ -50,11 +61,20 @@ export class Typegen extends Command {
this.error(err, { exit: 1 });
}

const [imports, ...restTypes] = await generateTypesForDocument(document, { transformOperationName: (name) => name });
const [imports, schemaTypes, operationTypings, banner, aliases] = await generateTypesForDocument(document, { transformOperationName: (name) => name });

if (flags.banner && banner) {
this.log(banner + '\n');
}

this.log([
imports + '\n',
...restTypes,
imports,
schemaTypes,
operationTypings,
].join('\n'));

if (flags['type-aliases'] && aliases) {
this.log(`\n${aliases}`);
}
}
}

0 comments on commit 9ff8b71

Please sign in to comment.