Skip to content

Commit

Permalink
refactor: update dependency versions and improve code examples in doc…
Browse files Browse the repository at this point in the history
…umentation
  • Loading branch information
rsaz committed Nov 26, 2024
1 parent 70b3e93 commit bf6455a
Show file tree
Hide file tree
Showing 25 changed files with 4,421 additions and 2,139 deletions.
5,891 changes: 4,109 additions & 1,782 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"typecheck": "tsc"
},
"dependencies": {
"@docusaurus/core": "^3.5.2",
"@docusaurus/preset-classic": "^3.5.2",
"@docusaurus/remark-plugin-npm2yarn": "^3.5.2",
"@docusaurus/theme-mermaid": "^3.5.2",
"@docusaurus/core": "^3.6.3",
"@docusaurus/preset-classic": "^3.6.3",
"@docusaurus/remark-plugin-npm2yarn": "^3.6.3",
"@docusaurus/theme-mermaid": "^3.6.3",
"@mdx-js/react": "3.1.0",
"axios": "1.7.8",
"clsx": "2.1.1",
Expand All @@ -30,9 +30,9 @@
"react-dom": "18.3.1"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "^3.5.2",
"@docusaurus/tsconfig": "^3.5.2",
"@docusaurus/types": "^3.5.2",
"@docusaurus/module-type-aliases": "^3.6.3",
"@docusaurus/tsconfig": "^3.6.3",
"@docusaurus/types": "^3.6.3",
"@types/react": "18.3.3",
"typescript": "5.5.4"
},
Expand Down
12 changes: 11 additions & 1 deletion versioned_docs/version-3.0.0/cli/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,26 @@ The CLI provides the following commands:
| entity | g e | Generate an entity |
| provider | g p | Generate internal provider |
| provider | add | Add external provider to the project |
| provider | remove | remove external provider |
| provider | create | Create external provider |
| module | g mo | Generate a module |
| middleware | g mi | Generate a middleware |

## Usage examples

The CLI has the following syntax:
You can use the CLI either with:

- `expressots`
- `ex` as an alias.

Command example:

```bash
expressots <command> [options]

// Or

ex <command> [options]
```

## Help command
Expand Down
35 changes: 12 additions & 23 deletions versioned_docs/version-3.0.0/core/app-container.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
sidebar_position: 2
title: App container
title: Container
description: The AppContainer simplifies module registration.
---

# App container
# Container

