Files
shields/services/dynamic/dynamic-xml.service.js
dependabot-preview[bot] 478d14300c Build(deps-dev): bump eslint-plugin-import from 2.20.1 to 2.20.2 (#4859)
* Build(deps-dev): bump eslint-plugin-import from 2.20.1 to 2.20.2

Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.20.1 to 2.20.2.
- [Release notes](https://github.com/benmosher/eslint-plugin-import/releases)
- [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md)
- [Commits](https://github.com/benmosher/eslint-plugin-import/compare/v2.20.1...v2.20.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Fixes

* refactor: combine imports

* refactor: combine imports

* refactor: combine imports

* refactor: update import ordering

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
Co-authored-by: Paul Melnikow <email@paulmelnikow.com>
Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
Co-authored-by: Caleb Cartwright <caleb.cartwright@outlook.com>
2020-04-16 18:39:13 -05:00

95 lines
2.4 KiB
JavaScript

'use strict'
const { DOMParser } = require('xmldom')
const xpath = require('xpath')
const { MetricNames } = require('../../core/base-service/metric-helper')
const { renderDynamicBadge, errorMessages } = require('../dynamic-common')
const { BaseService, InvalidResponse, InvalidParameter } = require('..')
const { createRoute } = require('./dynamic-helpers')
// This service extends BaseService because it uses a different XML parser
// than BaseXmlService which can be used with xpath.
//
// One way to create a more performant version would be to use the BaseXml
// JSON parser and write the queries in jsonpath instead. Then eventually
// deprecate the old version.
module.exports = class DynamicXml extends BaseService {
static get category() {
return 'dynamic'
}
static get enabledMetrics() {
return [MetricNames.SERVICE_RESPONSE_SIZE]
}
static get route() {
return createRoute('xml')
}
static get defaultBadgeData() {
return {
label: 'custom badge',
}
}
transform({ pathExpression, buffer }) {
// e.g. //book[2]/@id
const pathIsAttr = (
pathExpression.split('/').slice(-1)[0] || ''
).startsWith('@')
const parsed = new DOMParser().parseFromString(buffer)
let values
try {
values = xpath.select(pathExpression, parsed)
} catch (e) {
throw new InvalidParameter({ prettyMessage: e.message })
}
if (
typeof values === 'string' ||
typeof values === 'number' ||
typeof values === 'boolean'
) {
values = [values]
} else if (Array.isArray(values)) {
values = values.reduce((accum, node) => {
if (pathIsAttr) {
accum.push(node.value)
} else if (node.firstChild) {
accum.push(node.firstChild.data)
} else {
accum.push(node.data)
}
return accum
}, [])
} else {
throw new InvalidResponse({
prettyMessage: 'unsupported query',
})
}
if (!values.length) {
throw new InvalidResponse({ prettyMessage: 'no result' })
}
return { values }
}
async handle(_namedParams, { url, query: pathExpression, prefix, suffix }) {
const { buffer } = await this._request({
url,
options: { headers: { Accept: 'application/xml, text/xml' } },
errorMessages,
})
const { values: value } = this.transform({
pathExpression,
buffer,
})
return renderDynamicBadge({ value, prefix, suffix })
}
}