Skip to content

Commit 49d645b

Browse files
committed
feat(trpc): generate a client for machine to machine api calls
Generate a client for machine to machine usage as part of trpc backend. re #49
1 parent 03eb079 commit 49d645b

File tree

8 files changed

+99
-2
lines changed

8 files changed

+99
-2
lines changed

packages/nx-plugin/src/trpc/backend/__snapshots__/generator.spec.ts.snap

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,59 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`trpc backend generator > should generate backend and schema projects > apps/test-api/backend/src/client/index.ts 1`] = `
4+
"import {
5+
createTRPCClient,
6+
httpBatchLink,
7+
httpLink,
8+
HTTPBatchLinkOptions,
9+
HTTPLinkOptions,
10+
splitLink,
11+
} from '@trpc/client';
12+
13+
import { AppRouter } from '../router.js';
14+
import { sigv4Fetch } from './sigv4.js';
15+
16+
export interface TestApiClientConfig {
17+
readonly url: string;
18+
}
19+
20+
export const createTestApiClient = (config: TestApiClientConfig) => {
21+
const linkOptions: HTTPLinkOptions<any> & HTTPBatchLinkOptions<any> = {
22+
url: config.url,
23+
fetch: sigv4Fetch,
24+
};
25+
return createTRPCClient<AppRouter>({
26+
links: [
27+
splitLink({
28+
condition(op) {
29+
return op.context.skipBatch === true;
30+
},
31+
true: httpLink(linkOptions),
32+
false: httpBatchLink(linkOptions),
33+
}),
34+
],
35+
});
36+
};
37+
"
38+
`;
39+
40+
exports[`trpc backend generator > should generate backend and schema projects > apps/test-api/backend/src/client/sigv4.ts 1`] = `
41+
"import { AwsClient } from 'aws4fetch';
42+
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
43+
44+
const credentialProvider = fromNodeProviderChain();
45+
46+
export const sigv4Fetch = (async (...args) => {
47+
const client = new AwsClient(await credentialProvider());
48+
return client.fetch(...args);
49+
}) satisfies AwsClient['fetch'];
50+
"
51+
`;
52+
353
exports[`trpc backend generator > should generate backend and schema projects > apps/test-api/backend/src/index.ts 1`] = `
454
"export type { AppRouter } from './router.js';
555
export type { Context } from './init.js';
56+
export * from './client/index.js';
657
"
758
`;
859
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
createTRPCClient,
3+
httpBatchLink,
4+
httpLink,
5+
HTTPBatchLinkOptions,
6+
HTTPLinkOptions,
7+
splitLink,
8+
} from '@trpc/client';
9+
10+
import { AppRouter } from "../router.js";
11+
import { sigv4Fetch } from './sigv4.js';
12+
13+
export interface <%= apiNameClassName %>ClientConfig {
14+
readonly url: string;
15+
}
16+
17+
export const create<%= apiNameClassName %>Client = (config: <%= apiNameClassName %>ClientConfig) => {
18+
const linkOptions: HTTPLinkOptions<any> & HTTPBatchLinkOptions<any> = {
19+
url: config.url,
20+
fetch: sigv4Fetch,
21+
};
22+
return createTRPCClient<AppRouter>({
23+
links: [
24+
splitLink({
25+
condition(op) {
26+
return op.context.skipBatch === true;
27+
},
28+
true: httpLink(linkOptions),
29+
false: httpBatchLink(linkOptions),
30+
}),
31+
],
32+
});
33+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { AwsClient } from 'aws4fetch';
2+
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
3+
4+
const credentialProvider = fromNodeProviderChain();
5+
6+
export const sigv4Fetch = (async (...args) => {
7+
const client = new AwsClient(await credentialProvider());
8+
return client.fetch(...args);
9+
}) satisfies AwsClient['fetch'];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export type { AppRouter } from './router.js';
22
export type { Context } from './init.js';
3+
export * from './client/index.js';

packages/nx-plugin/src/trpc/backend/generator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
import { getProjects, readProjectConfiguration, Tree } from '@nx/devkit';
5+
import { readProjectConfiguration, Tree } from '@nx/devkit';
66
import { trpcBackendGenerator } from './generator';
77
import {
88
createTreeUsingTsSolutionSetup,

packages/nx-plugin/src/trpc/backend/generator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ export async function trpcBackendGenerator(
211211
'@aws-lambda-powertools/metrics',
212212
'@aws-lambda-powertools/tracer',
213213
'@trpc/server',
214+
'aws4fetch',
215+
'@aws-sdk/credential-providers',
214216
]),
215217
withVersions(['@types/aws-lambda', 'tsx']),
216218
);

packages/nx-plugin/src/trpc/react/files/src/hooks/useSigV4.tsx.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ export const useSigV4 = () => {
5050
const awsClient = new AwsClient(credential);
5151
return awsClient.fetch(input, init);
5252
},
53-
[cognitoProps, user?.id_token]
53+
[cognitoProps, user?.id_token, user.profile.sub, withCachedCredentials]
5454
);
5555
};

packages/nx-plugin/src/utils/versions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const VERSIONS = {
66
'@cdklabs/cdk-validator-cfnguard': '^0.0.60',
77
'@aws-cdk/aws-cognito-identitypool-alpha': '^2.166.0-alpha.0',
88
'@aws-sdk/client-cognito-identity': '^3.721.0',
9+
'@aws-sdk/credential-providers': '^3.721.0',
910
'@aws-sdk/credential-provider-cognito-identity': '^3.721.0',
1011
'@aws-lambda-powertools/logger': '^2.11.0',
1112
'@aws-lambda-powertools/metrics': '^2.11.0',

0 commit comments

Comments
 (0)