UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

148 lines (124 loc) 3.74 kB
'use strict'; //Includes const filter_parser = require('../parsers/jison_filter_parser'); const orderBy_parser = require('../parsers/jison_orderby_parser'); const BadRequest = require('./../utils/errors/http/badRequest'); const NotSupported = require('./../utils/errors/http/notSupported'); const UnsupportedMediatype = require('./../utils/errors/http/unsupportedMediaType'); //API exports.parseExpand = parseExpand; exports.parseSelect = parseSelect; exports.parseFilter = parseFilter; exports.parseOrderBy = parseOrderBy; exports.parseTop = parseTop; exports.parseFormat = parseFormat; exports.parseSkip = parseSkip; exports.parseInlineCount = parseInlineCount; exports.parseSkipToken = parseSkipToken; //Code const onlyIntRegexObj = /^\d+$/; function parseTop(top) { if (!onlyIntRegexObj.test(top)) { throw new BadRequest('Invalid value for system query $top.'); } const ret = parseInt(top); if (ret > 2147483647 || ret < 0) { throw new BadRequest('Invalid value for system query $top.'); } return ret; } function parseSkip(skip) { if (!onlyIntRegexObj.test(skip)) { throw new BadRequest('Invalid value for system query $skip.'); } const ret = parseInt(skip); if (ret > 2147483647 || ret < 0) { throw new BadRequest('Invalid value for system query $skip.'); } return ret; } function parseSkipToken() { throw new NotSupported('$skipToken not supported'); } function parseInlineCount(inlineCount) { if (inlineCount === 'allpages') { return true; } else if (inlineCount === 'none') { return false; } throw new BadRequest('Invalid value for system query inlinecount.'); } function parseSelect(select) { const selectclauses = []; if (!select) { return selectclauses; } const rawSelectClauses = select.split(','); for (const rawSelectClause of rawSelectClauses) { const rawSelectItems = rawSelectClause.split('/'); const selectItems = []; for (const rawSelectItem of rawSelectItems) { selectItems.push(rawSelectItem); } selectclauses.push(selectItems); } return selectclauses; } function parseExpand(expand) { const expandclauses = []; if (!expand) { return expandclauses; } const rawExpandClauses = expand.split(','); for (const rawExpandClause of rawExpandClauses) { const rawExpandItems = rawExpandClause.split('/'); const expandList = []; for (const rawExpandItem of rawExpandItems) { expandList.push(rawExpandItem); } expandclauses.push(expandList); } return expandclauses; } function parseFilter(filter, done) { let ast, error; try { ast = filter_parser.parse(filter); } catch (ex) { error = new BadRequest('Provided $filter expression is not valid'); } if (done !== undefined) { done(error, ast); } else { if (error) { throw error; } return ast; } } function parseOrderBy(orderBy, done) { if (done !== undefined) { let ast; try { ast = orderBy_parser.parse(orderBy); } catch (ex) { return done(ex); } return done(undefined, ast); //don't catch any error from the cb } else { return orderBy_parser.parse(orderBy); } } function parseFormat(format) { //only lower case allowed! if (format === 'json') { return 'json'; } else if (format === 'atom') { return 'atom'; } else if (format === 'xml') { return 'xml'; } throw new UnsupportedMediatype( 'Unsupported value for $format query option' ); }