-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move api utility isNameUnique to core as isResourceNameUniq…
…ue (#8038)
- Loading branch information
Showing
8 changed files
with
71 additions
and
63 deletions.
There are no files selected for viewing
23 changes: 0 additions & 23 deletions
23
...y-api/src/__tests__/provider-utils/awscloudformation/utils/check-case-sensitivity.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...amplify-category-api/src/provider-utils/awscloudformation/utils/check-case-sensitivity.ts
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
packages/amplify-cli-core/src/__tests__/utils/isResourceNameUnique.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { isResourceNameUnique } from '../../utils'; | ||
import { stateManager } from '../../state-manager'; | ||
|
||
jest.mock('../../state-manager'); | ||
|
||
const stateManager_mock = stateManager as jest.Mocked<typeof stateManager>; | ||
|
||
stateManager_mock.getMeta.mockReturnValue({ | ||
api: { | ||
testBlog: {}, | ||
}, | ||
}); | ||
|
||
test('conflict exists if names differ by case only', () => { | ||
expect(() => isResourceNameUnique('api', 'testblog')).toThrowErrorMatchingInlineSnapshot( | ||
`"A resource named 'testBlog' already exists. Amplify resource names must be unique and are case-insensitive."`, | ||
); | ||
}); | ||
|
||
test('conflict does not exist if names differ by characters', () => { | ||
const result = isResourceNameUnique('api', 'newname'); | ||
expect(result).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './fileSize'; | ||
export * from './isResourceNameUnique'; | ||
export * from './open'; | ||
export * from './packageManager'; | ||
export * from './recursiveOmit'; |
17 changes: 17 additions & 0 deletions
17
packages/amplify-cli-core/src/utils/isResourceNameUnique.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { stateManager } from '../state-manager'; | ||
|
||
export const isResourceNameUnique = (category: string, resourceName: string, throwOnMatch = true) => { | ||
const meta = stateManager.getMeta(); | ||
const resourceNames = Object.keys(meta?.[category] || {}); | ||
const matchIdx = resourceNames.map(name => name.toLowerCase()).indexOf(resourceName.toLowerCase()); | ||
if (matchIdx === -1) { | ||
return true; | ||
} | ||
|
||
if (throwOnMatch) { | ||
const msg = `A resource named '${resourceNames[matchIdx]}' already exists. Amplify resource names must be unique and are case-insensitive.`; | ||
throw new Error(msg); | ||
} else { | ||
return false; | ||
} | ||
}; |