@aws/nx-plugin 0.14.2 → 0.15.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.
Files changed (34) hide show
  1. package/package.json +2 -2
  2. package/src/open-api/ts-client/__snapshots__/generator.additional-properties.spec.ts.snap +2236 -0
  3. package/src/open-api/ts-client/__snapshots__/generator.complex-types.spec.ts.snap +2307 -0
  4. package/src/open-api/ts-client/__snapshots__/generator.composite-types.spec.ts.snap +1495 -0
  5. package/src/open-api/ts-client/__snapshots__/generator.primitive-types.spec.ts.snap +1470 -0
  6. package/src/open-api/ts-client/__snapshots__/generator.request.spec.ts.snap +1138 -0
  7. package/src/open-api/ts-client/__snapshots__/generator.response.spec.ts.snap +732 -0
  8. package/src/open-api/ts-client/__snapshots__/generator.tags.spec.ts.snap +743 -0
  9. package/src/open-api/ts-client/files/client.gen.ts.template +52 -15
  10. package/src/open-api/ts-client/files/types.gen.ts.template +5 -0
  11. package/src/open-api/ts-hooks/__snapshots__/generator.spec.tsx.snap +1092 -0
  12. package/src/open-api/ts-hooks/files/options-proxy.gen.ts.template +210 -0
  13. package/src/open-api/ts-hooks/generator.d.ts +5 -0
  14. package/src/open-api/ts-hooks/generator.js +15 -2
  15. package/src/open-api/ts-hooks/generator.js.map +1 -1
  16. package/src/open-api/ts-hooks/generator.spec.tsx +1787 -0
  17. package/src/open-api/utils/codegen-data/types.d.ts +25 -0
  18. package/src/open-api/utils/codegen-data/types.js +26 -1
  19. package/src/open-api/utils/codegen-data/types.js.map +1 -1
  20. package/src/open-api/utils/codegen-data.js +187 -79
  21. package/src/open-api/utils/codegen-data.js.map +1 -1
  22. package/src/open-api/utils/normalise.js +11 -1
  23. package/src/open-api/utils/normalise.js.map +1 -1
  24. package/src/py/fast-api/react/__snapshots__/generator.spec.ts.snap +120 -10
  25. package/src/py/fast-api/react/files/website/components/__apiNameClassName__Provider.tsx.template +40 -0
  26. package/src/py/fast-api/react/files/website/hooks/use__apiNameClassName__.tsx.template +13 -18
  27. package/src/py/fast-api/react/files/website/hooks/use__apiNameClassName__Client.tsx.template +13 -0
  28. package/src/py/fast-api/react/generator.js +35 -9
  29. package/src/py/fast-api/react/generator.js.map +1 -1
  30. package/src/py/project/generator.js +5 -0
  31. package/src/py/project/generator.js.map +1 -1
  32. package/src/trpc/backend/__snapshots__/generator.spec.ts.snap +7 -9
  33. package/src/utils/files/http-api/common/constructs/src/core/http-api.ts.template +7 -9
  34. package/src/open-api/ts-client/__snapshots__/generator.spec.ts.snap +0 -7880
