-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathutils.ts
More file actions
120 lines (107 loc) · 3.61 KB
/
utils.ts
File metadata and controls
120 lines (107 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Amplify } from 'aws-amplify';
import {
CONNECTION_STATE_CHANGE,
ConnectionState,
generateClient,
} from 'aws-amplify/data';
import { Hub, ConsoleLogger } from 'aws-amplify/utils';
import type { Schema } from './amplify/data/resource';
import { WebSocket } from 'ws';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - outputs is loaded dynamically in GHA; expected undefined locally
import outputs from './amplify_outputs.json';
// TODO: use imported type from `aws-amplify/data` once it's fixed
export type Client = ReturnType<typeof generateClient<Schema>>;
type ConfigureAmplifyAndGenerateClientParams = {
enableDebugLogging?: boolean;
amplifyOptions?: any;
apiClientOptions?: any;
};
/**
* Configures Amplify and returns API client
* @param enableDebugLogging - enables debug logging
* @param amplifyOptions - options to pass to Amplify.configure
* @param apiClientOptions - options to pass to generateClient
* @returns API client
*/
export const configureAmplifyAndGenerateClient = ({
enableDebugLogging = false,
amplifyOptions = {},
apiClientOptions = {},
}: ConfigureAmplifyAndGenerateClientParams): Client => {
console.log('configuring Amplify and generating client..');
Amplify.configure(outputs, amplifyOptions);
if (enableDebugLogging) {
ConsoleLogger.LOG_LEVEL = 'DEBUG';
Hub.listen('core', (data: any) => {
if (data.payload.event === 'configure') {
console.log('API configuration details:', data.payload.data.API);
}
});
}
return generateClient<Schema>(apiClientOptions);
};
type AddWebsocketPolyfillParams = {
enableConnectionStateLogging?: boolean;
};
/**
* Adds the WebSocket API globally.
* Subscriptions do not work in Node.js environment without the WebSocket API.
*/
export const addWebsocketPolyfill = ({
enableConnectionStateLogging = false,
}: AddWebsocketPolyfillParams) => {
(global as any).WebSocket = WebSocket;
if (enableConnectionStateLogging) {
Hub.listen('api', (data: any) => {
const { payload } = data;
if (payload.event === CONNECTION_STATE_CHANGE) {
const connectionState = payload.data.connectionState as ConnectionState;
console.log(
'Connection state has changed. Connection state: ',
connectionState,
);
}
});
}
};
/**
* Util that takes model operation response and throws an error if errors
* are present, or if the data is undefined.
* @param response - model operation response
* @param operation - operation name
* @returns data from response
*/
export const expectDataReturnWithoutErrors = <T>(
response: { data?: T; errors?: any },
operation: string,
): T => {
if (response.errors) {
console.log(`error on ${operation}:`, response.errors);
throw new Error(JSON.stringify(response.errors));
}
if (!response.data) {
throw new Error(`no response data for ${operation}`);
}
return response.data;
};
/**
* Function that listens for "Subscription ack" event, and once it receives it,
* resolves.
* Jest will timeout if the test does not complete within the default timeout of
* 5 seconds, so no timeout handling is required.
*/
export const waitForSubscriptionAck = () =>
new Promise<void>((resolve) => {
const cancel = Hub.listen('api', async (data: any) => {
const { payload } = data;
// TODO: update once we add export:
if (payload.event === 'Subscription ack') {
console.log('Subscription ack received:', payload.data);
// Stop listening for msgs once we receive the ack:
cancel();
// Resolve so test execution can continue:
resolve();
}
});
});