@povio/openapi-codegen-cli 0.9.2 → 0.10.1

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,6 +1,6 @@
1
1
  # OpenAPI code generation CLI
2
2
 
3
- **NOTE:** This CLI tool is primarily designed for use within our organization. The generated code output aligns with our internal template. If you are using this tool without our internal template, make sure to use it in standalone mode.
3
+ **NOTE:** This CLI tool is primarily designed for use within our organization. The generated code output aligns with our internal template. If you are using this tool without our internal template, make sure to use it in **standalone** mode.
4
4
 
5
5
  Use this tool to generate code (Zod schemas, TypeScript types, API definitions, and React queries) from an OpenAPI v3 specification. API definitions are generated to use a REST client wrapper that utilizes Axios. React queries are generated in alignment with our code standards, without the need for explicit types.
6
6
 
@@ -36,7 +36,10 @@ yarn openapi-codegen generate --input http://localhost:3001/docs-json --standalo
36
36
 
37
37
  --splitByTags Organize output into separate folders based on OpenAPI operation tags (default: true)
38
38
  --defaultTag (Requires `--splitByTags`) Default tag for shared code across multiple tags (default: 'Common')
39
- --excludeTags (Requires `--splitByTags`) Comma-separated list of tags to exclude from generation
39
+
40
+ --excludeTags Comma-separated list of tags to exclude from generation
41
+ --excludePathRegex Exclude operations whose paths match the given regular expression
42
+ --excludeRedundantZodSchemas Exclude any redundant Zod schemas (default: true)
40
43
 
41
44
  --tsNamespaces Wrap generated files in TypeScript namespaces (default: true)
42
45
  --importPath Module import style for generated files (default: 'ts'; options: 'ts' | 'relative' | 'absolute')
@@ -50,6 +53,9 @@ yarn openapi-codegen generate --input http://localhost:3001/docs-json --standalo
50
53
 
51
54
  --standalone Generate any missing supporting classes/types, e.g., REST client class, React Query type extensions, etc. (default: false)
52
55
  --baseUrl (Requires `--standalone`) Base URL for the REST client; falls back to the OpenAPI spec if not provided
56
+
57
+ # Command args designed specifically for use within our organization
58
+ --monorepo Configure output structure and imports adjusted for monorepo architecture
53
59
  ```
54
60
 
55
61
  #### Check command (checks if OpenAPI spec is compliant)
@@ -60,7 +66,10 @@ yarn openapi-codegen generate --input http://localhost:3001/docs-json --standalo
60
66
 
61
67
  --splitByTags Organize output into separate folders based on OpenAPI operation tags (default: true)
62
68
  --defaultTag (Requires `--splitByTags`) Default tag for shared code across multiple tags (default: 'Common')
63
- --excludeTags (Requires `--splitByTags`) Comma-separated list of tags to exclude from generation
69
+
70
+ --excludeTags Comma-separated list of tags to exclude from generation
71
+ --excludePathRegex Exclude operations whose paths match the given regular expression
72
+ --excludeRedundantZodSchemas Exclude any redundant Zod schemas (default: true)
64
73
  ```
65
74
 
66
75
  ## Development
@@ -86,3 +95,78 @@ yarn build
86
95
  yarn start --help
87
96
  yarn start:dist generate --input ./test/petstore.yaml --verbose