@@ -0,0 +1,210 @@
1
+ import type {
2
+ <%_ if (allOperations.some(op => op.isQuery)) { _%>
3
+ QueryFilters,
4
+ UseQueryOptions,
5
+ <%_ } _%>
6
+ <%_ if (allOperations.some(op => op.isInfiniteQuery)) { _%>
7
+ UseInfiniteQueryOptions,
8
+ InfiniteData,
9
+ <%_ } _%>
10
+ <%_ if (allOperations.some(op => op.isMutation)) { _%>
11
+ UseMutationOptions,
12
+ <%_ } _%>
13
+ <%_ if (allOperations.some(op => op.isStreaming)) { _%>
14
+ QueryFunctionContext,
15
+ <%_ } _%>
16
+ } from '@tanstack/react-query';
17
+ <%_ if ((models.length + allOperations.length) > 0) { _%>
18
+ import type {
19
+ <%_ allOperations.filter(p => p.parameters.length > 0).forEach((op) => { _%>
20
+ <%- op.operationIdPascalCase %>Request,
21
+ <%_ }); _%>
22
+ <%_ allOperations.forEach((op) => { _%>
23
+ <%- op.operationIdPascalCase %>Error,
24
+ <%_ }); _%>
25
+ <%_ const uniq = (arr) => {
26
+ const set = new Set();
27
+ return arr.filter((item) => {
28
+ if (set.has(item)) {
29
+ return false;
30
+ }
31
+ set.add(item);
32
+ return true;
33
+ });
34
+ } _%>
35
+ <%_ const modelsByName = Object.fromEntries(models.map(m => [m.name, m])); _%>
36
+ <%_ uniq(allOperations.flatMap((op) => op.result && modelsByName[op.result.type] ? [op.result.typescriptType] : [])).forEach((returnTypeModel) => { _%>
37
+ <%- returnTypeModel %>,
38
+ <%_ }); _%>
39
+ } from './types.gen.js';
40
+ <%_ } _%>
41
+ import { <%- className %> } from './client.gen.js';
42
+
43
+ export interface <%- className %>OptionsProxyConfig {
44
+ client: <%- className %>;
45
+ }
46
+
47
+ export class <%- className %>OptionsProxy {
48
+ private $client: <%- className %>;
49
+
50
+ constructor({ client }: <%- className %>OptionsProxyConfig) {
51
+ this.$client = client;
52
+
53
+ <%_ allOperations.forEach((op) => { _%>
54
+ <%_ if (op.isQuery) { _%>
55
+ this._<%- op.uniqueName %>QueryKey = this._<%- op.uniqueName %>QueryKey.bind(this);
56
+ this._<%- op.uniqueName %>QueryOptions = this._<%- op.uniqueName %>QueryOptions.bind(this);
57
+ this._<%- op.uniqueName %>QueryFilter = this._<%- op.uniqueName %>QueryFilter.bind(this);
58
+ <%_ } _%>
59
+ <%_ if (op.isInfiniteQuery) { _%>
60
+ this._<%- op.uniqueName %>InfiniteQueryOptions = this._<%- op.uniqueName %>InfiniteQueryOptions.bind(this);
61
+ <%_ } _%>
62
+ <%_ if (op.isMutation) { _%>
63
+ this._<%- op.uniqueName %>MutationKey = this._<%- op.uniqueName %>MutationKey.bind(this);
64
+ this._<%- op.uniqueName %>MutationOptions = this._<%- op.uniqueName %>MutationOptions.bind(this);
65
+ <%_ } _%>
66
+ <%_ }); _%>
67
+ }
68
+
69
+ <%_ if (allOperations.some(op => op.isStreaming)) { _%>
70
+ protected async $queryStream<T>(context: QueryFunctionContext<any>, stream: AsyncIterable<T>): Promise<T[]> {
71
+ const query = context.client
72
+ .getQueryCache()
73
+ .find({ queryKey: context.queryKey, exact: true });
74
+
75
+ if (query && query.state.data !== undefined) {
76
+ query.setState({
77
+ status: 'pending',
78
+ data: undefined,
79
+ error: null,
80
+ fetchStatus: 'fetching',
81
+ });
82
+ }
83
+
84
+ const chunks: T[] = [];
85
+ for await (const chunk of stream) {
86
+ if (context.signal.aborted) {
87
+ break;
88
+ }
89
+ chunks.push(chunk);
90
+ query?.setState({
91
+ status: 'success',
92
+ fetchStatus: 'fetching',
93
+ });
94
+ context.client.setQueryData<T[]>(context.queryKey, (prev = []) =>
95
+ prev.concat(chunk),
96
+ );
97
+ }
98
+ query?.setState({
99
+ fetchStatus: 'idle',
100
+ });
101
+ return chunks;
102
+ }
103
+
104
+ protected async $waitForStream<T>(iterable: AsyncIterable<T>) {
105
+ const chunks: T[] = [];
106
+ for await (const chunk of iterable) {
107
+ chunks.push(chunk);
108
+ }
109
+ return chunks;
110
+ }
111
+ <%_ } _%>
112
+
113
+ public queryKey = () => ['<%- className %>'];
114
+
115
+ <%_ allOperations.forEach((op) => { _%>
116
+ <%_ const hasTag = op.tags && op.tags.length > 0; _%>
117
+ <%_ const isInputOptional = (op.parameters.length === 1 && op.parametersBody && !op.parametersBody.isRequired) || op.parameters.length === 0; _%>
118
+ <%_ const input = op.parameters.length > 0 ? `input${isInputOptional ? '?' : ''}: ${op.operationIdPascalCase}Request` : ''; _%>
119
+ <%_ const isStreaming = op.isStreaming; _%>
120
+ <%_ const resultType = op.result ? op.result.typescriptType : 'void'; _%>
121
+ <%_ const queryResultType = `${resultType}${isStreaming ? '[]' : ''}`; _%>
122
+ <%_ const mutationResultType = isStreaming ? `AsyncIterableIterator<${resultType}>` : resultType; _%>
123
+ <%_ const errorType = `${op.operationIdPascalCase}Error`; _%>
124
+ <%_ if (op.isQuery) { _%>
125
+ <%_ const queryOptionsType = `UseQueryOptions<${queryResultType}, ${errorType}>`; _%>
126
+ private _<%- op.uniqueName %>QueryKey(<%- input %>) {
127
+ return [...this.queryKey(), '<%- op.uniqueName %>'<% if (op.parameters.length > 0) { %>, input<% } %>];
128
+ }
129
+ private _<%- op.uniqueName %>QueryOptions(<%- input %><% if (op.parameters.length > 0) { %>, <% } %>options?: Omit<<%- queryOptionsType %>, 'queryFn' | 'queryKey'> & Partial<Pick<<%- queryOptionsType %>, 'queryFn' | 'queryKey'>>): <%- queryOptionsType %> {
130
+ return {
131
+ queryFn: (<% if (isStreaming) { %>context<% } %>) => <% if (isStreaming) { %>this.$queryStream(context, <% } %><% if (hasTag) { %>(this.$client as any)._<% } else { %>this.$client.<% } %><%- op.uniqueName %>(<% if (op.parameters.length > 0) { %>input<% } %>)<% if (isStreaming) { %>)<% } %>,
132
+ queryKey: this._<%- op.uniqueName %>QueryKey(<% if (op.parameters.length > 0) { %>input<% } %>),
133
+ ...options,
134
+ };
135
+ }
136
+ private _<%- op.uniqueName %>QueryFilter(<%- input %><% if (op.parameters.length > 0) { %>, <% } %>filter?: QueryFilters<<%- queryResultType %>, <%- errorType %>>): QueryFilters<<%- queryResultType %>, <%- errorType %>> {
137
+ return {
138
+ queryKey: this._<%- op.uniqueName %>QueryKey(<% if (op.parameters.length > 0) { %>input<% } %>),
139
+ ...filter,
140
+ };
141
+ }
142
+ <%_ } _%>
143
+ <%_ if (op.isInfiniteQuery) { _%>
144
+ <%_ const cursorType = `${op.infiniteQueryCursorProperty.typescriptType}${op.infiniteQueryCursorProperty.isRequired ? '' : ' | undefined'}${op.infiniteQueryCursorProperty.isNullable && op.infiniteQueryCursorProperty.type !== 'null' ? '' : ' | null'}`; _%>
145
+ <%_ const infiniteQueryOptionsType = `UseInfiniteQueryOptions<${queryResultType}, ${errorType}, InfiniteData<${queryResultType}>, ${queryResultType}, unknown[], ${cursorType}>`; _%>
146
+ <%_ emptyInitialPageParam = !op.infiniteQueryCursorProperty.isRequired ? 'undefined' : (op.infiniteQueryCursorProperty.isNullable ? 'null' : undefined); _%>
147
+ private _<%- op.uniqueName %>InfiniteQueryOptions(<%- input %><% if (op.parameters.length > 0) { %>, <% } %>options: Omit<<%- infiniteQueryOptionsType %>, 'queryFn' | 'queryKey'<%- emptyInitialPageParam ? ` | 'initialPageParam'` : '' %>> & Partial<Pick<<%- infiniteQueryOptionsType %>, 'queryFn' | 'queryKey'<%- emptyInitialPageParam ? ` | 'initialPageParam'` : '' %>>>): <%- infiniteQueryOptionsType %> {
148
+ return {
149
+ queryKey: this._<%- op.uniqueName %>QueryKey(<% if (op.parameters.length > 0) { %>input<% } %>),
150
+ queryFn: ({ pageParam }) => <% if (isStreaming) { %>this.$waitForStream(<% } %><% if (hasTag) { %>(this.$client as any)._<% } else { %>this.$client.<% } %><%- op.uniqueName %>({ ...input, <%- op.infiniteQueryCursorProperty.typescriptName %>: pageParam as any })<% if (isStreaming) { %>)<% } %>,
151
+ <%_ if (emptyInitialPageParam) { _%>
152
+ initialPageParam: <%- emptyInitialPageParam %>,
153
+ <%_ } _%>
154
+ ...options,
155
+ };
156
+ }
157
+ <%_ } _%>
158
+ <%_ if (op.isMutation) { _%>
159
+ private _<%- op.uniqueName %>MutationKey() {
160
+ return [...this.queryKey(), '<%- op.uniqueName %>'];
161
+ }
162
+ <%_ const mutationOptionsType = `UseMutationOptions<${mutationResultType}, ${errorType}, ${op.parameters.length > 0 ? `${op.operationIdPascalCase}Request` : 'void'}>` _%>
163
+ private _<%- op.uniqueName %>MutationOptions(options?: <%- mutationOptionsType %>): <%- mutationOptionsType %> {
164
+ return {
165
+ mutationFn: <% if (isStreaming) { %>async <% } %>(<% if (op.parameters.length > 0) { %>input<% } %>) => <% if (hasTag) { %>(this.$client as any)._<% } else { %>this.$client.<% } %><%- op.uniqueName %>(<% if (op.parameters.length > 0) { %>input<% } %>),
166
+ mutationKey: this._<%- op.uniqueName %>MutationKey(),
167
+ ...options,
168
+ };
169
+ }
170
+ <%_ } _%>
171
+ <%_ }); _%>
172
+
173
+ <%_ Object.entries(operationsByTag).forEach(([tag, operations]) => { _%>
174
+ public <%- tag %> = {
175
+ <%_ operations.forEach((op) => { _%>
176
+ <%- op.name %>: {
177
+ <%_ if (op.isQuery) { _%>
178
+ queryKey: this._<%- op.uniqueName %>QueryKey.bind(this),
179
+ queryOptions: this._<%- op.uniqueName %>QueryOptions.bind(this),
180
+ queryFilter: this._<%- op.uniqueName %>QueryFilter.bind(this),
181
+ <%_ } _%>
182
+ <%_ if (op.isInfiniteQuery) { _%>
183
+ infiniteQueryOptions: this._<%- op.uniqueName %>InfiniteQueryOptions.bind(this),
184
+ <%_ } _%>
185
+ <%_ if (op.isMutation) { _%>
186
+ mutationKey: this._<%- op.uniqueName %>MutationKey.bind(this),
187
+ mutationOptions: this._<%- op.uniqueName %>MutationOptions.bind(this),
188
+ <%_ } _%>
189
+ },
190
+ <%_ }); _%>
191
+ };
192
+ <%_ }); _%>
193
+
194
+ <%_ untaggedOperations.forEach((op) => { _%>
195
+ public <%- op.name %> = {
196
+ <%_ if (op.isQuery) { _%>
197
+ queryKey: this._<%- op.uniqueName %>QueryKey.bind(this),
198
+ queryOptions: this._<%- op.uniqueName %>QueryOptions.bind(this),
199
+ queryFilter: this._<%- op.uniqueName %>QueryFilter.bind(this),
200
+ <%_ } _%>
201
+ <%_ if (op.isInfiniteQuery) { _%>
202
+ infiniteQueryOptions: this._<%- op.uniqueName %>InfiniteQueryOptions.bind(this),
203
+ <%_ } _%>
204
+ <%_ if (op.isMutation) { _%>
205
+ mutationKey: this._<%- op.uniqueName %>MutationKey.bind(this),
206
+ mutationOptions: this._<%- op.uniqueName %>MutationOptions.bind(this),
207
+ <%_ } _%>
208
+ };
209
+ <%_ }); _%>
210
+ }
@@ -4,8 +4,13 @@
4
4
  */
