Skip to content

Commit

Permalink
add resolveConfig + resolveCredentials + error utils
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mcafee committed Sep 1, 2023
1 parent 1d88977 commit 8b869b4
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/api-graphql/src/utils/errors/APIError.ts
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 packages/api-graphql/src/utils/errors/assertValidationError.ts
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 });
}
}
6 changes: 6 additions & 0 deletions packages/api-graphql/src/utils/errors/index.ts
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';
23 changes: 23 additions & 0 deletions packages/api-graphql/src/utils/errors/validation.ts
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.',
},
};
5 changes: 5 additions & 0 deletions packages/api-graphql/src/utils/index.ts
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';
17 changes: 17 additions & 0 deletions packages/api-graphql/src/utils/resolveConfig.ts
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 { Amplify } from '@aws-amplify/core';
import { APIValidationErrorCode, assertValidationError } from './errors';

/**
* @internal
*/
export const resolveConfig = () => {
// TODO V6
const { appId, region } = Amplify.getConfig().API ?? {};
assertValidationError(!!appId, APIValidationErrorCode.NoAppId);
assertValidationError(!!region, APIValidationErrorCode.NoRegion);
// TODO V6
return { appId, region };
};
14 changes: 14 additions & 0 deletions packages/api-graphql/src/utils/resolveCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { fetchAuthSession } from '@aws-amplify/core';
import { APIValidationErrorCode, assertValidationError } from './errors';

/**
* @internal
*/
export const resolveCredentials = async () => {
const { credentials, identityId } = await fetchAuthSession();
assertValidationError(!!credentials, APIValidationErrorCode.NoCredentials);
return { credentials, identityId };
};

0 comments on commit 8b869b4

Please sign in to comment.