88
97
  ```
98
+
99
+ ## Common Issues
100
+
101
+ ### Enums
102
+
103
+ If you're using Enums in your backend DTOs with `@Expose()` and `@IsEnum`, they may still not appear correctly in the OpenAPI schema unless you also provide both `enum` **and** `enumName` to `@ApiProperty`.
104
+
105
+ ```ts
106
+ enum Status {
107
+ ACTIVE = "active",
108
+ INACTIVE = "inactive",
109
+ }
110
+
111
+ export class ExampleDto {
112
+ @ApiProperty({ enum: Status, enumName: "Status" })
113
+ @Expose()
114
+ @IsEnum(Status)
115
+ status: Status;
116
+ }
117
+ ```
118
+
119
+ ```ts
120
+ enum Status {
121
+ ACTIVE = "active",
122
+ INACTIVE = "inactive",
123
+ }
124
+
125
+ export class ExampleDto {
126
+ @ApiProperty({ enum: Status, enumName: "Status", isArray: true })
127
+ @Expose()
128
+ @IsEnum(Status, { each: true })
129
+ @IsArray()
130
+ status: Status[];
131
+ }
132
+ ```
133
+
134
+ ---
135
+
136
+ ### Nested objects
137
+
138
+ When using nested DTOs, ensure you explicitly specify the type using `@ApiProperty({ type: NestedDto })`:
139
+
140
+ ```ts
141
+ export class NestedDto {
142
+ @ApiProperty()
143
+ @Expose()
144
+ name: string;
145
+ }
146
+
147
+ export class ParentDto {
148
+ @ApiProperty({ type: NestedDto })
149
+ @Expose()
150
+ @ValidateNested()
151
+ @Type(() => NestedDto)
152
+ @IsObject()
153
+ nested: NestedDto;
154
+ }
155
+ ```
156
+
157
+ ```ts
158
+ export class NestedDto {
159
+ @ApiProperty()
160
+ @Expose()
161
+ name: string;
162
+ }
163
+
164
+ export class ParentDto {
165
+ @ApiProperty({ type: NestedDto, isArray: true })
166
+ @Expose()
167
+ @ValidateNested({ each: true })
168
+ @Type(() => NestedDto)
169
+ @IsArray()
170
+ nestedList: NestedDto[];
171
+ }
172
+ ```
@@ -1,7 +1,8 @@
1
1
  import { GenerateOptions } from "src/generators/types/options";
2
2
  export type GenerateParams = {
3
3
  excludeTags: string;
4
+ monorepo: boolean;
4
5
  prettier: boolean;
5
6
  verbose: boolean;
6
7
  } & Pick<GenerateOptions, "input" | "output" | "tsNamespaces" | "splitByTags" | "defaultTag" | "removeOperationPrefixEndingWith" | "importPath" | "extractEnums" | "standalone" | "baseUrl" | "replaceOptionalWithNullish" | "infiniteQueries" | "axiosRequestConfig" | "invalidateQueryOptions">;
7
- export declare function generate({ input, excludeTags, prettier, verbose, ...params }: GenerateParams): Promise<void>;
8
+ export declare function generate({ input, excludeTags, monorepo, prettier, verbose, ...params }: GenerateParams): Promise<void>;
@@ -7,8 +7,10 @@ export declare const QUERY_OPTIONS_TYPES: {
7
7
  };
8
8
  export declare const TEMPLATE_DATA_FILE_PATH = "src/data";
9
9
  export declare const TEMPLATE_DATA_TS_PATH = "@/data";
10
- export declare const TEMPLATE_IMPORT_PATH_APP_REST_CLIENT = "@/util/rest/clients/app-rest-client";
11
- export declare const TEMPLATE_IMPORT_PATH_QUERY_TYPES = "@/types/react-query";
10
+ export declare const TEMPLATE_IMPORTS: Record<string, {
11
+ template: string;
12
+ monorepoTemplate: string;
13
+ }>;
12
14
  export declare enum StandaloneAssetEnum {
13
15
  ReactQueryTypes = "reactQueryTypes",
14
16
  RestClient = "restClient",
@@ -4,6 +4,13 @@ import { GenerateOptions } from "../types/options";
4
4
  import { ValidationError } from "../types/validation";
5
5
  import { DependencyGraph } from "./openapi/getOpenAPISchemaDependencyGraph";
6
6
  import { ZodSchema } from "./zod/ZodSchema.class";
7
+ interface SchemaData {
8
+ ref: string;
9
+ name: string;
10
+ zodSchemaName: string;
11
+ tags: string[];
12
+ deepRefOperations: OperationObject[];
13
+ }
7
14
  export interface EnumZodSchemaData {
8
15
  code: string;
9
16
  zodSchemaName: string;
@@ -36,6 +43,7 @@ export declare class SchemaResolver {
36
43
  private get schemaRefs();
37
44
  constructor(openApiDoc: OpenAPIV3.Document, options: GenerateOptions);
38
45
  getSchemaByRef(ref: string): OpenAPIV3.SchemaObject;
46
+ getSchemaDataByName(name: string): SchemaData | undefined;
39
47
  getZodSchemaNameByRef(ref: string): string;
40
48
  getRefByZodSchemaName(zodSchemaName: string): string | undefined;
41
49
  getTagByZodSchemaName(zodSchemaName: string): string;
@@ -60,3 +68,4 @@ export declare class SchemaResolver {
60
68
  private initialize;
61
69
  private handleDuplicateEnumZodSchemas;
62
70
  }
71
+ export {};
@@ -0,0 +1,2 @@
1
+ import { SchemaResolver } from "../../SchemaResolver.class";
2
+ export declare function resolveExtractedEnumZodSchemaNames(resolver: SchemaResolver): void;
@@ -0,0 +1,2 @@
1
+ import { SchemaResolver } from "../../SchemaResolver.class";
2
+ export declare function resolveExtractedEnumZodSchemaTags(resolver: SchemaResolver): void;
@@ -1,5 +1,5 @@
1
1
  import { OpenAPIV3 } from "openapi-types";
2
- import { SchemaResolver } from "../SchemaResolver.class";
2
+ import { SchemaResolver } from "../../SchemaResolver.class";
3
3
  export declare function updateExtractedEnumZodSchemaData({ schema, nameSegments, includeSelf, ...params }: {
4
4
  resolver: SchemaResolver;
5
5
  schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined;
@@ -9,5 +9,3 @@ export declare function updateExtractedEnumZodSchemaData({ schema, nameSegments,
9
9
  nameSegments?: string[];
10
10
  includeSelf?: boolean;
11
11
  }): void;
12
- export declare function resolveExtractedEnumZodSchemaNames(resolver: SchemaResolver): void;
13
- export declare function resolveExtractedEnumZodSchemaTags(resolver: SchemaResolver): void;
@@ -1,7 +1,5 @@
1
- import { OpenAPIV3 } from "openapi-types";
2
1
  import { ALLOWED_PARAM_MEDIA_TYPES } from "../const/openapi.const";
3
- import { OperationObject, ParameterObject, PrimitiveType, SingleType, SortingParameterObject } from "../types/openapi";
4
- import { GenerateOptions } from "../types/options";
2
+ import { ParameterObject, PrimitiveType, SingleType, SortingParameterObject } from "../types/openapi";
5
3
  export declare const getSchemaRef: (schemaName: string) => string;
6
4
  export declare const autocorrectRef: (ref: string) => string;
7
5
  export declare const getSchemaNameByRef: (ref: string) => string;
@@ -16,24 +14,10 @@ export declare function isParamMediaTypeAllowed(mediaType: string): mediaType is
16
14
  export declare function isMainResponseStatus(status: number): boolean;
17
15
  export declare function isErrorStatus(status: number): boolean;
18
16
  export declare function isMediaTypeAllowed(mediaType: string): boolean;
19
- export declare function getOperationName({ path, method, operation, options, tag, keepOperationTag, keepOperationPrefix, }: {
20
- path: string;
21
- method: string;
22
- operation: OperationObject;
23
- options: GenerateOptions;
24
- tag: string;
25
- keepOperationTag?: boolean;
26
- keepOperationPrefix?: boolean;
27
- }): string;
28
- export declare function getUniqueOperationName({ operationsByTag, ...params }: {
29
- path: string;
30
- method: string;
31
- operation: OperationObject;
32
- operationsByTag: Record<string, OperationObject[]>;
33
- options: GenerateOptions;
34
- }): string;
35
- export declare function getUniqueOperationNamesWithoutSplitByTags(openApiDoc: OpenAPIV3.Document, operationsByTag: Record<string, OperationObject[]>, options: GenerateOptions): string[];
36
17
  /** @example turns `/media-objects/{id}` into `MediaObjectsById` */
37
18
  export declare function pathToVariableName(path: string): string;
38
19
  export declare function replaceHyphenatedPath(path: string): string;
39
20
  export declare const isSortingParameterObject: (param: ParameterObject) => param is SortingParameterObject;
21
+ export declare const isPathExcluded: (path: string, options: {
22
+ excludePathRegex: string;
23
+ }) => boolean;
@@ -0,0 +1,22 @@
1
+ import { OpenAPIV3 } from "openapi-types";
2
+ import { OperationObject } from "../types/openapi";
3
+ import { GenerateOptions } from "../types/options";
4
+ export declare function isOperationExcluded(operation: OperationObject, options: GenerateOptions): boolean;
5
+ export declare function getOperationName({ path, method, operation, options, tag, keepOperationTag, keepOperationPrefix, }: {
6
+ path: string;
7
+ method: string;
8
+ operation: OperationObject;
9
+ options: GenerateOptions;
10
+ tag: string;
11
+ keepOperationTag?: boolean;
12
+ keepOperationPrefix?: boolean;
13
+ }): string;
14
+ export declare function getUniqueOperationName({ operationsByTag, ...params }: {
15
+ path: string;
16
+ method: string;
17
+ operation: OperationObject;
18
+ operationsByTag: Record<string, OperationObject[]>;
19
+ options: GenerateOptions;
20
+ }): string;
21
+ export declare function getUniqueOperationNamesWithoutSplitByTags(openApiDoc: OpenAPIV3.Document, operationsByTag: Record<string, OperationObject[]>, options: GenerateOptions): string[];
22
+ export declare function getOperationsByTag(openApiDoc: OpenAPIV3.Document, options: GenerateOptions): Record<string, OperationObject[]>;
@@ -1,6 +1,5 @@
1
- import { OpenAPIV3 } from "openapi-types";
2
1
  import { OperationObject } from "../types/openapi";
3
2
  import { GenerateOptions } from "../types/options";
4
3
  export declare function formatTag(tag: string): string;
5
4
  export declare function getOperationTag(operation: OperationObject, options: GenerateOptions): string;
6
- export declare function getOperationsByTag(openApiDoc: OpenAPIV3.Document, options: GenerateOptions): Record<string, OperationObject[]>;
5
+ export declare function isTagExcluded(tag: string, options: GenerateOptions): boolean;