@bitstack/ng-query-codegen-openapi 0.0.30

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.
Files changed (45) hide show
  1. package/README.md +77 -0
  2. package/lib/bin/cli.mjs +186 -0
  3. package/lib/bin/cli.mjs.map +1 -0
  4. package/lib/index.d.mts +191 -0
  5. package/lib/index.d.ts +191 -0
  6. package/lib/index.js +1392 -0
  7. package/lib/index.js.map +1 -0
  8. package/lib/index.mjs +1376 -0
  9. package/lib/index.mjs.map +1 -0
  10. package/package.json +91 -0
  11. package/src/bin/cli.ts +77 -0
  12. package/src/bin/utils.ts +85 -0
  13. package/src/generators/api-service-generator.ts +112 -0
  14. package/src/generators/cache-keys-generator.ts +43 -0
  15. package/src/generators/common-types-generator.ts +33 -0
  16. package/src/generators/component-schema-generator.ts +25 -0
  17. package/src/generators/do-not-modify-generator.ts +27 -0
  18. package/src/generators/index-generator.ts +11 -0
  19. package/src/generators/query-service-generator.ts +108 -0
  20. package/src/generators/types-generator.ts +285 -0
  21. package/src/generators/utils-generator.ts +33 -0
  22. package/src/index.ts +21 -0
  23. package/src/services/api-code-generator.ts +157 -0
  24. package/src/services/api-service-generator.ts +24 -0
  25. package/src/services/endpoint-info-extractor.ts +119 -0
  26. package/src/services/file-writer-service.ts +148 -0
  27. package/src/services/group-service.ts +84 -0
  28. package/src/services/openapi-parser-service.ts +72 -0
  29. package/src/services/openapi-service.ts +61 -0
  30. package/src/services/query-code-generator.ts +24 -0
  31. package/src/services/unified-code-generator.ts +353 -0
  32. package/src/types.ts +248 -0
  33. package/src/utils/capitalize.ts +3 -0
  34. package/src/utils/directory.ts +75 -0
  35. package/src/utils/downloadSchema.ts +33 -0
  36. package/src/utils/factory.ts +29 -0
  37. package/src/utils/getOperationDefinitions.ts +20 -0
  38. package/src/utils/getV3Doc.ts +24 -0
  39. package/src/utils/http.ts +86 -0
  40. package/src/utils/index.ts +9 -0
  41. package/src/utils/isQuery.ts +16 -0
  42. package/src/utils/isValidUrl.ts +9 -0
  43. package/src/utils/messages.ts +7 -0
  44. package/src/utils/prettier.ts +51 -0
  45. package/src/utils/removeUndefined.ts +3 -0
