1 | 'use strict';
|
2 |
|
3 | var fetchres = require('fetchres');
|
4 | var logger = require('ft-next-logger').logger;
|
5 | var elasticAgent = require('./utils/elastic-agent');
|
6 | var signedFetch = require('signed-aws-es-fetch');
|
7 |
|
8 | module.exports = function(opts) {
|
9 | var uuid = [].concat(opts.uuid);
|
10 | var returnMany = Array.isArray(opts.uuid);
|
11 |
|
12 | if (uuid.length === 0) {
|
13 | return Promise.resolve([]);
|
14 | }
|
15 |
|
16 | return signedFetch(`https://next-elastic.ft.com/v3_api_v2/item/_mget`, {
|
17 | method: 'POST',
|
18 | body: JSON.stringify({ ids: uuid }),
|
19 | timeout: 3000,
|
20 | agent: elasticAgent
|
21 | })
|
22 | .then(function(response) {
|
23 | if (!response.ok) {
|
24 | logger.warn('Failed getting ElasticSearch content', {
|
25 | uuid: uuid,
|
26 | status: response.status
|
27 | });
|
28 | }
|
29 | return response;
|
30 | })
|
31 | .then(fetchres.json)
|
32 | .then(function(data) {
|
33 | var foundCount = 0;
|
34 | var docs = data.docs
|
35 |
|
36 |
|
37 | .filter(function(item) {
|
38 | if (item.found) {
|
39 | foundCount++;
|
40 | return true;
|
41 | }
|
42 | })
|
43 | .map(function(item) {
|
44 | return item._source;
|
45 | });
|
46 | if (returnMany) {
|
47 | return docs;
|
48 | }
|
49 | if (foundCount === 0) {
|
50 | throw new fetchres.BadServerResponseError(404);
|
51 | }
|
52 | return docs[0];
|
53 | });
|
54 | };
|