Skip to content

Commit

Permalink
feat: project structure setup
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberboyanmol committed Aug 23, 2024
1 parent 4181ff2 commit c347665
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default { extends: ['@commitlint/config-conventional'] };
export default { extends: ['@commitlint/config-conventional'] }
10 changes: 5 additions & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import globals from 'globals'
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
export default [
{ files: ['**/*.{js,mjs,cjs,ts}'] },
{
Expand All @@ -15,4 +15,4 @@ export default [
pluginJs.configs.recommended,
...tseslint.configs.recommended,
eslintPluginPrettierRecommended
];
]
4 changes: 2 additions & 2 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
semi: true,
semi: false,
trailingComma: 'none',
singleQuote: true,
printWidth: 120,
tabWidth: 2
};
}
62 changes: 31 additions & 31 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { apiReference } from '@scalar/hono-api-reference';
import { logger } from 'hono/logger';
import { prettyJSON } from 'hono/pretty-json';
import { Home } from './pages/home';
import { Routes } from '#common/types';
import type { HTTPException } from 'hono/http-exception';
import { OpenAPIHono } from '@hono/zod-openapi'
import { apiReference } from '@scalar/hono-api-reference'
import { logger } from 'hono/logger'
import { prettyJSON } from 'hono/pretty-json'
import { Home } from './pages/home'
import { Routes } from '#common/types'
import type { HTTPException } from 'hono/http-exception'

export class App {
private app: OpenAPIHono;
private app: OpenAPIHono

constructor(routes: Routes[]) {
this.app = new OpenAPIHono();
this.initializeGlobalMiddleware();
this.initializeRoutes(routes);
this.initializeSwaggerUI();
this.initializeRouteFallback();
this.initializeErrorHandler();
this.app = new OpenAPIHono()
this.initializeGlobalMiddleware()
this.initializeRoutes(routes)
this.initializeSwaggerUI()
this.initializeRouteFallback()
this.initializeErrorHandler()
}

private initializeRoutes(routes: Routes[]) {
routes.forEach((route) => {
route.initRoutes();
this.app.route('/api', route.controller);
});
this.app.route('/', Home);
route.initRoutes()
this.app.route('/api', route.controller)
})
this.app.route('/', Home)
}

private initializeGlobalMiddleware() {
this.app.use(logger());
this.app.use(prettyJSON());
this.app.use(logger())
this.app.use(prettyJSON())
}

private initializeSwaggerUI() {
this.app.doc31('/swagger', (c) => {
const { protocol: urlProtocol, hostname, port } = new URL(c.req.url);
const protocol = c.req.header('x-forwarded-proto') ? `${c.req.header('x-forwarded-proto')}:` : urlProtocol;
const { protocol: urlProtocol, hostname, port } = new URL(c.req.url)
const protocol = c.req.header('x-forwarded-proto') ? `${c.req.header('x-forwarded-proto')}:` : urlProtocol

return {
openapi: '3.1.0',
Expand All @@ -52,8 +52,8 @@ export class App {
description: 'Current environment'
}
]
};
});
}
})

this.app.get(
'/docs',
Expand All @@ -76,7 +76,7 @@ export class App {
url: '/swagger'
}
})
);
)
}

private initializeRouteFallback() {
Expand All @@ -87,16 +87,16 @@ export class App {
message: 'oops route not found!!. check docs at https://exercisedb-api.vercel.app/docs'
},
404
);
});
)
})
}
private initializeErrorHandler() {
this.app.onError((err, c) => {
const error = err as HTTPException;
return c.json({ success: false, message: error.message }, error.status || 500);
});
const error = err as HTTPException
return c.json({ success: false, message: error.message }, error.status || 500)
})
}
public getApp() {
return this.app;
return this.app
}
}
2 changes: 1 addition & 1 deletion src/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './route.type';
export * from './route.type'
6 changes: 3 additions & 3 deletions src/common/types/route.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OpenAPIHono } from '@hono/zod-openapi';
import type { OpenAPIHono } from '@hono/zod-openapi'

export interface Routes {
controller: OpenAPIHono;
initRoutes: () => void;
controller: OpenAPIHono
initRoutes: () => void
}
20 changes: 10 additions & 10 deletions src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Hono } from 'hono';
import { Hono } from 'hono'

export const Home = new Hono();
export const Home = new Hono()

export const Meteors = ({ number }: { number: number }) => {
return (
<>
{Array.from({ length: number || 30 }, (_, idx) => {
<span
;<span
key={idx}
class="meteor animate-[meteorAnimation_3s_linear_infinite] absolute h-1 w-1 rounded-[9999px] shadow-[0_0_0_1px_#ffffff10] rotate-[215deg]"
style={{
Expand All @@ -15,16 +15,16 @@ export const Meteors = ({ number }: { number: number }) => {
animationDelay: `${Math.random() * (0.8 - 0.2) + 0.2}s`,
animationDuration: `${Math.floor(Math.random() * (10 - 2) + 2)}s`
}}
></span>;
></span>
})}
</>
);
};
)
}

Home.get('/', (c) => {
const title = 'ExerciseDB API';
const title = 'ExerciseDB API'
const description =
'Access detailed data on over 1300+ exercises with the ExerciseDB API. This API offers extensive information on each exercise, including target body parts, equipment needed, GIFs for visual guidance, and step-by-step instructions.';
'Access detailed data on over 1300+ exercises with the ExerciseDB API. This API offers extensive information on each exercise, including target body parts, equipment needed, GIFs for visual guidance, and step-by-step instructions.'
return c.html(
<html>
<head>
Expand Down Expand Up @@ -172,5 +172,5 @@ Home.get('/', (c) => {
</main>
</body>
</html>
);
});
)
})
6 changes: 3 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App } from './app';
import { App } from './app'

const app = new App([]).getApp();
const app = new App([]).getApp()

export default app;
export default app
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { configDefaults, defineConfig } from 'vitest/config';
import { configDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
test: {
Expand All @@ -18,4 +18,4 @@ export default defineConfig({
}
}
}
});
})

0 comments on commit c347665

Please sign in to comment.