@cubejs-backend/api-gateway
Advanced tools
Comparing version 0.0.28 to 0.3.1
118
index.js
@@ -8,8 +8,8 @@ const jwt = require('jsonwebtoken'); | ||
const toConfigMap = (metaConfig) => { | ||
return R.pipe( | ||
const toConfigMap = (metaConfig) => ( | ||
R.pipe( | ||
R.map(c => [c.config.name, c.config]), | ||
R.fromPairs | ||
)(metaConfig); | ||
}; | ||
)(metaConfig) | ||
); | ||
@@ -31,3 +31,3 @@ const prepareAnnotation = (metaConfig, query) => { | ||
format: config.format | ||
}] | ||
}]; | ||
}; | ||
@@ -40,3 +40,3 @@ | ||
timeDimensions: R.fromPairs((query.timeDimensions || []).map(td => annotation('dimensions')(td.dimension)).filter(a => !!a)), // TODO | ||
} | ||
}; | ||
}; | ||
@@ -63,3 +63,3 @@ | ||
.filter(a => !!a) | ||
) | ||
); | ||
}; | ||
@@ -75,12 +75,10 @@ | ||
const transformData = (aliasToMemberNameMap, annotation, data) => { | ||
return data.map(r => R.pipe( | ||
R.toPairs, | ||
R.map(p => [ | ||
aliasToMemberNameMap[p[0]], | ||
transformValue(p[1], annotation[aliasToMemberNameMap[p[0]]].type) | ||
]), | ||
R.fromPairs | ||
)(r)); | ||
}; | ||
const transformData = (aliasToMemberNameMap, annotation, data) => (data.map(r => R.pipe( | ||
R.toPairs, | ||
R.map(p => [ | ||
aliasToMemberNameMap[p[0]], | ||
transformValue(p[1], annotation[aliasToMemberNameMap[p[0]]].type) | ||
]), | ||
R.fromPairs | ||
)(r))); | ||
@@ -129,2 +127,3 @@ const id = Joi.string().regex(/^[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+$/); | ||
const normalizeQuery = (query) => { | ||
// eslint-disable-next-line no-unused-vars | ||
const { error, value } = Joi.validate(query, querySchema); | ||
@@ -139,23 +138,21 @@ if (error) { | ||
const filterWithIncorrectOperator = (query.filters || []) | ||
.find(f => | ||
[ | ||
'equals', | ||
'notEquals', | ||
'contains', | ||
'notContains', | ||
'in', | ||
'notIn', | ||
'gt', | ||
'gte', | ||
'lt', | ||
'lte', | ||
'set', | ||
'notSet', | ||
'inDateRange', | ||
'notInDateRange', | ||
'onTheDate', | ||
'beforeDate', | ||
'afterDate' | ||
].indexOf(f.operator) === -1 | ||
); | ||
.find(f => [ | ||
'equals', | ||
'notEquals', | ||
'contains', | ||
'notContains', | ||
'in', | ||
'notIn', | ||
'gt', | ||
'gte', | ||
'lt', | ||
'lte', | ||
'set', | ||
'notSet', | ||
'inDateRange', | ||
'notInDateRange', | ||
'onTheDate', | ||
'beforeDate', | ||
'afterDate' | ||
].indexOf(f.operator) === -1); | ||
if (filterWithIncorrectOperator) { | ||
@@ -182,13 +179,10 @@ throw new UserError(`Operator ${filterWithIncorrectOperator.operator} not supported for filter: ${JSON.stringify(filterWithIncorrectOperator)}`); | ||
})).concat(regularToTimeDimension) | ||
} | ||
}; | ||
}; | ||
const coerceForSqlQuery = (query) => { | ||
return { | ||
...query, | ||
timeDimensions: (query.timeDimensions || []).map(td => { | ||
return td.granularity === 'day' ? { ...td, granularity: 'date' } : td; | ||
}) | ||
} | ||
}; | ||
const coerceForSqlQuery = (query) => ({ | ||
...query, | ||
timeDimensions: (query.timeDimensions || []) | ||
.map(td => (td.granularity === 'day' ? { ...td, granularity: 'date' } : td)) | ||
}); | ||
@@ -206,3 +200,3 @@ class ApiGateway { | ||
try { | ||
let query = JSON.parse(req.query.query); | ||
const query = JSON.parse(req.query.query); | ||
this.log(req, { | ||
@@ -218,6 +212,8 @@ type: 'Load Request', | ||
const sqlQuery = compilerSqlResult; | ||
let metaConfig = metaConfigResult; | ||
const metaConfig = metaConfigResult; | ||
const annotation = prepareAnnotation(metaConfig, normalizedQuery); | ||
let aliasToMemberNameMap = prepareAliasToMemberNameMap(metaConfig, sqlQuery, normalizedQuery); | ||
const toExecute = { ...sqlQuery, query: sqlQuery.sql[0], values: sqlQuery.sql[1], continueWait: true }; | ||
const aliasToMemberNameMap = prepareAliasToMemberNameMap(metaConfig, sqlQuery, normalizedQuery); | ||
const toExecute = { | ||
...sqlQuery, query: sqlQuery.sql[0], values: sqlQuery.sql[1], continueWait: true | ||
}; | ||
const response = await this.adapterApi.executeQuery(toExecute); | ||
@@ -245,3 +241,3 @@ this.log(req, { | ||
try { | ||
let query = JSON.parse(req.query.query); | ||
const query = JSON.parse(req.query.query); | ||
const normalizedQuery = normalizeQuery(query); | ||
@@ -256,5 +252,15 @@ const sqlQuery = await this.compilerApi.getSql(coerceForSqlQuery(normalizedQuery)); | ||
})); | ||
app.get('/cubejs-api/v1/meta', this.checkAuth.bind(this), (async (req, res) => { | ||
try { | ||
const metaConfig = await this.compilerApi.metaConfig(); | ||
const cubes = metaConfig.map(c => c.config); | ||
res.json({ cubes }); | ||
} catch (e) { | ||
this.handleError(e, req, res); | ||
} | ||
})); | ||
} | ||
handleError (e, req, res) { | ||
handleError(e, req, res) { | ||
if (e instanceof UserError) { | ||
@@ -292,3 +298,3 @@ this.log(req, { | ||
async checkAuth(req, res, next) { | ||
const auth = req.headers['authorization']; | ||
const auth = req.headers.authorization; | ||
@@ -306,2 +312,3 @@ if (auth) { | ||
} | ||
return null; | ||
} | ||
@@ -313,5 +320,4 @@ | ||
} | ||
} | ||
module.exports = ApiGateway; | ||
module.exports = ApiGateway; |
@@ -5,3 +5,3 @@ { | ||
"author": "Statsbot, Inc.", | ||
"version": "0.0.28", | ||
"version": "0.3.1", | ||
"engines": { | ||
@@ -27,3 +27,3 @@ "node": ">=8.11.1" | ||
"license": "Apache-2.0", | ||
"gitHead": "f46b13e1bf02b96cfb961c3e02fb77b0ed682be1" | ||
"gitHead": "fd69ad6e3fcdeab323932b56211cb5733727cf3a" | ||
} |
22610
311