-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathgenerateServerClient.ts
More file actions
101 lines (88 loc) · 3.32 KB
/
generateServerClient.ts
File metadata and controls
101 lines (88 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
CommonPublicClientOptions,
DefaultCommonClientOptions,
V6ClientSSRCookies,
V6ClientSSRRequest,
generateClientWithAmplifyInstance,
} from 'aws-amplify/api/internals';
import { AmplifyError } from 'aws-amplify/adapter-core/internals';
import { parseAmplifyConfig } from 'aws-amplify/utils';
import { NextServer } from '../types';
import { createServerRunnerForAPI } from './createServerRunnerForAPI';
interface CookiesClientParams {
cookies: NextServer.ServerComponentContext['cookies'];
config: NextServer.CreateServerRunnerInput['config'];
}
interface ReqClientParams {
config: NextServer.CreateServerRunnerInput['config'];
}
/**
* Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering
*
* @example
* import { cookies } from "next/headers"
*
* const client = generateServerClientUsingCookies({ cookies });
* const result = await client.graphql({ query: listPosts });
*/
export function generateServerClientUsingCookies<
T extends Record<any, any> = never,
Options extends CommonPublicClientOptions &
CookiesClientParams = DefaultCommonClientOptions & CookiesClientParams,
>(options: Options): V6ClientSSRCookies<T, Options> {
if (typeof options.cookies !== 'function') {
throw new AmplifyError({
name: 'InvalidCookiesError',
message:
'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',
recoverySuggestion:
'Use `generateServerClient` inside of `runWithAmplifyServerContext` with the `request` object.',
});
}
const { runWithAmplifyServerContext, resourcesConfig } =
createServerRunnerForAPI({ config: options.config });
// This function reference gets passed down to InternalGraphQLAPI.ts.graphql
// where this._graphql is passed in as the `fn` argument
// causing it to always get invoked inside `runWithAmplifyServerContext`
const getAmplify = (fn: (amplify: any) => Promise<any>) =>
runWithAmplifyServerContext({
nextServerContext: { cookies: options.cookies },
operation: contextSpec => fn(contextSpec),
});
const { cookies: _cookies, config: _config, ...params } = options;
return generateClientWithAmplifyInstance<T, V6ClientSSRCookies<T, Options>>({
amplify: getAmplify,
config: resourcesConfig,
...params,
});
}
/**
* Generates an API client that can be used with both Pages Router and App Router
*
* @example
* import config from './amplifyconfiguration.json';
* import { listPosts } from './graphql/queries';
*
* const client = generateServerClientUsingReqRes({ config });
*
* const result = await runWithAmplifyServerContext({
* nextServerContext: { request, response },
* operation: (contextSpec) => client.graphql(contextSpec, {
* query: listPosts,
* }),
* });
*/
export function generateServerClientUsingReqRes<
T extends Record<any, any> = never,
Options extends CommonPublicClientOptions &
ReqClientParams = DefaultCommonClientOptions & ReqClientParams,
>(options: Options): V6ClientSSRRequest<T, Options> {
const amplifyConfig = parseAmplifyConfig(options.config);
const { config: _config, ...params } = options;
return generateClientWithAmplifyInstance<T, V6ClientSSRRequest<T, Options>>({
config: amplifyConfig,
...params,
});
}