Skip to content

Commit

Permalink
chore: update all changes made from website production
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jul 9, 2024
1 parent 5a3e52a commit c7f9ec1
Show file tree
Hide file tree
Showing 10 changed files with 998 additions and 41 deletions.
27 changes: 23 additions & 4 deletions docs/docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,30 @@ on [Response filters](/docs/response-filter).

Object configure Multer. See more on [Upload file](/docs/upload-files).

## router

```typescript
@Configuration({
router: {
appendChildrenRoutesFirst: true
}
})
```

### router.appendChildrenRoutesFirst

- type: `boolean`

Append children routes before the controller routes itself. Defaults to `false`, but will be deprecated and set to `true` in next major version.

## jsonMapper

```typescript
@Configuration({
jsonMapper: {
additionalProperties: false,
disableUnsecureConstructor: true
disableUnsecureConstructor: true,
strictGroups: false
}
})
```
Expand All @@ -352,10 +369,12 @@ class MyModel {
}
```

::: tip Note
Recommended: Set this options to `true` in your new project.
### jsonMapper.strictGroups

Enable strict mode for `@Groups` decorator. By default, `false`. See [Groups](/docs/models#groups-strict-mode) for more information.

In v7 this option will be set to true by default.
::: warning
The `strictGroups` option is enabled by default in the next major version of Ts.ED.
:::

## Platform Options
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {$log} from "@tsed/common";
import {PlatformExpress} from "@tsed/platform-express";
import filedirname from "filedirname";
import {Server} from "./server";

// FIXME remove when esm is ready
const [, rootDir] = filedirname();
const rootDir = __dirname

// /!\ configuration file must be outside of your src directory
process.env["NODE_CONFIG_DIR"] = `${rootDir}/../config`;
Expand Down
10 changes: 10 additions & 0 deletions docs/docs/json-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ to a plain javascript object to your consumer.
jsonMapper: {
additionalProperties: false,
disableUnsecureConstructor: false,
strictGroups: false
}
})
```
Expand All @@ -49,6 +50,15 @@ class MyModel {
}
```

### jsonMapper.strictGroups

Enable strict mode for `@Groups` decorator. By default, `false`. See [Groups](/docs/models#groups-strict-mode) for more information.

::: warning
The `strictGroups` option is enabled by default in the next major version of Ts.ED.
:::


## Usage

JsonMapper works with a class and decorators. Use decorators on properties to describe a model and use this model as an input parameter or return value by your endpoint. Here is a model example:
Expand Down
187 changes: 153 additions & 34 deletions docs/docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,132 @@ json type or when you use a mixed TypeScript types.

:::

You can also use @@Any@@ decorator to allow all types:

<Tabs class="-code">
<Tab label="Model">

```typescript
import {Any} from "@tsed/schema";

export class Model {
@Any()
prop: any;
}
```

</Tab>
<Tab label="Json schema">

```json
{
"properties": {
"prop": {
"anyOf": [
{
"type": "null"
},
{
"type": "integer",
"multipleOf": 1
},
{
"type": "number"
},
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "array"
},
{
"type": "object"
}
]
}
},
"type": "object"
}
```

</Tab>
<Tab label="OS3">

```json
{
"properties": {
"prop": {
"nullable": true,
"anyOf": [
{
"type": "integer",
"multipleOf": 1
},
{
"type": "number"
},
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "array"
},
{
"type": "object"
}
]
}
},
"type": "object"
}
```

</Tab>
</Tabs>

Since v7.75.0, when you use @@Any@@ decorator combined with other decorators like @@MinLength@@, @@Minimum@@, etc. metadata will be automatically assigned to the right
type. For example, if you add a @@Minimum@@ decorator, it will be assigned to the number type.

```ts
import {Any} from "@tsed/schema";

class Model {
@Any(String, Number)
@Minimum(0)
@MaxLength(100)
prop: string | number;
}
```

Produce a json-schema as follows:

```json
{
"properties": {
"prop": {
"allOf": [
{
"type": "string",
"maxLength": 100
},
{
"type": "number",
"minimum": 0
}
]
}
},
"type": "object"
}
```


## Nullable

The @@Nullable@@ decorator is used allow a null value on a field while preserving the original Typescript type.
Expand Down Expand Up @@ -167,55 +293,50 @@ class NullableModel {
`returnsCoercedValue` will become true by default in the next major version of Ts.ED.
:::

## Any
## Nullable and mixed types <Badge text="7.75.0+"/>

The @@Any@@ decorator is used to allow any types:
The @@Nullable@@ decorator can be used with Tuple types:

::: code-group

```typescript [Model]
import {Any} from "@tsed/schema";
```ts
import {Nullable} from "@tsed/schema";

export class Model {
@Any()
prop: any;
class Model {
@Nullable(String, Number)
prop: string | number | null;
}
```

```json [JSON Schema]
{
"properties": {
"prop": {
"type": ["integer", "number", "string", "boolean", "array", "object", "null"]
}
},
"type": "object"
Since v7.75.0, when you use @@Nullable@@ decorator combined with other decorators like @@MinLength@@, @@Minimum@@, etc. metadata will be automatically assigned to the right
type. For example, if you add a @@Minimum@@ decorator, it will be assigned to the number type.

```ts
import {Nullable} from "@tsed/schema";

class Model {
@Nullable(String, Number)
@Minimum(0)
@MaxLength(100)
prop: string | number | null;
}
```

```json [OS3]
Produce a json-schema as follows:

```json
{
"properties": {
"prop": {
"nullable": true,
"oneOf": [
{
"type": "integer"
},
{
"type": "number"
},
"anyOf": [
{
"type": "string"
},
{
"type": "boolean"
"type": "null"
},
{
"type": "array"
"type": "string",
"maxLength": 100
},
{
"type": "object"
"type": "number",
"minimum": 0
}
]
}
Expand All @@ -224,8 +345,6 @@ export class Model {
}
```

:::

## Regular expressions

The @@Pattern@@ decorator is used to restrict a string to a particular regular expression. The regular expression syntax
Expand Down
40 changes: 40 additions & 0 deletions docs/docs/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,46 @@ An injection token may be either a string, a symbol, a class constructor.

> Just don't forget to import your provider in your project !
## Import a provider from configuration <Badge text="7.74.0+" />

Sometimes you need to import a provider depending on the environment or depending on a runtime context.

This is possible using the DI configuration `imports` option that let you fine-tune the provider registration.

Here is an example of how to import a provider from a configuration:

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

const TimeslotsRepository = Symbol.for("TimeslotsRepository");

interface TimeslotsRepository {
findTimeslots(): Promise<any[]>;
}

class DevTimeslotsRepository implements TimeslotsRepository {
findTimeslots(): Promise<any[]> {
return ["hello dev"];
}
}

class ProdTimeslotsRepository implements TimeslotsRepository {
findTimeslots(): Promise<any[]> {
return ["hello prod"];
}
}

@Configuration({
imports: [
{
token: "TimeslotsRepository",
useClass: process.env.NODE_ENV === "production" ? ProdTimeslotsRepository : DevTimeslotsRepository
}
]
})
export class Server {}
```

## Lazy load provider <Badge text="6.81.0+"/>

By default, modules are eagerly loaded, which means that as soon as the application loads, so do all the modules,
Expand Down
Binary file added docs/public/serverless.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/public/serverless.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c7f9ec1

Please sign in to comment.