forked from chrisuehlinger/openGraphScraperLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (49 loc) · 1.35 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
const openGraphScraperLite = require('./lib/openGraphScraperLite');
/*
* run
* @param string options - options the user has set
* @param function callback and promise
*/
exports.run = async (options, callback) => {
const hasCallback = typeof callback === 'function';
if (hasCallback) {
let results;
try {
results = await openGraphScraperLite(options);
} catch (exception) {
const returnError = {
success: false,
requestUrl: options.url,
error: exception.message,
errorDetails: exception,
};
return callback(true, returnError);
}
return callback(false, results.ogObject, results.response);
}
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
let results;
try {
results = await openGraphScraperLite(options);
} catch (exception) {
const returnError = {
error: true,
result: {
success: false,
requestUrl: options.url,
error: exception.message,
errorDetails: exception,
},
};
return reject(returnError);
}
const returnValues = {
error: false,
result: results.ogObject,
response: results.response,
};
return resolve(returnValues);
});
};
module.exports = (options, callback) => exports.run(options, callback);