Skip to content

Commit

Permalink
refactor: update doc with new features on v2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Mar 31, 2024
1 parent f92ed7a commit b6a3bac
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 185 deletions.
51 changes: 24 additions & 27 deletions docs/cli/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,25 @@ We provide two different structures to scaffold the resources:

Current available resources:

| Long form | short |
| ---------- | ----- |
| usecase | u |
| controller | c |
| dto | d |
| provider | p |
| service | s |
| entity | e |
| middleware | m |

## Usage Example

| Command | Expected result |
| -------------------------- | ---------------------------------------------------------------------------------------- |
| expressots g u user/find | Use case to be created in the folder `useCases` with this folder structure: user/find |
| expressots g c user/find | Controller to be created in the folder `useCases` inside of user/find |
| expressots g d user/find | DTO to be created in the folder `useCases` inside of user/find |
| expressots g p email/email | Provider to be created in the folder `providers` inside of user/find |
| expressots g s user/find | Service creates usecase, controller and DTO and add them in the desired folder user/find |
| expressots g e user | Entity to be created in the folder `entities` with this folder structure: user |
| expressots g m auth | Middleware to be created in the folder `middlewares` with this folder structure: auth |
| Long form | short | Command | Expected result |
| ---------- | ----- | -------------------------- | ---------------------------------------------------------------------------------------- |
| usecase | u | expressots g u user/find | Use case to be created in the folder `useCases` with this folder structure: user/find |
| controller | c | expressots g c user/find | Controller to be created in the folder `useCases` inside of user/find |
| dto | d | expressots g d user/find | DTO to be created in the folder `useCases` inside of user/find |
| provider | p | expressots g p email/email | Provider to be created in the folder `providers` inside of user/find |
| service | s | expressots g s user/find | Service creates usecase, controller and DTO and add them in the desired folder user/find |
| entity | e | expressots g e user | Entity to be created in the folder `entities` with this folder structure: user |
| middleware | mi | expressots g mi auth | Middleware to be created in the folder `middlewares` with this folder structure: auth |
| module | mo | expressots g mo user | Module to be created in the folder where `controllers` and `usecases` are located |

All resources can be created using the structure `folder/subfolder/resource`.

For services, you can take advantage of creating the use case, controller and DTO at once using the structure `entity_action` or `entity-action`. Example: `expressots g s user-create`.
## Scaffolding using hyphenated names

:::caution
The `expressots.config.ts` configuration file, located in the project root folder, determines where all resources will be created.
:::
For services, you can take advantage of creating the use case, controller and DTO at once using the structure `entity_action` or `entity-action`. Example: `expressots g s user-create`.

:::info
All usecases, controllers and DTOs are being inside of the `useCases` folder. However, this may change in the future.
The `expressots.config.ts` configuration file, located in the project root folder, determines where all resources will be created.
:::

## ExpressoTS Config File
Expand All @@ -91,7 +78,16 @@ import { ExpressoConfig, Pattern } from "@expressots/core";
const config: ExpressoConfig = {
sourceRoot: "src",
scaffoldPattern: Pattern.KEBAB_CASE,
opinionated: false,
opinionated: true,
scaffoldSchematics: {
entity: "entity",
provider: "provider",
module: "module",
controller: "controller",
dto: "dto",
middleware: "middleware",
usecase: "useCases",
},
};

export default config;
Expand All @@ -100,6 +96,7 @@ export default config;
- **sourceRoot**: the root folder that will be used by the CLI to create the resources. Default: `src`
- **scaffoldPattern**: the pattern that will be used to create the resources. Default: `Pattern.KEBAB_CASE`. Example: `user-create`
- **opinionated**: if true, the CLI will create the resources using the opinionated folder structure
- **scaffoldSchematics**: the folder convention names for each resource. Default: `useCases`, `entities`, `providers`, `middlewares`, `controllers`, `dtos`

## Recommendations