5
5
  import { Tree } from '@nx/devkit';
6
6
  import { OpenApiHooksSchema } from './schema';
7
+ import { CodeGenData } from '../utils/codegen-data/types';
7
8
  /**
8
9
  * Generates typescript hooks from an openapi spec
9
10
  */
10
11
  export declare const openApiTsHooksGenerator: (tree: Tree, options: OpenApiHooksSchema) => Promise<void>;
12
+ /**
13
+ * Generate OpenAPI typescript hooks in the target directory
14
+ */
15
+ export declare const generateOpenApiTsHooks: (tree: Tree, data: CodeGenData, outputPath: string) => void;
11
16
  export default openApiTsHooksGenerator;
@@ -1,11 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.openApiTsHooksGenerator = void 0;
3
+ exports.generateOpenApiTsHooks = exports.openApiTsHooksGenerator = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ /**
6
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
7
+ * SPDX-License-Identifier: Apache-2.0
8
+ */
9
+ const devkit_1 = require("@nx/devkit");
5
10
  const parse_1 = require("../utils/parse");
6
11
  const codegen_data_1 = require("../utils/codegen-data");
7
12
  const generator_1 = require("../ts-client/generator");
8
13
  const format_1 = require("../../utils/format");
14
+ const path_1 = tslib_1.__importDefault(require("path"));
9
15
  /**
10
16
  * Generates typescript hooks from an openapi spec
11
17
  */
