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
121
122
123
124
125
126import { get, post } from 'aws-amplify/api';
interface ApiResponse<T> {
status: string;
result: T;
total?: number;
}
interface ApiResponseSingle<Scan> {
status: string;
result: Scan;
}
interface Scan {
jobId: string;
url: {
id: string;
url: string;
};
property: {
id: string;
name: string;
};
}
const API_NAME = 'auth';
/**
* Fetch all scans
* @returns {Promise<Scan[]>} List of scans
* @throws Will throw an error if the fetch fails
*/
export const getScans = async (): Promise<Scan[]> => {
try {
const response = await get({
apiName: API_NAME,
path: '/get/scans',
}).response;
const { body } = response;
const { result } = (await body.json()) as unknown as ApiResponse<Scan[]>;
return result;
} catch (error) {
console.error('Error fetching scans', error);
throw error;
}
};
/**
* Fetch single scan
* @returns {Promise<Scan>} Single scan
* @throws Will throw an error if the fetch fails
*/
export const getScan = async (scanId: string): Promise<Scan> => {
try {
const response = await get({
apiName: API_NAME,
path: '/get/scan',
options: {
queryParams: {
scanId,
},
},
}).response;
const { body } = response;
const { result } = (await body.json()) as unknown as ApiResponseSingle<Scan>;
return result;
} catch (error) {
console.error('Error fetching scans', error);
throw error;
}
};
/**
* Send property to scan
* @param {string[]} propertyIds - The IDs of the properties to scan
* @returns {Promise<{ status: string }>} The status of the scan initiation
* @throws Will throw an error if the scan initiation fails
*/
export const sendToScan = async (
propertyIds: string[],
): Promise<{ status: string }> => {
try {
const response = await post({
apiName: API_NAME,
path: '/add/scans',
options: {
body: { propertyIds },
},
}).response;
const { statusCode, body } = response;
const parsedBody = await body.json();
return { status: statusCode === 200 ? parsedBody?.status ?? 'success' : 'error' };
} catch (error) {
console.error('Error sending to scan', error);
throw error;
}
};
/**
* Send url to scan
* @param {string} urlId - The url ID to scan
* @returns {Promise<{ status: string }>} The status of the scan initiation
* @throws Will throw an error if the scan initiation fails
*/
export const sendUrlToScan = async (
urlId: string,
): Promise<{ status: string }> => {
try {
const response = await post({
apiName: API_NAME,
path: '/add/scans',
options: {
body: { urlIds: [urlId] },
},
}).response;
const { statusCode } = response;
return { status: statusCode === 200 ? 'success' : 'error' };
} catch (error) {
console.error('Error sending to scan', error);
throw error;
}
};