Skip to content

Commit

Permalink
Merge pull request #2860 from tsedio/feat-move-testing
Browse files Browse the repository at this point in the history
Feat move testing
  • Loading branch information
Romakita authored Oct 12, 2024
2 parents e9079e8 + 4fcf708 commit e62bf72
Show file tree
Hide file tree
Showing 441 changed files with 926 additions and 712 deletions.
2 changes: 1 addition & 1 deletion PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Example to use your feature and to improve the documentation after merging your PR:
```typescript
import {} from "@tsed/common";
import {} from "@tsed/platform-http";
```
-->
Expand Down
55 changes: 40 additions & 15 deletions docs/docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ By using the @@UseCache@@ on endpoint methods or on service methods, you'll be a
the Ts.ED server
or the result returned by a Service.

## Installation

```shell
npm install @tsed/platform-cache
```

## Configuration

Cache-manager module is already installed with the `@tsed/common` package (since v6.30.0). You just have
to configure cache options and use the decorator to enable cache.
You just to configure cache options and use the decorator to enable cache.

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
cache: {
Expand Down Expand Up @@ -58,7 +63,7 @@ export class Server {}
### Example with mongoose

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";
import mongoose from "mongoose";

const mongooseStore = require("cache-manager-mongoose");
Expand Down Expand Up @@ -120,7 +125,7 @@ registerProvider({
Then:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";
import redisStore from "cache-manager-ioredis";

@Configuration({
Expand Down Expand Up @@ -198,7 +203,10 @@ await this.cache.reset();
To enable cache on endpoint, use @@UseCache@@ decorator on a method as follows:

```typescript
import {Controller, UseCache, Get, PathParams} from "@tsed/common";
import {UseCache} from "@tsed/platform-cache";
import {Controller} from "@tsed/di";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";

@Controller("/my-path")
export class MyController {
Expand Down Expand Up @@ -282,7 +290,7 @@ There are two ways to do that. The first one is to configure it globally on the

```typescript
import {Configuration} from "@tsed/di";
import {PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";

@Configuration({
cache: {
Expand All @@ -297,7 +305,10 @@ import {PlatformContext} from "@tsed/common";
The second way is to use the `key` option with @@UseCache@@ decorator:

```typescript
import {Controller, UseCache, Get, PathParams, PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Controller("/my-path")
export class MyController {
Expand All @@ -320,7 +331,10 @@ export class MyController {
TTL can be defined per endpoint with @@UseCache@@:

```typescript
import {Controller, UseCache, Get, PathParams, PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Controller("/my-path")
export class MyController {
Expand All @@ -338,7 +352,10 @@ Sometimes, you don't want to store in cache a value because isn't consistant to
For example, you can avoid caching data when the result is nullish:

```typescript
import {Controller, UseCache, Get, PathParams, PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Controller("/my-path")
export class MyController {
Expand All @@ -355,7 +372,10 @@ In this case, the UseCache interceptor will ignore result that `undefined` or `n
You can also provide a custom function to ignore result:

```typescript
import {Controller, UseCache, Get, PathParams, PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Controller("/my-path")
export class MyController {
Expand Down Expand Up @@ -389,7 +409,10 @@ if (currentTTL < ttl - refreshThreshold) {
In the meantime, the system will return the old value until expiration.

```typescript
import {Controller, UseCache, PathParams, PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Injectable()
export class MyService {
Expand All @@ -412,9 +435,11 @@ the data has changed.
Here is short example:

```typescript
import {Injectable} from "@tsed/di";
import {Controller, Injectable} from "@tsed/di";
import {PlatformCache, UseCache} from "@tsed/platform-cache";
import {Controller, Get, PathParams, PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {PathParams} from "@tsed/platform-params";
import {Get} from "@tsed/schema";

@Injectable()
export class ProductsService {
Expand Down Expand Up @@ -452,7 +477,7 @@ If you have several cached method calls, then the refresh will also be done on a
Cache-manager provides a way to use multiple caches. To use it, remove `store` option and use `caches` instead:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
cache: {
Expand Down
10 changes: 5 additions & 5 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ interface KoaStaticsOptions {
It's possible to change the HTTP and HTTPS server address as follows:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
httpPort: "127.0.0.1:8081",
Expand All @@ -545,7 +545,7 @@ export class Server {}
Random port assignment can be enabled with the value `0`. The port assignment will be delegated to the OS.

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
httpPort: "127.0.0.1:0",
Expand All @@ -557,7 +557,7 @@ export class Server {}
Or:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
httpPort: 0,
Expand All @@ -569,7 +569,7 @@ export class Server {}
### Disable HTTP

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
httpPort: false
Expand All @@ -580,7 +580,7 @@ export class Server {}
### Disable HTTPS

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
httpsPort: false
Expand Down
11 changes: 7 additions & 4 deletions docs/docs/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ Finally, @@BodyParams@@ accepts to give a @@ParamOptions@@ object as parameter t
@@QueryParams@@ decorator accept a model to transform `Express.request.query` plain object to a Class.

```typescript
import {Controller, Get, QueryParams} from "@tsed/common";
import {Required, MinLength, Property} from "@tsed/schema";
import {QueryParams} from "@tsed/platform-params";
import {Controller} from "@tsed/di";
import {Get, Required, MinLength, Property} from "@tsed/schema";

class QueryParamsModel {
@Required()
Expand Down Expand Up @@ -253,7 +254,7 @@ The @@Integer@@ decorator is used to set integer type for integral numbers.
@Controller("/")
class ExampleCtrl {
@Get("/")
@Returns(200, Array).OfInteger()
@(Returns(200, Array).OfInteger())
async get(@BodyParams() @Integer() list: number[]) {
return list;
}
Expand Down Expand Up @@ -419,7 +420,9 @@ class MyCtrl {
Finally, it also possible to perform redirection programmatically:

```typescript
import {Controller, Get, ctx} from "@tsed/common";
import {Controller} from "@tsed/di";
import {Context} from "@tsed/platform-params";
import {Get} from "@tsed/schema";

@Controller("/")
class MyCtrl {
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ Create a new ResourceNotFoundFilter in the filters directories and copy/paste th
Then import the custom filter in your server:

```typescript
import {Inject} from "@tsed/di";
import {Configuration, PlatformApplication} from "@tsed/common";
import {Configuration, Inject} from "@tsed/di";
import {PlatformApplication} from "@tsed/platform-http";
import "./filters/ResourceNotFoundFilter"; // Importing filter with ES6 import is enough

@Configuration({
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This example shows you how you can add an already constructed service like a npm

```typescript
// MyFooFactory.ts
import {registerProvider} from "@tsed/common";
import {registerProvider} from "@tsed/di";

export interface MyFooFactory {
getFoo(): string;
Expand Down
19 changes: 9 additions & 10 deletions docs/docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ By convention
You can subscribe to a hook in your Server:

```typescript
import {BeforeInit, Configuration} from "@tsed/common";
import {BeforeInit, Configuration} from "@tsed/di";

@Configuration({})
class Server implements BeforeInit {
Expand All @@ -65,7 +65,7 @@ class Server implements BeforeInit {
You can subscribe to a hook in your @@Module@@ or @@Service@@:

```typescript
import {Module, OnInit} from "@tsed/common";
import {Module, OnInit} from "@tsed/di";

@Module()
export class MyModule implements OnInit {
Expand All @@ -82,19 +82,18 @@ Database connection can be performed with Asynchronous Provider. See [custom pro
Ts.ED provide a way to intercept the request and response event. You can listen these hooks by implementing a `$onRequest` and `$onResponse` methods
on an injectable service:


```typescript
import {Module} from "@tsed/di";
import {PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";

@Module()
class CustomContextModule {
$onRequest($ctx: PlatformContext) {
// do something
}
$onResponse($ctx: PlatformContext) {
// do something
}
$onRequest($ctx: PlatformContext) {
// do something
}
$onResponse($ctx: PlatformContext) {
// do something
}
}
```

Expand Down
19 changes: 11 additions & 8 deletions docs/docs/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ You add this code to switch the logger to Json layout in production mode:

```typescript
import {env} from "@tsed/core";
import {$log, Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";
import {$log} from "@tsed/logger";
import "@tsed/platform-express";

export const isProduction = process.env.NODE_ENV === Env.PROD;
Expand Down Expand Up @@ -111,7 +112,7 @@ It's more useful if you planed to parse the log with LogStash or any log tool pa
Logger can be injected in any injectable provider as follows:

```typescript
import {Logger} from "@tsed/common";
import {Logger} from "@tsed/logger";
import {Injectable, Inject} from "@tsed/di";

@Injectable()
Expand All @@ -134,8 +135,10 @@ Prefer the @@ContextLogger@@ usage if you want to attach your log the current re
For each Request, a logger will be attached to the @@PlatformContext@@ and can be used like here:

```typescript
import {Controller, Context, Get} from "@tsed/common";
import {Controller} from "@tsed/di";
import {Logger} from "@tsed/logger";
import {Context} from "@tsed/platform-params";
import {Get} from "@tsed/schema";
import {MyService} from "../services/MyService";

@Controller("/")
Expand All @@ -159,7 +162,7 @@ class MyController {
```

```typescript
import {PlatformContext} from "@tsed/common";
import {PlatformContext} from "@tsed/platform-http";
import {Injectable, Inject} from "@tsed/di";

@Injectable()
Expand Down Expand Up @@ -205,7 +208,7 @@ A call with one of these methods will generate a log according to the `logger.re
You can configure the displayed fields from the server configuration:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";

@Configuration({
logger: {
Expand All @@ -220,7 +223,8 @@ or you can override the middleware with @@OverrideProvider@@.
Example:

```ts
import {Context, OverrideProvider, PlatformLogMiddleware} from "@tsed/common";
import {Context, OverrideProvider} from "@tsed/di";
import {PlatformLogMiddleware} from "@tsed/platform-log-middleware";

@OverrideProvider(PlatformLogMiddleware)
export class CustomPlatformLogMiddleware extends PlatformLogMiddleware {
Expand Down Expand Up @@ -249,8 +253,7 @@ export class CustomPlatformLogMiddleware extends PlatformLogMiddleware {
Another example to redact some fields:

```typescript
import {Context} from "@tsed/common";
import {OverrideProvider} from "@tsed/di";
import {Context, OverrideProvider} from "@tsed/di";
import {PlatformLogMiddleware} from "@tsed/platform-log-middleware";

@OverrideProvider(PlatformLogMiddleware)
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ function (error, req, res, next){}
Ts.ED has the same mechanism with the @@Err@@ decorator. Use this decorator on a middleware to create a handler which will only be called when an error occurs on the decorated endpoint.

```typescript
import {Err, Middleware, Next} from "@tsed/common";
import {Err, Next} from "@tsed/platform-params";
import {Middleware} from "@tsed/platform-middlewares";

@Middleware()
export class MyMiddlewareError {
Expand Down Expand Up @@ -226,7 +227,7 @@ By default, the server imports automatically your middlewares that match with th
If not, just import your middleware into your server or edit the [componentScan configuration](/docs/configuration.md).

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";
import "./src/other/directory/CustomMiddleware";

@Configuration({
Expand Down
Loading

0 comments on commit e62bf72

Please sign in to comment.