Skip to content

Commit

Permalink
chore: add biome
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshPatel5940 committed Aug 1, 2024
1 parent d21bf39 commit d4ca000
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 128 deletions.
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

20 changes: 10 additions & 10 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { createSignal } from 'solid-js'
import solidLogo from './assets/solid.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { createSignal } from 'solid-js';
import solidLogo from './assets/solid.svg';
import viteLogo from '/vite.svg';
import './App.css';

function App() {
const [count, setCount] = createSignal(0)
const [count, setCount] = createSignal(0);

return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<a href="https://vitejs.dev" target="_blank" rel="noreferrer">
<img src={viteLogo} class="logo" alt="Vite logo" />
</a>
<a href="https://solidjs.com" target="_blank">
<a href="https://solidjs.com" target="_blank" rel="noreferrer">
<img src={solidLogo} class="logo solid" alt="Solid logo" />
</a>
</div>
<h1>Vite + Solid</h1>
<div class="card">
<button onClick={() => setCount((count) => count + 1)}>
<button type="button" onClick={() => setCount(count => count + 1)}>
count is {count()}
</button>
<p>
Expand All @@ -29,7 +29,7 @@ function App() {
Click on the Vite and Solid logos to learn more
</p>
</>
)
);
}

export default App
export default App;
10 changes: 5 additions & 5 deletions client/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* @refresh reload */
import { render } from 'solid-js/web'
import { render } from 'solid-js/web';

import './index.css'
import App from './App'
import './index.css';
import App from './App';

const root = document.getElementById('root')
const root = document.getElementById('root') as HTMLElement;

render(() => <App />, root!)
render(() => <App />, root);
6 changes: 3 additions & 3 deletions client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from 'vite'
import solid from 'vite-plugin-solid'
import { defineConfig } from 'vite';
import solid from 'vite-plugin-solid';

export default defineConfig({
plugins: [solid()],
})
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "kzilla.xyz-server",
"version": "3.0",
"scripts": {
"start": "tsx src/index.ts",
"lint": "biome check .",
"lint:fix": "biome check . --write --unsafe",
"dev": "tsx watch src/index.ts"
},
"dependencies": {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import CONFIG from "./utils/env";
import { BootstrapServer } from "./utils/server";
import { serve } from '@hono/node-server';
import { Hono } from 'hono';
import CONFIG from './utils/env';
import { BootstrapServer } from './utils/server';

function main() {
const app = new Hono();
Expand Down
6 changes: 3 additions & 3 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Hono } from "hono";
import linkRouter from "./links-router";
import { Hono } from 'hono';
import linkRouter from './links-router';

const appRouter = new Hono();

appRouter.route("/links", linkRouter);
appRouter.route('/links', linkRouter);

export default appRouter;
4 changes: 2 additions & 2 deletions src/routes/links-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hono } from "hono";
import { Hono } from 'hono';

const linkRouter = new Hono();

linkRouter.get("/:id", (c) => c.json(`test get ${c.req.param("id")}`));
linkRouter.get('/:id', c => c.json(`test get ${c.req.param('id')}`));

export default linkRouter;
4 changes: 2 additions & 2 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from "./env";
import { type Db, MongoClient } from "mongodb";
import config from './env';
import { type Db, MongoClient } from 'mongodb';

let db: Db;

Expand Down
14 changes: 7 additions & 7 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { config } from "dotenv";
import { z } from "zod";
import { MONGODB_URI_REGEX_PATTERN } from "./contants";
import { config } from 'dotenv';
import { z } from 'zod';
import { MONGODB_URI_REGEX_PATTERN } from './contants';

config();

const envSchema = z.object({
NODE_ENV: z.enum(["development", "production"]).default("development"),
PORT: z.string().regex(/^\d+$/).optional().default("5050"),
MONGODB_URI: z.string().refine((value) => {
NODE_ENV: z.enum(['development', 'production']).default('development'),
PORT: z.string().regex(/^\d+$/).optional().default('5050'),
MONGODB_URI: z.string().refine(value => {
return MONGODB_URI_REGEX_PATTERN.test(value);
}, "Invalid MongoDB URI"),
}, 'Invalid MongoDB URI'),
});

