@aws/nx-plugin 0.26.0 → 0.28.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/package.json +1 -1
- package/src/api-connection/generator.js +0 -2
- package/src/api-connection/generator.js.map +1 -1
- package/src/api-connection/schema.d.ts +0 -1
- package/src/api-connection/schema.json +0 -7
- package/src/cloudscape-website/app/generator.js +1 -0
- package/src/cloudscape-website/app/generator.js.map +1 -1
- package/src/infra/app/__snapshots__/generator.spec.ts.snap +9 -0
- package/src/infra/app/generator.js +1 -0
- package/src/infra/app/generator.js.map +1 -1
- package/src/py/fast-api/__snapshots__/generator.spec.ts.snap +31 -30
- package/src/py/fast-api/generator.js +3 -0
- package/src/py/fast-api/generator.js.map +1 -1
- package/src/py/fast-api/react/__snapshots__/generator.spec.ts.snap +68 -1
- package/src/py/fast-api/react/files/website/components/__apiNameClassName__Provider.tsx.template +27 -1
- package/src/py/fast-api/react/generator.js +7 -17
- package/src/py/fast-api/react/generator.js.map +1 -1
- package/src/py/fast-api/react/schema.d.ts +0 -1
- package/src/py/fast-api/react/schema.json +0 -7
- package/src/py/fast-api/schema.d.ts +1 -0
- package/src/py/fast-api/schema.json +9 -1
- package/src/py/project/generator.js +1 -0
- package/src/py/project/generator.js.map +1 -1
- package/src/py/project/schema.json +7 -2
- package/src/trpc/backend/__snapshots__/generator.spec.ts.snap +680 -84
- package/src/trpc/backend/files/backend/src/client/index.ts.template +22 -2
- package/src/trpc/backend/files/backend/src/middleware/index.ts.template +2 -3
- package/src/trpc/backend/files/backend/src/router.ts.template +2 -3
- package/src/trpc/backend/generator.js +21 -5
- package/src/trpc/backend/generator.js.map +1 -1
- package/src/trpc/backend/schema.d.ts +2 -1
- package/src/trpc/backend/schema.json +10 -2
- package/src/trpc/react/__snapshots__/generator.spec.ts.snap +186 -73
- package/src/trpc/react/files/src/components/__apiNameClassName__ClientProvider.tsx.template +54 -0
- package/src/trpc/react/files/src/hooks/use__apiNameClassName__.tsx.template +2 -2
- package/src/trpc/react/generator.js +13 -69
- package/src/trpc/react/generator.js.map +1 -1
- package/src/trpc/react/schema.d.ts +0 -1
- package/src/trpc/react/schema.json +0 -7
- package/src/ts/lib/__snapshots__/generator.spec.ts.snap +9 -0
- package/src/ts/lib/generator.js +1 -0
- package/src/ts/lib/generator.js.map +1 -1
- package/src/ts/lib/schema.json +7 -2
- package/src/ts/mcp-server/__snapshots__/generator.spec.ts.snap +3 -0
- package/src/ts/mcp-server/generator.js +1 -0
- package/src/ts/mcp-server/generator.js.map +1 -1
- package/src/ts/mcp-server/schema.json +5 -0
- package/src/utils/api-constructs/api-constructs.d.ts +1 -0
- package/src/utils/api-constructs/api-constructs.js.map +1 -1
- package/src/utils/api-constructs/files/app/apis/http/__apiNameKebabCase__.ts.template +53 -2
- package/src/utils/api-constructs/files/app/apis/rest/__apiNameKebabCase__.ts.template +57 -2
- package/src/utils/api-constructs/files/core/api/http/http-api.ts.template +1 -15
- package/src/utils/api-constructs/files/core/api/rest/rest-api.ts.template +0 -14
- package/src/utils/nx.d.ts +4 -0
- package/src/utils/nx.js +9 -1
- package/src/utils/nx.js.map +1 -1
- package/src/api-connection/README.md +0 -0
- package/src/cloudscape-website/app/README.md +0 -289
- package/src/cloudscape-website/cognito-auth/README.md +0 -193
- package/src/infra/app/README.md +0 -200
- package/src/license/README.md +0 -290
- package/src/trpc/backend/README.md +0 -571
- package/src/trpc/backend/files/backend/src/client/sigv4.ts.template +0 -9
- package/src/trpc/react/README.md +0 -330
- package/src/trpc/react/files/src/components/TrpcClients/TrpcApis.tsx.template +0 -1
- package/src/trpc/react/files/src/components/TrpcClients/TrpcClientProviders.tsx.template +0 -10
- package/src/trpc/react/files/src/components/TrpcClients/TrpcProvider.tsx.template +0 -55
- package/src/trpc/react/files/src/components/TrpcClients/index.tsx.template +0 -5
package/src/trpc/react/README.md
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
# tRPC React Generator
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
|
|
5
|
-
This generator adds tRPC client integration to your React application, enabling type-safe API calls to your tRPC backend. It sets up all necessary configuration for connecting to your tRPC backends, including AWS IAM authentication support and proper error handling. The integration provides full end-to-end type safety between your frontend and tRPC backend(s).
|
|
6
|
-
|
|
7
|
-
## Prerequisites
|
|
8
|
-
|
|
9
|
-
Before using this generator, ensure your React application has:
|
|
10
|
-
|
|
11
|
-
1. A `main.tsx` file that renders your application
|
|
12
|
-
2. An `<App/>` JSX element where the tRPC provider will be automatically injected
|
|
13
|
-
3. A working tRPC backend (generated using the tRPC backend generator)
|
|
14
|
-
|
|
15
|
-
Example of required `main.tsx` structure:
|
|
16
|
-
|
|
17
|
-
```tsx
|
|
18
|
-
import { StrictMode } from 'react';
|
|
19
|
-
import * as ReactDOM from 'react-dom/client';
|
|
20
|
-
import App from './app/app';
|
|
21
|
-
|
|
22
|
-
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
|
|
23
|
-
root.render(
|
|
24
|
-
<StrictMode>
|
|
25
|
-
<App />
|
|
26
|
-
</StrictMode>,
|
|
27
|
-
);
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Usage
|
|
31
|
-
|
|
32
|
-
You can generate the tRPC React integration in two ways:
|
|
33
|
-
|
|
34
|
-
### 1. Using VSCode IDE
|
|
35
|
-
|
|
36
|
-
First, install the NX Console extension for VSCode:
|
|
37
|
-
|
|
38
|
-
1. Open VSCode
|
|
39
|
-
2. Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
|
|
40
|
-
3. Search for "Nx Console"
|
|
41
|
-
4. Install [Nx Console](https://marketplace.visualstudio.com/items?itemName=nrwl.angular-console)
|
|
42
|
-
|
|
43
|
-
Then add tRPC to your React application:
|
|
44
|
-
|
|
45
|
-
1. Open the NX Console in VSCode
|
|
46
|
-
2. Click on "Generate"
|
|
47
|
-
3. Search for "api-connection"
|
|
48
|
-
4. Fill in the required parameters in the form
|
|
49
|
-
5. Click "Run"
|
|
50
|
-
|
|
51
|
-
### 2. Using CLI
|
|
52
|
-
|
|
53
|
-
Add tRPC to your React application:
|
|
54
|
-
|
|
55
|
-
```bash
|
|
56
|
-
nx g @aws/nx-plugin:api-connection --sourceProject=my-app --targetProject=my-api --auth=IAM
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
You can also perform a dry-run to see what files would be generated without actually creating them:
|
|
60
|
-
|
|
61
|
-
```bash
|
|
62
|
-
nx g @aws/nx-plugin:api-connection --sourceProject=my-app --targetProject=my-api --auth=IAM --dry-run
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Both methods will add tRPC client integration to your React application with all the necessary configuration.
|
|
66
|
-
|
|
67
|
-
### Input Parameters
|
|
68
|
-
|
|
69
|
-
| Parameter | Type | Default | Description |
|
|
70
|
-
| --------------- | ------ | ------- | ------------------------------------------------------ |
|
|
71
|
-
| sourceProject\* | string | - | The name of your React application project (required). |
|
|
72
|
-
| targetProject\* | string | - | The name of your tRPC backend project (required). |
|
|
73
|
-
| auth\* | string | "IAM" | Authentication strategy. Options: "IAM", "None" |
|
|
74
|
-
|
|
75
|
-
\*Required parameter
|
|
76
|
-
|
|
77
|
-
## Expected Output
|
|
78
|
-
|
|
79
|
-
The generator creates the following structure in your React application:
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
src/
|
|
83
|
-
├── components/
|
|
84
|
-
│ └── TrpcClients/
|
|
85
|
-
│ └── index.tsx
|
|
86
|
-
│ └── IsolatedTrpcProvider.tsx # Supports add connections to multiple tRPC APIs
|
|
87
|
-
│ └── TrpcApis.tsx # Object containing all of your trpc API connections
|
|
88
|
-
│ └── TrpcClientProviders.tsx # Sets up the trpc clients and bindings to your backend schema(s)
|
|
89
|
-
└── hooks/
|
|
90
|
-
└── useSigV4.tsx # Custom hook for signing HTTP(s) requests with SigV4 (IAM only)
|
|
91
|
-
└── use<ApiName>.tsx # Adds a hook for the given backend API. ApiName will resolve to the name of the api.
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
Additionally, it:
|
|
95
|
-
|
|
96
|
-
1. Installs required dependencies:
|
|
97
|
-
- @trpc/client
|
|
98
|
-
- @trpc/tanstack-react-query
|
|
99
|
-
- @tanstack/react-query
|
|
100
|
-
- aws4fetch (if using IAM auth)
|
|
101
|
-
|
|
102
|
-
## Using the Generated Code
|
|
103
|
-
|
|
104
|
-
### Using the tRPC Hook
|
|
105
|
-
|
|
106
|
-
The generator provides a `use<ApiName>` hook that gives you access to the type-safe tRPC client:
|
|
107
|
-
|
|
108
|
-
```tsx
|
|
109
|
-
import { useQuery, useMutation } from '@tanstack/react-query';
|
|
110
|
-
import { useMyApi } from './hooks/useMyApi';
|
|
111
|
-
|
|
112
|
-
function MyComponent() {
|
|
113
|
-
const trpc = useMyApi();
|
|
114
|
-
|
|
115
|
-
// Example query
|
|
116
|
-
const { data, isLoading, error } = useQuery(trpc.users.list.queryOptions());
|
|
117
|
-
|
|
118
|
-
// Example mutation
|
|
119
|
-
const mutation = useMutation(trpc.users.create.mutationOptions());
|
|
120
|
-
|
|
121
|
-
const handleCreate = () => {
|
|
122
|
-
mutation.mutate({
|
|
123
|
-
name: 'John Doe',
|
|
124
|
-
email: 'john@example.com',
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
if (isLoading) return <div>Loading...</div>;
|
|
129
|
-
|
|
130
|
-
return (
|
|
131
|
-
<ul>
|
|
132
|
-
{data.map((user) => (
|
|
133
|
-
<li key={user.id}>{user.name}</li>
|
|
134
|
-
))}
|
|
135
|
-
</ul>
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
### Error Handling
|
|
141
|
-
|
|
142
|
-
The integration includes built-in error handling that properly processes tRPC errors:
|
|
143
|
-
|
|
144
|
-
```tsx
|
|
145
|
-
function MyComponent() {
|
|
146
|
-
const trpc = useMyApi();
|
|
147
|
-
|
|
148
|
-
const { data, error } = useQuery(trpc.users.list.queryOptions());
|
|
149
|
-
|
|
150
|
-
if (error) {
|
|
151
|
-
return (
|
|
152
|
-
<div>
|
|
153
|
-
<h2>Error occurred:</h2>
|
|
154
|
-
<p>{error.message}</p>
|
|
155
|
-
{error.data?.code && <p>Code: {error.data.code}</p>}
|
|
156
|
-
</div>
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return (
|
|
161
|
-
<ul>
|
|
162
|
-
{data.map((user) => (
|
|
163
|
-
<li key={user.id}>{user.name}</li>
|
|
164
|
-
))}
|
|
165
|
-
</ul>
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
## Best Practices
|
|
171
|
-
|
|
172
|
-
### 1. Handle Loading States
|
|
173
|
-
|
|
174
|
-
Always handle loading and error states for a better user experience:
|
|
175
|
-
|
|
176
|
-
```tsx
|
|
177
|
-
function UserList() {
|
|
178
|
-
const trpc = useMyApi();
|
|
179
|
-
|
|
180
|
-
const users = useQuery(trpc.users.list.queryOptions());
|
|
181
|
-
|
|
182
|
-
if (users.isLoading) {
|
|
183
|
-
return <LoadingSpinner />;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (users.error) {
|
|
187
|
-
return <ErrorMessage error={users.error} />;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return (
|
|
191
|
-
<ul>
|
|
192
|
-
{users.data.map((user) => (
|
|
193
|
-
<li key={user.id}>{user.name}</li>
|
|
194
|
-
))}
|
|
195
|
-
</ul>
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### 2. Optimistic Updates
|
|
201
|
-
|
|
202
|
-
Use optimistic updates for a better user experience:
|
|
203
|
-
|
|
204
|
-
```tsx
|
|
205
|
-
import { useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
|
|
206
|
-
|
|
207
|
-
function UserList() {
|
|
208
|
-
const trpc = useMyApi();
|
|
209
|
-
const users = useQuery(trpc.users.list.queryOptions());
|
|
210
|
-
const queryClient = useQueryClient();
|
|
211
|
-
|
|
212
|
-
const deleteMutation = useMutation(
|
|
213
|
-
trpc.users.delete.mutationOptions({
|
|
214
|
-
onMutate: async (userId) => {
|
|
215
|
-
// Cancel outgoing fetches
|
|
216
|
-
await queryClient.cancelQueries(trpc.users.list.queryFilter());
|
|
217
|
-
|
|
218
|
-
// Get snapshot of current data
|
|
219
|
-
const previousUsers = queryClient.getQueryData(trpc.users.list.queryKey());
|
|
220
|
-
|
|
221
|
-
// Optimistically remove the user
|
|
222
|
-
queryClient.setQueryData(trpc.users.list.queryKey(), (old) => old?.filter((user) => user.id !== userId));
|
|
223
|
-
|
|
224
|
-
return { previousUsers };
|
|
225
|
-
},
|
|
226
|
-
onError: (err, userId, context) => {
|
|
227
|
-
// Restore previous data on error
|
|
228
|
-
queryClient.setQueryData(trpc.users.list.queryKey(), context?.previousUsers);
|
|
229
|
-
},
|
|
230
|
-
}),
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
return (
|
|
234
|
-
<ul>
|
|
235
|
-
{users.map((user) => (
|
|
236
|
-
<li key={user.id}>
|
|
237
|
-
{user.name}
|
|
238
|
-
<button onClick={() => deleteMutation.mutate(user.id)}>Delete</button>
|
|
239
|
-
</li>
|
|
240
|
-
))}
|
|
241
|
-
</ul>
|
|
242
|
-
);
|
|
243
|
-
}
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
### 3. Prefetching Data
|
|
247
|
-
|
|
248
|
-
Prefetch data for better performance:
|
|
249
|
-
|
|
250
|
-
```tsx
|
|
251
|
-
function UserList() {
|
|
252
|
-
const trpc = useMyApi();
|
|
253
|
-
const users = useQuery(trpc.users.list.queryOptions());
|
|
254
|
-
const queryClient = useQueryClient();
|
|
255
|
-
|
|
256
|
-
// Prefetch user details on hover
|
|
257
|
-
const prefetchUser = async (userId: string) => {
|
|
258
|
-
await queryClient.prefetchQuery(trpc.users.getById.queryOptions(userId));
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
return (
|
|
262
|
-
<ul>
|
|
263
|
-
{users.map((user) => (
|
|
264
|
-
<li key={user.id} onMouseEnter={() => prefetchUser(user.id)}>
|
|
265
|
-
<Link to={`/users/${user.id}`}>{user.name}</Link>
|
|
266
|
-
</li>
|
|
267
|
-
))}
|
|
268
|
-
</ul>
|
|
269
|
-
);
|
|
270
|
-
}
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
### 4. Infinite Queries
|
|
274
|
-
|
|
275
|
-
Handle pagination with infinite queries:
|
|
276
|
-
|
|
277
|
-
```tsx
|
|
278
|
-
function UserList() {
|
|
279
|
-
const trpc = useMyApi();
|
|
280
|
-
|
|
281
|
-
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery(
|
|
282
|
-
trpc.users.list.infiniteQueryOptions(
|
|
283
|
-
{ limit: 10 },
|
|
284
|
-
{
|
|
285
|
-
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
286
|
-
},
|
|
287
|
-
),
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
return (
|
|
291
|
-
<div>
|
|
292
|
-
{data?.pages.map((page) => page.users.map((user) => <UserCard key={user.id} user={user} />))}
|
|
293
|
-
|
|
294
|
-
{hasNextPage && (
|
|
295
|
-
<button onClick={() => fetchNextPage()} disabled={isFetchingNextPage}>
|
|
296
|
-
{isFetchingNextPage ? 'Loading...' : 'Load More'}
|
|
297
|
-
</button>
|
|
298
|
-
)}
|
|
299
|
-
</div>
|
|
300
|
-
);
|
|
301
|
-
}
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
It is important to note that infinite queries can only be used for procedures with an input property named `cursor`.
|
|
305
|
-
|
|
306
|
-
## Type Safety
|
|
307
|
-
|
|
308
|
-
The integration provides complete end-to-end type safety. Your IDE will provide full autocompletion and type checking for all your API calls:
|
|
309
|
-
|
|
310
|
-
```tsx
|
|
311
|
-
function UserForm() {
|
|
312
|
-
const trpc = useMyApi();
|
|
313
|
-
|
|
314
|
-
// ✅ Input is fully typed
|
|
315
|
-
const createUser = trpc.users.create.useMutation();
|
|
316
|
-
|
|
317
|
-
const handleSubmit = (data: CreateUserInput) => {
|
|
318
|
-
// ✅ Type error if input doesn't match schema
|
|
319
|
-
createUser.mutate(data);
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
return <form onSubmit={handleSubmit}>{/* ... */}</form>;
|
|
323
|
-
}
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
The types are automatically inferred from your backend's router and schema definitions, ensuring that any changes to your API are immediately reflected in your frontend code without the need to build.
|
|
327
|
-
|
|
328
|
-
## More Information
|
|
329
|
-
|
|
330
|
-
For more information, please refer to the [tRPC TanStack React Query documentation](https://trpc.io/docs/client/tanstack-react-query/usage).
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default {}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
-
import { createTRPCContext } from '@trpc/tanstack-react-query';
|
|
3
|
-
import {
|
|
4
|
-
httpLink,
|
|
5
|
-
HTTPLinkOptions,
|
|
6
|
-
createTRPCClient,
|
|
7
|
-
} from '@trpc/client';
|
|
8
|
-
import {
|
|
9
|
-
FC,
|
|
10
|
-
useMemo,
|
|
11
|
-
PropsWithChildren,
|
|
12
|
-
} from 'react';
|
|
13
|
-
<% if(auth === 'IAM') { %> import { useSigV4 } from '../../hooks/useSigV4'; <% } %>
|
|
14
|
-
import { AnyTRPCRouter } from '@trpc/server';
|
|
15
|
-
|
|
16
|
-
export interface TrpcClientProviderProps extends PropsWithChildren {
|
|
17
|
-
readonly apiUrl: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const createTrpcClientProvider = <
|
|
21
|
-
TAppRouter extends AnyTRPCRouter,
|
|
22
|
-
TContext
|
|
23
|
-
>() => {
|
|
24
|
-
const { TRPCProvider, useTRPC } = createTRPCContext<TAppRouter>();
|
|
25
|
-
|
|
26
|
-
const TrpcClientProvider: FC<TrpcClientProviderProps> = ({
|
|
27
|
-
apiUrl,
|
|
28
|
-
children,
|
|
29
|
-
}) => {
|
|
30
|
-
const queryClient = useQueryClient();
|
|
31
|
-
<% if(auth === 'IAM') { %> const sigv4Client = useSigV4(); <% } %>
|
|
32
|
-
|
|
33
|
-
const trpcClient = useMemo(() => {
|
|
34
|
-
const linkOptions: HTTPLinkOptions<any> = {
|
|
35
|
-
url: apiUrl,
|
|
36
|
-
<% if(auth === 'IAM') { %> fetch: sigv4Client <% } %>
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
return createTRPCClient({
|
|
40
|
-
links: [httpLink(linkOptions)],
|
|
41
|
-
});
|
|
42
|
-
}, [apiUrl<% if(auth === 'IAM') { %>, sigv4Client<% } %>]);
|
|
43
|
-
|
|
44
|
-
return (
|
|
45
|
-
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
|
|
46
|
-
{children}
|
|
47
|
-
</TRPCProvider>
|
|
48
|
-
);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
useTRPC,
|
|
53
|
-
Provider: TrpcClientProvider,
|
|
54
|
-
};
|
|
55
|
-
};
|