@@ -13,9 +19,16 @@ const openApiTsHooksGenerator = (tree, options) => tslib_1.__awaiter(void 0, voi
13
19
  const spec = yield (0, parse_1.parseOpenApiSpec)(tree, options.openApiSpecPath);
14
20
  const data = yield (0, codegen_data_1.buildOpenApiCodeGenData)(spec);
15
21
  (0, generator_1.generateOpenApiTsClient)(tree, data, options.outputPath);
16
- // TODO: generate hooks which wrap the client
22
+ (0, exports.generateOpenApiTsHooks)(tree, data, options.outputPath);
17
23
  yield (0, format_1.formatFilesInSubtree)(tree);
18
24
  });
19
25
  exports.openApiTsHooksGenerator = openApiTsHooksGenerator;
26
+ /**
27
+ * Generate OpenAPI typescript hooks in the target directory
28
+ */
29
+ const generateOpenApiTsHooks = (tree, data, outputPath) => {
30
+ (0, devkit_1.generateFiles)(tree, path_1.default.join(__dirname, 'files'), outputPath, data);
31
+ };
32
+ exports.generateOpenApiTsHooks = generateOpenApiTsHooks;
20
33
  exports.default = exports.openApiTsHooksGenerator;
21
34
  //# sourceMappingURL=generator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"generator.js","sourceRoot":"","sources":["../../../../../../packages/nx-plugin/src/open-api/ts-hooks/generator.ts"],"names":[],"mappings":";;;;AAMA,0CAAkD;AAClD,wDAAgE;AAChE,sDAAiE;AACjE,+CAA0D;AAE1D;;GAEG;AACI,MAAM,uBAAuB,GAAG,CACrC,IAAU,EACV,OAA2B,EAC3B,EAAE;IACF,MAAM,IAAI,GAAG,MAAM,IAAA,wBAAgB,EAAC,IAAI,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;IAEnE,MAAM,IAAI,GAAG,MAAM,IAAA,sCAAuB,EAAC,IAAI,CAAC,CAAC;IAEjD,IAAA,mCAAuB,EAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAExD,6CAA6C;IAE7C,MAAM,IAAA,6BAAoB,EAAC,IAAI,CAAC,CAAC;AACnC,CAAC,CAAA,CAAC;AAbW,QAAA,uBAAuB,2BAalC;AAEF,kBAAe,+BAAuB,CAAC"}