ExpressoTS uses **[InversifyJS](https://inversify.io/)** for dependency injection, making it easy to manage object creation, resolution, and lifecycle.
The use of `appContainer` abstracts away the intricacies of InversifyJS direct usage, providing a straightforward path to integrating and managing modules within ExpressoTS applications.
ExpressoTS uses an IoC Container for dependency injection, making it easy to manage object creation, resolution, and lifecycle.

## Setup

Expand All @@ -32,24 +31,17 @@ interface ContainerOptions {

## Creating the container

```typescript title="app.container.ts"
export const appContainer: AppContainer = new AppContainer({
defaultScope: BindingScopeEnum.Singleton,
skipBaseClassChecks: true,
autoBindInjectable: false,
```typescript
private config: AppContainer = this.configContainer([AppModule], {
autoBindInjectable: true,
defaultScope: "Request",
skipBaseClassChecks: true,
});

const container = appContainer.create([
// Add your modules here
AppModule,
]);

export { container };
```

## Modules registration

The `appContainer` facilitates module registration via its `create([])` method, which accepts an array of modules.
The `Container` facilitates module registration via its `configContainer([])` method, which accepts an array of modules.

Here an example of PaymentModule creation:

Expand All @@ -58,10 +50,7 @@ export const PaymentModule: ContainerModule = CreateModule([CreatePaymentControl
```

```typescript title="Module registration"
// Create a new container
const appContainer: AppContainer = new AppContainer();

const container: Container = appContainer.create([UserModule, PaymentModule]);
private config: AppContainer = this.configContainer([AppModule, PaymentModule]);
```

:::tip
Expand All @@ -80,8 +69,8 @@ After creating the container, you have the following options available:

See below image for a visual representation of the container bindings.

```typescript title="app.container.ts"
appContainer.viewContainerBindings();
```typescript title="app.ts"
this.config.viewContainerBindings();
```

![Container Bindings View](./img/container-bindings.png)
Expand Down
111 changes: 75 additions & 36 deletions versioned_docs/version-3.0.0/core/app-provider.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
sidebar_position: 1
title: App provider
title: App
description: The App class is the core of an ExpressoTS application.
---

Expand All @@ -21,45 +21,45 @@ and configuring applications within ExpressoTS.
With full support for Express.js middleware, `AppExpress` enables developers to efficiently manage application setup, execution, and shutdown processes,
all while leveraging the familiar Express.js environment.

:::info
The framework currently supports Express as its primary adapter, with Fastify and other possible in house server under development.
:::

Here is the `App` class extending the `AppExpress` adapter:
Here is the `App` class extending the `AppExpress` adapter from the Opinionated template:

```typescript title="app.provider.ts"
```typescript title="app.ts"
export class App extends AppExpress {
private middleware: IMiddleware;
private provider: ProviderManager;
private config: AppContainer = this.configContainer([AppModule]);

protected globalConfiguration(): void | Promise<void> {
this.setGlobalRoutePrefix("/v1");

constructor() {
super();
this.middleware = container.get<IMiddleware>(Middleware);
this.provider = container.get(ProviderManager);
this.initEnvironment("development", {
env: {
development: ".env.development",
production: ".env.production",
},
});
}

protected configureServices(): void {
this.provider.register(Env);
this.Provider.register(Env);

this.middleware.addBodyParser();
this.middleware.setErrorHandler();
this.Middleware.addBodyParser();
this.Middleware.setErrorHandler({ showStackTrace: true });
}

protected postServerInitialization(): void {
protected async postServerInitialization(): Promise<void> {
if (this.isDevelopment()) {
this.provider.get(Env).checkAll();
this.Provider.get(Env).checkFile(".env.development");
}
}

protected serverShutdown(): void {}
}
```

- `this.provider`: an object containing a register method that allows developers to register internal
- `this.Provider`: an object containing a register method that allows developers to register internal
or external ExpressoTS-compatible providers. This method registers classes in the ExpressoTS container
to be part of the dependency injection system, enabling the use of different scopes such as Transient, Scoped, and Singleton.

- `this.middleware`: offers a set of out-of-the-box middlewares that can be used to configure your application.
- `this.Middleware`: offers a set of out-of-the-box middlewares that can be used to configure your application.
See the complete list in the **[Middleware section](../features/middleware.mdx)**

:::tip
Expand Down Expand Up @@ -92,6 +92,43 @@ protected serverShutdown(): void | Promise<void> { }
<img src={imgAppLifecycle} width="700px" height="700px" alt="Lifecycle events" />
</div>

## Method configContainer

The `configContainer` method is used to configure the application container with the modules that will be used in the application.

```typescript title="Example of configuring the application container."
private config: AppContainer = this.configContainer([AppModule]);
```

## Middleware property

The `Middleware` property is an instance of the `MiddlewareManager` class, which provides a set of methods to add middleware to the application.

```typescript title="Example of adding a middleware to the application."
this.Middleware.addBodyParser();
```

## Provider property

The `Provider` property is an instance of the `ProviderManager` class, which provides a set of methods to register and get providers in the application.

```typescript title="Example of registering a provider in the application."
this.Provider.register(Env);
```

## Method initEnvironment

The `initEnvironment` method is used to set the environment variables for the application.

```typescript title="Example of setting environment variables for the application."
this.initEnvironment("development", {
env: {
development: ".env.development",
production: ".env.production",
},
});
```

## Method isDevelopment

It is a helper method that returns a boolean value indicating whether the application is running in development mode.
Expand All @@ -107,31 +144,33 @@ If it is, the `Env` provider is used to check all environment variables.
```typescript title="Check all environment variables in development mode."
protected postServerInitialization(): void {
if (this.isDevelopment()) {
this.provider.get(Env).checkAll();
this.Provider.get(Env).checkFile(".env.development");
}
}
```

## Set global route prefix
## Method set global route prefix

You can set a global prefix for all routes in your application. This is useful when you want to version your APIs.

```typescript
@provide(App)
class App extends AppExpress {
private middleware: IMiddleware;
private provider: IProvider;

constructor() {
super();
this.middleware = container.get<IMiddleware>(Middleware);
this.provider = container.get<IProvider>(Provider);
}
this.setGlobalRoutePrefix("/v1");
```

protected configureServices(): void {
this.setGlobalRoutePrefix("/v1");
}
}
## Method get http server

The `getHttpServer` method is used to get the HTTP server instance.

```typescript title="Example of getting the HTTP server instance."
this.getHttpServer(): http.Server
```

## Method to set the engine

The `setEngine` method is used to set the engine for the application.

```typescript title="Example of setting the engine for the application."
this.setEngine(RenderEngine.Engine.EJS);
```

---
Expand Down
74 changes: 24 additions & 50 deletions versioned_docs/version-3.0.0/core/controller.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,43 +150,6 @@ class ProductController {}

The controller above will be scoped as `Singleton` and will be shared across all requests.

## Base controller

In ExpressoTS, we enhance controller classes with a `BaseController` class, which offers two methods—`callUseCase()`
and `callUseCaseAsync()`—to simplify the interaction between controllers and use cases.
These methods reinforce the principle of "one use case per controller" by focusing the controller's role
on invoking the business logic encapsulated within a use case.

:::info
`BaseController` class is only a syntatic sugar to help developers to call use cases in a more organized way.
:::

```typescript title="Method signature"
callUseCase(usecase: unknown, res: Response, successStatusCode: number);

```

- `useCase`: The use case that will be called. This use case is injected in the controller constructor.
- `res`: The response object from the express request.
- `successStatusCode`: The status code that will be sent to the client if the use case is executed successfully.
Please see the **[Http status code](../features/status-code.mdx)** type for more information.

```typescript title="Default response format"
return res.status(successStatusCode).json(dataReturn);
```

```typescript title="Complete example"
@controller("/")
class AppController extends BaseController {
constructor(private appUseCase: AppUseCase) {}

@Get("")
execute(@response() res: Response) {
return this.callUseCase(this.appUseCase.execute(), res, StatusCode.OK);
}
}
```

## Request object

In order to access the request object, you can use the `@request()` decorator.
Expand Down Expand Up @@ -256,20 +219,13 @@ For a full list of available decorators, please refer to the **[Decorators](../f

## Status code

By default, all HTTP response status codes are set to 200, except for the Post method, which defaults to 201.
Status Code is an enum that provides a list of HTTP status codes that can be used in the application.

```typescript title="Using the @Http() decorator"
import { controller, Get, Http } from "@expressots/adapter-express";
The default status code based on the HTTP method:

@controller("/")
export class AppController {
@Get("")
@Http(204)
execute() {
return "App is running!";
}
}
```
- **GET**: 200
- **POST**: 201
- **PUT - PATCH - DELETE**: 204

:::tip You can import <span class="span-table">StatusCode</span> from `@expressots/core` to use the default status codes.
:::
Expand All @@ -282,6 +238,24 @@ import { StatusCode } from "@expressots/core";
@Http(StatusCode.NoContent)
```

## HTTP decorator

The `@Http()` decorator is used to define the status code of the response. This decorator is useful when you need to return a specific status code for a request.

```typescript title="Using the @Http() decorator"
import { controller, Get, Http } from "@expressots/adapter-express";

@controller("/")
export class AppController {
@Get("")
@Http(200)
execute() {
return "App is running!";
}
}
```


## Payloads

In a request, payloads refer to the data sent between the client and server, typically in the body of the request. This data is crucial for operations like creating, updating, or querying resources.
Expand Down Expand Up @@ -329,7 +303,7 @@ export class UserController {
## Error handling

For error handling, ExpressoTS provides a built-in error handler middleware that can be used to catch errors thrown in the application.
This middleware is automatically added to the application when it is initialized in the `app.provider.ts` file.
This middleware is automatically added to the application when it is initialized in the `app.ts` file.

Read more about error handling in ExpressoTS in the **[Error Handling](../features/error-handling.mdx)** section.

Expand Down
Loading

0 comments on commit bf6455a

Please sign in to comment.