Skip to content

Commit

Permalink
better use code, prettier json, update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Mar 15, 2024
1 parent 0f26303 commit e81f02b
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 342 deletions.
18 changes: 9 additions & 9 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": false,
"printWidth": 100,
"useTabs": true,
"endOfLine": "lf",
"bracketSpacing": true,
"arrowParens": "always",
"parser": "typescript"
"semi": true,
"trailingComma": "all",
"singleQuote": false,
"printWidth": 100,
"useTabs": true,
"endOfLine": "lf",
"bracketSpacing": true,
"arrowParens": "always",
"parser": "typescript"
}
42 changes: 21 additions & 21 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "API",
"type": "node",
"request": "launch",
"args": [
"src/index.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
]
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "API",
"type": "node",
"request": "launch",
"args": [
"src/index.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
]
}
14 changes: 7 additions & 7 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"exec": "npm run exec",
"watch": [
"src",
"public/custom.js",
".env"
],
"ext": "ts, css, json"
"exec": "npm run exec",
"watch": [
"src",
"public/custom.js",
".env"
],
"ext": "ts, css, json"
}
118 changes: 59 additions & 59 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
{
"name": "faithful-api",
"version": "2.0.0",
"description": "Public RESTful API for Faithful's texture database, add-on submission, and more.",
"scripts": {
"dev": "npm run spec-and-routes && npm run nodemon",
"nodemon": "nodemon",
"compile": "tsc",
"spec-and-routes": "tsoa spec-and-routes",
"exec": "ts-node src/index.ts",
"build": "npm run spec-and-routes && npm run compile && cp -R public dist/",
"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"
},
"keywords": [],
"author": "",
"devDependencies": {
"@types/api-error-handler": "^1.0.36",
"@types/body-parser": "^1.19.5",
"@types/express": "^4.17.21",
"@types/multer": "^1.4.11",
"@types/node": "^20.11.20",
"@types/swagger-ui-express": "^4.1.6",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"nodemon": "^3.1.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
},
"dependencies": {
"@tsoa/runtime": "^6.0.0",
"api-error-handler": "^1.0.0",
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"cloudflare": "^2.9.1",
"cors": "^2.8.5",
"discord-api-types": "^0.37.73",
"dotenv": "^16.4.5",
"express": "^4.18.3",
"firestorm-db": "^1.12.0",
"form-data": "^4.0.0",
"isomorphic-dompurify": "^2.4.0",
"multer": "^1.4.5-lts.1",
"response-time": "^2.3.2",
"statuses": "^2.0.1",
"swagger-ui-express": "^5.0.0",
"tsoa": "^6.0.1"
}
}
{
"name": "faithful-api",
"version": "2.0.0",
"description": "Public RESTful API for Faithful's texture database, add-on submission, and more.",
"scripts": {
"dev": "npm run spec-and-routes && npm run nodemon",
"nodemon": "nodemon",
"compile": "tsc",
"spec-and-routes": "tsoa spec-and-routes",
"exec": "ts-node src/index.ts",
"build": "npm run spec-and-routes && npm run compile && cp -R public dist/",
"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"
},
"keywords": [],
"author": "",
"devDependencies": {
"@types/api-error-handler": "^1.0.36",
"@types/body-parser": "^1.19.5",
"@types/express": "^4.17.21",
"@types/multer": "^1.4.11",
"@types/node": "^20.11.27",
"@types/swagger-ui-express": "^4.1.6",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^18.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"nodemon": "^3.1.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
},
"dependencies": {
"@tsoa/runtime": "^6.1.4",
"api-error-handler": "^1.0.0",
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"cloudflare": "^2.9.1",
"cors": "^2.8.5",
"discord-api-types": "^0.37.74",
"dotenv": "^16.4.5",
"express": "^4.18.3",
"firestorm-db": "^1.12.0",
"form-data": "^4.0.0",
"isomorphic-dompurify": "^2.4.0",
"multer": "^1.4.5-lts.1",
"response-time": "^2.3.2",
"statuses": "^2.0.1",
"swagger-ui-express": "^5.0.0",
"tsoa": "^6.1.4"
}
}
124 changes: 62 additions & 62 deletions public/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,86 @@
* @author TheRolf
*/
(function () {
const API_KEY = 'ApiKey';
const API_KEY = "ApiKey";

function startUntil(func, cond) {
const it = setInterval(() => {
if (!cond()) return;
function startUntil(func, cond) {
const it = setInterval(() => {
if (!cond()) return;

// if condition true, disable interval
clearInterval(it);
// if condition true, disable interval
clearInterval(it);

// start function
func();
}, 20);
}
// start function
func();
}, 20);
}

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

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

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

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

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

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

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

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

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

// call original key
return originalAuthorize(payload);
};
// call original key
return originalAuthorize(payload);
};

// if logout is clicked delete the api key in the local storage
const originalLogout = ui.authActions.logout;
// if logout is clicked delete the api key in the local storage
const originalLogout = ui.authActions.logout;

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

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

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

// call original key
return originalLogout(payload);
};
// call original key
return originalLogout(payload);
};

// on load
// load each token,
// For each existing token, pre auth
const apiKeys = getKeys();
// on load
// load each token,
// For each existing token, pre auth
const apiKeys = getKeys();

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

if (keys.length) console.info(`Pre-authed to ${keys.join(', ')}`);
},
() => window.ui !== undefined,
);
}());
if (keys.length) console.info(`Pre-authed to ${keys.join(", ")}`);
},
() => window.ui !== undefined,
);
})();
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ app.use(async (err: any, req: Request, res: Response, next: NextFunction): Promi
const finalError = new ApiError(name, code, message);

// modify error to give more context and details with data
let modified:
| {
name: string;
message: string;
}
| undefined;
let modified: {
name: string;
message: string;
};

if (err?.response?.data !== undefined) {
modified = {
name: finalError.name,
Expand Down
4 changes: 3 additions & 1 deletion src/v2/repository/use.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export default class UseFirestormRepository implements UseRepository {
}

set(use: Use): Promise<Use> {
return uses.set(use.id, use).then(() => uses.get(use.id));
// Clone object because Firestorm removes ID fields internally
// JS works on references so use.id would become undefined after being set
return uses.set(use.id, structuredClone(use)).then(() => uses.get(use.id));
}

setMultiple(useArray: Uses): Promise<Uses> {
Expand Down
Loading

0 comments on commit e81f02b

Please sign in to comment.