const parsedSchema = envSchema.parse(process.env);
Expand Down
152 changes: 76 additions & 76 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
import consola from "consola";
import type { NextFunction, Request, Response } from "express";
import postgres from "postgres";
import { ZodError } from "zod";
import consola from 'consola';
import type { NextFunction, Request, Response } from 'express';
import postgres from 'postgres';
import { ZodError } from 'zod';

type HttpErrorCode =
| "BAD_REQUEST"
| "UNAUTHORIZED"
| "NOT_FOUND"
| "METHOD_NOT_ALLOWED"
| "NOT_ACCEPTABLE"
| "REQUEST_TIMEOUT"
| "CONFLICT"
| "GONE"
| "LENGTH_REQUIRED"
| "PRECONDITION_FAILED"
| "PAYLOAD_TOO_LARGE"
| "URI_TOO_LONG"
| "UNSUPPORTED_MEDIA_TYPE"
| "RANGE_NOT_SATISFIABLE"
| "EXPECTATION_FAILED"
| "TEAPOT";
| 'BAD_REQUEST'
| 'UNAUTHORIZED'
| 'NOT_FOUND'
| 'METHOD_NOT_ALLOWED'
| 'NOT_ACCEPTABLE'
| 'REQUEST_TIMEOUT'
| 'CONFLICT'
| 'GONE'
| 'LENGTH_REQUIRED'
| 'PRECONDITION_FAILED'
| 'PAYLOAD_TOO_LARGE'
| 'URI_TOO_LONG'
| 'UNSUPPORTED_MEDIA_TYPE'
| 'RANGE_NOT_SATISFIABLE'
| 'EXPECTATION_FAILED'
| 'TEAPOT';

type BackendErrorCode =
| "VALIDATION_ERROR"
| "USER_NOT_FOUND"
| "INVALID_PASSWORD";
| 'VALIDATION_ERROR'
| 'USER_NOT_FOUND'
| 'INVALID_PASSWORD';

type ErrorCode = HttpErrorCode | BackendErrorCode | "INTERNAL_ERROR";
type ErrorCode = HttpErrorCode | BackendErrorCode | 'INTERNAL_ERROR';

