/
webServiceClient.ts
160 lines (137 loc) · 4.02 KB
/
webServiceClient.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as http from 'http';
import * as https from 'https';
import { version } from '../package.json';
import Transaction from './request/transaction';
import TransactionReport from './request/transaction-report';
import * as models from './response/models';
import { WebServiceClientError } from './types';
interface ResponseError {
code?: string;
error?: string;
}
type servicePath = 'factors' | 'insights' | 'score' | 'transactions/report';
export default class WebServiceClient {
private accountID: string;
private host: string;
private licenseKey: string;
private timeout: number;
public constructor(accountID: string, licenseKey: string, timeout = 3000) {
this.accountID = accountID;
this.licenseKey = licenseKey;
this.timeout = timeout;
this.host = 'minfraud.maxmind.com';
}
public factors(transaction: Transaction): Promise<models.Factors> {
return this.responseFor<models.Factors>(
'factors',
transaction.toString(),
models.Factors
);
}
public insights(transaction: Transaction): Promise<models.Insights> {
return this.responseFor<models.Insights>(
'insights',
transaction.toString(),
models.Insights
);
}
public score(transaction: Transaction): Promise<models.Score> {
return this.responseFor<models.Score>(
'score',
transaction.toString(),
models.Score
);
}
public reportTransaction(report: TransactionReport): Promise<void> {
return this.responseFor<void>('transactions/report', report.toString());
}
private responseFor<T>(
path: servicePath,
postData: string,
modelClass?: any
): Promise<T> {
const parsedPath = `/minfraud/v2.0/${path}`;
const url = `https://${this.host}${parsedPath}`;
const options = {
auth: `${this.accountID}:${this.licenseKey}`,
headers: {
Accept: 'application/json',
'Content-Length': Buffer.byteLength(postData),
'Content-Type': 'application/json',
'User-Agent': `minfraud-api-node/${version}`,
},
host: this.host,
method: 'POST',
path: parsedPath,
timeout: this.timeout,
};
return new Promise((resolve, reject) => {
const req = https.request(options, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
if (response.statusCode && response.statusCode === 204) {
return resolve();
}
try {
data = JSON.parse(data);
} catch {
return reject(this.handleError({}, response, url));
}
if (response.statusCode && response.statusCode !== 200) {
return reject(
this.handleError(data as ResponseError, response, url)
);
}
return resolve(new modelClass(data));
});
});
req.on('error', (err: NodeJS.ErrnoException) => {
return reject({
code: err.code,
error: err.message,
url,
} as WebServiceClientError);
});
req.write(postData);
req.end();
});
}
private handleError(
data: ResponseError,
response: http.IncomingMessage,
url: string
): WebServiceClientError {
if (
response.statusCode &&
response.statusCode >= 500 &&
response.statusCode < 600
) {
return {
code: 'SERVER_ERROR',
error: `Received a server error with HTTP status code: ${response.statusCode}`,
url,
};
}
if (
response.statusCode &&
(response.statusCode < 400 || response.statusCode >= 600)
) {
return {
code: 'HTTP_STATUS_CODE_ERROR',
error: `Received an unexpected HTTP status code: ${response.statusCode}`,
url,
};
}
if (!data.code || !data.error) {
return {
code: 'INVALID_RESPONSE_BODY',
error: 'Received an invalid or unparseable response body',
url,
};
}
return { ...data, url } as WebServiceClientError;
}
}