Skip to content

Commit 2509d0c

Browse files
committed
refactor to enable multiple mime types for requests and responses
1 parent e41db7d commit 2509d0c

32 files changed

Lines changed: 595 additions & 235 deletions

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "OpenAPI DevTools",
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"devtools_page": "index.html",
66
"permissions": [],
77
"icons": {

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@seriousme/openapi-schema-validator": "^2.1.2",
4141
"@types/chrome": "^0.0.246",
4242
"@types/cookie": "^0.5.4",
43+
"@types/har-format": "^1.2.15",
4344
"@types/json-stable-stringify": "^1.0.35",
4445
"@types/lodash": "^4.14.199",
4546
"@types/react": "^18.2.28",

resources/dist.zip

1.06 KB
Binary file not shown.

src/lib/RequestStore.test.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ const host = "test.com";
1717
const base = `https://${host}`;
1818
const POST = "POST";
1919

20-
const getResBodyTypes = (store: RequestStore, host: string, path: string, propName = 'foo') => {
20+
const getResBodyJSONTypes = (store: RequestStore, host: string, path: string, propName = 'foo') => {
2121
const match = store.get()[host].lookup(path);
2222
if (!match) throw new Error("Could not match path");
2323
const properties =
24-
match.data.methods[POST][200].responseBody?.properties?.[propName].type;
24+
match.data.methods[POST][200].response['application/json'].body?.properties?.[propName].type;
2525
return properties;
2626
};
2727

@@ -35,7 +35,7 @@ it("parameterises and merges paths", () => {
3535
store.parameterise(2, "/1/2/a", host);
3636
store.insert(req3, { foo: null });
3737
store.parameterise(1, "/1/2/:param2", host);
38-
const properties = getResBodyTypes(store, host, "/1/zzz/asbds");
38+
const properties = getResBodyJSONTypes(store, host, "/1/zzz/asbds");
3939
expect(properties).toContain("string");
4040
expect(properties).toContain("integer");
4141
expect(properties).toContain("null");
@@ -46,7 +46,7 @@ it("inserts data and can retrieve it", () => {
4646
const store = new RequestStore();
4747
const req = createSimpleRequest(`${base}/1/2/a`);
4848
store.insert(req, { foo: 1 });
49-
const properties = getResBodyTypes(store, host, "/1/2/a");
49+
const properties = getResBodyJSONTypes(store, host, "/1/2/a");
5050
expect(properties).toBe("integer");
5151
});
5252

@@ -77,9 +77,9 @@ it("sets leafMap correctly after multiple add and parameterise operations", () =
7777
};
7878
// @ts-expect-error accessing private property
7979
expect(store.leafMap).toEqual(expected);
80-
expect(getResBodyTypes(store, host, "/1/x/x")).toEqual(["null", "integer", "string"]);
81-
expect(getResBodyTypes(store, host, "/dynamicPath/2/x")).toEqual(["integer", "string"]);
82-
expect(getResBodyTypes(store, host, "/staticPath/2/3/4/5")).toBe('string');
80+
expect(getResBodyJSONTypes(store, host, "/1/x/x")).toEqual(["null", "integer", "string"]);
81+
expect(getResBodyJSONTypes(store, host, "/dynamicPath/2/x")).toEqual(["integer", "string"]);
82+
expect(getResBodyJSONTypes(store, host, "/staticPath/2/3/4/5")).toBe('string');
8383
});
8484

8585
it("sets leafMap correctly after many parameterise operations", () => {
@@ -104,9 +104,9 @@ it("sets leafMap correctly after many parameterise operations", () => {
104104
};
105105
// @ts-expect-error accessing private property
106106
expect(store.leafMap).toEqual(expected);
107-
expect(getResBodyTypes(store, host, "/1/2/3/ANY/ANY")).toEqual(["null", "string"]);
108-
expect(getResBodyTypes(store, host, "/1/2/b")).toBe("boolean");
109-
expect(getResBodyTypes(store, host, "/1/x/y/ANY/b")).toBe('integer');
107+
expect(getResBodyJSONTypes(store, host, "/1/2/3/ANY/ANY")).toEqual(["null", "string"]);
108+
expect(getResBodyJSONTypes(store, host, "/1/2/b")).toBe("boolean");
109+
expect(getResBodyJSONTypes(store, host, "/1/x/y/ANY/b")).toBe('integer');
110110
});
111111

112112
it("collapses into a single route when paramaterised", () => {
@@ -126,7 +126,7 @@ it("collapses into a single route when paramaterised", () => {
126126
};
127127
// @ts-expect-error accessing private property
128128
expect(store.leafMap).toEqual(expected);
129-
expect(getResBodyTypes(store, host, "/1/2/3/ANY/ANY")).toEqual(["null", "integer", "string"]);
129+
expect(getResBodyJSONTypes(store, host, "/1/2/3/ANY/ANY")).toEqual(["null", "integer", "string"]);
130130
});
131131

132132
it("can parameterise paths that are subsets of another path", () => {
@@ -144,19 +144,22 @@ it("can parameterise paths that are subsets of another path", () => {
144144
};
145145
// @ts-expect-error accessing private property
146146
expect(store.leafMap).toEqual(expected);
147-
expect(getResBodyTypes(store, host, "/1/2/a")).toBe("string");
148-
expect(getResBodyTypes(store, host, "/1/ANY")).toBe("integer");
147+
expect(getResBodyJSONTypes(store, host, "/1/2/a")).toBe("string");
148+
expect(getResBodyJSONTypes(store, host, "/1/ANY")).toBe("integer");
149149
});
150150

151151
it("can parameterise paths that exist along the same segment", () => {
152152
const store = new RequestStore();
153153
const req1 = createSimpleRequest(`${base}/1/2/a`);
154154
const req2 = createSimpleRequest(`${base}/1/2`);
155-
store.insert(createSimpleRequest(`${base}/1`), { foo: null });
156-
store.insert(createSimpleRequest(`${base}/1/2/3/4`), { foo: null });
155+
const req3 = createSimpleRequest(`${base}/1`);
156+
const req4 = createSimpleRequest(`${base}/1/2/3/4`);
157157
store.insert(req1, { foo: "bar" });
158158
store.insert(req2, { foo: 1 });
159+
store.insert(req3, { foo: null });
160+
store.insert(req4, { foo: null });
159161
store.parameterise(1, "/1/2/a", host);
162+
// Bug happens below. When /1/2 is parameterised, router.remove removes /1/2/3/4
160163
store.parameterise(1, "/1/2", host);
161164
const expected = {
162165
[host]: {
@@ -168,8 +171,27 @@ it("can parameterise paths that exist along the same segment", () => {
168171
};
169172
// @ts-expect-error accessing private property
170173
expect(store.leafMap).toEqual(expected);
171-
expect(getResBodyTypes(store, host, "/1/ANY/a")).toBe("string");
172-
expect(getResBodyTypes(store, host, "/1/ANY")).toBe("integer");
174+
expect(getResBodyJSONTypes(store, host, "/1/ANY/a")).toBe("string");
175+
expect(getResBodyJSONTypes(store, host, "/1/ANY")).toBe("integer");
176+
});
177+
178+
it("parameterising a path catches future requests to the same path", () => {
179+
const store = new RequestStore();
180+
const req1 = createSimpleRequest(`${base}/1/2/a`);
181+
const req2 = createSimpleRequest(`${base}/1/2/b`);
182+
store.insert(req1, { foo: "bar" });
183+
store.insert(req2, { foo: "bar" });
184+
store.parameterise(1, "/1/2/a", host);
185+
store.insert(req1, { foo: 1 });
186+
store.insert(req2, { foo: 1 });
187+
const expected = {
188+
[host]: {
189+
'/1/:param1/a': expect.any(Object),
190+
'/1/2/b': expect.any(Object),
191+
}
192+
};
193+
// @ts-expect-error accessing private property
194+
expect(store.leafMap).toEqual(expected);
173195
});
174196

175197
it("parameterisation works after export and import", () => {

src/lib/RequestStore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { omit, unset } from "lodash";
1010
import leafMapToEndpoints from "./leafmap-to-endpoints";
1111
import stringify from "json-stable-stringify";
12+
import type { Entry } from 'har-format';
1213

1314
export type Options = {
1415
// Includes additional data such as response samples
@@ -84,7 +85,7 @@ export default class RequestStore {
8485
}
8586

8687
public insert(
87-
harRequest: chrome.devtools.network.Request,
88+
harRequest: Entry,
8889
responseBody: JSONType
8990
) {
9091
const result = upsert({

src/lib/__fixtures__/apikey.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import type { Entry } from 'har-format';
2+
13
// Has header x-api-key
24
// Has cookie foo
3-
const apikey: chrome.devtools.network.Request = {
4-
getContent() {},
5+
const apikey: Entry = {
56
_priority: "High",
67
_resourceType: "fetch",
78
cache: {},

src/lib/__fixtures__/basic.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const basic: chrome.devtools.network.Request = {
2-
getContent() {},
1+
import type { Entry } from 'har-format';
2+
3+
const basic: Entry = {
34
_priority: "High",
45
_resourceType: "fetch",
56
cache: {},

src/lib/__fixtures__/bearer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const bearer: chrome.devtools.network.Request = {
2-
getContent() {},
1+
import type { Entry } from 'har-format';
2+
3+
const bearer: Entry = {
34
cache: {},
45
_resourceType: "xhr",
56
connection: "194622",

src/lib/__fixtures__/digest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const bearer: chrome.devtools.network.Request = {
2-
getContent() {},
1+
import type { Entry } from 'har-format';
2+
3+
const bearer: Entry = {
34
cache: {},
45
_resourceType: "xhr",
56
connection: "194622",

0 commit comments

Comments
 (0)