Skip to content

Commit 82ac565

Browse files
committed
Convert aws-amplify-vue to Typescript
1 parent 9a1055a commit 82ac565

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2214
-1735
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
"pre-commit": "pretty-quick --staged"
2727
}
2828
},
29-
"workspaces": [
30-
"packages/*"
31-
],
29+
"workspaces": {
30+
"packages": ["packages/*"],
31+
"nohoist": ["**/@vue/**"]
32+
},
3233
"repository": {
3334
"type": "git",
3435
"url": "https://github.com/aws-amplify/amplify-js.git"

packages/amazon-cognito-identity-js/index.d.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ declare module 'amazon-cognito-identity-js' {
5757
Storage?: ICognitoStorage;
5858
}
5959

60+
export type CognitoSigninChallenge = 'SMS_MFA' | 'SOFTWARE_TOKEN_MFA';
61+
export type CognitoChallenge =
62+
| CognitoSigninChallenge
63+
| 'CUSTOM_CHALLENGE'
64+
| 'DEVICE_SRP_AUTH'
65+
| 'MFA_SETUP'
66+
| 'NEW_PASSWORD_REQUIRED'
67+
| 'SELECT_MFA_TYPE'
68+
| 'DEVICE_PASSWORD_VERIFIER';
69+
6070
export class CognitoUser {
71+
challengeName?: CognitoChallenge;
72+
6173
constructor(data: ICognitoUserData);
6274

6375
public setSignInUserSession(
@@ -316,10 +328,10 @@ declare module 'amazon-cognito-identity-js' {
316328
public isValid(): boolean;
317329
}
318330
/*
319-
export class CognitoIdentityServiceProvider {
320-
public config: AWS.CognitoIdentityServiceProvider.Types.ClientConfiguration;
321-
}
322-
*/
331+
export class CognitoIdentityServiceProvider {
332+
public config: AWS.CognitoIdentityServiceProvider.Types.ClientConfiguration;
333+
}
334+
*/
323335
export class CognitoAccessToken {
324336
payload: { [key: string]: any };
325337

packages/api/src/API.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
1111
* and limitations under the License.
1212
*/
13-
import { OperationDefinitionNode, GraphQLError } from 'graphql';
14-
import { print } from 'graphql/language/printer';
15-
import { parse } from 'graphql/language/parser';
13+
import {
14+
OperationDefinitionNode,
15+
OperationTypeNode,
16+
GraphQLError,
17+
parse,
18+
print,
19+
} from 'graphql';
1620
import * as Observable from 'zen-observable';
1721
import { RestClient as RestClass } from './RestClient';
1822
import Amplify, {
@@ -40,8 +44,7 @@ export default class APIClass {
4044
* @private
4145
*/
4246
private _options;
43-
private _api = null;
44-
private _pubSub = Amplify.PubSub;
47+
private _api: RestClass = null;
4548

4649
/**
4750
* Initialize Storage with AWS configuration
@@ -334,7 +337,7 @@ export default class APIClass {
334337
* to get the operation type
335338
* @param operation
336339
*/
337-
getGraphqlOperationType(operation) {
340+
getGraphqlOperationType(operation: string): OperationTypeNode {
338341
const doc = parse(operation);
339342
const {
340343
definitions: [{ operation: operationType }],

packages/auth/src/Auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
CognitoIdToken,
6060
CognitoRefreshToken,
6161
CognitoAccessToken,
62+
CognitoSigninChallenge,
6263
} from 'amazon-cognito-identity-js';
6364

6465
import { parse } from 'url';
@@ -857,7 +858,7 @@ export default class AuthClass {
857858
public confirmSignIn(
858859
user: CognitoUser | any,
859860
code: string,
860-
mfaType?: 'SMS_MFA' | 'SOFTWARE_TOKEN_MFA' | null
861+
mfaType?: CognitoSigninChallenge | null
861862
): Promise<CognitoUser | any> {
862863
if (!code) {
863864
return this.rejectAuthError(AuthErrorTypes.EmptyCode);
@@ -1767,7 +1768,7 @@ export default class AuthClass {
17671768
logger.debug('AWS credentials', credentials);
17681769
}
17691770

1770-
/*
1771+
/*
17711772
Prior to the request we do sign the custom state along with the state we set. This check will verify
17721773
if there is a dash indicated when setting custom state from the request. If a dash is contained
17731774
then there is custom state present on the state string.

packages/aws-amplify-vue/__mocks__/Amplify.mocks.js

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export class Logger {
2+
private name: string;
3+
public info = jest.fn();
4+
public error = jest.fn();
5+
6+
constructor(name: string) {
7+
this.name = name;
8+
}
9+
};
10+
11+
export const Auth = {
12+
currentAuthenticatedUser: jest.fn(() => Promise.resolve({})),
13+
verifyCurrentUserAttribute: jest.fn(() =>
14+
Promise.resolve({ username: 'myTestUsername' })
15+
),
16+
completeNewPassword: jest.fn(() => Promise.resolve({})),
17+
confirmSignIn: jest.fn(() => Promise.resolve({})),
18+
confirmSignUp: jest.fn(() => Promise.resolve({})),
19+
forgotPassword: jest.fn(() => Promise.resolve({})),
20+
forgotPasswordSubmit: jest.fn(() => Promise.resolve({})),
21+
resendSignUp: jest.fn(() => Promise.resolve({})),
22+
setPreferredMFA: jest.fn(() => Promise.resolve({})),
23+
setupTOTP: jest.fn(() => Promise.resolve('gibberish')),
24+
signIn: jest.fn(() =>
25+
Promise.resolve({
26+
challengeName: 'CUSTOM_CHALLENGE',
27+
challengeParam: { trigger: 'true' },
28+
})
29+
),
30+
signOut: jest.fn(() => Promise.resolve({})),
31+
signUp: jest.fn(() => Promise.resolve({})),
32+
verifiedContact: jest.fn(() => Promise.resolve({})),
33+
verifyTotpToken: jest.fn(() => Promise.resolve({})),
34+
};
35+
36+
export class AuthClass {};
37+
38+
export const Interactions = {
39+
onComplete: jest.fn(() => Promise.resolve({})),
40+
send: jest.fn(() => Promise.resolve({})),
41+
};
42+
43+
export const Storage = {
44+
get: jest.fn(() => Promise.resolve({})),
45+
put: jest.fn(() => Promise.resolve({ key: 'testKey' })),
46+
list: jest.fn(() => Promise.resolve({})),
47+
};
48+
49+
export const I18n = {
50+
get: jest.fn(key => `i18n ${key}`),
51+
};
52+
53+
export const XR = {
54+
loadScene: jest.fn(() => Promise.resolve({})),
55+
start: jest.fn(),
56+
isMuted: jest.fn(() => false),
57+
isVRCapable: jest.fn(() => false),
58+
onSceneEvent: jest.fn(),
59+
setMuted: jest.fn(),
60+
enableAudio: jest.fn(),
61+
enterVR: jest.fn(),
62+
exitVR: jest.fn(),
63+
isVRPresentationActive: jest.fn(() => false),
64+
};

packages/aws-amplify-vue/__tests__/Amplify.test.js renamed to packages/aws-amplify-vue/__tests__/Amplify.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue';
2-
import App from '../src/Amplify.vue';
3-
import AmplifyEventBus from '../src/events/AmplifyEventBus';
4-
import AmplifyPlugin from '../src/plugins/AmplifyPlugin';
2+
import App from '@/Amplify.vue';
3+
import AmplifyEventBus from '@/events/AmplifyEventBus';
4+
import AmplifyPlugin from '@/plugins/AmplifyPlugin';
55
import * as AmplifyMocks from '../__mocks__/Amplify.mocks';
66

77
Vue.use(AmplifyPlugin, AmplifyMocks);

packages/aws-amplify-vue/__tests__/AmplifyPlugin.test.js renamed to packages/aws-amplify-vue/__tests__/AmplifyPlugin.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue';
2-
import App from '../src/Amplify.vue';
3-
import AmplifyPlugin from '../src/plugins/AmplifyPlugin';
2+
import App from '@/Amplify.vue';
3+
import AmplifyPlugin from '@/plugins/AmplifyPlugin';
44

55
Vue.use(AmplifyPlugin, {});
66

packages/aws-amplify-vue/__tests__/Authenticator.test.js renamed to packages/aws-amplify-vue/__tests__/Authenticator.test.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import Vue from 'vue';
2-
import { shallowMount } from '@vue/test-utils'; // eslint-disable-line
3-
import Authenticator from '../src/components/authenticator/Authenticator.vue';
4-
import AmplifyEventBus from '../src/events/AmplifyEventBus';
5-
import * as components from '../src/components';
6-
import * as AmplifyEntry from '../src/Amplify.vue'; //eslint-disable-line
7-
import AmplifyPlugin from '../src/plugins/AmplifyPlugin';
2+
import { mocked } from 'ts-jest/utils';
3+
import { shallowMount } from '@vue/test-utils';
4+
import Authenticator from '@/components/authenticator/Authenticator.vue';
5+
import AmplifyEventBus from '@/events/AmplifyEventBus';
6+
import * as components from '@/components';
7+
import AmplifyPlugin from '@/plugins/AmplifyPlugin';
88
import * as AmplifyMocks from '../__mocks__/Amplify.mocks';
9-
import dependency from '../src/services/getUser';
9+
import dependency from '@/services/getUser';
1010

1111
Vue.use(AmplifyPlugin, AmplifyMocks);
12-
jest.mock('../src/services/getUser');
12+
jest.mock('@/services/getUser');
1313

1414
describe('Authenticator', () => {
15+
let authenticator;
16+
17+
beforeEach(() => {
18+
authenticator = new Authenticator();
19+
});
20+
1521
it('has a mounted hook', () => {
16-
expect(typeof Authenticator.mounted).toBe('function');
22+
expect(authenticator.$options.mounted.length).toEqual(1);
23+
expect(typeof authenticator.$options.mounted[0]).toBe('function');
1724
});
1825

1926
it('sets the correct default data', () => {
20-
expect(typeof Authenticator.data).toBe('function');
21-
const defaultData = Authenticator.data();
22-
expect(defaultData.user.username).toBe(null);
23-
expect(defaultData.logger).toEqual({});
27+
expect(typeof authenticator.$options.data).toEqual('function');
28+
const defaultData = authenticator.$options.data();
29+
expect(defaultData.user).toBe(null);
30+
expect(defaultData.logger).toEqual(null);
2431
expect(defaultData.displayMap).toEqual({});
2532
expect(defaultData.error).toEqual('');
2633
});
@@ -30,7 +37,7 @@ describe('Authenticator', () => {
3037

3138
beforeEach(() => {
3239
wrapper = shallowMount(Authenticator);
33-
dependency.mockImplementation(() => Promise.resolve({}));
40+
mocked(dependency).mockImplementation(() => Promise.resolve(null));
3441
});
3542

3643
it('...it should use the amplify plugin with passed modules', () => {
@@ -60,15 +67,15 @@ describe('Authenticator', () => {
6067

6168
describe('...when it is mounted and displaying components...', () => {
6269
let wrapper;
63-
let mockDisplayMap = {};
70+
let mockDisplayMap = {} as any;
6471
beforeEach(() => {
6572
wrapper = shallowMount(Authenticator, {
6673
// methods: {
6774
// updateDisplayMap: mockUpdateDisplayMap,
6875
// }
6976
});
7077
wrapper.vm.updateDisplayMap = jest.fn(() => mockDisplayMap);
71-
dependency.mockImplementation(() => Promise.reject(new Error()));
78+
mocked(dependency).mockImplementation(() => Promise.reject(new Error()));
7279
mockDisplayMap = {
7380
showSignIn: false,
7481
showSignUp: false,
@@ -122,9 +129,9 @@ describe('Authenticator', () => {
122129
});
123130

124131
it('...should set user and options data on localUser event emission', () => {
125-
const testUser = { username: 'IAMEMITTED' };
132+
const testUser = { getUsername: () => 'IAMEMITTED' };
126133
AmplifyEventBus.$emit('localUser', testUser);
127-
expect(wrapper.vm.user.username).toEqual(testUser.username);
134+
expect(wrapper.vm.user.getUsername()).toEqual(testUser.getUsername());
128135
});
129136
});
130137
});

packages/aws-amplify-vue/__tests__/Chatbot.test.js renamed to packages/aws-amplify-vue/__tests__/Chatbot.test.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
/* eslint-disable */
21
import Vue from 'vue';
3-
import { shallowMount, mount } from '@vue/test-utils';
4-
import * as AmplifyUI from '@aws-amplify/ui';
5-
import Chatbot from '../src/components/interactions/Chatbot.vue';
6-
import AmplifyEventBus from '../src/events/AmplifyEventBus';
7-
import AmplifyPlugin from '../src/plugins/AmplifyPlugin';
2+
import { shallowMount } from '@vue/test-utils';
3+
import Chatbot from '@/components/interactions/Chatbot.vue';
4+
import AmplifyEventBus from '@/events/AmplifyEventBus';
5+
import AmplifyPlugin from '@/plugins/AmplifyPlugin';
86
import * as AmplifyMocks from '../__mocks__/Amplify.mocks';
9-
/* eslint-enable */
107

118
Vue.use(AmplifyPlugin, AmplifyMocks);
129

1310
describe('Chatbot', () => {
11+
let chatbot;
12+
13+
beforeEach(() => {
14+
chatbot = new Chatbot();
15+
});
16+
1417
it('has a mounted hook', () => {
15-
expect(typeof Chatbot.mounted).toBe('function');
18+
expect(chatbot.$options.mounted.length).toEqual(1);
19+
expect(typeof chatbot.$options.mounted[0]).toBe('function');
1620
});
1721

1822
it('sets the correct default data', () => {
19-
expect(typeof Chatbot.data).toBe('function');
20-
const defaultData = Chatbot.data();
23+
expect(typeof chatbot.$options.data).toBe('function');
24+
const defaultData = chatbot.$options.data();
2125
expect(defaultData.inputText).toBe('');
2226
expect(defaultData.messages.length).toEqual(0);
23-
expect(defaultData.logger).toEqual({});
27+
expect(defaultData.logger).toEqual(null);
2428
expect(defaultData.error).toEqual('');
2529
});
2630

0 commit comments

Comments
 (0)