Skip to content

Commit

Permalink
Add support for wildcard media types
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed Dec 21, 2024
1 parent 6ca978e commit 2ea238a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
13 changes: 8 additions & 5 deletions lib/media-types/media-types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse as parseContentType } from "content-type";
import { match as mediaTypeMatch } from "type-is";


const mediaTypePlugins = {};
Expand All @@ -22,13 +23,15 @@ export const parseResponse = (response) => {
}

const contentType = parseContentType(contentTypeText);
if (!(contentType.type in mediaTypePlugins)) {
throw new UnsupportedMediaTypeError(contentType.type, `'${contentType.type}' is not supported. Use the 'addMediaTypePlugin' function to add support for this media type.`, {
cause: response
});
for (const pattern in mediaTypePlugins) {
if (mediaTypeMatch(pattern, contentType.type)) {
return mediaTypePlugins[pattern].parse(response);
}
}

return mediaTypePlugins[contentType.type].parse(response);
throw new UnsupportedMediaTypeError(contentType.type, `'${contentType.type}' is not supported. Use the 'addMediaTypePlugin' function to add support for this media type.`, {
cause: response
});
};

export const getFileMediaType = async (path) => {
Expand Down
38 changes: 37 additions & 1 deletion lib/media-types/media-types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, test, beforeEach, afterEach, expect } from "vitest";
import { addMediaTypePlugin, removeMediaTypePlugin, setMediaTypeQuality } from "../index.js";
import { MockAgent, setGlobalDispatcher } from "undici";
import { addMediaTypePlugin, get, removeMediaTypePlugin, setMediaTypeQuality } from "../index.js";
import { acceptableMediaTypes } from "./media-types.js";


Expand Down Expand Up @@ -76,5 +77,40 @@ describe("JSON Browser", () => {
expect(accept).to.equal("application/reference+json, application/foo; q=0.9, */*; q=0.001");
});
});

describe("Wildcard media type plugins", () => {
const testDomain = "https://example.com";
let mockAgent: MockAgent;

beforeEach(() => {
addMediaTypePlugin("application/*+foo", {
parse: async () => { // eslint-disable-line @typescript-eslint/require-await
return {
baseUri: "https://exmple.com/foo",
root: null,
anchorLocation: (fragment) => fragment ?? ""
};
},
fileMatcher: async (path) => path.endsWith(".foo") // eslint-disable-line @typescript-eslint/require-await
});

mockAgent = new MockAgent();
mockAgent.disableNetConnect();
setGlobalDispatcher(mockAgent);
mockAgent.get(testDomain)
.intercept({ method: "GET", path: "/foo" })
.reply(200, `{}`, { headers: { "content-type": "application/whatever+foo" } });
});

afterEach(async () => {
removeMediaTypePlugin("application/*+foo");
await mockAgent.close();
});

test("should match wildcard media type", async () => {
// Expect not to throw
await get(`${testDomain}/foo`);
});
});
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@hyperjump/json-pointer": "^1.1.0",
"@hyperjump/uri": "^1.2.0",
"content-type": "^1.0.5",
"just-curry-it": "^5.3.0"
"just-curry-it": "^5.3.0",
"type-is": "^1.6.18"
}
}

0 comments on commit 2ea238a

Please sign in to comment.