Skip to content

Commit 27a300f

Browse files
committed
Replace type-is because it doesn't work in browsers
1 parent 4b70189 commit 27a300f

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

lib/jref/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type JRef = null | boolean | string | number | Reference | JRefObject | JRef[];
2-
export type JRefObject = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
2+
export type JRefObject = {
33
[property: string]: JRef;
44
};
55

lib/media-types/media-types.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { parse as parseContentType } from "content-type";
2-
import { match as mediaTypeMatch } from "type-is";
32

43

54
const mediaTypePlugins = {};
@@ -24,7 +23,7 @@ export const parseResponse = (response) => {
2423

2524
const contentType = parseContentType(contentTypeText);
2625
for (const pattern in mediaTypePlugins) {
27-
if (mediaTypeMatch(pattern, contentType.type)) {
26+
if (mimeMatch(pattern, contentType.type)) {
2827
return mediaTypePlugins[pattern].parse(response);
2928
}
3029
}
@@ -34,6 +33,39 @@ export const parseResponse = (response) => {
3433
});
3534
};
3635

36+
const alpha = `A-Za-z`;
37+
const token = `[!#$%&'*\\-_.^\`|~\\d${alpha}]+`;
38+
const mediaRange = `(?<type>${token})/(?<subType>${token}(?:\\+(?<suffix>${token}))?)`;
39+
const mediaRangePattern = new RegExp(mediaRange);
40+
41+
export const mimeMatch = (expected, actual) => {
42+
if (expected === actual) {
43+
return true;
44+
}
45+
46+
const expectedMatches = mediaRangePattern.exec(expected)?.groups;
47+
if (!expectedMatches) {
48+
throw Error(`Unable to parse media-range: ${expected}`);
49+
}
50+
51+
const actualMatches = mediaRangePattern.exec(actual)?.groups;
52+
if (!actualMatches) {
53+
throw Error(`Unable to parse media-type: ${actual}`);
54+
}
55+
56+
if (expectedMatches.type === actualMatches.type || expectedMatches.type === "*") {
57+
if (expectedMatches.subType === actualMatches.subType || expectedMatches.subType === "*") {
58+
return true;
59+
}
60+
61+
if (expectedMatches.subType === actualMatches.suffix) {
62+
return true;
63+
}
64+
}
65+
66+
return false;
67+
};
68+
3769
export const getFileMediaType = async (path) => {
3870
for (const contentType in mediaTypePlugins) {
3971
if (await mediaTypePlugins[contentType].fileMatcher(path)) {

lib/media-types/media-types.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ describe("JSON Browser", () => {
7878
});
7979
});
8080

81-
describe("Wildcard media type plugins", () => {
81+
describe("Wildcard content types", () => {
8282
const testDomain = "https://example.com";
8383
let mockAgent: MockAgent;
8484

8585
beforeEach(() => {
86-
addMediaTypePlugin("application/*+foo", {
86+
addMediaTypePlugin("application/foo", {
8787
parse: async () => { // eslint-disable-line @typescript-eslint/require-await
8888
return {
8989
baseUri: "https://exmple.com/foo",

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"@hyperjump/json-pointer": "^1.1.0",
5353
"@hyperjump/uri": "^1.2.0",
5454
"content-type": "^1.0.5",
55-
"just-curry-it": "^5.3.0",
56-
"type-is": "^1.6.18"
55+
"just-curry-it": "^5.3.0"
5756
}
5857
}

0 commit comments

Comments
 (0)