Skip to content

Commit

Permalink
chore(adapter-nextjs): update withAmplify documentation and import pa…
Browse files Browse the repository at this point in the history
…th of parseAmplifyConfig (#11979)

* chore(adapter-nextjs): update withAmplify documentation and import path of parseAmplifyConfig

* Remove the TODO item
  • Loading branch information
HuiSF authored Sep 6, 2023
1 parent 7188d04 commit 65f21cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/adapter-nextjs/__tests__/withAmplify.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig } from '@aws-amplify/core';
import { ResourcesConfig } from 'aws-amplify';
import { withAmplify } from '../src/withAmplify';

const mockAmplifyConfig: ResourcesConfig = {
Expand All @@ -16,7 +16,7 @@ const mockAmplifyConfig: ResourcesConfig = {
S3: {
bucket: 'bucket',
region: 'us-east-1',
}
},
},
};

Expand Down
10 changes: 5 additions & 5 deletions packages/adapter-nextjs/src/runWithAmplifyServerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export const runWithAmplifyServerContext: NextServer.RunOperationWithContext =
// context with token and credentials provider.
if (amplifyConfig.Auth) {
const keyValueStorage =
// When `null` is passed as the value of `nextServerContext`, opt-in
// unauthenticated role (primarily for static rendering). It's
// safe to use the singleton `MemoryKeyValueStorage` here, as the
// static rendering uses the same unauthenticated role cross-sever.
nextServerContext === null
? // When `null` is passed as the value of `nextServerContext`, opt-in
// unauthenticated role (primarily for static rendering). It's
// safe to use the singleton `MemoryKeyValueStorage` here, as the
// static rendering uses the same unauthenticated role cross-sever.
MemoryKeyValueStorage
? MemoryKeyValueStorage
: createKeyValueStorageFromCookieStorageAdapter(
createCookieStorageAdapterFromNextServerContext(nextServerContext)
);
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-nextjs/src/utils/getAmplifyConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig } from '@aws-amplify/core';
import { ResourcesConfig } from 'aws-amplify';
import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-core';

export const getAmplifyConfig = (): ResourcesConfig => {
Expand Down
18 changes: 15 additions & 3 deletions packages/adapter-nextjs/src/withAmplify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig } from '@aws-amplify/core';
import { ResourcesConfig, parseAmplifyConfig } from 'aws-amplify';
import { NextConfig } from 'next';

// NOTE: this function is exported from the subpath `/with-amplify`.
Expand All @@ -10,8 +10,21 @@ import { NextConfig } from 'next';

/**
* Merges the `amplifyConfig` into the `nextConfig.env`.
*
* @param nextConfig The next config for a Next.js app.
* @param amplifyConfig
* @param amplifyConfig The Amplify configuration.
*
* **NOTE**: If you are using Amplify CLI to generate the `aws-exports.js`
* file, you need to use {@link parseAmplifyConfig} to reformat the configuration.
* E.g.
* ```javascript
* const { parseAmplifyConfig } = require('aws-amplify');
* const { withAmplify } = require('@aws-amplify/adapter-nextjs/with-amplify');
* const config = require('./src/aws-exports');
*
* const nextConfig = {};
* module.exports = withAmplify(nextConfig, parseAmplifyConfig(config));
* ```
* @returns The updated `nextConfig`.
*/
export const withAmplify = (
Expand All @@ -20,7 +33,6 @@ export const withAmplify = (
) => {
nextConfig.env = {
...nextConfig.env,
// TODO(Hui): follow up the validation of the amplifyConfig.
amplifyConfig: JSON.stringify(amplifyConfig),
};

Expand Down

0 comments on commit 65f21cb

Please sign in to comment.