@aws-amplify/adapter-nextjs 1.0.1 → 1.0.2-unstable.b778a9e.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.
@@ -18,7 +18,7 @@ const utils_1 = require("../utils");
18
18
  * const client = generateServerClientUsingCookies({ cookies });
19
19
  * const result = await client.graphql({ query: listPosts });
20
20
  */
21
- function generateServerClientUsingCookies({ config, cookies, }) {
21
+ function generateServerClientUsingCookies({ config, cookies, authMode, authToken, }) {
22
22
  if (typeof cookies !== 'function') {
23
23
  throw new adapter_core_1.AmplifyServerContextError({
24
24
  message: 'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',
@@ -37,6 +37,8 @@ function generateServerClientUsingCookies({ config, cookies, }) {
37
37
  return (0, internals_1.generateServerClient)({
38
38
  amplify: getAmplify,
39
39
  config: resourcesConfig,
40
+ authMode,
41
+ authToken,
40
42
  });
41
43
  }
42
44
  exports.generateServerClientUsingCookies = generateServerClientUsingCookies;
@@ -56,13 +58,15 @@ exports.generateServerClientUsingCookies = generateServerClientUsingCookies;
56
58
  * }),
57
59
  * });
58
60
  */
59
- function generateServerClientUsingReqRes({ config }) {
61
+ function generateServerClientUsingReqRes({ config, authMode, authToken }) {
60
62
  const amplifyConfig = (0, utils_1.getAmplifyConfig)(config);
61
63
  // passing `null` instance because each (future model) method must retrieve a valid instance
62
64
  // from server context
63
65
  const client = (0, internals_1.generateServerClient)({
64
66
  amplify: null,
65
67
  config: amplifyConfig,
68
+ authMode,
69
+ authToken,
66
70
  });
67
71
  // TODO: improve this and the next type
68
72
  const prevGraphql = client.graphql;
@@ -1 +1 @@
1
- {"version":3,"file":"generateServerClient.js","sources":["../../../src/api/generateServerClient.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.generateServerClientUsingReqRes = exports.generateServerClientUsingCookies = void 0;\nconst internals_1 = require(\"@aws-amplify/api/internals\");\nconst adapter_core_1 = require(\"@aws-amplify/core/internals/adapter-core\");\nconst api_graphql_1 = require(\"@aws-amplify/api-graphql\");\nconst createServerRunnerForAPI_1 = require(\"./createServerRunnerForAPI\");\nconst utils_1 = require(\"../utils\");\n/**\n * Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering\n *\n * @example\n * import { cookies } from \"next/headers\"\n *\n * const client = generateServerClientUsingCookies({ cookies });\n * const result = await client.graphql({ query: listPosts });\n */\nfunction generateServerClientUsingCookies({ config, cookies, }) {\n if (typeof cookies !== 'function') {\n throw new adapter_core_1.AmplifyServerContextError({\n message: 'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',\n // TODO: link to docs\n recoverySuggestion: 'use `generateServerClient` inside of `runWithAmplifyServerContext` with the `request` object.',\n });\n }\n const { runWithAmplifyServerContext, resourcesConfig } = (0, createServerRunnerForAPI_1.createServerRunnerForAPI)({ config });\n // This function reference gets passed down to InternalGraphQLAPI.ts.graphql\n // where this._graphql is passed in as the `fn` argument\n // causing it to always get invoked inside `runWithAmplifyServerContext`\n const getAmplify = (fn) => runWithAmplifyServerContext({\n nextServerContext: { cookies },\n operation: contextSpec => fn((0, adapter_core_1.getAmplifyServerContext)(contextSpec).amplify),\n });\n return (0, internals_1.generateServerClient)({\n amplify: getAmplify,\n config: resourcesConfig,\n });\n}\nexports.generateServerClientUsingCookies = generateServerClientUsingCookies;\n/**\n * Generates an API client that can be used with both Pages Router and App Router\n *\n * @example\n * import config from './amplifyconfiguration.json';\n * import { listPosts } from './graphql/queries';\n *\n * const client = generateServerClientUsingReqRes();\n *\n * result = await runWithAmplifyServerContext({\n * nextServerContext: { request, response },\n * operation: (contextSpec) => client.graphql(contextSpec, {\n * query: listPosts,\n * }),\n * });\n */\nfunction generateServerClientUsingReqRes({ config }) {\n const amplifyConfig = (0, utils_1.getAmplifyConfig)(config);\n // passing `null` instance because each (future model) method must retrieve a valid instance\n // from server context\n const client = (0, internals_1.generateServerClient)({\n amplify: null,\n config: amplifyConfig,\n });\n // TODO: improve this and the next type\n const prevGraphql = client.graphql;\n const wrappedGraphql = (contextSpec, options, additionalHeaders) => {\n const amplifyInstance = (0, adapter_core_1.getAmplifyServerContext)(contextSpec).amplify;\n return prevGraphql.call({ [api_graphql_1.__amplify]: amplifyInstance }, options, additionalHeaders);\n };\n client.graphql = wrappedGraphql;\n return client;\n}\nexports.generateServerClientUsingReqRes = generateServerClientUsingReqRes;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,+BAA+B,GAAG,OAAO,CAAC,gCAAgC,GAAG,KAAK,CAAC,CAAC;AAC5F,MAAM,WAAW,GAAG,OAAO,CAAC,4BAA4B,CAAC,CAAC;AAC1D,MAAM,cAAc,GAAG,OAAO,CAAC,0CAA0C,CAAC,CAAC;AAC3E,MAAM,aAAa,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC1D,MAAM,0BAA0B,GAAG,OAAO,CAAC,4BAA4B,CAAC,CAAC;AACzE,MAAM,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AACpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,gCAAgC,CAAC,EAAE,MAAM,EAAE,OAAO,GAAG,EAAE;AAChE,IAAI,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;AACvC,QAAQ,MAAM,IAAI,cAAc,CAAC,yBAAyB,CAAC;AAC3D,YAAY,OAAO,EAAE,yHAAyH;AAC9I;AACA,YAAY,kBAAkB,EAAE,+FAA+F;AAC/H,SAAS,CAAC,CAAC;AACX,KAAK;AACL,IAAI,MAAM,EAAE,2BAA2B,EAAE,eAAe,EAAE,GAAG,IAAI,0BAA0B,CAAC,wBAAwB,EAAE,E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,IAAI,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC;AACpC,IAAI,OAAO,MAAM,CAAC;AAClB,CAAC;AACD,OAAO,CAAC,+BAA+B,GAAG,+BAA+B;;"}
1
+ {"version":3,"file":"generateServerClient.js","sources":["../../../src/api/generateServerClient.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.generateServerClientUsingReqRes = exports.generateServerClientUsingCookies = void 0;\nconst internals_1 = require(\"@aws-amplify/api/internals\");\nconst adapter_core_1 = require(\"@aws-amplify/core/internals/adapter-core\");\nconst api_graphql_1 = require(\"@aws-amplify/api-graphql\");\nconst createServerRunnerForAPI_1 = require(\"./createServerRunnerForAPI\");\nconst utils_1 = require(\"../utils\");\n/**\n * Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering\n *\n * @example\n * import { cookies } from \"next/headers\"\n *\n * const client = generateServerClientUsingCookies({ cookies });\n * const result = await client.graphql({ query: listPosts });\n */\nfunction generateServerClientUsingCookies({ config, cookies, authMode, authToken, }) {\n if (typeof cookies !== 'function') {\n throw new adapter_core_1.AmplifyServerContextError({\n message: 'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',\n // TODO: link to docs\n recoverySuggestion: 'use `generateServerClient` inside of `runWithAmplifyServerContext` with the `request` object.',\n });\n }\n const { runWithAmplifyServerContext, resourcesConfig } = (0, createServerRunnerForAPI_1.createServerRunnerForAPI)({ config });\n // This function reference gets passed down to InternalGraphQLAPI.ts.graphql\n // where this._graphql is passed in as the `fn` argument\n // causing it to always get invoked inside `runWithAmplifyServerContext`\n const getAmplify = (fn) => runWithAmplifyServerContext({\n nextServerContext: { cookies },\n operation: contextSpec => fn((0, adapter_core_1.getAmplifyServerContext)(contextSpec).amplify),\n });\n return (0, internals_1.generateServerClient)({\n amplify: getAmplify,\n config: resourcesConfig,\n authMode,\n authToken,\n });\n}\nexports.generateServerClientUsingCookies = generateServerClientUsingCookies;\n/**\n * Generates an API client that can be used with both Pages Router and App Router\n *\n * @example\n * import config from './amplifyconfiguration.json';\n * import { listPosts } from './graphql/queries';\n *\n * const client = generateServerClientUsingReqRes();\n *\n * result = await runWithAmplifyServerContext({\n * nextServerContext: { request, response },\n * operation: (contextSpec) => client.graphql(contextSpec, {\n * query: listPosts,\n * }),\n * });\n */\nfunction generateServerClientUsingReqRes({ config, authMode, authToken }) {\n const amplifyConfig = (0, utils_1.getAmplifyConfig)(config);\n // passing `null` instance because each (future model) method must retrieve a valid instance\n // from server context\n const client = (0, internals_1.generateServerClient)({\n amplify: null,\n config: amplifyConfig,\n authMode,\n authToken,\n });\n // TODO: improve this and the next type\n const prevGraphql = client.graphql;\n const wrappedGraphql = (contextSpec, options, additionalHeaders) => {\n const amplifyInstance = (0, adapter_core_1.getAmplifyServerContext)(contextSpec).amplify;\n return prevGraphql.call({ [api_graphql_1.__amplify]: amplifyInstance }, options, additionalHeaders);\n };\n client.graphql = wrappedGraphql;\n return client;\n}\nexports.generateServerClientUsingReqRes = generateServerClientUsingReqRes;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,+BAA+B,GAAG,OAAO,CAAC,gCAAgC,GAAG,KAAK,CAAC,CAAC;AAC5F,MAAM,WAAW,GAAG,OAAO,CAAC,4BAA4B,CAAC,CAAC;AAC1D,MAAM,cAAc,GAAG,OAAO,CAAC,0CAA0C,CAAC,CAAC;AAC3E,MAAM,aAAa,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC1D,MAAM,0BAA0B,GAAG,OAAO,CAAC,4BAA4B,CAAC,CAAC;AACzE,MAAM,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AACpC;AACA;AACA;AACA;AACA;AACA;A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;AACzD,QAAQ,OAAO,EAAE,IAAI;AACrB,QAAQ,MAAM,EAAE,aAAa;AAC7B,QAAQ,QAAQ;AAChB,QAAQ,SAAS;AACjB,KAAK,CAAC,CAAC;AACP;AACA,IAAI,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC;AACvC,IAAI,MAAM,cAAc,GAAG,CAAC,WAAW,EAAE,OAAO,EAAE,iBAAiB,KAAK;AACxE,QAAQ,MAAM,eAAe,GAAG,IAAI,cAAc,CAAC,uBAAuB,EAAE,WAAW,CAAC,CAAC,OAAO,CAAC;AACjG,QAAQ,OAAO,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,SAAS,GAAG,eAAe,EAAE,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC;AAC5G,KAAK,CAAC;AACN,IAAI,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC;AACpC,IAAI,OAAO,MAAM,CAAC;AAClB,CAAC;AACD,OAAO,CAAC,+BAA+B,GAAG,+BAA+B;;"}
@@ -1,5 +1,17 @@
1
- import { V6Client, V6ClientSSR } from '@aws-amplify/api-graphql';
1
+ import { V6ClientSSRRequest, V6ClientSSRCookies } from '@aws-amplify/api-graphql';
2
2
  import { NextServer } from '../types';
3
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
4
+ type CookiesClientParams = {
5
+ cookies: NextServer.ServerComponentContext['cookies'];
6
+ config: NextServer.CreateServerRunnerInput['config'];
7
+ authMode?: GraphQLAuthMode;
8
+ authToken?: string;
9
+ };
10
+ type ReqClientParams = {
11
+ config: NextServer.CreateServerRunnerInput['config'];
12
+ authMode?: GraphQLAuthMode;
13
+ authToken?: string;
14
+ };
3
15
  /**
4
16
  * Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering
5
17
  *
@@ -9,7 +21,7 @@ import { NextServer } from '../types';
9
21
  * const client = generateServerClientUsingCookies({ cookies });
10
22
  * const result = await client.graphql({ query: listPosts });
11
23
  */
12
- export declare function generateServerClientUsingCookies<T extends Record<any, any> = never>({ config, cookies, }: NextServer.ServerComponentContext & NextServer.CreateServerRunnerInput): V6Client<T>;
24
+ export declare function generateServerClientUsingCookies<T extends Record<any, any> = never>({ config, cookies, authMode, authToken, }: CookiesClientParams): V6ClientSSRCookies<T>;
13
25
  /**
14
26
  * Generates an API client that can be used with both Pages Router and App Router
15
27
  *
@@ -26,4 +38,5 @@ export declare function generateServerClientUsingCookies<T extends Record<any, a
26
38
  * }),
27
39
  * });
28
40
  */
29
- export declare function generateServerClientUsingReqRes<T extends Record<any, any> = never>({ config }: NextServer.CreateServerRunnerInput): V6ClientSSR<T>;
41
+ export declare function generateServerClientUsingReqRes<T extends Record<any, any> = never>({ config, authMode, authToken }: ReqClientParams): V6ClientSSRRequest<T>;
42
+ export {};
@@ -18,7 +18,7 @@ import 'next/server.js';
18
18
  * const client = generateServerClientUsingCookies({ cookies });
19
19
  * const result = await client.graphql({ query: listPosts });
20
20
  */
21
- function generateServerClientUsingCookies({ config, cookies, }) {
21
+ function generateServerClientUsingCookies({ config, cookies, authMode, authToken, }) {
22
22
  if (typeof cookies !== 'function') {
23
23
  throw new AmplifyServerContextError({
24
24
  message: 'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',
@@ -37,6 +37,8 @@ function generateServerClientUsingCookies({ config, cookies, }) {
37
37
  return generateServerClient({
38
38
  amplify: getAmplify,
39
39
  config: resourcesConfig,
40
+ authMode,
41
+ authToken,
40
42
  });
41
43
  }
42
44
  /**
@@ -55,13 +57,15 @@ function generateServerClientUsingCookies({ config, cookies, }) {
55
57
  * }),
56
58
  * });
57
59
  */
58
- function generateServerClientUsingReqRes({ config }) {
60
+ function generateServerClientUsingReqRes({ config, authMode, authToken }) {
59
61
  const amplifyConfig = getAmplifyConfig(config);
60
62
  // passing `null` instance because each (future model) method must retrieve a valid instance
61
63
  // from server context
62
64
  const client = generateServerClient({
63
65
  amplify: null,
64
66
  config: amplifyConfig,
67
+ authMode,
68
+ authToken,
65
69
  });
66
70
  // TODO: improve this and the next type
67
71
  const prevGraphql = client.graphql;
@@ -1 +1 @@
1
- {"version":3,"file":"generateServerClient.mjs","sources":["../../../src/api/generateServerClient.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { generateServerClient } from '@aws-amplify/api/internals';\nimport { getAmplifyServerContext, AmplifyServerContextError, } from '@aws-amplify/core/internals/adapter-core';\nimport { __amplify, } from '@aws-amplify/api-graphql';\nimport { createServerRunnerForAPI } from './createServerRunnerForAPI';\nimport { getAmplifyConfig } from '../utils';\n/**\n * Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering\n *\n * @example\n * import { cookies } from \"next/headers\"\n *\n * const client = generateServerClientUsingCookies({ cookies });\n * const result = await client.graphql({ query: listPosts });\n */\nexport function generateServerClientUsingCookies({ config, cookies, }) {\n if (typeof cookies !== 'function') {\n throw new AmplifyServerContextError({\n message: 'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',\n // TODO: link to docs\n recoverySuggestion: 'use `generateServerClient` inside of `runWithAmplifyServerContext` with the `request` object.',\n });\n }\n const { runWithAmplifyServerContext, resourcesConfig } = createServerRunnerForAPI({ config });\n // This function reference gets passed down to InternalGraphQLAPI.ts.graphql\n // where this._graphql is passed in as the `fn` argument\n // causing it to always get invoked inside `runWithAmplifyServerContext`\n const getAmplify = (fn) => runWithAmplifyServerContext({\n nextServerContext: { cookies },\n operation: contextSpec => fn(getAmplifyServerContext(contextSpec).amplify),\n });\n return generateServerClient({\n amplify: getAmplify,\n config: resourcesConfig,\n });\n}\n/**\n * Generates an API client that can be used with both Pages Router and App Router\n *\n * @example\n * import config from './amplifyconfiguration.json';\n * import { listPosts } from './graphql/queries';\n *\n * const client = generateServerClientUsingReqRes();\n *\n * result = await runWithAmplifyServerContext({\n * nextServerContext: { request, response },\n * operation: (contextSpec) => client.graphql(contextSpec, {\n * query: listPosts,\n * }),\n * });\n */\nexport function generateServerClientUsingReqRes({ config }) {\n const amplifyConfig = getAmplifyConfig(config);\n // passing `null` instance because each (future model) method must retrieve a valid instance\n // from server context\n const client = generateServerClient({\n amplify: null,\n config: amplifyConfig,\n });\n // TODO: improve this and the next type\n const prevGraphql = client.graphql;\n const wrappedGraphql = (contextSpec, options, additionalHeaders) => {\n const amplifyInstance = getAmplifyServerContext(contextSpec).amplify;\n return prevGraphql.call({ [__amplify]: amplifyInstance }, options, additionalHeaders);\n };\n client.graphql = wrappedGraphql;\n return client;\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;AACA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gCAAgC,CAAC,EAAE,MAAM,EAAE,OAAO,GAAG,EAAE;AACvE,IAAI,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;AACvC,QAAQ,MAAM,IAAI,yBAAyB,CAAC;AAC5C,YAAY,OAAO,EAAE,yHAAyH;AAC9I;AACA,YAAY,kBAAkB,EAAE,+FAA+F;AAC/H,SAAS,CAAC,CAAC;AACX,KAAK;AACL,IAAI,MAAM,EAAE,2BAA2B,EAAE,eAAe,EAAE,GAAG,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AAClG;AACA;AACA;AACA,IAAI,MAAM,UAAU,GAAG,CAAC,EAAE,KAAK,2BAA2B,CAAC;AAC3D,QAAQ,iBAAiB,EAAE,EAAE,OAAO,EAAE;AACtC,QAAQ,SAAS,EAAE,WAAW,IAAI,EAAE,CAAC,uBAAuB,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC;AAClF,KAAK,CAAC,CAAC;AACP,IAAI,OAAO,oBAAoB,CAAC;AAChC,QAAQ,OAAO,EAAE,UAAU;AAC3B,QAAQ,MAAM,EAAE,eAAe;AAC/B,KAAK,CAAC,CAAC;AACP,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,+BAA+B,CAAC,EAAE,MAAM,EAAE,EAAE;AAC5D,IAAI,MAAM,aAAa,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;AACnD;AACA;AACA,IAAI,MAAM,MAAM,GAAG,oBAAoB,CAAC;AACxC,QAAQ,OAAO,EAAE,IAAI;AACrB,QAAQ,MAAM,EAAE,aAAa;AAC7B,KAAK,CAAC,CAAC;AACP;AACA,IAAI,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC;AACvC,IAAI,MAAM,cAAc,GAAG,CAAC,WAAW,EAAE,OAAO,EAAE,iBAAiB,KAAK;AACxE,QAAQ,MAAM,eAAe,GAAG,uBAAuB,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC;AAC7E,QAAQ,OAAO,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,GAAG,eAAe,EAAE,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC;AAC9F,KAAK,CAAC;AACN,IAAI,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC;AACpC,IAAI,OAAO,MAAM,CAAC;AAClB;;;;"}
1
+ {"version":3,"file":"generateServerClient.mjs","sources":["../../../src/api/generateServerClient.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { generateServerClient } from '@aws-amplify/api/internals';\nimport { getAmplifyServerContext, AmplifyServerContextError, } from '@aws-amplify/core/internals/adapter-core';\nimport { __amplify, } from '@aws-amplify/api-graphql';\nimport { createServerRunnerForAPI } from './createServerRunnerForAPI';\nimport { getAmplifyConfig } from '../utils';\n/**\n * Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering\n *\n * @example\n * import { cookies } from \"next/headers\"\n *\n * const client = generateServerClientUsingCookies({ cookies });\n * const result = await client.graphql({ query: listPosts });\n */\nexport function generateServerClientUsingCookies({ config, cookies, authMode, authToken, }) {\n if (typeof cookies !== 'function') {\n throw new AmplifyServerContextError({\n message: 'generateServerClientUsingCookies is only compatible with the `cookies` Dynamic Function available in Server Components.',\n // TODO: link to docs\n recoverySuggestion: 'use `generateServerClient` inside of `runWithAmplifyServerContext` with the `request` object.',\n });\n }\n const { runWithAmplifyServerContext, resourcesConfig } = createServerRunnerForAPI({ config });\n // This function reference gets passed down to InternalGraphQLAPI.ts.graphql\n // where this._graphql is passed in as the `fn` argument\n // causing it to always get invoked inside `runWithAmplifyServerContext`\n const getAmplify = (fn) => runWithAmplifyServerContext({\n nextServerContext: { cookies },\n operation: contextSpec => fn(getAmplifyServerContext(contextSpec).amplify),\n });\n return generateServerClient({\n amplify: getAmplify,\n config: resourcesConfig,\n authMode,\n authToken,\n });\n}\n/**\n * Generates an API client that can be used with both Pages Router and App Router\n *\n * @example\n * import config from './amplifyconfiguration.json';\n * import { listPosts } from './graphql/queries';\n *\n * const client = generateServerClientUsingReqRes();\n *\n * result = await runWithAmplifyServerContext({\n * nextServerContext: { request, response },\n * operation: (contextSpec) => client.graphql(contextSpec, {\n * query: listPosts,\n * }),\n * });\n */\nexport function generateServerClientUsingReqRes({ config, authMode, authToken }) {\n const amplifyConfig = getAmplifyConfig(config);\n // passing `null` instance because each (future model) method must retrieve a valid instance\n // from server context\n const client = generateServerClient({\n amplify: null,\n config: amplifyConfig,\n authMode,\n authToken,\n });\n // TODO: improve this and the next type\n const prevGraphql = client.graphql;\n const wrappedGraphql = (contextSpec, options, additionalHeaders) => {\n const amplifyInstance = getAmplifyServerContext(contextSpec).amplify;\n return prevGraphql.call({ [__amplify]: amplifyInstance }, options, additionalHeaders);\n };\n client.graphql = wrappedGraphql;\n return client;\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;AACA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gCAAgC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,GAAG,EAAE;AAC5F,IAAI,IAAI,OAAO,OAAO,KAAK,UAAU,EAA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,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC;AAC7E,QAAQ,OAAO,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,GAAG,eAAe,EAAE,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC;AAC9F,KAAK,CAAC;AACN,IAAI,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC;AACpC,IAAI,OAAO,MAAM,CAAC;AAClB;;;;"}
package/package.json CHANGED
@@ -1,119 +1,124 @@
1
1
  {
2
- "author": "Amazon Web Services",
3
- "name": "@aws-amplify/adapter-nextjs",
4
- "version": "1.0.1",
5
- "description": "The adapter for the supporting of using Amplify APIs in Next.js.",
6
- "peerDependencies": {
7
- "aws-amplify": "^6.0.0",
8
- "next": ">=13.5.0 <15.0.0"
9
- },
10
- "dependencies": {
11
- "cookie": "0.5.0"
12
- },
13
- "devDependencies": {
14
- "@rollup/plugin-typescript": "11.1.5",
15
- "@types/cookie": "0.5.1",
16
- "@types/node": "^20.3.1",
17
- "@types/react": "^18.2.13",
18
- "@types/react-dom": "^18.2.6",
19
- "aws-amplify": "6.0.1",
20
- "jest-fetch-mock": "3.0.3",
21
- "next": ">= 13.5.0 < 15.0.0",
22
- "rollup": "3.29.4",
23
- "typescript": "5.1.6"
24
- },
25
- "publishConfig": {
26
- "access": "public"
27
- },
28
- "bugs": {
29
- "url": "https://github.com/aws/aws-amplify/issues"
30
- },
31
- "exports": {
32
- ".": {
33
- "types": "./dist/esm/index.d.ts",
34
- "import": "./dist/esm/index.mjs",
35
- "require": "./dist/cjs/index.js"
36
- },
37
- "./api": {
38
- "types": "./dist/esm/api/index.d.ts",
39
- "import": "./dist/esm/api/index.mjs",
40
- "require": "./dist/cjs/api/index.js"
41
- },
42
- "./package.json": "./package.json"
43
- },
44
- "files": [
45
- "dist/cjs",
46
- "dist/esm",
47
- "src",
48
- "api"
49
- ],
50
- "homepage": "https://aws-amplify.github.io/",
51
- "license": "Apache-2.0",
52
- "main": "./dist/cjs/index.js",
53
- "module": "./dist/esm/index.mjs",
54
- "typings": "./dist/esm/index.d.ts",
55
- "sideEffects": false,
56
- "scripts": {
57
- "build": "npm run clean && npm run build:esm-cjs",
58
- "build-with-test": "npm test && npm run build",
59
- "build:cjs:watch": "rimraf dist/cjs && tsc -m commonjs --outDir dist/cjs --watch",
60
- "build:esm-cjs": "rollup -c rollup.config.mjs",
61
- "build:esm:watch": "rimraf dist/esm && tsc -m esnext --outDir dist/esm --watch",
62
- "clean": "npm run clean:size && rimraf dist",
63
- "clean:size": "rimraf dual-publish-tmp tmp*",
64
- "format": "echo \"Not implemented\"",
65
- "lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
66
- "test": "npm run lint && jest -w 1 --coverage",
67
- "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
68
- },
69
- "jest": {
70
- "coveragePathIgnorePatterns": [
71
- "/node_modules/",
72
- "dist",
73
- "lib",
74
- "lib-esm",
75
- "__tests__/mocks"
76
- ],
77
- "coverageThreshold": {
78
- "global": {
79
- "branches": 88,
80
- "functions": 90,
81
- "lines": 92,
82
- "statements": 93
83
- }
84
- },
85
- "globals": {
86
- "ts-jest": {
87
- "diagnostics": {
88
- "pathRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$"
89
- },
90
- "tsConfig": {
91
- "allowJs": true,
92
- "types": [
93
- "@types/jest"
94
- ]
95
- }
96
- }
97
- },
98
- "moduleFileExtensions": [
99
- "ts",
100
- "tsx",
101
- "js",
102
- "json",
103
- "jsx"
104
- ],
105
- "testEnvironment": "node",
106
- "testPathIgnorePatterns": [
107
- "xmlParser-fixture.ts",
108
- "testUtils",
109
- "cases",
110
- "mocks"
111
- ],
112
- "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
113
- "testURL": "http://localhost/",
114
- "transform": {
115
- "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
116
- }
117
- },
118
- "gitHead": "f1408db3f570c4a18ada40cecbe7e68a0b31f1fe"
2
+ "author": "Amazon Web Services",
3
+ "name": "@aws-amplify/adapter-nextjs",
4
+ "version": "1.0.2-unstable.b778a9e.0+b778a9e",
5
+ "description": "The adapter for the supporting of using Amplify APIs in Next.js.",
6
+ "peerDependencies": {
7
+ "aws-amplify": "6.0.2-unstable.b778a9e.0+b778a9e",
8
+ "next": ">=13.5.0 <15.0.0"
9
+ },
10
+ "dependencies": {
11
+ "cookie": "0.5.0"
12
+ },
13
+ "devDependencies": {
14
+ "@rollup/plugin-typescript": "11.1.5",
15
+ "@types/cookie": "0.5.1",
16
+ "@types/node": "^20.3.1",
17
+ "@types/react": "^18.2.13",
18
+ "@types/react-dom": "^18.2.6",
19
+ "aws-amplify": "6.0.2-unstable.b778a9e.0+b778a9e",
20
+ "jest-fetch-mock": "3.0.3",
21
+ "next": ">= 13.5.0 < 15.0.0",
22
+ "rollup": "3.29.4",
23
+ "typescript": "5.1.6"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/aws/aws-amplify/issues"
30
+ },
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/esm/index.d.ts",
34
+ "import": "./dist/esm/index.mjs",
35
+ "require": "./dist/cjs/index.js"
36
+ },
37
+ "./api": {
38
+ "types": "./dist/esm/api/index.d.ts",
39
+ "import": "./dist/esm/api/index.mjs",
40
+ "require": "./dist/cjs/api/index.js"
41
+ },
42
+ "./data": {
43
+ "types": "./dist/esm/api/index.d.ts",
44
+ "import": "./dist/esm/api/index.mjs",
45
+ "require": "./dist/cjs/api/index.js"
46
+ },
47
+ "./package.json": "./package.json"
48
+ },
49
+ "files": [
50
+ "dist/cjs",
51
+ "dist/esm",
52
+ "src",
53
+ "api"
54
+ ],
55
+ "homepage": "https://aws-amplify.github.io/",
56
+ "license": "Apache-2.0",
57
+ "main": "./dist/cjs/index.js",
58
+ "module": "./dist/esm/index.mjs",
59
+ "typings": "./dist/esm/index.d.ts",
60
+ "sideEffects": false,
61
+ "scripts": {
62
+ "build": "npm run clean && npm run build:esm-cjs",
63
+ "build-with-test": "npm test && npm run build",
64
+ "build:cjs:watch": "rimraf dist/cjs && tsc -m commonjs --outDir dist/cjs --watch",
65
+ "build:esm-cjs": "rollup -c rollup.config.mjs",
66
+ "build:esm:watch": "rimraf dist/esm && tsc -m esnext --outDir dist/esm --watch",
67
+ "clean": "npm run clean:size && rimraf dist",
68
+ "clean:size": "rimraf dual-publish-tmp tmp*",
69
+ "format": "echo \"Not implemented\"",
70
+ "lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
71
+ "test": "npm run lint && jest -w 1 --coverage",
72
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
73
+ },
74
+ "jest": {
75
+ "coveragePathIgnorePatterns": [
76
+ "/node_modules/",
77
+ "dist",
78
+ "lib",
79
+ "lib-esm",
80
+ "__tests__/mocks"
81
+ ],
82
+ "coverageThreshold": {
83
+ "global": {
84
+ "branches": 88,
85
+ "functions": 90,
86
+ "lines": 92,
87
+ "statements": 93
88
+ }
89
+ },
90
+ "globals": {
91
+ "ts-jest": {
92
+ "diagnostics": {
93
+ "pathRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$"
94
+ },
95
+ "tsConfig": {
96
+ "allowJs": true,
97
+ "types": [
98
+ "@types/jest"
99
+ ]
100
+ }
101
+ }
102
+ },
103
+ "moduleFileExtensions": [
104
+ "ts",
105
+ "tsx",
106
+ "js",
107
+ "json",
108
+ "jsx"
109
+ ],
110
+ "testEnvironment": "node",
111
+ "testPathIgnorePatterns": [
112
+ "xmlParser-fixture.ts",
113
+ "testUtils",
114
+ "cases",
115
+ "mocks"
116
+ ],
117
+ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
118
+ "testURL": "http://localhost/",
119
+ "transform": {
120
+ "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
121
+ }
122
+ },
123
+ "gitHead": "b778a9e3b05c6f0f52fcfaab79729f4d7d439444"
119
124
  }
@@ -8,8 +8,8 @@ import {
8
8
  AmplifyServer,
9
9
  } from '@aws-amplify/core/internals/adapter-core';
10
10
  import {
11
- V6Client,
12
- V6ClientSSR,
11
+ V6ClientSSRRequest,
12
+ V6ClientSSRCookies,
13
13
  GraphQLMethod,
14
14
  GraphQLMethodSSR,
15
15
  __amplify,
@@ -18,6 +18,20 @@ import {
18
18
  import { NextServer } from '../types';
19
19
  import { createServerRunnerForAPI } from './createServerRunnerForAPI';
20
20
  import { getAmplifyConfig } from '../utils';
21
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
22
+
23
+ type CookiesClientParams = {
24
+ cookies: NextServer.ServerComponentContext['cookies'];
25
+ config: NextServer.CreateServerRunnerInput['config'];
26
+ authMode?: GraphQLAuthMode;
27
+ authToken?: string;
28
+ };
29
+
30
+ type ReqClientParams = {
31
+ config: NextServer.CreateServerRunnerInput['config'];
32
+ authMode?: GraphQLAuthMode;
33
+ authToken?: string;
34
+ };
21
35
 
22
36
  /**
23
37
  * Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering
@@ -33,8 +47,9 @@ export function generateServerClientUsingCookies<
33
47
  >({
34
48
  config,
35
49
  cookies,
36
- }: NextServer.ServerComponentContext &
37
- NextServer.CreateServerRunnerInput): V6Client<T> {
50
+ authMode,
51
+ authToken,
52
+ }: CookiesClientParams): V6ClientSSRCookies<T> {
38
53
  if (typeof cookies !== 'function') {
39
54
  throw new AmplifyServerContextError({
40
55
  message:
@@ -58,9 +73,11 @@ export function generateServerClientUsingCookies<
58
73
  fn(getAmplifyServerContext(contextSpec).amplify),
59
74
  });
60
75
 
61
- return generateServerClient<T, V6Client<T>>({
76
+ return generateServerClient<T, V6ClientSSRCookies<T>>({
62
77
  amplify: getAmplify,
63
78
  config: resourcesConfig,
79
+ authMode,
80
+ authToken,
64
81
  });
65
82
  }
66
83
 
@@ -82,13 +99,15 @@ export function generateServerClientUsingCookies<
82
99
  */
83
100
  export function generateServerClientUsingReqRes<
84
101
  T extends Record<any, any> = never
85
- >({ config }: NextServer.CreateServerRunnerInput): V6ClientSSR<T> {
102
+ >({ config, authMode, authToken }: ReqClientParams): V6ClientSSRRequest<T> {
86
103
  const amplifyConfig = getAmplifyConfig(config);
87
104
  // passing `null` instance because each (future model) method must retrieve a valid instance
88
105
  // from server context
89
- const client = generateServerClient<T, V6ClientSSR<T>>({
106
+ const client = generateServerClient<T, V6ClientSSRRequest<T>>({
90
107
  amplify: null,
91
108
  config: amplifyConfig,
109
+ authMode,
110
+ authToken,
92
111
  });
93
112
 
94
113
  // TODO: improve this and the next type