This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
123 lines (117 loc) · 3.94 KB
/
index.js
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
/*!
* Infermedica.
*
* Main entry file.
* @author Jesse Okeya <[email protected]>
* @created 07/12/2018 NZDT
*/
const classes = require('extends-classes')
const axios = require('axios')
const {
Condition,
Info,
LabTest,
LookUp,
Search,
RiskFactor,
Symptom,
Suggest,
Explain,
Parse,
Diagnosis,
Triage
} = require('./lib')
const Classes = classes(
Condition,
Info,
LabTest,
LookUp,
Search,
RiskFactor,
Symptom,
Suggest,
Explain,
Parse,
Diagnosis,
Triage
)
class Infermedica extends Classes {
constructor(options = {}) {
const { appId, appKey } = options
const host = 'https://api.infermedica.com/v2'
axios.defaults.headers.common['app_id'] = appId
axios.defaults.headers.common['app_key'] = appKey
super({ host, axios })
this.appId = appId
this.appKey = appKey
}
/**
* Gives context to methods called and thier parameters
* @param {Object} method
* @param {Object} args
* @throws {Error} throws an error if error occurs
*/
__call(method, args) {
console.log(`${method.toString()}(${args.toString() || ''}) is missing!`);
}
/**
* Handles errors that occur while applicationnis running
* @param {Object} err - An error object
* @throws {Error} throws an error if error occurs
*/
_handleError(err) {
let errorCtx
if (err.response) {
const status = err.response.status
switch (status) {
case 400:
errorCtx = JSON.stringify({
statusText: err.response.statusText,
status,
message: `Request Error: invalid JSON (e.g. extra comma), missing or invalid parameter (e.g. missing fields in request body)`,
method: err.response.config.method.toUpperCase(),
url: err.response.config.url,
data: err.response.config.data || "",
stack: err.stack.toString()
}, null, 4)
break
case 403:
errorCtx = JSON.stringify({
statusText: err.response.statusText,
status,
message: `Request Error: missing or invalid credentials for App-Id: ${this.appId} or App-Key: ${this.appKey}`,
method: err.response.config.method.toUpperCase(),
url: err.response.config.url,
data: err.response.config.data || "",
stack: err.stack.toString()
}, null, 4)
break
case 404:
errorCtx = JSON.stringify({
statusText: err.response.statusText,
status,
message: `Request Error: invalid URL or object not found`,
method: err.response.config.method.toUpperCase(),
url: err.response.config.url,
data: err.response.config.data || "",
stack: err.stack.toString()
}, null, 4)
break
case 405:
errorCtx = JSON.stringify({
statusText: err.response.statusText,
status,
message: `Request Error: invalid HTTP method (e.g. GET instead of POST)`,
method: err.response.config.method.toUpperCase(),
url: err.response.config.url,
data: err.response.config.data || "",
stack: err.stack.toString()
}, null, 4)
}
} else {
errorCtx = err.stack.toString()
}
throw new Error(errorCtx)
}
}
module.exports = Infermedica