diff --git a/packages/amplify-category-api/src/commands/api/override.ts b/packages/amplify-category-api/src/commands/api/override.ts index b5c57c0bef4..a7d10476369 100644 --- a/packages/amplify-category-api/src/commands/api/override.ts +++ b/packages/amplify-category-api/src/commands/api/override.ts @@ -54,7 +54,7 @@ export const run = async (context: $TSContext) => { throw 'To be implemented'; } else if (service === AmplifySupportedService.APIGW) { // Migration logic goes in here - const apigwInputState = new ApigwInputState(context, selectedResourceName); + const apigwInputState = ApigwInputState.getInstance(context, selectedResourceName); if (!apigwInputState.cliInputsFileExists()) { if (await prompter.yesOrNo('File migration required to continue. Do you want to continue?', true)) { await apigwInputState.migrateApigwResource(selectedResourceName); diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts index 0eb42cb4b3a..758850e5a87 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts @@ -17,13 +17,22 @@ import { ApigwInputs, ApigwStackTransform, CrudOperation, Path, PermissionSettin import { ApigwWalkthroughReturnPromise } from './service-walkthrough-types/apigw-types'; export class ApigwInputState { + private static instance: ApigwInputState; projectRootPath: string; resourceName: string; paths: { [pathName: string]: Path }; - constructor(private readonly context: $TSContext, resourceName?: string) { + private constructor(private readonly context: $TSContext, resourceName?: string) { this.projectRootPath = pathManager.findProjectRoot(); this.resourceName = resourceName; + ApigwInputState.instance = this; + } + + public static getInstance(context: $TSContext, resourceName?: string) { + if (!ApigwInputState.instance) { + new ApigwInputState(context, resourceName); + } + return ApigwInputState.instance; } public addAdminQueriesResource = async (adminQueriesProps: AdminQueriesProps) => { @@ -205,7 +214,7 @@ export class ApigwInputState { stateManager.setResourceParametersJson(this.projectRootPath, AmplifyCategories.API, this.resourceName, {}); - const stack = new ApigwStackTransform(this.context, this.resourceName, this); + const stack = new ApigwStackTransform(this.context, this.resourceName); await stack.transform(); } diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts index a7ec91c018d..b7212e3ef53 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts @@ -28,12 +28,12 @@ export class ApigwStackTransform { cfnInputParams!: {}; resourceName: string; - constructor(context: $TSContext, resourceName: string, cliInputState?: ApigwInputState) { + constructor(context: $TSContext, resourceName: string) { this._app = new cdk.App(); this.resourceName = resourceName; // Validate the cli-inputs.json for the resource - this.cliInputsState = cliInputState ?? new ApigwInputState(context, resourceName); + this.cliInputsState = ApigwInputState.getInstance(context, this.resourceName); this.cliInputs = this.cliInputsState.getCliInputPayload(); this.cliInputsState.isCLIInputsValid(); } diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts index f8e36add0fb..f4563957d0c 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts @@ -21,7 +21,7 @@ export async function addAdminQueriesApi( context: $TSContext, apiProps: { apiName: string; functionName: string; authResourceName: string; dependsOn: $TSObject[] }, ) { - const apigwInputState = new ApigwInputState(context, apiProps.apiName); + const apigwInputState = ApigwInputState.getInstance(context, apiProps.apiName); return apigwInputState.addAdminQueriesResource(apiProps); } @@ -29,13 +29,13 @@ export async function updateAdminQueriesApi( context: $TSContext, apiProps: { apiName: string; functionName: string; authResourceName: string; dependsOn: $TSObject[] }, ) { - const apigwInputState = new ApigwInputState(context, apiProps.apiName); + const apigwInputState = ApigwInputState.getInstance(context, apiProps.apiName); // Check for migration if (!apigwInputState.cliInputsFileExists()) { await apigwInputState.migrateAdminQueries(apiProps); } else { - return apigwInputState.updateAdminQueriesResource(apiProps); + return apigwInputState.addAdminQueriesResource(apiProps); } } @@ -79,7 +79,7 @@ async function addNonContainerResource(context: $TSContext, service: string, opt } return apiName; case AmplifySupportedService.APIGW: - const apigwInputState = new ApigwInputState(context); + const apigwInputState = ApigwInputState.getInstance(context); return apigwInputState.addApigwResource(serviceWalkthroughPromise, options); default: return legacyAddResource(serviceWalkthroughPromise, context, category, service, options); @@ -263,7 +263,7 @@ async function updateNonContainerResource(context: $TSContext, service: string) case AmplifySupportedService.APPSYNC: return updateWalkthroughPromise.then(getCfnApiArtifactHandler(context).updateArtifacts); default: - const apigwInputState = new ApigwInputState(context); + const apigwInputState = ApigwInputState.getInstance(context); return apigwInputState.updateApigwResource(updateWalkthroughPromise); } } diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts index dea104bcc0d..1bfbc637088 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts @@ -151,11 +151,6 @@ async function askApiName(context: $TSContext, defaultResourceName: string) { required: true, })(input); - const adminQueriesName = 'AdminQueries'; - if (input === adminQueriesName) { - return `${adminQueriesName} is a reserved name for REST API resources for use by the auth category. Run "amplify update auth" to create an Admin Queries API.`; - } - let uniqueCheck = false; try { uniqueCheck = isResourceNameUnique(category, input); @@ -590,7 +585,7 @@ async function askLambdaArn(context: $TSContext, currentPath?: ApigwPath) { } export async function migrate(context: $TSContext, projectPath: string, resourceName: string) { - const apigwInputState = new ApigwInputState(context, resourceName); + const apigwInputState = ApigwInputState.getInstance(context, resourceName); return apigwInputState.migrateApigwResource(resourceName); }