Skip to content

Commit

Permalink
feat: PubSub Add Options objects for all Providers and fix: Spelling …
Browse files Browse the repository at this point in the history
…error and deprecation of old exports (#9683)

* fix: Spelling error and add deprecation warnings to deprecated interfaces

* feat: PubSub - Add options extensions for each provider
  • Loading branch information
stocaaro authored Mar 23, 2022
1 parent 2ceaa44 commit b535af2
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 205 deletions.
10 changes: 2 additions & 8 deletions packages/api-graphql/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
import { Source, DocumentNode, GraphQLError } from 'graphql';
export { OperationTypeNode } from 'graphql';
import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
export { GRAPHQL_AUTH_MODE };

export interface GraphQLOptions {
query: string | DocumentNode;
Expand All @@ -20,14 +22,6 @@ export interface GraphQLOptions {
authToken?: string;
}

export enum GRAPHQL_AUTH_MODE {
API_KEY = 'API_KEY',
AWS_IAM = 'AWS_IAM',
OPENID_CONNECT = 'OPENID_CONNECT',
AMAZON_COGNITO_USER_POOLS = 'AMAZON_COGNITO_USER_POOLS',
AWS_LAMBDA = 'AWS_LAMBDA',
}

export interface GraphQLResult<T = object> {
data?: T;
errors?: GraphQLError[];
Expand Down
75 changes: 40 additions & 35 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

import { Auth } from './Auth';
import { CognitoHostedUIIdentityProvider, SignUpParams } from './types/Auth';
import {
CognitoUser,
CookieStorage,
appendToCognitoUserAgent,
} from 'amazon-cognito-identity-js';
import { AuthErrorStrings } from './common/AuthErrorStrings';

/**
* @deprecated use named import
*/
export default Auth;
export {
Auth,
CognitoUser,
CookieStorage,
CognitoHostedUIIdentityProvider,
SignUpParams,
appendToCognitoUserAgent,
AuthErrorStrings,
};
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

import { Auth } from './Auth';
import {
CognitoHostedUIIdentityProvider,
SignUpParams,
GRAPHQL_AUTH_MODE,
} from './types/Auth';
import {
CognitoUser,
CookieStorage,
appendToCognitoUserAgent,
} from 'amazon-cognito-identity-js';
import { AuthErrorStrings } from './common/AuthErrorStrings';

/**
* @deprecated use named import
*/
export default Auth;
export {
Auth,
CognitoUser,
CookieStorage,
CognitoHostedUIIdentityProvider,
SignUpParams,
appendToCognitoUserAgent,
AuthErrorStrings,
GRAPHQL_AUTH_MODE,
};
8 changes: 8 additions & 0 deletions packages/auth/src/types/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,11 @@ export interface IAuthDevice {
id: string;
name: string;
}

export enum GRAPHQL_AUTH_MODE {
API_KEY = 'API_KEY',
AWS_IAM = 'AWS_IAM',
OPENID_CONNECT = 'OPENID_CONNECT',
AMAZON_COGNITO_USER_POOLS = 'AMAZON_COGNITO_USER_POOLS',
AWS_LAMBDA = 'AWS_LAMBDA',
}
124 changes: 62 additions & 62 deletions packages/core/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import { InputLogEvent, LogGroup } from '@aws-sdk/client-cloudwatch-logs';
import { Credentials } from '@aws-sdk/types';

export interface AmplifyConfig {
Analytics?: object;
Auth?: object;
API?: object;
Logging?: object;
Storage?: object;
Cache?: object;
Geo?: object;
ssr?: boolean;
}

export interface ICredentials {
accessKeyId: string;
sessionToken: string;
secretAccessKey: string;
identityId: string;
authenticated: boolean;
// Long term creds do not provide an expiration date
expiration?: Date;
}

/**
* @private
* Internal use of Amplify only
*/

export type DelayFunction = (
attempt: number,
args?: any[],
error?: Error
) => number | false;

export interface LoggingProvider {
// return the name of you provider
getProviderName(): string;

// return the name of you category
getCategoryName(): string;

// configure the plugin
configure(config?: object): object;

// take logs and push to provider
pushLogs(logs: InputLogEvent[]): void;
}

export interface AWSCloudWatchProviderOptions {
logGroupName?: string;
logStreamName?: string;
region?: string;
credentials?: Credentials;
endpoint?: string;
}

export interface CloudWatchDataTracker {
eventUploadInProgress: boolean;
logEvents: InputLogEvent[];
verifiedLogGroup?: LogGroup;
}
import { InputLogEvent, LogGroup } from '@aws-sdk/client-cloudwatch-logs';
import { Credentials } from '@aws-sdk/types';

export interface AmplifyConfig {
Analytics?: object;
Auth?: object;
API?: object;
Logging?: object;
Storage?: object;
Cache?: object;
Geo?: object;
ssr?: boolean;
}

export interface ICredentials {
accessKeyId: string;
sessionToken: string;
secretAccessKey: string;
identityId: string;
authenticated: boolean;
// Long term creds do not provide an expiration date
expiration?: Date;
}

/**
* @private
* Internal use of Amplify only
*/

export type DelayFunction = (
attempt: number,
args?: any[],
error?: Error
) => number | false;

export interface LoggingProvider {
// return the name of you provider
getProviderName(): string;

// return the name of you category
getCategoryName(): string;

// configure the plugin
configure(config?: object): object;

// take logs and push to provider
pushLogs(logs: InputLogEvent[]): void;
}

export interface AWSCloudWatchProviderOptions {
logGroupName?: string;
logStreamName?: string;
region?: string;
credentials?: Credentials;
endpoint?: string;
}

export interface CloudWatchDataTracker {
eventUploadInProgress: boolean;
logEvents: InputLogEvent[];
verifiedLogGroup?: LogGroup;
}
17 changes: 10 additions & 7 deletions packages/datastore/__tests__/authStrategies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ModelOperation,
} from '../src/types';
import { NAMESPACES } from '../src/util';
import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';

describe('Auth Strategies', () => {
describe('multiAuthStrategy', () => {
Expand Down Expand Up @@ -423,8 +424,8 @@ describe('Auth Strategies', () => {

describe('defaultAuthStrategy', () => {
test('default', () => {
const defaultAuthStrategy = require('../src/authModeStrategies/defaultAuthStrategy')
.defaultAuthStrategy;
const defaultAuthStrategy =
require('../src/authModeStrategies/defaultAuthStrategy').defaultAuthStrategy;
const schema = getAuthSchema();
expect(
defaultAuthStrategy({
Expand All @@ -448,8 +449,8 @@ async function testMultiAuthStrategy({
}) {
mockCurrentUser({ hasAuthenticatedUser });

const multiAuthStrategy = require('../src/authModeStrategies/multiAuthStrategy')
.multiAuthStrategy;
const multiAuthStrategy =
require('../src/authModeStrategies/multiAuthStrategy').multiAuthStrategy;

const schema = getAuthSchema(authRules);

Expand Down Expand Up @@ -537,13 +538,15 @@ function mockCurrentUser({
hasAuthenticatedUser: boolean;
}) {
jest.mock('@aws-amplify/auth', () => ({
currentAuthenticatedUser: () =>
new Promise((res, rej) => {
currentAuthenticatedUser: () => {
return new Promise((res, rej) => {
if (hasAuthenticatedUser) {
res(hasAuthenticatedUser);
} else {
rej(hasAuthenticatedUser);
}
}),
});
},
GRAPHQL_AUTH_MODE,
}));
}
53 changes: 27 additions & 26 deletions packages/pubsub/src/Providers/AWSAppSyncProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { MqttOverWSProvider } from './MqttOverWSProvider';

const logger = new Logger('AWSAppSyncProvider');

/**
* @deprecated Unused, all usecases have migrated to AWSAppSyncRealtimeProvider
*/
export class AWSAppSyncProvider extends MqttOverWSProvider {
protected get endpoint() {
throw new Error('Not supported');
Expand Down Expand Up @@ -96,9 +99,9 @@ export class AWSAppSyncProvider extends MqttOverWSProvider {
const { mqttConnections = [], newSubscriptions } = options;

// creates a map of {"topic": "alias"}
const newAliases = Object.entries(
newSubscriptions
).map(([alias, v]: [string, { topic: string }]) => [v.topic, alias]);
const newAliases = Object.entries(newSubscriptions).map(
([alias, v]: [string, { topic: string }]) => [v.topic, alias]
);

// Merge new aliases with old ones
this._topicAlias = new Map([
Expand All @@ -107,31 +110,29 @@ export class AWSAppSyncProvider extends MqttOverWSProvider {
]);

// group by urls
const map: [
string,
{ url: string; topics: Set<string> }
][] = Object.entries(
targetTopics.reduce((acc, elem) => {
const connectionInfoForTopic = mqttConnections.find(
c => c.topics.indexOf(elem) > -1
);

if (connectionInfoForTopic) {
const { client: clientId, url } = connectionInfoForTopic;

if (!acc[clientId]) {
acc[clientId] = {
url,
topics: new Set(),
};
const map: [string, { url: string; topics: Set<string> }][] =
Object.entries(
targetTopics.reduce((acc, elem) => {
const connectionInfoForTopic = mqttConnections.find(
c => c.topics.indexOf(elem) > -1
);

if (connectionInfoForTopic) {
const { client: clientId, url } = connectionInfoForTopic;

if (!acc[clientId]) {
acc[clientId] = {
url,
topics: new Set(),
};
}

acc[clientId].topics.add(elem);
}

acc[clientId].topics.add(elem);
}

return acc;
}, {})
);
return acc;
}, {})
);

// reconnect everything we have in the map
await Promise.all(
Expand Down
Loading

0 comments on commit b535af2

Please sign in to comment.