Skip to content

Commit

Permalink
[Feat] Storage Path changes Gen 2 (#13294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashika112 authored Apr 26, 2024
2 parents 64acd49 + 4378e72 commit 588289c
Show file tree
Hide file tree
Showing 80 changed files with 4,700 additions and 857 deletions.
9 changes: 9 additions & 0 deletions .github/integ-config/integ-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,15 @@ tests:
spec: multi-part-copy
browser: *minimal_browser_list

# GEN2 STORAGE
- test_name: integ_react_storage
desc: 'React Storage Gen2'
framework: react
category: storage
sample_name: [storage-gen2]
spec: storage-gen2
browser: *minimal_browser_list

# INAPPMESSAGING
- test_name: integ_in_app_messaging
desc: 'React InApp Messaging'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ResourcesConfig } from '@aws-amplify/core';
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';

import {
generateServerClientUsingCookies,
generateServerClientUsingReqRes,
} from '../../src/api';
import {
createRunWithAmplifyServerContext,
getAmplifyConfig,
} from '../../src/utils';
import { createRunWithAmplifyServerContext } from '../../src/utils';
import { NextApiRequestMock, NextApiResponseMock } from '../mocks/headers';
import { createServerRunnerForAPI } from '../../src/api/createServerRunnerForAPI';

Expand All @@ -34,13 +32,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 @@ -77,7 +78,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
17 changes: 12 additions & 5 deletions packages/adapter-nextjs/__tests__/createServerRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 @@ -48,23 +48,23 @@ describe('createServerRunner', () => {
runWithAmplifyServerContext: mockRunWithAmplifyServerContextCore,
}));
jest.doMock('@aws-amplify/core/internals/utils', () => ({
parseAWSExports: mockParseAWSExports,
parseAmplifyConfig: mockParseAmplifyConfig,
}));

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

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 @@ -85,6 +85,9 @@ describe('createServerRunner', () => {
},
},
};

mockParseAmplifyConfig.mockReturnValue(mockAmplifyConfigWithoutAuth);

const { runWithAmplifyServerContext } = createServerRunner({
config: mockAmplifyConfigWithoutAuth,
});
Expand All @@ -99,6 +102,10 @@ describe('createServerRunner', () => {
});

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';
90 changes: 90 additions & 0 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -7,6 +8,7 @@ import {
ResourcesConfig,
defaultStorage,
} from '@aws-amplify/core';
import { AmplifyOutputs } from '@aws-amplify/core/internals/utils';

import {
cognitoCredentialsProvider,
Expand Down Expand Up @@ -68,6 +70,94 @@ describe('initSingleton (DefaultAmplify)', () => {
mockAmplifySingletonGetConfig.mockReset();
});

describe('Amplify configure with AmplifyOutputs format', () => {
it('should use AmplifyOutputs config type', () => {
const amplifyOutputs: AmplifyOutputs = {
version: '1',
storage: {
aws_region: 'us-east-1',
bucket_name: 'my-bucket-name',
},
auth: {
user_pool_id: 'us-east-1:',
user_pool_client_id: 'xxxx',
aws_region: 'us-east-1',
identity_pool_id: 'test',
},
analytics: {
amazon_pinpoint: {
app_id: 'xxxxx',
aws_region: 'us-east-1',
},
},
geo: {
aws_region: 'us-east-1',
maps: {
items: { map1: { name: 'map1', style: 'color' } },
default: 'map1',
},
geofence_collections: {
items: ['a', 'b', 'c'],
default: 'a',
},
search_indices: {
items: ['a', 'b', 'c'],
default: 'a',
},
},
};

Amplify.configure(amplifyOutputs);

expect(AmplifySingleton.configure).toHaveBeenCalledWith(
{
Storage: {
S3: {
bucket: 'my-bucket-name',
region: 'us-east-1',
},
},
Auth: {
Cognito: {
identityPoolId: 'test',
userPoolId: 'us-east-1:',
userPoolClientId: 'xxxx',
},
},
Analytics: {
Pinpoint: {
appId: 'xxxxx',
region: 'us-east-1',
},
},
Geo: {
LocationService: {
geofenceCollections: {
default: 'a',
items: ['a', 'b', 'c'],
},
maps: {
default: 'map1',
items: {
map1: {
name: 'map1',
style: 'color',
},
},
},
region: 'us-east-1',
searchIndices: {
default: 'a',
items: ['a', 'b', 'c'],
},
},
},
},
expect.anything(),
);
});
});

describe('DefaultAmplify.configure()', () => {
it('should take the legacy CLI shaped config object for configuring the underlying Amplify Singleton', () => {
const mockLegacyConfig = {
Expand Down
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 588289c

Please sign in to comment.