@@ -0,0 +1,191 @@
1
+ import SwaggerParser from '@apidevtools/swagger-parser';
2
+ import { OpenAPIV3 } from 'openapi-types';
3
+
4
+ /**
5
+ * 群組配置介面
6
+ */
7
+ interface GroupConfig {
8
+ outputDir: string;
9
+ groupKeyMatch: (path: string) => string | null;
10
+ filterEndpoint?: (operationName: string, path: string, groupKey: string) => boolean;
11
+ queryMatch?: (operationName: string) => boolean;
12
+ }
13
+
14
+ type OperationDefinition = {
15
+ path: string;
16
+ verb: (typeof operationKeys)[number];
17
+ pathItem: OpenAPIV3.PathItemObject;
18
+ operation: OpenAPIV3.OperationObject;
19
+ };
20
+ type ParameterDefinition = OpenAPIV3.ParameterObject;
21
+ type Require<T, K extends keyof T> = {
22
+ [k in K]-?: NonNullable<T[k]>;
23
+ } & Omit<T, K>;
24
+ type Id<T> = {
25
+ [K in keyof T]: T[K];
26
+ } & {};
27
+ type AtLeastOneKey<T> = {
28
+ [K in keyof T]-?: Pick<T, K> & Partial<T>;
29
+ }[keyof T];
30
+ declare const operationKeys: readonly ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
31
+ interface CommonOptions {
32
+ /**
33
+ * local schema file path (only supports local files)
34
+ */
35
+ schemaFile: string;
36
+ /**
37
+ * remote schema file URL (when provided, will download to schemaFile path)
38
+ */
39
+ remoteFile?: string;
40
+ /**
41
+ * Configuration for WebApiConfiguration import
42
+ * defaults to { file: "@core/api/web-api-configuration", importName: "WebApiConfiguration" }
43
+ */
44
+ apiConfiguration?: {
45
+ file: string;
46
+ importName: string;
47
+ };
48
+ /**
49
+ * HttpClient for WebApiConfiguration import
50
+ * defaults to { file: "@core/httpClient/webapi/webapi-http-client.providers", importName: "WEBAPI_HTTP_CLIENT" }
51
+ */
52
+ httpClient?: {
53
+ file: string;
54
+ importName: string;
55
+ };
56
+ /**
57
+ * defaults to "enhancedApi"
58
+ */
59
+ exportName?: string;
60
+ /**
61
+ * defaults to "Req"
62
+ */
63
+ argSuffix?: string;
64
+ /**
65
+ * defaults to "Res"
66
+ */
67
+ responseSuffix?: string;
68
+ /**
69
+ * defaults to empty
70
+ */
71
+ operationNameSuffix?: string;
72
+ /**
73
+ * defaults to `false`
74
+ * `true` will generate hooks for queries and mutations, but no lazyQueries
75
+ */
76
+ hooks?: boolean | {
77
+ queries: boolean;
78
+ lazyQueries: boolean;
79
+ mutations: boolean;
80
+ };
81
+ /**
82
+ * defaults to false
83
+ * `true` will generate a union type for `undefined` properties like: `{ id?: string | undefined }` instead of `{ id?: string }`
84
+ */
85
+ unionUndefined?: boolean;
86
+ /**
87
+ * defaults to false
88
+ * `true` will result in all generated endpoints having `providesTags`/`invalidatesTags` declarations for the `tags` of their respective operation definition
89
+ * @see https://redux-toolkit.js.org/rtk-query/usage/code-generation for more information
90
+ */
91
+ tag?: boolean;
92
+ /**
93
+ * defaults to false
94
+ * `true` will add `encodeURIComponent` to the generated path parameters
95
+ */
96
+ encodePathParams?: boolean;
97
+ /**
98
+ * defaults to false
99
+ * `true` will add `encodeURIComponent` to the generated query parameters
100
+ */
101
+ encodeQueryParams?: boolean;
102
+ /**
103
+ * defaults to false
104
+ * `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
105
+ */
106
+ flattenArg?: boolean;
107
+ /**
108
+ * default to false
109
+ * If set to `true`, the default response type will be included in the generated code for all endpoints.
110
+ * @see https://swagger.io/docs/specification/describing-responses/#default
111
+ */
112
+ includeDefault?: boolean;
113
+ /**
114
+ * default to false
115
+ * `true` will not generate separate types for read-only and write-only properties.
116
+ */
117
+ mergeReadWriteOnly?: boolean;
118
+ /**
119
+ *
120
+ * HTTPResolverOptions object that is passed to the SwaggerParser bundle function.
121
+ */
122
+ httpResolverOptions?: SwaggerParser.HTTPResolverOptions;
123
+ /**
124
+ * defaults to undefined
125
+ * If present the given file will be used as prettier config when formatting the generated code. If undefined the default prettier config
126
+ * resolution mechanism will be used.
127
+ */
128
+ prettierConfigFile?: string;
129
+ /**
130
+ * defaults to "@acrool/react-fetcher"
131
+ * File path for importing IRestFulEndpointsQueryReturn type
132
+ */
133
+ endpointsQueryReturnTypeFile?: string;
134
+ /**
135
+ * defaults to 60 (seconds)
136
+ * Number of seconds to wait before refetching data when component mounts or args change
137
+ */
138
+ refetchOnMountOrArgChange?: number;
139
+ }
140
+ type TextMatcher = string | RegExp | (string | RegExp)[];
141
+ type EndpointMatcherFunction = (operationName: string, operationDefinition: OperationDefinition) => boolean;
142
+ type EndpointMatcher = TextMatcher | EndpointMatcherFunction;
143
+ type ParameterMatcherFunction = (parameterName: string, parameterDefinition: ParameterDefinition) => boolean;
144
+ type ParameterMatcher = TextMatcher | ParameterMatcherFunction;
145
+ interface OutputFileOptions extends Partial<CommonOptions> {
146
+ outputFile: string;
147
+ filterEndpoints?: EndpointMatcher;
148
+ endpointOverrides?: EndpointOverrides[];
149
+ queryMatch?: (method: string, path: string) => boolean;
150
+ /**
151
+ * defaults to false
152
+ * If passed as true it will generate TS enums instead of union of strings
153
+ */
154
+ useEnumType?: boolean;
155
+ sharedTypesFile?: string;
156
+ /**
157
+ * groupKey for service class naming, e.g., "room" -> "RoomService"
158
+ */
159
+ groupKey?: string;
160
+ }
161
+ type EndpointOverrides = {
162
+ pattern: EndpointMatcher;
163
+ } & AtLeastOneKey<{
164
+ type: 'mutation' | 'query';
165
+ parameterFilter: ParameterMatcher;
166
+ }>;
167
+ type OutputFilesConfig = {
168
+ groupKeyMatch: (path: string) => string;
169
+ outputDir: string;
170
+ queryMatch?: (method: string, path: string) => boolean;
171
+ filterEndpoint?: (operationName: string, path: string, groupKey: string) => boolean;
172
+ };
173
+ type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
174
+ outputFiles: OutputFilesConfig;
175
+ }>;
176
+
177
+ /**
178
+ * 統一代碼生成器選項
179
+ */
180
+ interface UnifiedGenerationOptions extends CommonOptions {
181
+ outputFiles: GroupConfig;
182
+ remoteFile?: string;
183
+ }
184
+
185
+ /**
186
+ * 產生 Endpoints - 直接使用統一代碼生成器
187
+ * @param options - 端點生成選項
188
+ */
189
+ declare function generateEndpoints(options: UnifiedGenerationOptions): Promise<string | void>;
190
+
191
+ export { type ConfigFile, type OutputFilesConfig, generateEndpoints };
package/lib/index.d.ts ADDED
@@ -0,0 +1,191 @@
1
+ import SwaggerParser from '@apidevtools/swagger-parser';
2
+ import { OpenAPIV3 } from 'openapi-types';
3
+
4
+ /**
5
+ * 群組配置介面
6
+ */
7
+ interface GroupConfig {
8
+ outputDir: string;
9
+ groupKeyMatch: (path: string) => string | null;
10
+ filterEndpoint?: (operationName: string, path: string, groupKey: string) => boolean;
11
+ queryMatch?: (operationName: string) => boolean;
12
+ }
13
+
14
+ type OperationDefinition = {
15
+ path: string;
16
+ verb: (typeof operationKeys)[number];
17
+ pathItem: OpenAPIV3.PathItemObject;
18
+ operation: OpenAPIV3.OperationObject;
19
+ };
20
+ type ParameterDefinition = OpenAPIV3.ParameterObject;
21
+ type Require<T, K extends keyof T> = {
22
+ [k in K]-?: NonNullable<T[k]>;
23
+ } & Omit<T, K>;
24
+ type Id<T> = {
25
+ [K in keyof T]: T[K];
26
+ } & {};
27
+ type AtLeastOneKey<T> = {
28
+ [K in keyof T]-?: Pick<T, K> & Partial<T>;
29
+ }[keyof T];
30
+ declare const operationKeys: readonly ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
31
+ interface CommonOptions {
32
+ /**
33
+ * local schema file path (only supports local files)
34
+ */
35
+ schemaFile: string;
36
+ /**
37
+ * remote schema file URL (when provided, will download to schemaFile path)
38
+ */
39
+ remoteFile?: string;
40
+ /**
41
+ * Configuration for WebApiConfiguration import
42
+ * defaults to { file: "@core/api/web-api-configuration", importName: "WebApiConfiguration" }
43
+ */
44
+ apiConfiguration?: {
45
+ file: string;
46
+ importName: string;
47
+ };
48
+ /**
49
+ * HttpClient for WebApiConfiguration import
50
+ * defaults to { file: "@core/httpClient/webapi/webapi-http-client.providers", importName: "WEBAPI_HTTP_CLIENT" }
51
+ */
52
+ httpClient?: {
53
+ file: string;
54
+ importName: string;
55
+ };
56
+ /**
57
+ * defaults to "enhancedApi"
58
+ */
59
+ exportName?: string;
60
+ /**
61
+ * defaults to "Req"
62
+ */
63
+ argSuffix?: string;
64
+ /**
65
+ * defaults to "Res"
66
+ */
67
+ responseSuffix?: string;
68
+ /**
69
+ * defaults to empty
70
+ */
71
+ operationNameSuffix?: string;
72
+ /**
73
+ * defaults to `false`
74
+ * `true` will generate hooks for queries and mutations, but no lazyQueries
75
+ */
76
+ hooks?: boolean | {
77
+ queries: boolean;
78
+ lazyQueries: boolean;
79
+ mutations: boolean;
80
+ };
81
+ /**
82
+ * defaults to false
83
+ * `true` will generate a union type for `undefined` properties like: `{ id?: string | undefined }` instead of `{ id?: string }`
84
+ */
85
+ unionUndefined?: boolean;
86
+ /**
87
+ * defaults to false
88
+ * `true` will result in all generated endpoints having `providesTags`/`invalidatesTags` declarations for the `tags` of their respective operation definition
89
+ * @see https://redux-toolkit.js.org/rtk-query/usage/code-generation for more information
90
+ */
91
+ tag?: boolean;
92
+ /**
93
+ * defaults to false
94
+ * `true` will add `encodeURIComponent` to the generated path parameters
95
+ */
96
+ encodePathParams?: boolean;
97
+ /**
98
+ * defaults to false
99
+ * `true` will add `encodeURIComponent` to the generated query parameters
100
+ */
101
+ encodeQueryParams?: boolean;
102
+ /**
103
+ * defaults to false
104
+ * `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
105
+ */
106
+ flattenArg?: boolean;
107
+ /**
108
+ * default to false
109
+ * If set to `true`, the default response type will be included in the generated code for all endpoints.
110
+ * @see https://swagger.io/docs/specification/describing-responses/#default
111
+ */
112
+ includeDefault?: boolean;
113
+ /**
114
+ * default to false
115
+ * `true` will not generate separate types for read-only and write-only properties.
116
+ */
117
+ mergeReadWriteOnly?: boolean;
118
+ /**
119
+ *
120
+ * HTTPResolverOptions object that is passed to the SwaggerParser bundle function.
121
+ */
122
+ httpResolverOptions?: SwaggerParser.HTTPResolverOptions;
123
+ /**
124
+ * defaults to undefined
125
+ * If present the given file will be used as prettier config when formatting the generated code. If undefined the default prettier config
126
+ * resolution mechanism will be used.
127
+ */
128
+ prettierConfigFile?: string;
129
+ /**
130
+ * defaults to "@acrool/react-fetcher"
131
+ * File path for importing IRestFulEndpointsQueryReturn type
132
+ */
133
+ endpointsQueryReturnTypeFile?: string;
134
+ /**
135
+ * defaults to 60 (seconds)
136
+ * Number of seconds to wait before refetching data when component mounts or args change
137
+ */
138
+ refetchOnMountOrArgChange?: number;
139
+ }
140
+ type TextMatcher = string | RegExp | (string | RegExp)[];
141
+ type EndpointMatcherFunction = (operationName: string, operationDefinition: OperationDefinition) => boolean;
142
+ type EndpointMatcher = TextMatcher | EndpointMatcherFunction;
143
+ type ParameterMatcherFunction = (parameterName: string, parameterDefinition: ParameterDefinition) => boolean;
144
+ type ParameterMatcher = TextMatcher | ParameterMatcherFunction;
145
+ interface OutputFileOptions extends Partial<CommonOptions> {
146
+ outputFile: string;
147
+ filterEndpoints?: EndpointMatcher;
148
+ endpointOverrides?: EndpointOverrides[];
149
+ queryMatch?: (method: string, path: string) => boolean;
150
+ /**
151
+ * defaults to false
152
+ * If passed as true it will generate TS enums instead of union of strings
153
+ */
154
+ useEnumType?: boolean;
155
+ sharedTypesFile?: string;
156
+ /**
157
+ * groupKey for service class naming, e.g., "room" -> "RoomService"
158
+ */
159
+ groupKey?: string;
160
+ }
161
+ type EndpointOverrides = {
162
+ pattern: EndpointMatcher;
163
+ } & AtLeastOneKey<{
164
+ type: 'mutation' | 'query';
165
+ parameterFilter: ParameterMatcher;
166
+ }>;
167
+ type OutputFilesConfig = {
168
+ groupKeyMatch: (path: string) => string;
169
+ outputDir: string;
170
+ queryMatch?: (method: string, path: string) => boolean;
171
+ filterEndpoint?: (operationName: string, path: string, groupKey: string) => boolean;
172
+ };
173
+ type ConfigFile = Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>> | Id<Omit<CommonOptions, 'outputFile'> & {
174
+ outputFiles: OutputFilesConfig;
175
+ }>;
176
+
177
+ /**
178
+ * 統一代碼生成器選項
179
+ */
180
+ interface UnifiedGenerationOptions extends CommonOptions {
181
+ outputFiles: GroupConfig;
182
+ remoteFile?: string;
183
+ }
184
+
185
+ /**
186
+ * 產生 Endpoints - 直接使用統一代碼生成器
187
+ * @param options - 端點生成選項
188
+ */
189
+ declare function generateEndpoints(options: UnifiedGenerationOptions): Promise<string | void>;
190
+
191
+ export { type ConfigFile, type OutputFilesConfig, generateEndpoints };