UNPKG

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