From bf40d21ed02cc4e1bc8f689184f9062f0c72da47 Mon Sep 17 00:00:00 2001 From: David McAfee Date: Thu, 31 Aug 2023 17:50:05 -0700 Subject: [PATCH] add resolveConfig + resolveCredentials + error utils --- .../api-graphql/src/utils/errors/APIError.ts | 18 +++++++++++++++ .../src/utils/errors/assertValidationError.ts | 19 +++++++++++++++ .../api-graphql/src/utils/errors/index.ts | 6 +++++ .../src/utils/errors/validation.ts | 23 +++++++++++++++++++ packages/api-graphql/src/utils/index.ts | 5 ++++ .../api-graphql/src/utils/resolveConfig.ts | 15 ++++++++++++ .../src/utils/resolveCredentials.ts | 17 ++++++++++++++ 7 files changed, 103 insertions(+) create mode 100644 packages/api-graphql/src/utils/errors/APIError.ts create mode 100644 packages/api-graphql/src/utils/errors/assertValidationError.ts create mode 100644 packages/api-graphql/src/utils/errors/index.ts create mode 100644 packages/api-graphql/src/utils/errors/validation.ts create mode 100644 packages/api-graphql/src/utils/index.ts create mode 100644 packages/api-graphql/src/utils/resolveConfig.ts create mode 100644 packages/api-graphql/src/utils/resolveCredentials.ts diff --git a/packages/api-graphql/src/utils/errors/APIError.ts b/packages/api-graphql/src/utils/errors/APIError.ts new file mode 100644 index 00000000000..01945a2bc65 --- /dev/null +++ b/packages/api-graphql/src/utils/errors/APIError.ts @@ -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); + } +} diff --git a/packages/api-graphql/src/utils/errors/assertValidationError.ts b/packages/api-graphql/src/utils/errors/assertValidationError.ts new file mode 100644 index 00000000000..2a93d6e8bb2 --- /dev/null +++ b/packages/api-graphql/src/utils/errors/assertValidationError.ts @@ -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 }); + } +} diff --git a/packages/api-graphql/src/utils/errors/index.ts b/packages/api-graphql/src/utils/errors/index.ts new file mode 100644 index 00000000000..73dc1d1e31f --- /dev/null +++ b/packages/api-graphql/src/utils/errors/index.ts @@ -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'; diff --git a/packages/api-graphql/src/utils/errors/validation.ts b/packages/api-graphql/src/utils/errors/validation.ts new file mode 100644 index 00000000000..229d952f3b8 --- /dev/null +++ b/packages/api-graphql/src/utils/errors/validation.ts @@ -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.NoAppId]: { + message: 'Missing application id.', + }, + [APIValidationErrorCode.NoCredentials]: { + message: 'Credentials should not be empty.', + }, + [APIValidationErrorCode.NoRegion]: { + message: 'Missing region.', + }, +}; diff --git a/packages/api-graphql/src/utils/index.ts b/packages/api-graphql/src/utils/index.ts new file mode 100644 index 00000000000..0afb6284246 --- /dev/null +++ b/packages/api-graphql/src/utils/index.ts @@ -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'; diff --git a/packages/api-graphql/src/utils/resolveConfig.ts b/packages/api-graphql/src/utils/resolveConfig.ts new file mode 100644 index 00000000000..ec92a195458 --- /dev/null +++ b/packages/api-graphql/src/utils/resolveConfig.ts @@ -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 }; +}; diff --git a/packages/api-graphql/src/utils/resolveCredentials.ts b/packages/api-graphql/src/utils/resolveCredentials.ts new file mode 100644 index 00000000000..8a871e0014b --- /dev/null +++ b/packages/api-graphql/src/utils/resolveCredentials.ts @@ -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 }; +};