Skip to content

Commit 3a33502

Browse files
refactor: use dedicated library to parse contentType
1 parent 9a247bc commit 3a33502

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"main": "dist/core.js",
1616
"types": "dist/core.d.ts",
1717
"devDependencies": {
18+
"@types/content-type": "^1.1.8",
1819
"@types/debug": "^4.1.7",
1920
"@types/uritemplate": "^0.3.4",
2021
"@types/uuid": "^8.3.1"
@@ -24,6 +25,7 @@
2425
"@petamoriken/float16": "^3.1.1",
2526
"ajv": "^8.11.0",
2627
"cbor": "^8.1.0",
28+
"content-type": "^1.0.5",
2729
"debug": "^4.3.4",
2830
"uritemplate": "0.3.4",
2931
"uuid": "^7.0.3",

packages/core/src/consumed-thing.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import { ConsumedThing as IConsumedThing, InteractionInput, Subscription } from "wot-typescript-definitions";
1717

18+
import ContentType from "content-type";
19+
1820
import * as TD from "@node-wot/td-tools";
1921

2022
import Servient from "./servient";
@@ -569,9 +571,9 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
569571

570572
// check if returned media type is the same as expected media type (from TD)
571573
if (form.response != null) {
572-
const indexOfSemicolon = content.type.indexOf(";");
573-
const contentType = indexOfSemicolon < 0 ? content.type : content.type.substring(0, indexOfSemicolon);
574-
if (contentType !== form.response.contentType) {
574+
const parsedMediaTypeContent = ContentType.parse(content.type);
575+
const parsedMediaTypeForm = ContentType.parse(form.response.contentType);
576+
if (parsedMediaTypeContent.type !== parsedMediaTypeForm.type) {
575577
throw new Error(
576578
`Unexpected type '${content.type}' in response. Should be '${form.response.contentType}'`
577579
);

0 commit comments

Comments
 (0)