# @v-sandbox/shared-errors

> Shared errors for sandbox micro-services

Latest version **1.0.3** (published 2023-06-06) · UNLILCENSED license · 0 weekly downloads

## Install

```sh
npm install @v-sandbox/shared-errors
pnpm add @v-sandbox/shared-errors
yarn add @v-sandbox/shared-errors
bun add @v-sandbox/shared-errors
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2023-06-06 |
| First published | 2023-06-06 |
| Weekly downloads | 0 |
| License | UNLILCENSED |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 169.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | @zwanzigg |
| Maintainers | vshreder |

## Links

- npm: https://www.npmjs.com/package/@v-sandbox/shared-errors
- Repository: https://github.com/zwanzigg/npm-shared-errors
- Homepage: https://github.com/zwanzigg/npm-shared-errors#readme
- Issues: https://github.com/zwanzigg/npm-shared-errors/issues
- npm.io page: https://npm.io/package/@v-sandbox/shared-errors

## Dependencies (7)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.0
- [mongoose](https://npm.io/package/mongoose.md) ^7.2.2
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^9.3.8
- [class-validator](https://npm.io/package/class-validator.md) ^0.14.0
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.5.1
- [@typegoose/typegoose](https://npm.io/package/@typegoose/typegoose.md) ^11.2.0
- [@v-sandbox/shared-constants](https://npm.io/package/@v-sandbox/shared-constants.md) 1.0.0

## Recent versions

- 1.0.3 (latest) — 2023-06-06
- 1.0.2 — 2023-06-06
- 1.0.1 — 2023-06-06
- 1.0.0 — 2023-06-06

## README

# @v-sandbox/shared-errors


### 1. The type definitions and default configuration of Organizations' Features:
    - FeatureType
    - DefaultOrganizationFeatures
    - OrganizationFeatures
    - IOrganizationFeatures interface

### 2. The shared functionality:
    - UserType
    - UserPayload
    - IAppError
    - IAppErrorMessage
    - implementFunctionalError
    - AppError

```typescript
export enum UserType {
    tutor = 'tutor',
    student = 'student'
}
```

```typescript
export type UserPayload = {
    user_id: string;
    org_id?: string;
    user_type: UserType;
}
```

```typescript
export interface IAppError {
    [key: string]: IAppErrorMessage | ((message: string) => IAppErrorMessage) | ((options: Record<string, any>) => IAppErrorMessage);
}
```

```typescript
export interface IAppErrorMessage {
message: string;
errorCode: string;
}
```

```typescript
export declare const implementFunctionalError: <T>() => <U extends T>(argument: U) => U;
```

### 3. The shared validators:
    - REGEXP
    - ValidationErrorPipe
    - IsID
    - IsArrayOfID
    - IdValidationPipe
    - @ValidateTime
    - @ValidateDateInterval
    - IsValidTimezone

```typescript
export const REGEXP = {
    URL: (isEmptyValueAllowed = false) => RegExp,
    AT_LEAST_ONE_CHARACTER: (isEmptyValueAllowed = false) => RegExp
};
```

```typescript
import { ValidatorConstraintInterface } from 'class-validator';
declare class IsID implements ValidatorConstraintInterface {
    validate(value: unknown): boolean;
}
```

```typescript
import { ValidatorConstraintInterface } from 'class-validator';
export declare class IsArrayOfID implements ValidatorConstraintInterface {
    validate(array: unknown): boolean;
}
```

```typescript
import { PipeTransform, ArgumentMetadata } from '@nestjs/common';
import { ValidationError } from 'class-validator';
export declare class ValidationErrorPipe implements PipeTransform<ValidationError> {
    transform(value: any, { metatype }: ArgumentMetadata): Promise<void>;
    private toValidate;
}
```

```typescript
import { PipeTransform } from '@nestjs/common';
export declare class IdValidationPipe implements PipeTransform<string, string> {
    private _idName?;
    constructor(_idName?: string);
    transform(id: string): string;
}
```

```typescript
import { ValidationOptions, ValidatorConstraintInterface } from 'class-validator';
export declare function ValidateTime(validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
export declare class ValidateTimeConstraint implements ValidatorConstraintInterface {
    validate(value: string): boolean;
}
```

```typescript
import { ValidationArguments, ValidationOptions, ValidatorConstraintInterface } from 'class-validator';
export declare function ValidateDateInterval(property: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
export declare class ValidateDateIntervalConstraint implements ValidatorConstraintInterface {
    validate(value: any, args: ValidationArguments): boolean;
}
```

```typescript
import { ValidationArguments, ValidationOptions, ValidatorConstraintInterface } from 'class-validator';
export declare function IsValidTimezone(validationOptions?: ValidationOptions): (object: unknown, propertyName: string) => void;
export declare class IsValidTimezoneConstraint implements ValidatorConstraintInterface {
    validate(value: string, validationArguments?: ValidationArguments): Promise<boolean> | boolean;
}
```

### 4. Interceptors:
    - RestApiOrgIdInterceptor

```typescript
@Injectable()
export class RestApiOrgIdInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        const req = context.switchToHttp().getRequest();
        const {user} = req.body;
        const {org_id} = req.query;

        const isUserSuperAdmin = !user?.org_id;
        if (isUserSuperAdmin) {
            /* The super-admin does not belong to any organization, so they must specify org_id in the arguments. */
            if (!org_id)
                throw new BadRequestException(
                    AppError.FIELD_SHOULD_BE_PROVIDED('org_id'),
                );
        } else {
            /* For other users, org_id is taken from their tokens. */
            req.query.org_id = user.org_id;
        }

        return next.handle();
    }
}
```

### 5. Models:
    - BaseInteractionModel

```typescript
@modelOptions({options: {allowMixed: Severity.ALLOW}})
export abstract class BaseInteractionModel {
    @prop()
    public entity_type: InteractionEntityType

    @prop()
    public entity_id: string;

    @prop()
    public user_id: string;

    @prop()
    public created_by?: string;

    @prop()
    public interaction_type: string;

    @prop({nullable: true})
    public data?: Record<string, unknown>;

    @prop()
    public created_at: Date;

    static get schema(): Schema {
        return buildSchema(this as any, {
            timestamps: {
                createdAt: 'created_at',
            },
            toJSON: {
                getters: true,
                virtuals: true,
            },
        });
    }
}
```

### 6. Decorators:
    - ActionContext
    - Authorization
    - User

```typescript

export const ActionContext = createParamDecorator(
    (data: unknown, ctx: ExecutionContext) => {
        const request = ctx.switchToHttp().getRequest();
        return request.actionContext;
    },
);
```

```typescript
export const Authorization = createParamDecorator(
    (data: unknown, ctx: ExecutionContext) => {
        const request = ctx.switchToHttp().getRequest();

        return request.headers?.authorization;
    },
);
```

```typescript
export const User = createParamDecorator(
    (data: unknown, ctx: ExecutionContext) => {
        const request = ctx.switchToHttp().getRequest();

        if (!request.body.user) {
            throw new BadRequestException(
                AppError.FIELD_SHOULD_BE_PROVIDED('user')
            );
        }
        return request.body.user;
    },
);

```

---
_Source: https://npm.io/package/@v-sandbox/shared-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
