Skip to content

Commit

Permalink
Update eslint configuration and error handling
Browse files Browse the repository at this point in the history
Refactored the ESLint configurations for cleaner, more maintainable rule management across different modules. Additionally, enhanced the handling of errors in API calls in the chat application, providing clear return messages and reducing the risk of unhandled exceptions.
  • Loading branch information
evgenius1424 committed Jun 26, 2024
1 parent 2ff2530 commit b605439
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 23 deletions.
9 changes: 9 additions & 0 deletions apps/learnbefore-bff/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
extends: ["@repo/eslint-config/library.js"],
parser: "@typescript-eslint/parser",
rules: {
"no-redeclare": "off",
},
}
6 changes: 4 additions & 2 deletions apps/learnbefore-bff/api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OpenAI from "openai"
import { startExpress } from "../src/start-express"
import { getWords } from "../src/get-words"
import { Message, User, Word } from "../types"
import { Message, User } from "../types"
import { ClerkExpressRequireAuth } from "@clerk/clerk-sdk-node"
import { Store } from "../src/store"
import { splitText } from "../src/split-text"
Expand All @@ -10,7 +10,9 @@ import expressAsyncHandler from "express-async-handler"
import { retryableGenerator } from "../src/retryable-generator"

declare global {
// eslint-disable-next-line no-unused-vars
namespace Express {
// eslint-disable-next-line no-unused-vars
interface Request {
auth: { userId: string }
}
Expand All @@ -21,7 +23,7 @@ const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})

const app = startExpress(parseInt(process.env.PORT || "3000"))
const app = startExpress(parseInt(process.env.EXPRESS_PORT || "3000"))

const store = new Store(process.env.MONGO_CONNECTION_STRING!!)

Expand Down
1 change: 1 addition & 0 deletions apps/learnbefore-bff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/api/chat.js",
"scripts": {
"build": "npx tsc",
"lint": "eslint . --max-warnings 0",
"preview": "node dist/api/chat.js",
"dev": "nodemon api/chat.ts"
},
Expand Down
1 change: 1 addition & 0 deletions apps/learnbefore-bff/src/start-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function startExpress(
err: { stack: unknown },
req: Request,
res: Response,
// eslint-disable-next-line no-unused-vars
next: NextFunction,
) => {
console.error(err.stack)
Expand Down
1 change: 1 addition & 0 deletions apps/learnbefore-bff/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Message, User, Word } from "../types"
export class Store {
private db: Db | undefined

// eslint-disable-next-line no-unused-vars
constructor(private connectionString: string) {}

public async getUserMessages(userId: string, limit = 10): Promise<Message[]> {
Expand Down
19 changes: 5 additions & 14 deletions apps/learnbefore/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
extends: ["@repo/eslint-config/vite-react.js"],
parser: "@typescript-eslint/parser",
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
"no-redeclare": "off"
}
}
4 changes: 3 additions & 1 deletion apps/learnbefore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint": "eslint . --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
Expand All @@ -26,8 +26,10 @@
},
"devDependencies": {
"@repo/types": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@repo/eslint-config": "workspace:*",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@vitejs/plugin-react": "^4.3.0",
Expand Down
23 changes: 23 additions & 0 deletions apps/learnbefore/src/helpers/fetch-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type ApiError = {
message: string
}

// TODO: experimental
export async function fetchJson(url: string, init: RequestInit = {}) {
const response = await fetch(url, init)
if (!response.ok) {
throw {
message: experimentalErrorMessage,
} as ApiError
}

try {
return await response.json()
} catch (error) {
throw {
message: experimentalErrorMessage,
} as ApiError
}
}

const experimentalErrorMessage = "An error occurred, please try again later."
1 change: 0 additions & 1 deletion apps/learnbefore/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ ReactDOM.createRoot(document.getElementById("root")!).render(
<ClerkProvider publishableKey={PUBLISHABLE_KEY}>
<App />
</ClerkProvider>
,
</React.StrictMode>,
)
8 changes: 3 additions & 5 deletions apps/learnbefore/src/pages/chat-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { PaperclipIcon } from "../icons/paperclip-icon.tsx"
import { getTextFromFile } from "../helpers/get-text-from-file.ts"
import { createMessageFetcher } from "../helpers/fetchers.ts"
import { useTranslation } from "react-i18next"
import { fetchJson } from "../helpers/fetch-json.ts"

export const ChatPage: React.FC = () => {
const [messages, setMessages] = useState<Message[] | null>(null)
Expand Down Expand Up @@ -43,10 +44,7 @@ export const ChatPage: React.FC = () => {
expandedMessages.includes(message.id)

useEffect(() => {
fetch("/api/chat")
.then((res) => res.json())
.then(setMessages)
.catch(setError)
fetchJson("/api/chat").then(setMessages).catch(setError)
}, [])

const handleSend = async (e: React.FormEvent) => {
Expand Down Expand Up @@ -104,7 +102,7 @@ export const ChatPage: React.FC = () => {

setInputValue("")
}
if (error) return <div>Error occurred: {error}</div>
if (error) return <span>{JSON.stringify(error)}</span>

return (
<AppShell>
Expand Down
18 changes: 18 additions & 0 deletions packages/eslint-config/vite-react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
}
9 changes: 9 additions & 0 deletions packages/types/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
extends: ["@repo/eslint-config/library.js"],
parser: "@typescript-eslint/parser",
rules: {
"no-redeclare": "off",
},
}
3 changes: 3 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "@repo/types",
"version": "0.0.0",
"private": true,
"scripts": {
"lint": "eslint . --max-warnings 0"
},
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

5 changes: 5 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"globalDependencies": [
"**/.env.*local"
],
"globalEnv": [
"OPENAI_API_KEY",
"MONGO_CONNECTION_STRING",
"EXPRESS_PORT"
],
"tasks": {
"build": {
"dependsOn": [
Expand Down

0 comments on commit b605439

Please sign in to comment.