Skip to content

Commit

Permalink
slightly improve swagger page
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Dec 1, 2024
1 parent 18657c7 commit 650d804
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 106 deletions.
112 changes: 56 additions & 56 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
module.exports = {
root: true,
extends: ["airbnb-base", "prettier", "plugin:import/recommended", "plugin:import/typescript"],
ignorePatterns: ["dist/**/*", "build/**/*", "public/swagger.json", "public/custom.js"],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "prettier", "import"],
rules: {
// fixes prettier/eslint formatting conflict
indent: ["error", "tab", { SwitchCase: 1 }],
// snake_case used in emitted JSON
camelcase: "off",
// circular imports needed for collection sharing
"import/no-cycle": "off",
"no-plusplus": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"no-console": "off",
"no-param-reassign": "off",
"consistent-return": "off",
"import/prefer-default-export": "off",
"prefer-destructuring": "off",
"class-methods-use-this": "off",
"import/extensions": [
"error",
"ignorePackages",
{
js: "never",
jsx: "never",
ts: "never",
tsx: "never",
mjs: "never",
},
],
},
settings: {
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: "tsconfig.json",
},
node: {
extensions: [".js", ".ts", ".json"],
},
},
"import/extensions": [
"error",
"ignorePackages",
{
"": "never",
js: "never",
jsx: "never",
ts: "never",
tsx: "never",
},
],
},
};
module.exports = {
root: true,
extends: ["airbnb-base", "prettier", "plugin:import/recommended", "plugin:import/typescript"],
ignorePatterns: ["dist/**/*", "build/**/*", "public/swagger.json", "public/*"],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "prettier", "import"],
rules: {
// fixes prettier/eslint formatting conflict
indent: ["error", "tab", { SwitchCase: 1 }],
// snake_case used in emitted JSON
camelcase: "off",
// circular imports needed for collection sharing
"import/no-cycle": "off",
"no-plusplus": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"no-console": "off",
"no-param-reassign": "off",
"consistent-return": "off",
"import/prefer-default-export": "off",
"prefer-destructuring": "off",
"class-methods-use-this": "off",
"import/extensions": [
"error",
"ignorePackages",
{
js: "never",
jsx: "never",
ts: "never",
tsx: "never",
mjs: "never",
},
],
},
settings: {
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: "tsconfig.json",
},
node: {
extensions: [".js", ".ts", ".json"],
},
},
"import/extensions": [
"error",
"ignorePackages",
{
"": "never",
js: "never",
jsx: "never",
ts: "never",
tsx: "never",
},
],
},
};
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"exec": "npm run exec",
"watch": [
"src",
"public/custom.js",
"public/custom*",
".env"
],
"ext": "ts, css, json"
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start": "node dist/src/index.js",
"swag": "tsoa spec",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
"prettier": "prettier \"{,!(node_modules)/**/}*.ts\" --config .prettierrc --write"
"prettier": "prettier \"{,!(node_modules)/**/}*.{js,ts}\" --config .prettierrc --write"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -50,7 +50,7 @@
"express": "^4.21.1",
"firestorm-db": "^1.13.0",
"form-data": "^4.0.1",
"isomorphic-dompurify": "^2.17.0",
"isomorphic-dompurify": "^2.18.0",
"multer": "^1.4.5-lts.1",
"response-time": "^2.3.3",
"statuses": "^2.0.1",
Expand Down
37 changes: 18 additions & 19 deletions public/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Swagger pre-auth and auth script for Swagger UI
* @author TheRolf
*/
(function () {
(() => {
const API_KEY = "ApiKey";

function startUntil(func, cond) {
Expand All @@ -17,37 +17,37 @@
}, 20);
}

function getKeys() {
const value = window.localStorage.getItem(API_KEY);
function getTokens() {
const value = localStorage.getItem(API_KEY);

// if new guy, return empty object
if (!value) return {};

// try to parse and return value
try {
return JSON.parse(value);
} catch {}

// if not parsed, set empty object
return {};
} catch {
// if not parsed, set empty object
return {};
}
}

startUntil(
() => {
const originalAuthorize = ui.authActions.authorize;

// on login
ui.authActions.authorize = function (payload) {
ui.authActions.authorize = (payload) => {
const key = Object.keys(payload)[0];

// get stored keys
const apiKeys = getKeys();
const apiKeys = getTokens();

// add this one
apiKeys[key] = payload[key].value;

// update keys
window.localStorage.setItem(API_KEY, JSON.stringify(apiKeys));
localStorage.setItem(API_KEY, JSON.stringify(apiKeys));

// call original key
return originalAuthorize(payload);
Expand All @@ -57,15 +57,14 @@
const originalLogout = ui.authActions.logout;

// on logout
ui.authActions.logout = function (payload) {
const apiKeys = getKeys();
ui.authActions.logout = (payload) => {
const apiTokens = getTokens();

console.log(apiKeys);
// delete key if existing
if (payload[0] in apiKeys) delete apiKeys[payload[0]];
if (payload[0] in apiTokens) delete apiTokens[payload[0]];

// update keys
window.localStorage.setItem(API_KEY, JSON.stringify(apiKeys));
localStorage.setItem(API_KEY, JSON.stringify(apiTokens));

// call original key
return originalLogout(payload);
Expand All @@ -74,14 +73,14 @@
// on load
// load each token,
// For each existing token, pre auth
const apiKeys = getKeys();
const apiTokens = getTokens();

const keys = Object.keys(apiKeys);
const keys = Object.keys(apiTokens);
keys.forEach((key) => {
window.ui.preauthorizeApiKey(key, apiKeys[key]);
ui.preauthorizeApiKey(key, apiTokens[key]);
});

if (keys.length) console.info(`Pre-authed to ${keys.join(", ")}`);
if (keys.length) console.log(`Pre-authed to ${keys.join(", ")}`);
},
() => window.ui !== undefined,
);
Expand Down
40 changes: 18 additions & 22 deletions public/custom_dom.js → public/customDOM.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
/**
* Swagger custom dom for Swagger UI
* @author TheRolf
*/

const GITHUB_URL = 'https://github.com/Faithful-Resource-Pack/API';

(() => {
// eslint-disable-next-line no-undef
const doc = document;

doc.addEventListener('DOMContentLoaded', () => {
// Add custom footer
const { body } = doc;
body.innerHTML += `<a href="${GITHUB_URL}" target="_blank"\
style="position: absolute; top: 0; right: 0;">\
<img decoding="async" loading="lazy" width="149" height="149"\
src="https://github.blog/wp-content/uploads/2008/12/forkme_right_green_007200.png"\
alt="Fork me on GitHub" data-recalc-dims="1" />\
</a>`;
});
})();
/**
* Swagger custom dom for Swagger UI
* @author TheRolf
*/

const GITHUB_URL = "https://github.com/Faithful-Resource-Pack/API";

(() => {
document.addEventListener("DOMContentLoaded", () => {
// Add custom footer
document.body.innerHTML += `<a href="${GITHUB_URL}" target="_blank"\
style="position: absolute; top: 0; right: 0;">\
<img decoding="async" loading="lazy" width="149" height="149"\
src="https://github.blog/wp-content/uploads/2008/12/forkme_right_green_007200.png"\
alt="Fork me on GitHub" data-recalc-dims="1" />\
</a>`;
});
})();
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ app.use(
// @types/swagger-ui-express isn't updated and complains
{
customCssUrl: "/custom.css",
customJs: ["/custom.js", "/custom_dom.js"],
customJs: ["/custom.js", "/customDOM.js"],
swaggerOptions: {
tryItOutEnabled: true,
},
Expand Down

0 comments on commit 650d804

Please sign in to comment.