Skip to content

Commit

Permalink
docs: Add migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Oct 2, 2020
1 parent f1300d7 commit e256152
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 11 deletions.
13 changes: 6 additions & 7 deletions docs/docs/platform-api.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Platform API

Ts.ED use now, the Platform API to create an application. Platform API give an abstraction layer between your code written with Ts.ED and the [Express.js](https://expressjs.com/fr/) code.
It means, a large part of your code isn't coupled with Express itself and can be used with another Platform like [Koa.js](https://koajs.com/) in future (Ts.ED v6).
It means, a large part of your code isn't coupled with Express.js itself and can be used with another Platform like [Koa.js](https://koajs.com/).

There are some changes between ServerLoader API (v4/v5) and Platform API (v5.56.0+/v6), to get the original Express Application, Request or Response.
This page will describe how you can get these instance with the new API.
Expand All @@ -19,7 +19,7 @@ This page will describe how you can get these instance with the new API.

## Create application

The way to create Ts.ED application, add [middlewares](/docs/middlewares.html), configure Express.Application are impacted by the new Platform API.
The way to create Ts.ED application, add [middlewares](/docs/middlewares.html), configure Express or Koa are impacted by the new Platform API.

If you use @@ServerLoader@@, you'll probably know this example to create a Ts.ED application:

Expand Down Expand Up @@ -71,7 +71,10 @@ export const rootDir = __dirname;

@Configuration({
rootDir,
viewsDir: `${rootDir}/views`
views: {
root: `${rootDir}/views`,
viewEngine: 'ejs'
}
})
export class Server {
@Constant("viewsDir")
Expand All @@ -90,10 +93,6 @@ export class Server {
.use(bodyParser.urlencoded({
extended: true
}));

// configure express app
this.app.raw.set("views", this.viewsDir);
this.app.raw.engine("ejs", ejs);
}
}
```
Expand Down
137 changes: 133 additions & 4 deletions docs/getting-started/migration-from-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,140 @@ meta:
content: migration getting started ts.ed express typescript node.js javascript decorators mvc class models
---
# Migrate from v5

## What's news ?

### Platform API
V6 marks a major evolution of the Ts.ED framework.
A lot of work has been done on the internal Ts.ED code since v5 in order to prepare the arrival of this new version.
This work was mainly oriented on the creation of an abstraction layer between the Ts.ED framework and Express.js.

The v5 introduced the [Platform API](/docs/platform-api.md)
and the v6 is the confirmation of this API which allows supporting [Express.js](https://expressjs.com/) and [Koa.js](https://koajs.com/) and many more in the future.

We are glad this work resulted in the creation of the [@tsed/platform-express](/https://www.npmjs.com/package/@tsed/platform-express) and
[@tsed/platform-koa](/https://www.npmjs.com/package/@tsed/platform-koa).

!!!! ADD new platforms features here (views, statics, multer).

### Schema and OpenSpec

This release finally adds support for [OpenSpec 3](https://swagger.io/docs/specification/about/) while supporting
the previous version [Swagger2](https://swagger.io/docs/specification/2-0/basic-structure/).
The management of OpenSpec is at the heart of the framework as is [JsonSchema](https://json-schema.org/).

All decorators related to the declaration of schema, routes and endpoints are now integrated in a single module [`@tsed/schema`](https://www.npmjs.com/package/@tsed/schema).
This module has been designed to be used independently of the Ts.ED framework.
You can therefore use it for your projects without installing the whole framework!

New decorators have been added elsewhere to better support JsonSchema and allow the following more complex use cases:

- Managing models using Typescript generics,
- Management response models by content-type and status code (OAS3).

### JsonMapper

In the same idea, the convertersService code was taken out of the [`@tsed/common`](https://www.npmjs.com/package/@tsed/common) module
to the new [`@tsed/json-mapper`](https://www.npmjs.com/package/@tsed/json-mapper) module.
It's based on the [`@tsed/schema`](https://www.npmjs.com/package/@tsed/schema) module to perform the mapping of your classes
to a Plain Object JavaScript object and vice versa.

You can therefore use it for your projects without installing the whole framework!

## Breaking changes
### Features

- ServerLoader API have been remove in favor of Platform API. See [Platform API](/docs/platform-api.md).
- Filter feature have been removed in favor of [Pipes](/docs/pipes.md).
- GlobalErrorHandlerMiddleware have been.removed in favor of [Exception Filters](/docs/exceptions.md#exception-filter).

### Modules

The following modules have been removed:

- `@tsed/testing`: Use @@PlatformTest@@ from `@tsed/common`.
- `@tsed/multipartfiles`: Use @@MultipartFile@@ from `@tsed/common`.
- `ts-express-decorators`: Use `@tsed/common`.

### Decorators

The following decorators have been removed:

### @tsed/di

- `@OverrideService`: Use @@OverrideProvider@@

### @tsed/common

- `@ServerSettings`: Use @@Configuration@@.
- `@ExpressApplication`: Use @@PlatformApplication@@ instead of.
- `@ExpressRouter`: Use @@PlatformRouter@@ instead of.
- `@ResponseView`: Use @@View@@ decorator instead of.
- `@Converter`: @Converter is replaced by @@JsonMapper@@ from `@tsed/json-mapper`. See [Converter to JsonMapper](/getting-started/migration-from-v5.md#converter-to-jsonmapper) section for more details.
- `@Filter`: Filter feature have been removed.
- `@MiddlewareError`: Use @@Middleware@@.

#### @tsed/typeorm

- `@EntityRepository`: Use EntityRepository from `typeorm` directly.

#### @tsed/swagger

## GlobalErrorHandler middleware to Exception filter
Import the following decorators from `@tsed/schema`:

[Exception filter]() is the
<ApiList query="['Consumes', 'Deprecated', 'Description', 'Example', 'Name', 'Produces', 'Returns','Security', 'Summary', 'Title'].includes(symbolName)" />

- `@BaseParameter` have been removed.
- `@Operation` have been removed.
- `@Responses` have been remove.
- `@ReturnsArray` have been removed. Use @@Returns@@ from `@tsed/schema`.

## Migration guide
### ServerLoader to Platform API

All change related to [Platform API](/docs/platform-api.md) and how to migrate the Server on this new API, is described on a dedicated page.
We encourage you to browse the entire page to migrate from v4/v5 to v6.

See [Platform API documentation](/docs/platform-api.md).

### Inject service in the Server

With @@ServerLoader@@, inject a provider can be done as follows:

```typescript
import {ServerLoader, ServerSettings} from "@tsed/common";
import {MyService} from "./services/MyService";

@ServerLoader({
})
export class Server extends ServerLoader {
$beforeRoutesInit() {
const myService = this.injector.get<MyService>(MyService);

myService.getSomething();
}
}
```

Now Platform API, the Server class is considered as a @@Provider@@.
It means, you can use decorator like @@Constant@@ and @@Inject@@ to get any configuration, provider or service from the DI registry.

```typescript
import {Configuration} from "@tsed/common";
import {Inject} from "@tsed/di";
import {MyService} from "./services/MyService";

@Configuration({})
export class Server {
@Inject()
myService: MyService;

$beforeRoutesInit() {
this.myService.getSomething();
}
}
```

### GlobalErrorHandler to Exception Filter

To facilitate your migration, remove the line where you add you custom middleware in the server:

Expand All @@ -43,4 +168,8 @@ Now you are able to create your own exception filter. Start with the HttpExcepti

<<< @/docs/docs/snippets/exceptions/http-exception-filter.ts

Then try with another error type and finally, remove your custom middleware.
Then try with another error type and finally, remove your custom middleware.

### Converter to JsonMapper

To be redacted

0 comments on commit e256152

Please sign in to comment.