-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add resolveConfig + resolveCredentials + error utils
- Loading branch information
1 parent
1d88977
commit bf40d21
Showing
7 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
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,18 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { AmplifyError, ErrorParams } from '@aws-amplify/core/internals/utils'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export class APIError extends AmplifyError { | ||
constructor(params: ErrorParams) { | ||
super(params); | ||
|
||
// Hack for making the custom error class work when transpiled to es5 | ||
// TODO: Delete the following 2 lines after we change the build target to >= es2015 | ||
this.constructor = APIError; | ||
Object.setPrototypeOf(this, APIError.prototype); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/api-graphql/src/utils/errors/assertValidationError.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,19 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { APIError } from './APIError'; | ||
import { APIValidationErrorCode, validationErrorMap } from './validation'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function assertValidationError( | ||
assertion: boolean, | ||
name: APIValidationErrorCode | ||
): asserts assertion { | ||
const { message, recoverySuggestion } = validationErrorMap[name]; | ||
|
||
if (!assertion) { | ||
throw new APIError({ name, message, recoverySuggestion }); | ||
} | ||
} |
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,6 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { APIError } from './APIError'; | ||
export { assertValidationError } from './assertValidationError'; | ||
export { APIValidationErrorCode, validationErrorMap } from './validation'; |
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 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { AmplifyErrorMap } from '@aws-amplify/core/internals/utils'; | ||
|
||
// TODO V6 - include all errors: | ||
export enum APIValidationErrorCode { | ||
NoAppId = 'NoAppId', | ||
NoCredentials = 'NoCredentials', | ||
NoRegion = 'NoRegion', | ||
} | ||
|
||
export const validationErrorMap: AmplifyErrorMap<APIValidationErrorCode> = { | ||
[APIValidationErrorCode.NoAppId]: { | ||
message: 'Missing application id.', | ||
}, | ||
[APIValidationErrorCode.NoCredentials]: { | ||
message: 'Credentials should not be empty.', | ||
}, | ||
[APIValidationErrorCode.NoRegion]: { | ||
message: 'Missing region.', | ||
}, | ||
}; |
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,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { resolveConfig } from './resolveConfig'; | ||
export { resolveCredentials } from './resolveCredentials'; |
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,15 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Amplify } from '@aws-amplify/core'; | ||
import { APIValidationErrorCode, assertValidationError } from './errors'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const resolveConfig = () => { | ||
const { appId, region } = Amplify.getConfig().API ?? {}; | ||
assertValidationError(!!appId, APIValidationErrorCode.NoAppId); | ||
assertValidationError(!!region, APIValidationErrorCode.NoRegion); | ||
return { appId, region }; | ||
}; |
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 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { fetchAuthSession } from '@aws-amplify/core'; | ||
import { AnalyticsValidationErrorCode, assertValidationError } from './errors'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const resolveCredentials = async () => { | ||
const { credentials, identityId } = await fetchAuthSession(); | ||
assertValidationError( | ||
!!credentials, | ||
AnalyticsValidationErrorCode.NoCredentials | ||
); | ||
return { credentials, identityId }; | ||
}; |