Skip to content

Commit

Permalink
feat: pass matching mime type to xmldom
Browse files Browse the repository at this point in the history
If the `Content-Type` header contains one of the mime types supported by `DOMParser`, the first matching mime type will be used instead of `text/xml`.

The default is still `text/xml` for cases when the header is not present or none of the mime types from the list are present.
  • Loading branch information
karfau committed Jan 19, 2025
1 parent 152b8e9 commit 63bfb12
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions services/dynamic/dynamic-xml.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DOMParser } from '@xmldom/xmldom'
import { DOMParser, MIME_TYPE } from '@xmldom/xmldom'
import xpath from 'xpath'
import { MetricNames } from '../../core/base-service/metric-helper.js'
import { renderDynamicBadge, httpErrors } from '../dynamic-common.js'
Expand All @@ -10,6 +10,8 @@ import {
} from '../index.js'
import { createRoute } from './dynamic-helpers.js'

const MIME_TYPES = Object.values(MIME_TYPE)

const description = `
The Dynamic XML Badge allows you to extract an arbitrary value from any
XML Document using an XPath selector and show it on a badge.
Expand Down Expand Up @@ -136,11 +138,10 @@ export default class DynamicXml extends BaseService {
})

let contentType = 'text/xml'
if (
res.headers['content-type'] &&
res.headers['content-type'].includes('text/html')
) {
contentType = 'text/html'
if (res.headers['content-type']) {
contentType =
MIME_TYPES.find(mime => res.headers['content-type'].includes(mime)) ??
'text/xml'
}

const { values: value } = this.transform({
Expand Down

0 comments on commit 63bfb12

Please sign in to comment.