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 starts with one of the mime types supported by `DOMParser`, that mime type will be used instead of `text/xml`.
The default is still `text/xml` for cases when the Header is not present or contains a mime type that is not in the list.

This PR also sends all the mime types accepted as the `Acceppt` header.
  • Loading branch information
karfau authored Jan 19, 2025
1 parent 152b8e9 commit 238dc8a
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 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);

Check failure on line 13 in services/dynamic/dynamic-xml.service.js

View workflow job for this annotation

GitHub Actions / test-lint

Delete `;`

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 @@ -128,21 +130,18 @@ export default class DynamicXml extends BaseService {
}

async handle(_namedParams, { url, query: pathExpression, prefix, suffix }) {

Check failure on line 133 in services/dynamic/dynamic-xml.service.js

View workflow job for this annotation

GitHub Actions / test-lint

Delete `⏎····`
const { buffer, res } = await this._request({
url,
options: { headers: { Accept: 'application/xml, text/xml' } },
options: { headers: { Accept: MIME_TYPES.join(', ') } },
httpErrors,
logErrors: [],
})

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

const contentType = res.headers['content-type']

Check failure on line 141 in services/dynamic/dynamic-xml.service.js

View workflow job for this annotation

GitHub Actions / test-lint

Replace `·res.headers['content-type']·` with `⏎······(res.headers['content-type']·&&`
&& MIME_TYPES.find(mime => res.headers['content-type'].startsWith(mime))

Check failure on line 142 in services/dynamic/dynamic-xml.service.js

View workflow job for this annotation

GitHub Actions / test-lint

Replace `&&·MIME_TYPES.find(mime·=>·res.headers['content-type'].startsWith(mime))·⏎······||·MIME_TYPE.XML;` with `··MIME_TYPES.find(mime·=>⏎··········res.headers['content-type'].startsWith(mime),⏎········))·||⏎······MIME_TYPE.XML`
|| MIME_TYPE.XML;

Check failure on line 144 in services/dynamic/dynamic-xml.service.js

View workflow job for this annotation

GitHub Actions / test-lint

Delete `····`
const { values: value } = this.transform({
pathExpression,
buffer,
Expand Down

0 comments on commit 238dc8a

Please sign in to comment.