@galaxy-stack/orbit-common 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # @galaxy-stack/orbit-common
2
+
3
+ ## Mô tả
4
+ Package chứa các decorators, pipes, guards, interceptors và exceptions dùng chung trong Orbit framework.
5
+
6
+ ## Tính năng chính
7
+
8
+ ### 1. HTTP Decorators
9
+ ```typescript
10
+ import {
11
+ Controller,
12
+ Get, Post, Put, Patch, Delete,
13
+ Body, Query, Param, Headers, Req, Res
14
+ } from '@galaxy-stack/orbit-common';
15
+
16
+ @Controller('api')
17
+ class ApiController {
18
+ @Get('items')
19
+ getItems(@Query('page') page: string) {}
20
+
21
+ @Post('items')
22
+ createItem(@Body() data: any) {}
23
+ }
24
+ ```
25
+
26
+ ### 2. Built-in Pipes
27
+ - `ParseIntPipe`: Chuyển string thành number
28
+ - `ParseFloatPipe`: Chuyển string thành float
29
+ - `ParseBoolPipe`: Chuyển string thành boolean
30
+ - `ParseArrayPipe`: Chuyển string thành array
31
+ - `DefaultValuePipe`: Giá trị mặc định
32
+ - `TrimPipe`: Xóa khoảng trắng
33
+
34
+ ```typescript
35
+ @Get(':id')
36
+ getUser(@Param('id', ParseIntPipe) id: number) {
37
+ // id đã được convert sang number
38
+ }
39
+ ```
40
+
41
+ ### 3. HTTP Exceptions
42
+ ```typescript
43
+ import {
44
+ BadRequestException,
45
+ UnauthorizedException,
46
+ ForbiddenException,
47
+ NotFoundException,
48
+ ConflictException,
49
+ InternalServerErrorException,
50
+ } from '@galaxy-stack/orbit-common';
51
+
52
+ throw new NotFoundException('User not found');
53
+ ```
54
+
55
+ ### 4. Guards
56
+ ```typescript
57
+ import { CanActivate, ExecutionContext } from '@galaxy-stack/orbit-common';
58
+
59
+ class AuthGuard implements CanActivate {
60
+ canActivate(context: ExecutionContext): boolean {
61
+ const request = context.switchToHttp().getRequest();
62
+ return !!request.headers.authorization;
63
+ }
64
+ }
65
+ ```
66
+
67
+ ### 5. Interceptors
68
+ ```typescript
69
+ import { Interceptor, ExecutionContext } from '@galaxy-stack/orbit-common';
70
+
71
+ class LoggingInterceptor implements Interceptor {
72
+ async intercept(context: ExecutionContext, next: () => Promise<any>) {
73
+ console.log('Before...');
74
+ const result = await next();
75
+ console.log('After...');
76
+ return result;
77
+ }
78
+ }
79
+ ```
80
+
81
+ ## Cách sử dụng
82
+
83
+ ```typescript
84
+ import {
85
+ Controller, Get, UseGuards, UsePipes,
86
+ ParseIntPipe, AuthGuard
87
+ } from '@galaxy-stack/orbit-common';
88
+
89
+ @Controller('users')
90
+ @UseGuards(AuthGuard)
91
+ class UserController {
92
+ @Get(':id')
93
+ @UsePipes(ParseIntPipe)
94
+ getUser(@Param('id') id: number) {
95
+ return { id };
96
+ }
97
+ }
98
+ ```
@@ -0,0 +1,19 @@
1
+ import 'reflect-metadata';
2
+ import type { Type } from '../interfaces/type.interface';
3
+ export interface ArgumentsHost {
4
+ getArgs<T extends any[] = any[]>(): T;
5
+ getArgByIndex<T = any>(index: number): T;
6
+ getType<T extends string = string>(): T;
7
+ }
8
+ export interface HttpArgumentsHost {
9
+ getRequest<T = any>(): T;
10
+ getResponse<T = any>(): T;
11
+ getNext<T = any>(): T;
12
+ }
13
+ export interface ExceptionFilter<T = any> {
14
+ catch(exception: T, host: ArgumentsHost): any;
15
+ }
16
+ export declare function Catch(...exceptions: Type<any>[]): ClassDecorator;
17
+ export declare function UseFilters(...filters: (Type<ExceptionFilter> | ExceptionFilter)[]): MethodDecorator & ClassDecorator;
18
+ export declare function getCatchExceptions(target: Type): Type<any>[];
19
+ export declare function getFilters(target: Object, propertyKey?: string | symbol): (Type<ExceptionFilter> | ExceptionFilter)[];
@@ -0,0 +1,11 @@
1
+ import 'reflect-metadata';
2
+ export declare function HttpCode(statusCode: number): MethodDecorator;
3
+ export declare function Header(name: string, value: string): MethodDecorator;
4
+ export declare function Redirect(url: string, statusCode?: number): MethodDecorator;
5
+ export declare function Render(template: string): MethodDecorator;
6
+ export declare function getHttpCode(target: Object, propertyKey: string | symbol): number | undefined;
7
+ export declare function getHeaders(target: Object, propertyKey: string | symbol): Record<string, string>;
8
+ export declare function getRedirect(target: Object, propertyKey: string | symbol): {
9
+ url: string;
10
+ statusCode: number;
11
+ } | undefined;
@@ -0,0 +1,7 @@
1
+ export * from './params.decorator';
2
+ export * from './http-code.decorator';
3
+ export * from './use-guards.decorator';
4
+ export * from './use-pipes.decorator';
5
+ export * from './use-interceptors.decorator';
6
+ export * from './catch.decorator';
7
+ export * from './transform.decorator';
@@ -0,0 +1,31 @@
1
+ import 'reflect-metadata';
2
+ export declare enum ParamType {
3
+ BODY = "body",
4
+ QUERY = "query",
5
+ PARAM = "param",
6
+ HEADERS = "headers",
7
+ REQUEST = "request",
8
+ RESPONSE = "response",
9
+ IP = "ip",
10
+ SESSION = "session",
11
+ FILE = "file",
12
+ FILES = "files"
13
+ }
14
+ export interface ParamMetadata {
15
+ type: ParamType;
16
+ data?: string;
17
+ index: number;
18
+ }
19
+ export declare const Body: (data?: string) => ParameterDecorator;
20
+ export declare const Query: (data?: string) => ParameterDecorator;
21
+ export declare const Param: (data?: string) => ParameterDecorator;
22
+ export declare const Headers: (data?: string) => ParameterDecorator;
23
+ export declare const Req: (data?: string) => ParameterDecorator;
24
+ export declare const Request: (data?: string) => ParameterDecorator;
25
+ export declare const Res: (data?: string) => ParameterDecorator;
26
+ export declare const Response: (data?: string) => ParameterDecorator;
27
+ export declare const Ip: (data?: string) => ParameterDecorator;
28
+ export declare const Session: (data?: string) => ParameterDecorator;
29
+ export declare const UploadedFile: (data?: string) => ParameterDecorator;
30
+ export declare const UploadedFiles: (data?: string) => ParameterDecorator;
31
+ export declare function getParamMetadata(target: Object, propertyKey: string | symbol): ParamMetadata[];
@@ -0,0 +1,20 @@
1
+ export declare const TRANSFORM_METADATA: unique symbol;
2
+ export interface TransformOptions {
3
+ toClassOnly?: boolean;
4
+ toPlainOnly?: boolean;
5
+ groups?: string[];
6
+ }
7
+ export interface TransformFn {
8
+ (value: any, obj: any): any;
9
+ }
10
+ export declare function Transform(transformFn: TransformFn, options?: TransformOptions): PropertyDecorator;
11
+ export declare function ToInt(): PropertyDecorator;
12
+ export declare function ToFloat(): PropertyDecorator;
13
+ export declare function ToBoolean(): PropertyDecorator;
14
+ export declare function ToDate(): PropertyDecorator;
15
+ export declare function ToLowerCase(): PropertyDecorator;
16
+ export declare function ToUpperCase(): PropertyDecorator;
17
+ export declare function Trim(): PropertyDecorator;
18
+ export declare function ToArray(): PropertyDecorator;
19
+ export declare function DefaultValue(defaultVal: any): PropertyDecorator;
20
+ export declare function applyTransforms<T extends object>(instance: T): T;
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ import type { CanActivate, GuardClass } from '../interfaces/guard.interface';
3
+ export declare const GUARDS_METADATA = "orbit:guards";
4
+ export declare function UseGuards(...guards: (GuardClass | CanActivate)[]): MethodDecorator & ClassDecorator;
5
+ export declare function getGuards(target: Object, propertyKey?: string | symbol): (GuardClass | CanActivate)[];
@@ -0,0 +1,11 @@
1
+ import 'reflect-metadata';
2
+ import type { Type } from '../interfaces/type.interface';
3
+ import type { ExecutionContext } from '../interfaces/execution-context.interface';
4
+ export interface CallHandler<T = any> {
5
+ handle(): Promise<T>;
6
+ }
7
+ export interface OrbitInterceptor<T = any, R = any> {
8
+ intercept(context: ExecutionContext, next: CallHandler<T>): Promise<R>;
9
+ }
10
+ export declare function UseInterceptors(...interceptors: (Type<OrbitInterceptor> | OrbitInterceptor)[]): MethodDecorator & ClassDecorator;
11
+ export declare function getInterceptors(target: Object, propertyKey?: string | symbol): (Type<OrbitInterceptor> | OrbitInterceptor)[];
@@ -0,0 +1,12 @@
1
+ import 'reflect-metadata';
2
+ import type { Type } from '../interfaces/type.interface';
3
+ export interface ArgumentMetadata {
4
+ type: 'body' | 'query' | 'param' | 'custom';
5
+ metatype?: Type;
6
+ data?: string;
7
+ }
8
+ export interface PipeTransform<T = any, R = any> {
9
+ transform(value: T, metadata: ArgumentMetadata): R | Promise<R>;
10
+ }
11
+ export declare function UsePipes(...pipes: (Type<PipeTransform> | PipeTransform)[]): MethodDecorator & ClassDecorator;
12
+ export declare function getPipes(target: Object, propertyKey?: string | symbol): (Type<PipeTransform> | PipeTransform)[];
@@ -0,0 +1,58 @@
1
+ export declare class HttpException extends Error {
2
+ private readonly response;
3
+ private readonly status;
4
+ constructor(response: string | Record<string, any>, status: number);
5
+ private initMessage;
6
+ private initName;
7
+ getResponse(): string | Record<string, any>;
8
+ getStatus(): number;
9
+ toJSON(): Record<string, any>;
10
+ }
11
+ export declare class BadRequestException extends HttpException {
12
+ constructor(message?: string | Record<string, any>);
13
+ }
14
+ export declare class UnauthorizedException extends HttpException {
15
+ constructor(message?: string | Record<string, any>);
16
+ }
17
+ export declare class ForbiddenException extends HttpException {
18
+ constructor(message?: string | Record<string, any>);
19
+ }
20
+ export declare class NotFoundException extends HttpException {
21
+ constructor(message?: string | Record<string, any>);
22
+ }
23
+ export declare class MethodNotAllowedException extends HttpException {
24
+ constructor(message?: string | Record<string, any>);
25
+ }
26
+ export declare class NotAcceptableException extends HttpException {
27
+ constructor(message?: string | Record<string, any>);
28
+ }
29
+ export declare class ConflictException extends HttpException {
30
+ constructor(message?: string | Record<string, any>);
31
+ }
32
+ export declare class GoneException extends HttpException {
33
+ constructor(message?: string | Record<string, any>);
34
+ }
35
+ export declare class PayloadTooLargeException extends HttpException {
36
+ constructor(message?: string | Record<string, any>);
37
+ }
38
+ export declare class UnsupportedMediaTypeException extends HttpException {
39
+ constructor(message?: string | Record<string, any>);
40
+ }
41
+ export declare class UnprocessableEntityException extends HttpException {
42
+ constructor(message?: string | Record<string, any>);
43
+ }
44
+ export declare class InternalServerErrorException extends HttpException {
45
+ constructor(message?: string | Record<string, any>);
46
+ }
47
+ export declare class NotImplementedException extends HttpException {
48
+ constructor(message?: string | Record<string, any>);
49
+ }
50
+ export declare class BadGatewayException extends HttpException {
51
+ constructor(message?: string | Record<string, any>);
52
+ }
53
+ export declare class ServiceUnavailableException extends HttpException {
54
+ constructor(message?: string | Record<string, any>);
55
+ }
56
+ export declare class GatewayTimeoutException extends HttpException {
57
+ constructor(message?: string | Record<string, any>);
58
+ }
@@ -0,0 +1 @@
1
+ export * from './http.exception';
@@ -0,0 +1,2 @@
1
+ export * from './decorators';
2
+ export * from './exceptions';