1
+ {"version":3,"file":"generator.js","sourceRoot":"","sources":["../../../../../../packages/nx-plugin/src/open-api/ts-hooks/generator.ts"],"names":[],"mappings":";;;;AAAA;;;GAGG;AACH,uCAAiD;AAEjD,0CAAkD;AAClD,wDAAgE;AAChE,sDAAiE;AACjE,+CAA0D;AAC1D,wDAAwB;AAGxB;;GAEG;AACI,MAAM,uBAAuB,GAAG,CACrC,IAAU,EACV,OAA2B,EAC3B,EAAE;IACF,MAAM,IAAI,GAAG,MAAM,IAAA,wBAAgB,EAAC,IAAI,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;IAEnE,MAAM,IAAI,GAAG,MAAM,IAAA,sCAAuB,EAAC,IAAI,CAAC,CAAC;IAEjD,IAAA,mCAAuB,EAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACxD,IAAA,8BAAsB,EAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAEvD,MAAM,IAAA,6BAAoB,EAAC,IAAI,CAAC,CAAC;AACnC,CAAC,CAAA,CAAC;AAZW,QAAA,uBAAuB,2BAYlC;AAEF;;GAEG;AACI,MAAM,sBAAsB,GAAG,CACpC,IAAU,EACV,IAAiB,EACjB,UAAkB,EAClB,EAAE;IACF,IAAA,sBAAa,EAAC,IAAI,EAAE,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;AACvE,CAAC,CAAC;AANW,QAAA,sBAAsB,0BAMjC;AAEF,kBAAe,+BAAuB,CAAC"}