Skip to content

Commit

Permalink
Merge pull request #4 from geniqks/improve-typing
Browse files Browse the repository at this point in the history
Improve typing
  • Loading branch information
mathysth authored Apr 30, 2024
2 parents 3b25032 + 4a09483 commit b49b220
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/adapters/http-adapter.ts
Original file line number Diff line number Diff line change
@@ -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<T extends FactoryBaseConfig>(_config: T): Serve {
public listen<T extends FactoryBaseConfig>(_config: T): IHttpServe {
throw Error('listen function need to be implemented');
}

Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/interfaces/start/bootstrap-config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface IBootstrapConfig<T = any> {
import type { HttpAdapter } from 'src/adapters';

export interface IBootstrapConfig<Metatadas> {
/**
* Adapters let you choose the tools you'll use during application development
*/
Expand All @@ -10,11 +12,15 @@ export interface IBootstrapConfig<T = any> {
/** 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<any>;
provider: <T extends HttpAdapter>() => Promise<
any & {
HttpFactory: T;
}
>;
/** Exceptions handler */
exceptions?: () => Promise<any>;
exceptions?: () => Promise<unknown>;
};
};

Expand All @@ -25,22 +31,17 @@ export interface IBootstrapConfig<T = any> {
/**
* Inject the env config to validate your environment
*/
env: () => Promise<any>;
env: () => Promise<unknown>;
/**
* Inject all classes into our ioc library "Inversify"
*/
ioc: () => Promise<any>;
ioc: () => Promise<unknown>;
};

/**
* You can inject custom providers to perform actions not native to the framework
*/
providers?: () => Promise<any>[];

/**
* The entry point to your application
*/
entrypoint?: () => Promise<any>;
entrypoint?: () => Promise<unknown>;
}

export interface IHttpServe {
Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/start/bootstrap-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = {} as IBootstrapConfig;
class AppBootstrap<T> {
private config: IBootstrapConfig<T> | undefined;

public async defineConfigAndBootstrapApp(config: (injectedConfig: ConfigService) => IBootstrapConfig): Promise<IHttpServe | void> {
let returnedConfig: IHttpServe | void = undefined;
public async defineConfigAndBootstrapApp(config: (injectedConfig: ConfigService) => IBootstrapConfig<T>): Promise<IHttpServe | void> {
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;
Expand All @@ -23,6 +22,7 @@ class AppBootstrap {
* Bind all object to inversify
*/
private async bindApp(): Promise<void> {
if (!this.config?.loaders?.ioc) return;
const iocBindingLoader = await loadModule(this.config.loaders.ioc);
iocBindingLoader(IocContainer.container);
}
Expand All @@ -31,6 +31,7 @@ class AppBootstrap {
* Process env variables
*/
private async processEnv(): Promise<void> {
if (!this.config?.loaders?.env) return;
const env = IocContainer.container.get(Environment);
const envLoader = await loadModule(this.config.loaders.env);
env.process(envLoader);
Expand All @@ -40,15 +41,15 @@ class AppBootstrap {
* Load http server
*/
private async loadHttp(): Promise<void | IHttpServe> {
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);
}
}
Expand All @@ -57,11 +58,11 @@ class AppBootstrap {
* Load and run entrypoint
*/
private async loadEntrypoint(): Promise<void> {
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 = <T>() => new AppBootstrap<T>();
2 changes: 1 addition & 1 deletion packages/hono-openapi-adapter/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/hono-openapi-adapter/src/guards/guard.abstract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Context } from 'hono';
import { injectable } from 'inversify';

@injectable()
export abstract class GuardAbstract {
public run(_ctx: Context): void {}
Expand Down
2 changes: 1 addition & 1 deletion packages/hono-openapi-adapter/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { defineReflection } from './relfection.helper';
export { defineReflection } from './reflection.helper';
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/hono-openapi-adapter/src/start/hono-adapter.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,7 +12,7 @@ class HonoAdapter extends HttpAdapter {
bindToContainers(container);
}

public listen(config: FactoryConfig<string>): Serve {
public listen(config: FactoryConfig<string>): IHttpServe {
const app = IocContainer.container.get(Server);
const configService = IocContainer.container.get(ConfigService);
defineReflection(app);
Expand All @@ -33,6 +32,7 @@ class HonoAdapter extends HttpAdapter {
);
}
}

return {
port: config.port,
fetch: app.hono.fetch,
Expand Down

0 comments on commit b49b220

Please sign in to comment.