diff --git a/packages/core/package.json b/packages/core/package.json index 4135b87..c451f26 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,7 +1,7 @@ { "name": "@cosmoosjs/core", "description": "create backend applications quickly and easily with bun.sh", - "version": "1.1.2", + "version": "1.1.6", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", diff --git a/packages/core/src/adapters/http-adapter.ts b/packages/core/src/adapters/http-adapter.ts index ac40611..18f8921 100644 --- a/packages/core/src/adapters/http-adapter.ts +++ b/packages/core/src/adapters/http-adapter.ts @@ -1,11 +1,11 @@ import type { FactoryBaseConfig } from '@customTypes/index'; -import type { Serve } from 'bun'; +import type { IHttpServe } from '@interfaces/index'; import type { Container } from 'inversify'; export abstract class HttpAdapter { public bindContainers(_container: Container): void {} - public listen(_config: T): Serve { + public listen(_config: T): IHttpServe { throw Error('listen function need to be implemented'); } diff --git a/packages/core/src/interfaces/start/bootstrap-config.interface.ts b/packages/core/src/interfaces/start/bootstrap-config.interface.ts index e764981..0dc4e8b 100644 --- a/packages/core/src/interfaces/start/bootstrap-config.interface.ts +++ b/packages/core/src/interfaces/start/bootstrap-config.interface.ts @@ -1,4 +1,6 @@ -export interface IBootstrapConfig { +import type { HttpAdapter } from 'src/adapters'; + +export interface IBootstrapConfig { /** * Adapters let you choose the tools you'll use during application development */ @@ -10,11 +12,15 @@ export interface IBootstrapConfig { /** Port to be used */ port: number; /** You can pass metadata to handle them using metadata */ - metadata?: T; + metadata?: Metatadas; /** HTTP server to be used */ - provider: () => Promise; + provider: () => Promise< + any & { + HttpFactory: T; + } + >; /** Exceptions handler */ - exceptions?: () => Promise; + exceptions?: () => Promise; }; }; @@ -25,22 +31,17 @@ export interface IBootstrapConfig { /** * Inject the env config to validate your environment */ - env: () => Promise; + env: () => Promise; /** * Inject all classes into our ioc library "Inversify" */ - ioc: () => Promise; + ioc: () => Promise; }; - /** - * You can inject custom providers to perform actions not native to the framework - */ - providers?: () => Promise[]; - /** * The entry point to your application */ - entrypoint?: () => Promise; + entrypoint?: () => Promise; } export interface IHttpServe { diff --git a/packages/core/src/start/bootstrap-config.ts b/packages/core/src/start/bootstrap-config.ts index f8f5adb..0777cc6 100644 --- a/packages/core/src/start/bootstrap-config.ts +++ b/packages/core/src/start/bootstrap-config.ts @@ -3,17 +3,16 @@ import { loadModule } from '@helpers/module.helper'; import { Environment, IocContainer } from 'src'; import type { IBootstrapConfig, IHttpServe } from '../interfaces'; -class AppBootstrap { - private config: IBootstrapConfig = {} as IBootstrapConfig; +class AppBootstrap { + private config: IBootstrapConfig | undefined; - public async defineConfigAndBootstrapApp(config: (injectedConfig: ConfigService) => IBootstrapConfig): Promise { - let returnedConfig: IHttpServe | void = undefined; + public async defineConfigAndBootstrapApp(config: (injectedConfig: ConfigService) => IBootstrapConfig): Promise { const configService = IocContainer.container.get(ConfigService); this.config = config(configService); this.processEnv(); await this.bindApp(); - returnedConfig = await this.loadHttp(); + const returnedConfig = await this.loadHttp(); await this.loadEntrypoint(); return returnedConfig; @@ -23,6 +22,7 @@ class AppBootstrap { * Bind all object to inversify */ private async bindApp(): Promise { + if (!this.config?.loaders?.ioc) return; const iocBindingLoader = await loadModule(this.config.loaders.ioc); iocBindingLoader(IocContainer.container); } @@ -31,6 +31,7 @@ class AppBootstrap { * Process env variables */ private async processEnv(): Promise { + if (!this.config?.loaders?.env) return; const env = IocContainer.container.get(Environment); const envLoader = await loadModule(this.config.loaders.env); env.process(envLoader); @@ -40,15 +41,15 @@ class AppBootstrap { * Load http server */ private async loadHttp(): Promise { - if (this.config.adapters?.server) { - const server = await this.config.adapters?.server.provider(); + if (this.config?.adapters?.server) { + const { provider, exceptions, ...httpConfig } = this.config.adapters.server; + const server = await provider(); server.HttpFactory.bindContainers(IocContainer.container); - if (this.config.adapters?.server.exceptions) { - const exceptionHandler = await loadModule(this.config.adapters?.server.exceptions); + if (exceptions) { + const exceptionHandler = await loadModule(exceptions); server.HttpFactory.exceptionHandler(exceptionHandler); } - const { provider, exceptions, ...httpConfig } = this.config.adapters.server; return server.HttpFactory.listen(httpConfig); } } @@ -57,11 +58,11 @@ class AppBootstrap { * Load and run entrypoint */ private async loadEntrypoint(): Promise { - if (this.config.entrypoint) { + if (this.config?.entrypoint) { const entrypoint = await loadModule(this.config.entrypoint); await entrypoint(); } } } -export const AppFactory = new AppBootstrap(); +export const AppFactory = () => new AppBootstrap(); diff --git a/packages/hono-openapi-adapter/package.json b/packages/hono-openapi-adapter/package.json index 4990034..af5383d 100644 --- a/packages/hono-openapi-adapter/package.json +++ b/packages/hono-openapi-adapter/package.json @@ -1,6 +1,6 @@ { "name": "@cosmoosjs/hono-openapi", - "version": "1.1.4", + "version": "1.1.8", "description": "Http server using hono zod for @cosmoosjs/core", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/packages/hono-openapi-adapter/src/guards/guard.abstract.ts b/packages/hono-openapi-adapter/src/guards/guard.abstract.ts index 9ae7e63..7bc68a6 100644 --- a/packages/hono-openapi-adapter/src/guards/guard.abstract.ts +++ b/packages/hono-openapi-adapter/src/guards/guard.abstract.ts @@ -1,5 +1,6 @@ import type { Context } from 'hono'; import { injectable } from 'inversify'; + @injectable() export abstract class GuardAbstract { public run(_ctx: Context): void {} diff --git a/packages/hono-openapi-adapter/src/helpers/index.ts b/packages/hono-openapi-adapter/src/helpers/index.ts index afa0ab4..9ba3045 100644 --- a/packages/hono-openapi-adapter/src/helpers/index.ts +++ b/packages/hono-openapi-adapter/src/helpers/index.ts @@ -1 +1 @@ -export { defineReflection } from './relfection.helper'; +export { defineReflection } from './reflection.helper'; diff --git a/packages/hono-openapi-adapter/src/helpers/relfection.helper.ts b/packages/hono-openapi-adapter/src/helpers/reflection.helper.ts similarity index 86% rename from packages/hono-openapi-adapter/src/helpers/relfection.helper.ts rename to packages/hono-openapi-adapter/src/helpers/reflection.helper.ts index 3ecf418..76e3827 100644 --- a/packages/hono-openapi-adapter/src/helpers/relfection.helper.ts +++ b/packages/hono-openapi-adapter/src/helpers/reflection.helper.ts @@ -2,6 +2,9 @@ import { IocContainer } from '@cosmoosjs/core'; import type { Server } from '@server/server'; import { CONTAINER, SERVER, SERVER_TARGET } from 'src/constants/reflector.constant'; +/** + * Define values by reflection for decorators + */ export function defineReflection(app: Server) { Reflect.defineMetadata(SERVER, app, SERVER_TARGET); Reflect.defineMetadata(CONTAINER, IocContainer.container, SERVER_TARGET); diff --git a/packages/hono-openapi-adapter/src/start/hono-adapter.ts b/packages/hono-openapi-adapter/src/start/hono-adapter.ts index b531058..8d42496 100644 --- a/packages/hono-openapi-adapter/src/start/hono-adapter.ts +++ b/packages/hono-openapi-adapter/src/start/hono-adapter.ts @@ -1,8 +1,7 @@ -import { ConfigService, ENV_STATE_ENUM, HttpAdapter, IocContainer, LoggerService } from '@cosmoosjs/core'; +import { ConfigService, ENV_STATE_ENUM, HttpAdapter, type IHttpServe, IocContainer, LoggerService } from '@cosmoosjs/core'; import type { FactoryConfig } from '@customTypes/index'; -import { defineReflection } from '@helpers/relfection.helper'; +import { defineReflection } from '@helpers/reflection.helper'; import { swaggerUI } from '@hono/swagger-ui'; -import type { Serve } from 'bun'; import { ReasonPhrases, StatusCodes } from 'http-status-codes'; import type { Container } from 'inversify'; import { bindToContainers } from '../ioc'; @@ -13,7 +12,7 @@ class HonoAdapter extends HttpAdapter { bindToContainers(container); } - public listen(config: FactoryConfig): Serve { + public listen(config: FactoryConfig): IHttpServe { const app = IocContainer.container.get(Server); const configService = IocContainer.container.get(ConfigService); defineReflection(app); @@ -33,6 +32,7 @@ class HonoAdapter extends HttpAdapter { ); } } + return { port: config.port, fetch: app.hono.fetch,