@discord-nestjs/common
Version:
170 lines (127 loc) โข 3.79 kB
Markdown
# Common module
## ๐งพ Description
This package contains some ready-made templates
## ๐จ๐ปโ๐ป Installation <a name="Installation"></a>
```bash
$ npm install -nestjs/common
```
Or via yarn
```bash
$ yarn add -nestjs/common
```
## ๐ Overview <a name="Overview"></a>
### โน๏ธ Pipes template
`SlashCommandPipe` fills in the fields `DTO` from `CommandInteraction`.
For prefix command use `PrefixCommandPipe`.
#### ๐ก Example
```typescript
/* registration-email.dto.ts */
import {City} from '../definitions/city';
import {Param, ParamType, Choice} from '@discord-nestjs/core';
export class RegistrationEmailDto {
email: string;
name: string;
age: number;
city: City;
}
```
```typescript
/* play.command.ts */
import { RegistrationDto } from './registration.dto';
import { Command, IA } from '@discord-nestjs/core';
import { SlashCommandPipe } from '@discord-nestjs/common';
import { CommandInteraction } from 'discord.js';
export class BaseInfoCommand
{
handler( dto: RegistrationEmailDto): string {
// dto instance must have the following fields: email, name, age, city
}
}
```
`ValidationPipe` validate the resulting `DTO` based on class-validator. If the `DTO` is invalid then an exception will be thrown,
which can be caught by the filter from the package `-nestjs/core`.
Also, suitable for prefix commands.
> for validation, you need to install package `class-validator`
#### ๐ก Example
```typescript
/* registration-email.dto.ts */
import {Param, ParamType} from '@discord-nestjs/core';
import {IsEmail, Length, Max, Min} from 'class-validator';
export class RegistrationEmailDto {
email: string;
name: string;
age: number;
}
```
```typescript
/* play.command.ts */
import {RegistrationDto} from './registration.dto';
import {Command, IA} from '@discord-nestjs/core';
import {SlashCommandPipe, ValidationPipe, UsePipes} from '@discord-nestjs/common';
import {CommandInteraction} from 'discord.js';
export class BaseInfoCommand {
handler( dto: RegistrationEmailDto): string {
// dto instance must be valid
}
}
```
### โน๏ธ Mapped types
Intersection type allows you to infer a new type
#### ๐ก Example
```typescript
import { Param } from '@discord-nestjs/core';
class FullName {
name: string;
}
```
```typescript
import { Param } from '@discord-nestjs/core';
class ResidencePlace {
city: string;
street: string;
}
```
```typescript
import { DiscordIntersectionType } from '@discord-nestjs/common';
// Must have properties name, city and street
class Profile extends DiscordIntersectionType(FullName, ResidencePlace) {
}
```