Expand Down
32 changes: 21 additions & 11 deletions docs/cli/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ First install the CLI globally using the command below:
npm install -g @expressots/cli
```

## Basic Usage
## Basic usage

```bash
expressots <command> [options]
Expand All @@ -37,9 +37,19 @@ expressots --help
Providers information about your Operational System, Project and CLI version:

```bash
expressots info
expressots info or i
```

### Resources list

List all available resources to scaffold:

```bash
expressots resources or r
```

![Resources](../overview/img/scaffold-resources.png)

## Create a Project

There are two options to create a new project, interactively or silently (passing the options as arguments).
Expand All @@ -61,9 +71,9 @@ expressots new <project-name>
expressots new <project-name> -p <package-manager> -t <template> -d <directory>
```

- **package-manager**: `npm`, `yarn` or `pnpm`
- **template**: `opinionated` or `non-opinionated`
- **directory**: `./my-project`
- **package-manager**: `npm`, `yarn` or `pnpm`
- **template**: `opinionated` or `non-opinionated`
- **directory**: `./my-project`

The flag `-d` is optional and is used to specify the directory where the project will be created. If not specified, the project will be created in the current directory.

Expand All @@ -73,9 +83,9 @@ The flag `-d` is optional and is used to specify the directory where the project

ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please consider:

- Become a **[sponsor on GitHub](https://github.com/sponsors/expressots)**
- Follow the **[organization](https://github.com/expressots)** on GitHub and Star ⭐ the project
- Subscribe to the Twitch channel: **[Richard Zampieri](https://www.twitch.tv/richardzampieri)**
- Join our **[Discord](https://discord.com/invite/PyPJfGK)**
- Contribute submitting **[issues and pull requests](https://github.com/expressots/expressots/issues/new/choose)**
- Share the project with your friends and colleagues
- Become a **[sponsor on GitHub](https://github.com/sponsors/expressots)**
- Follow the **[organization](https://github.com/expressots)** on GitHub and Star ⭐ the project
- Subscribe to the Twitch channel: **[Richard Zampieri](https://www.twitch.tv/richardzampieri)**
- Join our **[Discord](https://discord.com/invite/PyPJfGK)**
- Contribute submitting **[issues and pull requests](https://github.com/expressots/expressots/issues/new/choose)**
- Share the project with your friends and colleagues
42 changes: 22 additions & 20 deletions docs/codebyexample/app-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,48 @@ The application container is a dependency injection container. See [Application
const appContainer = new AppContainer();

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

## Creating AppContainer

The `AppContainer` class takes one optional parameter:

- `defaultScope` is the default binding scope. Use `BindingScopeEnum` to set the default scope (Request, Singleton, Transient).
- `skipBaseClassChecks` is a boolean value that indicates if the container should skip the base class checks. This is useful when you are using abstract classes as binding identifiers or you are extending classes from third-party libraries. Set this value to `true` to skip the base class checks.
- `defaultScope` is the default binding scope. Use `BindingScopeEnum` to set the default scope (Request, Singleton, Transient).
- `skipBaseClassChecks` is a boolean value that indicates if the container should skip the base class checks. This is useful when you are using abstract classes as binding identifiers or you are extending classes from third-party libraries. Set this value to `true` to skip the base class checks.
- `autoBindInjectable` is a boolean value that indicates if the container should automatically bind injectable classes. Set this value to `true` to automatically bind injectable classes.

:::info
The default scope is `BindingScopeEnum.Request` and `skipBaseClassChecks` is `false`.
:::

```typescript
const appContainer = new AppContainer({
defaultScope: BindingScopeEnum.Request,
skipBaseClassChecks: true,
defaultScope: BindingScopeEnum.Request,
skipBaseClassChecks: true,
autoBindInjectable: false,
});
```

The `AppContainer` instance returns an instance of `AppContainer` that gives you access to the following methods:

- `create()` creates the application container.
- `Container` is the dependency injection container instance.
- `getBindingDictionary()` returns the binding dictionary map of classes injected in the dependency injection system.
- `getContainerOptions()` returns the container options.
- `create()` creates the application container.
- `Container` is the dependency injection container instance.
- `getBindingDictionary()` returns the binding dictionary map of classes injected in the dependency injection system.
- `getContainerOptions()` returns the container options.

### Method Create

The `create` method takes one mandatory parameter (array of modules);

```typescript
export const container: Container = appContainer.create([
// Add your modules here
AppModule,
UserModule,
ProductModule,
// Add your modules here
AppModule,
UserModule,
ProductModule,
]);
```

Expand All @@ -59,9 +61,9 @@ export const container: Container = appContainer.create([

ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please consider:

- Become a **[sponsor on GitHub](https://github.com/sponsors/expressots)**
- Follow the **[organization](https://github.com/expressots)** on GitHub and Star ⭐ the project
- Subscribe to the Twitch channel: **[Richard Zampieri](https://www.twitch.tv/richardzampieri)**
- Join our **[Discord](https://discord.com/invite/PyPJfGK)**
- Contribute submitting **[issues and pull requests](https://github.com/expressots/expressots/issues/new/choose)**
- Share the project with your friends and colleagues
- Become a **[sponsor on GitHub](https://github.com/sponsors/expressots)**
- Follow the **[organization](https://github.com/expressots)** on GitHub and Star ⭐ the project
- Subscribe to the Twitch channel: **[Richard Zampieri](https://www.twitch.tv/richardzampieri)**
- Join our **[Discord](https://discord.com/invite/PyPJfGK)**
- Contribute submitting **[issues and pull requests](https://github.com/expressots/expressots/issues/new/choose)**
- Share the project with your friends and colleagues
Binary file added docs/overview/img/middleware-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/overview/img/plugin-pattern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/overview/img/scaffold-resources.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 45 additions & 38 deletions docs/overview/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ sidebar_position: 5

# Middleware

Middleware is a function that has access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.
Middleware functions are pivotal in the request-response cycle of an ExpressoTS application, providing the capability to execute code, modify request and response objects, end the request-response cycle, or call the next middleware in the stack. It's essential to call next() to avoid request timeouts unless your middleware concludes the cycle.

Middleware can:

- Run code.
- Modify request and response objects.
- Conclude the request-response process.
- Proceed to the next middleware in the sequence.
- Ensure to invoke next() if the middleware does not close the request-response loop to avoid stalling the request.
ExpressoTS seamlessly integrates with Express middleware, enabling the use of its extensive ecosystem to enhance your application.

:::info
ExpressoTS fully supports [Express](https://expressjs.com/) middleware.
ExpressoTS fully supports [Express](https://expressjs.com/en/resources/middleware.html) middleware.
:::

## Adding Middleware
## List of middlewares available

Expressjs middlewares supported by ExpressoTS:

| Middleware Name | Description |
| ---------------- | ------------------------------------------------- |
| addBodyParser | Add body parser middleware to the application. |
| addCompression | Add compression middleware to the application. |
| addCors | Add cors middleware to the application. |
| addHelmet | Add helmet middleware to the application. |
| addCookieParser | Add cookie parser middleware to the application. |
| addCookieSession | Add cookie session middleware to the application. |
| addSession | Add session middleware to the application. |
| addServerStatic | Add static middleware to the application. |
| addRateLimit | Add rate limit middleware to the application. |
| addMorgan | Add morgan middleware to the application. |
| addPassport | Add passport middleware to the application. |
| setMulter | Add multer middleware to the application. |
| addServeFavicon | Add serve favicon middleware to the application. |
| setErrorHandler | Add error handler middleware to the application. |

## Adding middleware

ExpressoTS application supports adding middleware globally to the application as well as per route. It offers all the [middleware supported by Express Team](https://expressjs.com/en/resources/middleware.html) out-of-the-box through the use of `this.middleware` property.

Expand All @@ -44,28 +59,7 @@ export class App extends AppExpress {
If you add a middleware that is not installed as dependency, the application will throw a warning message and continue to run.
:::

### List of Middlewares Available

Expressjs middlewares supported by ExpressoTS:

| Middleware Name | Description |
| ---------------- | ------------------------------------------------- |
| addBodyParser | Add body parser middleware to the application. |
| addCompression | Add compression middleware to the application. |
| addCors | Add cors middleware to the application. |
| addHelmet | Add helmet middleware to the application. |
| addCookieParser | Add cookie parser middleware to the application. |
| addCookieSession | Add cookie session middleware to the application. |
| addSession | Add session middleware to the application. |
| addServerStatic | Add static middleware to the application. |
| addRateLimit | Add rate limit middleware to the application. |
| addMorgan | Add morgan middleware to the application. |
| addPassport | Add passport middleware to the application. |
| setMulter | Add multer middleware to the application. |
| addServeFavicon | Add serve favicon middleware to the application. |
| setErrorHandler | Add error handler middleware to the application. |

### Global Middleware
## Adding global middleware

Middlewares can be added globally using the `App` class through the `this.middleware` property, using the out-of-the-box middleware list provided by Express Team:

Expand All @@ -77,6 +71,8 @@ protected configureServices(): void {
}
```

