Skip to content

Commit c10f0dc

Browse files
committed
UIE-204 Narrow Ajax Usage pt6
- more workspaces ajax shifts to use ajax subareas directly. - converted Cbas ajax sub-module to Typescript (minimal conversion) - updated a small set of workflow related tests with usage overlap to use shifted ajax mocks. - shifted useMetrics ajax usage
1 parent 9647d6a commit c10f0dc

21 files changed

+564
-536
lines changed

src/libs/ajax/Groups.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ export const Groups = (signal?: AbortSignal) => ({
9797
},
9898
});
9999
export type GroupsContract = ReturnType<typeof Groups>;
100+
export type GroupContract = ReturnType<GroupsContract['group']>;

src/libs/ajax/WorkspaceDataService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export type Capabilities = {
7373
[key in Capability]: boolean;
7474
};
7575

76-
export const WorkspaceData = (signal) => ({
76+
export const WorkspaceData = (signal?: AbortSignal) => ({
7777
getSchema: async (root: string, instanceId: string): Promise<RecordTypeSchema[]> => {
7878
const res = await fetchWDS(root)(`${instanceId}/types/v0.2`, _.merge(authOpts(), { signal }));
7979
return res.json();
@@ -227,3 +227,5 @@ export const WorkspaceData = (signal) => ({
227227
return res.json();
228228
},
229229
});
230+
231+
export type WorkspaceDataAjaxContract = ReturnType<typeof WorkspaceData>;

src/libs/ajax/metrics/useMetrics.test.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
import { renderHook } from '@testing-library/react';
2-
import { Ajax } from 'src/libs/ajax';
2+
import { Metrics, MetricsContract } from 'src/libs/ajax/Metrics';
33
import Events from 'src/libs/events';
4-
import { asMockedFn } from 'src/testing/test-utils';
4+
import { asMockedFn, partial } from 'src/testing/test-utils';
55

66
import { useMetricsEvent } from './useMetrics';
77

8-
type AjaxExports = typeof import('src/libs/ajax');
9-
jest.mock('src/libs/ajax', (): AjaxExports => {
10-
return {
11-
...jest.requireActual('src/libs/ajax'),
12-
Ajax: jest.fn(),
13-
};
14-
});
15-
16-
type AjaxContract = ReturnType<typeof Ajax>;
17-
type AjaxMetricsContract = AjaxContract['Metrics'];
8+
jest.mock('src/libs/ajax/Metrics');
189

1910
describe('useMetricsEvent', () => {
2011
it('calls event logger', () => {
2112
// Arrange
2213
const watchCaptureEvent = jest.fn();
23-
const mockMetrics: Partial<AjaxMetricsContract> = {
24-
captureEvent: (event, details) => watchCaptureEvent(event, details),
25-
};
26-
const mockAjax: Partial<AjaxContract> = {
27-
Metrics: mockMetrics as AjaxMetricsContract,
28-
};
29-
asMockedFn(Ajax).mockImplementation(() => mockAjax as AjaxContract);
14+
15+
asMockedFn(Metrics).mockReturnValue(
16+
partial<MetricsContract>({
17+
captureEvent: (event, details) => watchCaptureEvent(event, details),
18+
})
19+
);
3020

3121
// Act
3222
const renderedHook = renderHook(() => useMetricsEvent());

src/libs/ajax/metrics/useMetrics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMemo } from 'react';
2-
import { Ajax } from 'src/libs/ajax';
2+
import { Metrics } from 'src/libs/ajax/Metrics';
33
import { MetricsEventName } from 'src/libs/events';
44

55
export type CaptureEventFn = (event: MetricsEventName, details?: Record<string, any>, refreshAppcues?: boolean) => void;
@@ -9,7 +9,7 @@ export interface MetricsProvider {
99
}
1010

1111
export const useMetricsEvent = (): MetricsProvider => {
12-
const sendEvent = useMemo(() => Ajax().Metrics.captureEvent, []);
12+
const sendEvent = useMemo(() => Metrics().captureEvent, []);
1313
// By returning a wrapper function, we can handle the fire-and-forget promise mechanics properly here
1414
// instead of burdening the consumer side with silencing the Typescript/lint complaints, which can be
1515
// quite awkward in some nested functional uses.

src/libs/ajax/workflows-app/Cbas.js renamed to src/libs/ajax/workflows-app/Cbas.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import qs from 'qs';
44
import { authOpts } from 'src/auth/auth-session';
55
import { fetchFromProxy } from 'src/libs/ajax/ajax-common';
66

7-
export const Cbas = (signal) => ({
7+
export const Cbas = (signal?: AbortSignal) => ({
88
status: async (cbasUrlRoot) => {
99
const res = await fetchFromProxy(cbasUrlRoot)('status', _.mergeAll([authOpts(), { signal, method: 'GET' }]));
1010
return res.json();
@@ -14,19 +14,28 @@ export const Cbas = (signal) => ({
1414
return res.json();
1515
},
1616
capabilities: async (cbasUrlRoot) => {
17-
const res = await fetchFromProxy(cbasUrlRoot)('capabilities/v1', _.mergeAll([authOpts(), { signal, method: 'GET' }]));
17+
const res = await fetchFromProxy(cbasUrlRoot)(
18+
'capabilities/v1',
19+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
20+
);
1821
return res.json();
1922
},
2023
runs: {
2124
get: async (cbasUrlRoot, submissionId) => {
2225
const keyParams = qs.stringify({ run_set_id: submissionId });
23-
const res = await fetchFromProxy(cbasUrlRoot)(`api/batch/v1/runs?${keyParams}`, _.mergeAll([authOpts(), { signal, method: 'GET' }]));
26+
const res = await fetchFromProxy(cbasUrlRoot)(
27+
`api/batch/v1/runs?${keyParams}`,
28+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
29+
);
2430
return res.json();
2531
},
2632
},
2733
methods: {
2834
post: async (cbasUrlRoot, payload) => {
29-
const res = await fetchFromProxy(cbasUrlRoot)('api/batch/v1/methods', _.mergeAll([authOpts(), jsonBody(payload), { signal, method: 'POST' }]));
35+
const res = await fetchFromProxy(cbasUrlRoot)(
36+
'api/batch/v1/methods',
37+
_.mergeAll([authOpts(), jsonBody(payload), { signal, method: 'POST' }])
38+
);
3039
return res.json();
3140
},
3241
archive: async (cbasUrlRoot, methodId) => {
@@ -39,38 +48,62 @@ export const Cbas = (signal) => ({
3948
},
4049
getWithVersions: async (cbasUrlRoot) => {
4150
const keyParams = qs.stringify({ show_versions: true });
42-
const res = await fetchFromProxy(cbasUrlRoot)(`api/batch/v1/methods?${keyParams}`, _.mergeAll([authOpts(), { signal, method: 'GET' }]));
51+
const res = await fetchFromProxy(cbasUrlRoot)(
52+
`api/batch/v1/methods?${keyParams}`,
53+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
54+
);
4355
return res.json();
4456
},
4557
getWithoutVersions: async (cbasUrlRoot) => {
4658
const keyParams = qs.stringify({ show_versions: false });
47-
const res = await fetchFromProxy(cbasUrlRoot)(`api/batch/v1/methods?${keyParams}`, _.mergeAll([authOpts(), { signal, method: 'GET' }]));
59+
const res = await fetchFromProxy(cbasUrlRoot)(
60+
`api/batch/v1/methods?${keyParams}`,
61+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
62+
);
4863
return res.json();
4964
},
5065
getById: async (cbasUrlRoot, methodId) => {
5166
const keyParams = qs.stringify({ method_id: methodId });
52-
const res = await fetchFromProxy(cbasUrlRoot)(`api/batch/v1/methods?${keyParams}`, _.mergeAll([authOpts(), { signal, method: 'GET' }]));
67+
const res = await fetchFromProxy(cbasUrlRoot)(
68+
`api/batch/v1/methods?${keyParams}`,
69+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
70+
);
5371
return res.json();
5472
},
5573
},
5674
runSets: {
5775
get: async (cbasUrlRoot) => {
58-
const res = await fetchFromProxy(cbasUrlRoot)('api/batch/v1/run_sets', _.mergeAll([authOpts(), { signal, method: 'GET' }]));
76+
const res = await fetchFromProxy(cbasUrlRoot)(
77+
'api/batch/v1/run_sets',
78+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
79+
);
5980
return res.json();
6081
},
6182
post: async (cbasUrlRoot, payload) => {
62-
const res = await fetchFromProxy(cbasUrlRoot)('api/batch/v1/run_sets', _.mergeAll([authOpts(), { signal, method: 'POST' }, jsonBody(payload)]));
83+
const res = await fetchFromProxy(cbasUrlRoot)(
84+
'api/batch/v1/run_sets',
85+
_.mergeAll([authOpts(), { signal, method: 'POST' }, jsonBody(payload)])
86+
);
6387
return res.json();
6488
},
6589
getForMethod: async (cbasUrlRoot, methodId, pageSize) => {
6690
const keyParams = qs.stringify({ method_id: methodId, page_size: pageSize }, { arrayFormat: 'repeat' });
67-
const res = await fetchFromProxy(cbasUrlRoot)(`api/batch/v1/run_sets?${keyParams}`, _.mergeAll([authOpts(), { signal, method: 'GET' }]));
91+
const res = await fetchFromProxy(cbasUrlRoot)(
92+
`api/batch/v1/run_sets?${keyParams}`,
93+
_.mergeAll([authOpts(), { signal, method: 'GET' }])
94+
);
6895
return res.json();
6996
},
7097
cancel: async (cbasUrlRoot, runSetId) => {
7198
const keyParams = qs.stringify({ run_set_id: runSetId });
72-
const res = await fetchFromProxy(cbasUrlRoot)(`api/batch/v1/run_sets/abort?${keyParams}`, _.mergeAll([authOpts(), { signal, method: 'POST' }]));
99+
const res = await fetchFromProxy(cbasUrlRoot)(
100+
`api/batch/v1/run_sets/abort?${keyParams}`,
101+
_.mergeAll([authOpts(), { signal, method: 'POST' }])
102+
);
73103
return res.json();
74104
},
75105
},
76106
});
107+
108+
export type CbasAjaxContract = ReturnType<typeof Cbas>;
109+
export type CbasMethodsContract = CbasAjaxContract['methods'];

src/libs/ajax/workspaces/Workspaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,4 @@ export const Workspaces = (signal?: AbortSignal) => ({
641641

642642
export type WorkspacesAjaxContract = ReturnType<typeof Workspaces>;
643643
export type WorkspaceContract = ReturnType<WorkspacesAjaxContract['workspace']>;
644+
export type WorkspaceV2Contract = ReturnType<WorkspacesAjaxContract['workspaceV2']>;

src/pages/ImportWorkflow/ImportWorkflow.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { ValidatedInput } from 'src/components/input';
88
import { TopBar } from 'src/components/TopBar';
99
import WDLViewer from 'src/components/WDLViewer';
1010
import importBackground from 'src/images/hex-import-background.svg';
11-
import { Ajax } from 'src/libs/ajax';
1211
import { Apps } from 'src/libs/ajax/leonardo/Apps';
12+
import { Metrics } from 'src/libs/ajax/Metrics';
1313
import { useMetricsEvent } from 'src/libs/ajax/metrics/useMetrics';
14+
import { Cbas } from 'src/libs/ajax/workflows-app/Cbas';
1415
import colors from 'src/libs/colors';
1516
import { withErrorReporting } from 'src/libs/error';
1617
import Events, { extractWorkspaceDetails } from 'src/libs/events';
@@ -96,7 +97,7 @@ export const ImportWorkflow = ({ path, version, source }) => {
9697
importPage: 'ImportWorkflow',
9798
});
9899

99-
const res = await Ajax(signal).Cbas.methods.post(appUrls.cbasUrl, postRequestBody);
100+
const res = await Cbas(signal).methods.post(appUrls.cbasUrl, postRequestBody);
100101
const methodId = res.method_id;
101102

102103
Nav.goToPath('workspace-workflows-app', { name, namespace, methodId });
@@ -125,7 +126,7 @@ export const ImportWorkflow = ({ path, version, source }) => {
125126
},
126127
options
127128
);
128-
Ajax(signal).Metrics.captureEvent(Events.workflowImport, { ...eventData, success: true });
129+
void Metrics(signal).captureEvent(Events.workflowImport, { ...eventData, success: true });
129130
Nav.goToPath('workflow', { namespace, name, workflowNamespace: namespace, workflowName });
130131
} else {
131132
if (workspace.createdBy !== getTerraUser()?.email) {
@@ -138,7 +139,7 @@ export const ImportWorkflow = ({ path, version, source }) => {
138139
if (error.status === 409) {
139140
setConfirmOverwriteInWorkspace(workspace);
140141
} else {
141-
Ajax().Metrics.captureEvent(Events.workflowImport, { ...eventData, success: false });
142+
void Metrics().captureEvent(Events.workflowImport, { ...eventData, success: false });
142143
throw error;
143144
}
144145
}

0 commit comments

Comments
 (0)