Skip to content

Commit

Permalink
fix: Fix SSR & AmplifyOutput types when using Gen2 configuration files (
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblanc authored Apr 17, 2024
1 parent a81fc29 commit f797dc5
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ResourcesConfig } from '@aws-amplify/core';
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';
import {
generateServerClientUsingCookies,
generateServerClientUsingReqRes,
} from '../../src/api';
import {
getAmplifyConfig,
createRunWithAmplifyServerContext,
} from '../../src/utils';
import { NextApiRequestMock, NextApiResponseMock } from '../mocks/headers';
Expand Down Expand Up @@ -33,13 +33,16 @@ const mockAmplifyConfig: ResourcesConfig = {

jest.mock('../../src/utils', () => ({
createRunWithAmplifyServerContext: jest.fn(() => jest.fn()),
getAmplifyConfig: jest.fn(() => mockAmplifyConfig),
createCookieStorageAdapterFromNextServerContext: jest.fn(),
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
parseAmplifyConfig: jest.fn(() => mockAmplifyConfig),
}));

jest.mock('aws-amplify/adapter-core');

const mockGetAmplifyConfig = getAmplifyConfig as jest.Mock;
const mockParseAmplifyConfig = parseAmplifyConfig as jest.Mock;
const mockCreateRunWithAmplifyServerContext =
createRunWithAmplifyServerContext as jest.Mock;

Expand Down Expand Up @@ -76,7 +79,7 @@ describe('generateServerClient', () => {

it('should call getAmlifyConfig', async () => {
generateServerClientUsingReqRes({ config: mockAmplifyConfig });
expect(mockGetAmplifyConfig).toHaveBeenCalled();
expect(mockParseAmplifyConfig).toHaveBeenCalled();
});

// TODO: figure out proper mocks and unskip
Expand Down
23 changes: 15 additions & 8 deletions packages/adapter-nextjs/__tests__/createServerRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jest.mock(
describe('createServerRunner', () => {
let createServerRunner: any;

const mockParseAWSExports = jest.fn();
const mockParseAmplifyConfig = jest.fn();
const mockCreateAWSCredentialsAndIdentityIdProvider = jest.fn();
const mockCreateKeyValueStorageFromCookieStorageAdapter = jest.fn();
const mockCreateUserPoolsTokenProvider = jest.fn();
Expand All @@ -47,23 +47,23 @@ describe('createServerRunner', () => {
runWithAmplifyServerContext: mockRunWithAmplifyServerContextCore,
}));
jest.doMock('@aws-amplify/core/internals/utils', () => ({
parseAWSExports: mockParseAWSExports,
parseAmplifyConfig: mockParseAmplifyConfig,
}));

createServerRunner = require('../src').createServerRunner;
});

afterEach(() => {
mockParseAWSExports.mockClear();
mockParseAmplifyConfig.mockClear();
mockCreateAWSCredentialsAndIdentityIdProvider.mockClear();
mockCreateKeyValueStorageFromCookieStorageAdapter.mockClear();
mockCreateUserPoolsTokenProvider.mockClear();
mockRunWithAmplifyServerContextCore.mockClear();
});

it('calls parseAWSExports when the config object is imported from amplify configuration file', () => {
it('calls parseAmplifyConfig when the config object is imported from amplify configuration file', () => {
createServerRunner({ config: { aws_project_region: 'us-west-2' } });
expect(mockParseAWSExports).toHaveBeenCalled();
expect(mockParseAmplifyConfig).toHaveBeenCalled();
});

it('returns runWithAmplifyServerContext function', () => {
Expand All @@ -76,28 +76,35 @@ describe('createServerRunner', () => {
describe('runWithAmplifyServerContext', () => {
describe('when amplifyConfig.Auth is not defined', () => {
it('should call runWithAmplifyServerContextCore without Auth library options', () => {
const mockAmplifyConfig: ResourcesConfig = {
const mockAmplifyConfigAnalytics: ResourcesConfig = {
Analytics: {
Pinpoint: {
appId: 'app-id',
region: 'region',
},
},
};

mockParseAmplifyConfig.mockReturnValue(mockAmplifyConfigAnalytics);

const { runWithAmplifyServerContext } = createServerRunner({
config: mockAmplifyConfig,
config: mockAmplifyConfigAnalytics,
});
const operation = jest.fn();
runWithAmplifyServerContext({ operation, nextServerContext: null });
expect(mockRunWithAmplifyServerContextCore).toHaveBeenCalledWith(
mockAmplifyConfig,
mockAmplifyConfigAnalytics,
{},
operation,
);
});
});

describe('when amplifyConfig.Auth is defined', () => {
beforeEach(() => {
mockParseAmplifyConfig.mockReturnValue(mockAmplifyConfig);
})

describe('when nextServerContext is null (opt-in unauthenticated role)', () => {
it('should create auth providers with sharedInMemoryStorage', () => {
const { runWithAmplifyServerContext } = createServerRunner({
Expand Down
46 changes: 0 additions & 46 deletions packages/adapter-nextjs/__tests__/utils/getAmplifyConfig.test.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/adapter-nextjs/src/api/createServerRunnerForAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig } from '@aws-amplify/core';
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';

import { createRunWithAmplifyServerContext, getAmplifyConfig } from '../utils';
import { createRunWithAmplifyServerContext } from '../utils';
import { NextServer } from '../types';

export const createServerRunnerForAPI = ({
config,
}: NextServer.CreateServerRunnerInput): NextServer.CreateServerRunnerOutput & {
resourcesConfig: ResourcesConfig;
} => {
const amplifyConfig = getAmplifyConfig(config);
const amplifyConfig = parseAmplifyConfig(config);

return {
runWithAmplifyServerContext: createRunWithAmplifyServerContext({
Expand Down
8 changes: 5 additions & 3 deletions packages/adapter-nextjs/src/api/generateServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
V6ClientSSRCookies,
V6ClientSSRRequest,
} from '@aws-amplify/api-graphql';
import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
import {
GraphQLAuthMode,
parseAmplifyConfig,
} from '@aws-amplify/core/internals/utils';

import { NextServer } from '../types';
import { getAmplifyConfig } from '../utils';

import { createServerRunnerForAPI } from './createServerRunnerForAPI';

Expand Down Expand Up @@ -98,7 +100,7 @@ export function generateServerClientUsingCookies<
export function generateServerClientUsingReqRes<
T extends Record<any, any> = never,
>({ config, authMode, authToken }: ReqClientParams): V6ClientSSRRequest<T> {
const amplifyConfig = getAmplifyConfig(config);
const amplifyConfig = parseAmplifyConfig(config);

return generateClient<T>({
config: amplifyConfig,
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-nextjs/src/createServerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig } from 'aws-amplify';
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';

import { createRunWithAmplifyServerContext, getAmplifyConfig } from './utils';
import { createRunWithAmplifyServerContext } from './utils';
import { NextServer } from './types';

/**
Expand All @@ -27,7 +28,7 @@ import { NextServer } from './types';
export const createServerRunner: NextServer.CreateServerRunner = ({
config,
}) => {
const amplifyConfig = getAmplifyConfig(config);
const amplifyConfig = parseAmplifyConfig(config);

return {
runWithAmplifyServerContext: createRunWithAmplifyServerContext({
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-nextjs/src/types/NextServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { GetServerSidePropsContext as NextGetServerSidePropsContext } from 'next';
import { NextRequest, NextResponse } from 'next/server.js';
import { cookies } from 'next/headers.js';
import { LegacyConfig } from 'aws-amplify/adapter-core';
import { AmplifyOutputs, LegacyConfig } from 'aws-amplify/adapter-core';
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
import { ResourcesConfig } from '@aws-amplify/core';

Expand Down Expand Up @@ -74,7 +74,7 @@ export declare namespace NextServer {
) => Promise<OperationResult>;

export interface CreateServerRunnerInput {
config: ResourcesConfig | LegacyConfig;
config: ResourcesConfig | LegacyConfig | AmplifyOutputs;
}

export interface CreateServerRunnerOutput {
Expand Down
13 changes: 0 additions & 13 deletions packages/adapter-nextjs/src/utils/getAmplifyConfig.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/adapter-nextjs/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export { getAmplifyConfig } from './getAmplifyConfig';
export { createRunWithAmplifyServerContext } from './createRunWithAmplifyServerContext';
2 changes: 1 addition & 1 deletion packages/aws-amplify/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 85,
functions: 66,
functions: 65.5,
lines: 90,
statements: 91,
},
Expand Down
Loading

0 comments on commit f797dc5

Please sign in to comment.