forked from microsoft/ccf-app-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.ts
81 lines (63 loc) · 2.83 KB
/
api.ts
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
import { DemoMemberProps, DemoProps } from './index';
import axios from 'axios';
export interface ReportItem {
group_status: string;
majority_minority: string;
count_of_unique_values: number;
members_in_agreement: number;
lei: string;
nace: string;
}
export interface ValidationProps {
url: string;
method: 'GET' | 'POST';
member: DemoMemberProps;
expectedStatus: number;
testMessage: string;
}
export class Validator {
public static async validateRequest(props: ValidationProps) {
const result = await axios({
method: props.method,
url: props.url,
data: props.member.data,
httpsAgent: props.member.httpsAgent,
validateStatus: function (status) {
return status < 500; // Resolve only if the status code is less than 500
},
});
if (result.status !== props.expectedStatus) {
throw new Error(`🛑 [TEST FAILURE]: ${props.member.name} - ${props.testMessage}: ${props.expectedStatus} expected, but got ${result.status}.`);
}
console.log(`✅ [PASS] ${props.member.name} - ${props.testMessage}`);
}
}
export default class Api {
public static async ingest(apiUrl: string, member: DemoMemberProps) {
const result = await axios.post(apiUrl, member.data, { httpsAgent: member.httpsAgent });
if (result.status !== 200) {
throw new Error(`🛑 [TEST FAILURE]: Unexpected status code: ${result.status}`);
}
console.log(`✅ [PASS] [${result.status} : ${result.statusText}] - ${member.name} ${result.data.content}`);
}
public static async report(props: DemoProps, member: DemoMemberProps): Promise<Array<ReportItem>> {
console.log(`📝 ${member.name} Full Report:`);
const result = await axios.get(props.reportUrl, { httpsAgent: member.httpsAgent });
if (result.status !== 200) {
throw new Error(`🛑 [TEST FAILURE]: Unexpected status code: ${result.status}`);
}
console.log(`✅ [PASS] [${result.status} : ${result.statusText}] - ${member.name}`);
console.table(result.data.content);
return result.data.content as Array<ReportItem>;
}
public static async reportById(props: DemoProps, member: DemoMemberProps, recordId: string): Promise<ReportItem> {
console.log(`📝 ${member.name} Reporting Data for id ${recordId}...`);
const result = await axios.get(`${props.reportUrl}/${recordId}`, { httpsAgent: member.httpsAgent });
if (result.status !== 200) {
throw new Error(`🛑 [TEST FAILURE]: Unexpected status code: ${result.status}`);
}
console.log(`✅ [PASS] [${result.status} : ${result.statusText}] - ${member.name}`);
console.table(result.data.content);
return result.data.content;
}
}