## Using `addMiddleware` method

For any other middleware, or a custom middleware, you can add it using the `this.middleware.addMiddleware()` method. Using the `addMiddleware` method, you can add any middleware from NPM Registry, custom Expressjs middleware or a custom ExpressoTS middleware.

NPM Registry middleware:
Expand All @@ -85,7 +81,7 @@ NPM Registry middleware:
this.middleware.addMiddleware(cors());
```

Custom Expressjs middleware:
### Creating expressjs middleware

```typescript
function myMiddleware(req: Request, res: Response, next: NextFunction) {
Expand All @@ -98,6 +94,8 @@ function myMiddleware(req: Request, res: Response, next: NextFunction) {
this.middleware.addMiddleware(myMiddleware);
```

### Creating expressots middleware

Custom ExpressoTS middleware:

```typescript
Expand All @@ -113,7 +111,7 @@ class CustomMiddleware extends ExpressoMiddleware {
this.middleware.addMiddleware(new CustomMiddleware());
```

### Route Middleware
## Route Middleware

Middlewares can be added per route in the `App` class through the `this.middleware.addMiddleware()` method. You can add any middleware from NPM Registry, custom Expressjs middleware or a custom ExpressoTS middleware.

Expand All @@ -125,6 +123,8 @@ this.middleware.addMiddleware({ path: "/api", middlewares: [] });
Each route can have multiple middlewares.
:::

### Adding middleware to a specific route

Or you add a middleware to a specific route in the `Controller` class through the `@controller()` and/Or `http Method` decorators.

```typescript
Expand All @@ -137,6 +137,8 @@ export class AppController {
}
```

### Adding middleware to all routes in a controller

If you want to apply a middleware to all routes under a specific controller, you can add it to the `@controller()` decorator.

```typescript
Expand All @@ -154,7 +156,7 @@ export class AppController {
}
```

## Creating Custom ExpressoTS Middleware
## Example of custom expressoTS middleware

To create a custom middleware, you need to extend the `ExpressoMiddleware` class and implement the `use` method.

Expand All @@ -180,14 +182,19 @@ class CustomMiddleware extends ExpressoMiddleware {

Custom middleware allows you to pass parameters to the constructor and use them as options in the `use` method of your middleware.

:::tip
Use ExpressoTS CLI to scaffold a custom middleware.
## View all middlewares pipeline

You can view all the middlewares added to the application using the `this.middleware.viewMiddlewarePipeline()` method.

![View Middleware Pipeline](./img/middleware-view.png)

:::tip Use ExpressoTS CLI to scaffold a custom middleware.
:::

CLI command to scaffold a custom middleware:

```bash
expressots g m <<middleware-name>>
expressots g mi <<middleware-name>>
```
---
Expand Down
Loading

0 comments on commit b6a3bac

Please sign in to comment.