1.0.3 • Published 12 months ago
@v-sandbox/shared-errors v1.0.3
@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
export enum UserType {
tutor = 'tutor',
student = 'student'
}
export type UserPayload = {
user_id: string;
org_id?: string;
user_type: UserType;
}
export interface IAppError {
[key: string]: IAppErrorMessage | ((message: string) => IAppErrorMessage) | ((options: Record<string, any>) => IAppErrorMessage);
}
export interface IAppErrorMessage {
message: string;
errorCode: string;
}
export declare const implementFunctionalError: <T>() => <U extends T>(argument: U) => U;
3. The shared validators:
- REGEXP
- ValidationErrorPipe
- IsID
- IsArrayOfID
- IdValidationPipe
- @ValidateTime
- @ValidateDateInterval
- IsValidTimezone
export const REGEXP = {
URL: (isEmptyValueAllowed = false) => RegExp,
AT_LEAST_ONE_CHARACTER: (isEmptyValueAllowed = false) => RegExp
};
import { ValidatorConstraintInterface } from 'class-validator';
declare class IsID implements ValidatorConstraintInterface {
validate(value: unknown): boolean;
}
import { ValidatorConstraintInterface } from 'class-validator';
export declare class IsArrayOfID implements ValidatorConstraintInterface {
validate(array: unknown): boolean;
}
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;
}
import { PipeTransform } from '@nestjs/common';
export declare class IdValidationPipe implements PipeTransform<string, string> {
private _idName?;
constructor(_idName?: string);
transform(id: string): string;
}
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;
}
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;
}
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
@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
@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
export const ActionContext = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.actionContext;
},
);
export const Authorization = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.headers?.authorization;
},
);
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;
},
);