diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..ddab9bf --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,11 @@ +{ + "trailingComma": "es5", + "arrowParens": "always", + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": true, + "jsxSingleQuote": true, + "printWidth": 100, + "proseWrap": "always" +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f0b6ff6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "editor.rulers": [100], + "editor.formatOnSave": true +} diff --git a/index.ts b/index.ts index 7f0283f..6304c17 100644 --- a/index.ts +++ b/index.ts @@ -61,12 +61,19 @@ type RPCError = { // the spec exposes under a consolidated name with no single legacy equivalent, e.g. "beach", plus // a few one-off spec/backend naming or shape mismatches). Update this file directly as gaps are // found through testing. -const namedArgumentMapper: Record = argumentMap.positional as Record; +const namedArgumentMapper: Record = argumentMap.positional as Record< + MethodRef, + Argument[] +>; // RPC methods whose Perl implementation already takes a single hashref/hash of named arguments. // Two versions of this are needed because sometimes named arguments are [{ args }] or { args } depending on context -const namedOnlyFirstPositionSet: Set = new Set(argumentMap.namedOnlyFirstPosition as MethodRef[]); -const namedOnlyArgumentHashSet: Set = new Set(argumentMap.namedOnlyArgumentHash as MethodRef[]); +const namedOnlyFirstPositionSet: Set = new Set( + argumentMap.namedOnlyFirstPosition as MethodRef[] +); +const namedOnlyArgumentHashSet: Set = new Set( + argumentMap.namedOnlyArgumentHash as MethodRef[] +); const port = process.env.PORT || 5999; const app = express(); @@ -128,7 +135,10 @@ app.post('/v2/:module/:method', async (req, res) => { const newArgList = [...namedArgumentMapper[methodRef]]; for (const arg of newArgList) { - if ((arg.required && typeof body.params[arg.name] === 'undefined') || body.params[arg.name] === null) { + if ( + (arg.required && typeof body.params[arg.name] === 'undefined') || + body.params[arg.name] === null + ) { return { id: 1, jsonrpc: '2.0', @@ -153,15 +163,25 @@ app.post('/v2/:module/:method', async (req, res) => { // positional args instead, which crashes those methods (confirmed via curl against // /map/get_star_map: bare-object params throws "Can't use string (...) as a HASH ref"). // However that said.. - if (namedOnlyFirstPositionSet.has(methodRef) && !Array.isArray(body.params) && body.params !== null) { + if ( + namedOnlyFirstPositionSet.has(methodRef) && + !Array.isArray(body.params) && + body.params !== null + ) { body.params = [token ? { session_id: token, ...body.params } : body.params]; } // .. there are cases when we need to support a bare hash being passed in as well! - else if (namedOnlyArgumentHashSet.has(methodRef) && !Array.isArray(body.params) && body.params !== null) { + else if ( + namedOnlyArgumentHashSet.has(methodRef) && + !Array.isArray(body.params) && + body.params !== null + ) { body.params = token ? { session_id: token, ...body.params } : body.params; } else if (methodRef !== '/empire/login' && !!token && body.params !== null) { - body.params = Array.isArray(body.params) ? [token, ...body.params] : { session_id: token, ...body.params }; + body.params = Array.isArray(body.params) + ? [token, ...body.params] + : { session_id: token, ...body.params }; } return body; diff --git a/package-lock.json b/package-lock.json index 5548a26..1b85df9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@types/cors": "^2.8.19", "@types/express": "^5.0.3", "@types/morgan": "^1.9.10", + "prettier": "^3.9.6", "typescript": "^5.9.2" }, "engines": { @@ -787,6 +788,22 @@ "node": ">=16" } }, + "node_modules/prettier": { + "version": "3.9.6", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.6.tgz", + "integrity": "sha512-OpN0zzVdiaiAhxpuuj5efpIS4sY9j7bY6uR5mnj5yPzGkdkjNKSJeUThPb60Jw29QuAZgA4o+/iB49kFiaBX6g==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", diff --git a/package.json b/package.json index 570185e..5cb9c7a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,10 @@ "node": ">= 22.6.0", "npm": ">= 10.0.0" }, + "scripts": { + "format:check": "prettier --check .", + "format:fix": "prettier --write ." + }, "dependencies": { "body-parser": "^2.2.0", "cors": "^2.8.5", @@ -19,6 +23,7 @@ "@types/cors": "^2.8.19", "@types/express": "^5.0.3", "@types/morgan": "^1.9.10", + "prettier": "^3.9.6", "typescript": "^5.9.2" } }