-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson.check.js
40 lines (35 loc) · 944 Bytes
/
json.check.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
'use strict';
const status = require('./status');
const Check = require('./check');
const fetch = require('node-fetch');
const fetchres = require('fetchres');
const logger = require('@dotcom-reliability-kit/logger');
class JsonCheck extends Check {
constructor(options) {
super(options);
this.callback = options.callback;
this.url = options.url;
this.checkResultInternal = options.checkResult;
this.fetchOptions = options.fetchOptions;
}
get checkOutput() {
return this.checkResultInternal[this.status];
}
async tick() {
try {
const json = await fetch(this.url, this.fetchOptions).then(fetchres.json);
let result = this.callback(json);
this.status = result ? status.PASSED : status.FAILED;
} catch (error) {
logger.error(
{
event: 'JSON_CHECK_ERROR',
message: `Failed to fetch JSON from ${this.url}`
},
error
);
this.status = status.FAILED;
}
}
}
module.exports = JsonCheck;