Skip to content

Commit 6d8fcae

Browse files
feat: initial commit
1 parent ecf5cf1 commit 6d8fcae

17 files changed

Lines changed: 6363 additions & 3 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Leonardo Dalcin
3+
Copyright (c) 2020 Leonardo Dalcin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# auto-api-open-api
2-
Chrome extension that filter local requests and generates an open API documentation.
1+
# chrome-open-api-generetor

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
roots: ["src"],
3+
transform: {
4+
"^.+\\.ts$": "ts-jest",
5+
},
6+
};

package-lock.json

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

package.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "chrome-extension-typescript-starter",
3+
"version": "1.0.0",
4+
"description": "chrome-extension-typescript-starter",
5+
"main": "index.js",
6+
"watch": {
7+
"compile-ts": {
8+
"patterns": [
9+
"src"
10+
],
11+
"extensions": "ts"
12+
},
13+
"copy-static-files": {
14+
"patterns": [
15+
"src"
16+
],
17+
"extensions": [
18+
"json",
19+
"html",
20+
"js",
21+
"png"
22+
]
23+
}
24+
},
25+
"scripts": {
26+
"dev": "npm-watch",
27+
"copy-static-files": "rsync -zarv $(find ./src -print | grep 'json\\|html\\|js\\|png') ./dist",
28+
"compile-ts": "tsc --sourceMap",
29+
"clean": "rm -rf dist",
30+
"test": "jest"
31+
},
32+
"author": "Leonardo Vogel Dalcin",
33+
"license": "MIT",
34+
"repository": {
35+
"type": "git",
36+
"url": "https://github.com/leonardodalcin/chrome-open-api-generetor.git"
37+
},
38+
"dependencies": {
39+
"generate-schema": "^2.6.0",
40+
"openapi-types": "^7.0.0"
41+
},
42+
"devDependencies": {
43+
"@types/chrome": "0.0.125",
44+
"@types/jest": "^26.0.15",
45+
"jest": "^26.6.1",
46+
"npm-watch": "^0.7.0",
47+
"prettier": "^2.2.0",
48+
"ts-jest": "^26.4.2",
49+
"ts-loader": "^8.0.7",
50+
"typescript": "~4.0.3"
51+
}
52+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @ts-ignore
2+
import * as GenerateSchema from "generate-schema";
3+
export function convertObjectArrayIntoJSONSchema(array: any[]): object {
4+
const jsonSchema = GenerateSchema.json("", array);
5+
delete jsonSchema.$schema;
6+
return JSON.parse(JSON.stringify(jsonSchema).replace(/null/g, "string"));
7+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// import { OpenAPIV3 } from "openapi-types";
2+
// import { groupRequestSamplesByURLAndMethod } from "./groupRequestSamplesByURLAndMethod";
3+
// import { convertObjectArrayIntoJSONSchema } from "./convertObjectArrayIntoJSONSchema";
4+
// import { IRequestSample } from "../interfaces/IRequestSample";
5+
// import { format } from "prettier";
6+
//
7+
// export function convertGroupedRequestSamplesToOpenAPISpecification(
8+
// samples: ReturnType<typeof groupRequestSamplesByURLAndMethod>
9+
// ): OpenAPIV3.Document {
10+
// const specification: OpenAPIV3.Document = {
11+
// openapi: "3.0.1",
12+
// info: { title: "api_pt", version: "1.0.0" },
13+
// servers: [
14+
// {
15+
// url: "https://api.oiwarren.com",
16+
// description: "production",
17+
// },
18+
// ],
19+
// paths: {},
20+
// };
21+
// const paths = Object.assign({}, samples);
22+
// for (let requestURL in paths) {
23+
// for (const requestMethod in paths[requestURL]) {
24+
// const requestSamples: IRequestSample[] = paths[requestURL][requestMethod];
25+
// specification.paths[requestURL] = {};
26+
// specification.paths[requestURL][requestMethod] = {
27+
// requestBody: {
28+
// content: {
29+
// "application/json": {
30+
// schema: convertObjectArrayIntoJSONSchema(
31+
// requestSamples.map((r) => r.requestBody)
32+
// ),
33+
// },
34+
// },
35+
// },
36+
// } as OpenAPIV3.OperationObject;
37+
// const codes = new Set(requestSamples.map((r) => r.statusCode));
38+
// specification.paths[requestURL][requestMethod].responses = {};
39+
// for (const code of codes.values()) {
40+
// specification.paths[requestURL][requestMethod].responses[code] = {
41+
// description: "",
42+
// content: {
43+
// "application/json": {
44+
// schema: convertObjectArrayIntoJSONSchema(
45+
// requestSamples
46+
// .filter((r) => r.statusCode === code)
47+
// .map((r) => r.responseBody)
48+
// ),
49+
// },
50+
// },
51+
// };
52+
// }
53+
// }
54+
// }
55+
//
56+
// return specification;
57+
// }

src/create-devtools-panel.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
chrome.devtools.panels.create(
2+
"My Panel",
3+
"icon.png",
4+
"devtools.html",
5+
function (panel) {}
6+
);
7+
8+
import { Cookie, Header, QueryString } from "har-format";
9+
10+
11+
interface RequestSample {
12+
id: string;
13+
request: {
14+
method: string;
15+
url: string;
16+
body: string | undefined;
17+
cookies: Cookie[];
18+
headers: Header[];
19+
queryString: QueryString[];
20+
href: string;
21+
search: string;
22+
pathname: string;
23+
};
24+
response: {
25+
status: number;
26+
body: string;
27+
headers: Header[];
28+
};
29+
}
30+
let isFirst = true;
31+
let filterURLRegex = "github.com";
32+
let items: { [pathname: string]: RequestSample[] } = {};
33+
const requestSamplerListener = (req: chrome.devtools.network.Request) => {
34+
req.getContent((content) => {
35+
const { request, response } = req;
36+
if (request.url) {
37+
const url = new URL(request.url);
38+
39+
// only save requests that passes the given URL regex
40+
if (!url.pathname.includes(filterURLRegex)) return;
41+
const requestSample: RequestSample = {
42+
id: req._request_id as string,
43+
request: {
44+
method: request.method,
45+
url: request.url,
46+
body: request.postData?.text,
47+
cookies: request.cookies,
48+
headers: request.headers,
49+
queryString: request.queryString,
50+
href: url.href,
51+
search: url.search,
52+
pathname: url.pathname,
53+
},
54+
response: {
55+
status: response.status,
56+
body: content,
57+
headers: response.headers,
58+
},
59+
};
60+
if (isFirst) {
61+
chrome.storage.sync.clear();
62+
isFirst = false;
63+
} else {
64+
if (!items[requestSample.request.pathname]) {
65+
items[requestSample.request.pathname] = [];
66+
}
67+
items[requestSample.request.pathname].push(requestSample);
68+
}
69+
}
70+
});
71+
};
72+
chrome.devtools.network.onRequestFinished.addListener(requestSamplerListener);
73+
74+
function saveDBAsJSON() {
75+
chrome.storage.sync.get((items) => {
76+
console.log(JSON.stringify(items));
77+
});
78+
}
79+
document
80+
.getElementById("save-db-as-json")!
81+
.addEventListener("click", saveDBAsJSON);
82+
function startSampling() {}
83+
function stopSampling() {
84+
chrome.storage.sync.clear();
85+
86+
chrome.devtools.network.onRequestFinished.removeListener(
87+
requestSamplerListener
88+
);
89+
}

0 commit comments

Comments
 (0)