export function getStatusFromErrorCode(code: ErrorCode): number {
switch (code) {
case "BAD_REQUEST":
case "VALIDATION_ERROR":
case 'BAD_REQUEST':
case 'VALIDATION_ERROR':
return 400;
case "UNAUTHORIZED":
case "INVALID_PASSWORD":
case 'UNAUTHORIZED':
case 'INVALID_PASSWORD':
return 401;
case "NOT_FOUND":
case "USER_NOT_FOUND":
case 'NOT_FOUND':
case 'USER_NOT_FOUND':
return 404;
case "METHOD_NOT_ALLOWED":
case 'METHOD_NOT_ALLOWED':
return 405;
case "NOT_ACCEPTABLE":
case 'NOT_ACCEPTABLE':
return 406;
case "REQUEST_TIMEOUT":
case 'REQUEST_TIMEOUT':
return 408;
case "CONFLICT":
case 'CONFLICT':
return 409;
case "GONE":
case 'GONE':
return 410;
case "LENGTH_REQUIRED":
case 'LENGTH_REQUIRED':
return 411;
case "PRECONDITION_FAILED":
case 'PRECONDITION_FAILED':
return 412;
case "PAYLOAD_TOO_LARGE":
case 'PAYLOAD_TOO_LARGE':
return 413;
case "URI_TOO_LONG":
case 'URI_TOO_LONG':
return 414;
case "UNSUPPORTED_MEDIA_TYPE":
case 'UNSUPPORTED_MEDIA_TYPE':
return 415;
case "RANGE_NOT_SATISFIABLE":
case 'RANGE_NOT_SATISFIABLE':
return 416;
case "EXPECTATION_FAILED":
case 'EXPECTATION_FAILED':
return 417;
case "TEAPOT":
case 'TEAPOT':
return 418; // I'm a teapot
case "INTERNAL_ERROR":
case 'INTERNAL_ERROR':
return 500;
default:
return 500;
Expand All @@ -74,24 +74,24 @@ export function getStatusFromErrorCode(code: ErrorCode): number {

export function getMessageFromErrorCode(code: ErrorCode): string {
switch (code) {
case "BAD_REQUEST":
return "The request is invalid.";
case "VALIDATION_ERROR":
return "The request contains invalid or missing fields.";
case "UNAUTHORIZED":
return "You are not authorized to access this resource.";
case "NOT_FOUND":
return "The requested resource was not found.";
case "USER_NOT_FOUND":
return "The user was not found.";
case "INTERNAL_ERROR":
return "An internal server error occurred.";
case "CONFLICT":
return "The request conflicts with the current state of the server.";
case "INVALID_PASSWORD":
return "The password is incorrect.";
case 'BAD_REQUEST':
return 'The request is invalid.';
case 'VALIDATION_ERROR':
return 'The request contains invalid or missing fields.';
case 'UNAUTHORIZED':
return 'You are not authorized to access this resource.';
case 'NOT_FOUND':
return 'The requested resource was not found.';
case 'USER_NOT_FOUND':
return 'The user was not found.';
case 'INTERNAL_ERROR':
return 'An internal server error occurred.';
case 'CONFLICT':
return 'The request conflicts with the current state of the server.';
case 'INVALID_PASSWORD':
return 'The password is incorrect.';
default:
return "An internal server error occurred.";
return 'An internal server error occurred.';
}
}

Expand All @@ -103,9 +103,9 @@ export function handleValidationError(err: ZodError): {
const requiredFields = [];

for (const error of err.errors) {
if (error.code === "invalid_type") invalidFields.push(error.path.join("."));
else if (error.message === "Required")
requiredFields.push(error.path.join("."));
if (error.code === 'invalid_type') invalidFields.push(error.path.join('.'));
else if (error.message === 'Required')
requiredFields.push(error.path.join('.'));
}

return {
Expand All @@ -125,7 +125,7 @@ export class BackendError extends Error {
}: {
message?: string;
details?: unknown;
} = {}
} = {},
) {
super(message ?? getMessageFromErrorCode(code));
this.code = code;
Expand All @@ -141,7 +141,7 @@ export function errorHandler(
message: string;
details?: unknown;
}>,
_next: NextFunction
_next: NextFunction,
) {
let statusCode = 500;
let code: ErrorCode | undefined;
Expand All @@ -160,26 +160,26 @@ export function errorHandler(
}

if (error instanceof postgres.PostgresError) {
code = "INTERNAL_ERROR";
message = "The DB crashed maybe because they dont like you :p";
code = 'INTERNAL_ERROR';
message = 'The DB crashed maybe because they dont like you :p';
statusCode = getStatusFromErrorCode(code);
details = error;
}

if (error instanceof ZodError) {
code = "VALIDATION_ERROR";
code = 'VALIDATION_ERROR';
message = getMessageFromErrorCode(code);
details = handleValidationError(error);
statusCode = getStatusFromErrorCode(code);
}

if ((error as { code: string }).code === "ECONNREFUSED") {
code = "INTERNAL_ERROR";
message = "The DB crashed maybe because they dont like you :p";
if ((error as { code: string }).code === 'ECONNREFUSED') {
code = 'INTERNAL_ERROR';
message = 'The DB crashed maybe because they dont like you :p';
details = error;
}

code = code ?? "INTERNAL_ERROR";
code = code ?? 'INTERNAL_ERROR';
message = message ?? getMessageFromErrorCode(code);
details = details ?? error;

Expand All @@ -193,10 +193,10 @@ export function errorHandler(
}

export function handle404Error(_req: Request, res: Response) {
const code: ErrorCode = "NOT_FOUND";
const code: ErrorCode = 'NOT_FOUND';
res.status(getStatusFromErrorCode(code)).json({
code,
message: "Route not found",
details: "The route you are trying to access does not exist",
message: 'Route not found',
details: 'The route you are trying to access does not exist',
});
}
Loading

0 comments on commit d4ca000

Please sign in to comment.