UNPKG

@ocap/indexdb-elasticsearch

Version:
55 lines (44 loc) 1.7 kB
const { formatNextPagination, parseDateTime } = require('@ocap/indexdb/lib/util'); const getTableName = (prefix, name) => (prefix ? [prefix, name].join('_') : name); const formatQueryResult = (queryResult, pagination) => { const { hits, total } = queryResult.hits; // eslint-disable-next-line no-underscore-dangle return { items: hits.map((x) => x._source), paging: formatNextPagination(total.value, pagination) }; }; const getAuthHeaders = (auth) => { if (!auth) { return {}; } const headers = {}; if (auth.apiKey) { if (typeof auth.apiKey === 'object') { headers.authorization = `ApiKey ${Buffer.from(`${auth.apiKey.id}:${auth.apiKey.api_key}`).toString('base64')}`; } else { headers.authorization = `ApiKey ${auth.apiKey}`; } } else if (auth.username && auth.password) { headers.authorization = `Basic ${Buffer.from(`${auth.username}:${auth.password}`).toString('base64')}`; } return headers; }; const getTimeCondition = ( filter = {}, supportedFields = ['genesisTime', 'renaissanceTime'], defaultField = 'renaissanceTime' ) => { let { startDateTime, endDateTime, field } = filter; startDateTime = parseDateTime(startDateTime); endDateTime = parseDateTime(endDateTime); field = supportedFields.includes(field) ? field : defaultField; if (startDateTime && endDateTime) { return { range: { [field]: { gt: startDateTime, lte: endDateTime } } }; } if (startDateTime) { return { range: { [field]: { gt: startDateTime } } }; } if (endDateTime) { return { range: { [field]: { lte: endDateTime } } }; } return null; }; module.exports = { getTableName, formatQueryResult, getAuthHeaders, getTimeCondition };