denwa-nest-shared 1.0.3

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/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true,
4
+ "trailingComma": "all",
5
+ "printWidth": 100,
6
+ "useTabs": false,
7
+ "bracketSameLine": false
8
+ }
package/.swcrc ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "jsc": {
3
+ "parser": {
4
+ "syntax": "typescript",
5
+ "tsx": false,
6
+ "decorators": true
7
+ },
8
+ "transform": {
9
+ "legacyDecorator": true,
10
+ "decoratorMetadata": true
11
+ },
12
+ "target": "ESNext"
13
+ },
14
+ "module": {
15
+ "type": "commonjs"
16
+ }
17
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+ ### [1.0.3](https://github.com/web-bee-ru/openapi-axios/compare/v1.0.2...v1.0.3) (2025-02-16)
6
+
7
+ ### [1.0.2](https://github.com/web-bee-ru/openapi-axios/compare/v1.0.1...v1.0.2) (2025-02-16)
8
+
9
+ ### 1.0.1 (2025-02-16)
@@ -0,0 +1,45 @@
1
+ // @ts-check
2
+ import eslint from '@eslint/js';
3
+ import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4
+ import globals from 'globals';
5
+ import tseslint from 'typescript-eslint';
6
+
7
+ export default tseslint.config(
8
+ {
9
+ ignores: ['eslint.config.mjs'],
10
+ },
11
+ eslint.configs.recommended,
12
+ ...tseslint.configs.recommendedTypeChecked,
13
+ eslintPluginPrettierRecommended,
14
+ {
15
+ languageOptions: {
16
+ globals: {
17
+ ...globals.node,
18
+ ...globals.jest,
19
+ },
20
+ ecmaVersion: 5,
21
+ sourceType: 'module',
22
+ parserOptions: {
23
+ projectService: true,
24
+ tsconfigRootDir: import.meta.dirname,
25
+ },
26
+ },
27
+ },
28
+ {
29
+ rules: {
30
+ '@typescript-eslint/interface-name-prefix': 'off',
31
+ '@typescript-eslint/no-namespace': 'off',
32
+ '@typescript-eslint/no-unsafe-enum-comparison': 'off',
33
+ '@typescript-eslint/await-thenable': 'off',
34
+ '@typescript-eslint/no-unsafe-call': 'off',
35
+ '@typescript-eslint/no-unsafe-assignment': 'off',
36
+ '@typescript-eslint/no-unsafe-member-access': 'off',
37
+ '@typescript-eslint/require-await': 'off',
38
+ '@typescript-eslint/explicit-function-return-type': 'error',
39
+ '@typescript-eslint/explicit-module-boundary-types': 'error',
40
+ '@typescript-eslint/no-explicit-any': 'error',
41
+ '@typescript-eslint/no-floating-promises': 'error',
42
+ '@typescript-eslint/no-unsafe-argument': 'error',
43
+ },
44
+ },
45
+ );
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package.json",
3
+ "name": "denwa-nest-shared",
4
+ "version": "1.0.3",
5
+ "main": "./dist/src/main.js",
6
+ "module": "./dist/src/main.js",
7
+ "types": "./dist/main.d.ts",
8
+ "author": "Denwa",
9
+ "type": "module",
10
+ "private": false,
11
+ "scripts": {
12
+ "build": "swc src -d dist --delete-dir-on-start && tsc --emitDeclarationOnly --declaration --outDir dist",
13
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
14
+ "format": "prettier --write \"src/**/*.ts\" ",
15
+ "prepare-push": "npm run lint && npm run format && npm run build",
16
+ "release:minor": "standard-version minor",
17
+ "release:patch": "standard-version patch"
18
+ },
19
+ "keywords": [],
20
+ "license": "ISC",
21
+ "description": "",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/web-bee-ru/openapi-axios.git"
25
+ },
26
+ "exports": {
27
+ ".": {
28
+ "types": {
29
+ "import": "./dist/main.d.ts"
30
+ },
31
+ "import": "./dist/src/main.js"
32
+ }
33
+ },
34
+ "devDependencies": {
35
+ "@eslint/eslintrc": "^3.2.0",
36
+ "@eslint/js": "^9.20.0",
37
+ "@swc/cli": "^0.6.0",
38
+ "@swc/core": "^1.10.16",
39
+ "@types/node": "^22.13.4",
40
+ "eslint": "^9.20.1",
41
+ "eslint-config-prettier": "^10.0.1",
42
+ "eslint-plugin-prettier": "^5.2.3",
43
+ "globals": "^15.15.0",
44
+ "prettier": "^3.5.1",
45
+ "standard-version": "^9.5.0",
46
+ "tsconfig-paths": "^4.2.0",
47
+ "typescript": "^5.7.3",
48
+ "typescript-eslint": "^8.24.0"
49
+ },
50
+ "dependencies": {
51
+ "@nestjs/swagger": "^11.0.3",
52
+ "class-validator": "^0.14.1",
53
+ "libphonenumber-js": "^1.11.20"
54
+ }
55
+ }
package/src/dto.ts ADDED
@@ -0,0 +1,172 @@
1
+ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
2
+ import { IsArray, IsString, IsUUID } from 'class-validator';
3
+
4
+ export class Paginate {
5
+ @ApiProperty({
6
+ example: '1',
7
+ description: 'Страница',
8
+ })
9
+ page: number;
10
+
11
+ @ApiProperty({
12
+ example: '10',
13
+ description: 'Всего страниц',
14
+ })
15
+ pages: number;
16
+
17
+ @ApiPropertyOptional({
18
+ example: '2',
19
+ description: 'Предыдущая страница',
20
+ })
21
+ previous?: number;
22
+
23
+ @ApiPropertyOptional({
24
+ example: '3',
25
+ description: 'Следующая страница',
26
+ })
27
+ next?: number;
28
+
29
+ @ApiProperty({
30
+ example: '1000',
31
+ description: 'Количество',
32
+ })
33
+ count: number;
34
+
35
+ @ApiProperty({
36
+ example: '100',
37
+ description: 'Лимит записей',
38
+ })
39
+ limit: number;
40
+ }
41
+
42
+ export class DeleteDto {
43
+ @ApiProperty({
44
+ example: '3422b448-2460-4fd2-9183-8000de6f8343',
45
+ description: 'Уникальный идентификатор',
46
+ })
47
+ @IsUUID()
48
+ id: string;
49
+ }
50
+
51
+ export class ImageFileDto {
52
+ @ApiProperty({ example: 'qwert', description: 'Название файла' })
53
+ @IsString()
54
+ imageName: string;
55
+
56
+ @ApiProperty({ example: 'jpg', description: 'Расширение исходного файла' })
57
+ @IsString()
58
+ originalFileExtension: string;
59
+
60
+ @ApiProperty({ example: 'qwert', description: 'Id сущности' })
61
+ @IsUUID()
62
+ entityId: string;
63
+
64
+ @ApiProperty({ example: 'qwert/qwert.jpg', description: 'Пример пути к файлу' })
65
+ @IsString()
66
+ fullPathExample: string;
67
+
68
+ @ApiProperty({ example: '[webp, jpg]', description: 'Расширения готовых файлов' })
69
+ @IsArray()
70
+ @IsString({ each: true })
71
+ fileExtensions: string[];
72
+ }
73
+
74
+ export class OkRequestDto {
75
+ @ApiProperty({ example: '200', description: 'Статус код' })
76
+ statusCode: 200;
77
+
78
+ @ApiPropertyOptional({ example: 'Успех', description: 'Сообщение' })
79
+ message?: string;
80
+ }
81
+
82
+ export class CreatedRequestDto {
83
+ @ApiProperty({ example: '201', description: 'Статус код' })
84
+ statusCode: 201;
85
+
86
+ @ApiPropertyOptional({ example: 'Успех', description: 'Сообщение' })
87
+ message?: string;
88
+ }
89
+
90
+ export class BadRequestDto {
91
+ @ApiProperty({ example: '400', description: 'Статус код' })
92
+ statusCode: 400;
93
+
94
+ @ApiPropertyOptional({ example: 'Ошибка', description: 'Сообщение' })
95
+ message?: string;
96
+
97
+ @ApiPropertyOptional({ example: '["Ошибка в поле name"]', description: 'Массив сообщений' })
98
+ messages?: string[];
99
+
100
+ @ApiPropertyOptional({ example: 'Bad Request', description: 'Сообщение ошибки' })
101
+ error?: string;
102
+ }
103
+
104
+ export class UnauthorizedDto {
105
+ @ApiProperty({ example: '401', description: 'Статус код' })
106
+ statusCode: 401;
107
+
108
+ @ApiPropertyOptional({ example: 'Ошибка', description: 'Сообщение' })
109
+ message?: string;
110
+
111
+ @ApiPropertyOptional({ example: '["Ошибка в поле name"]', description: 'Массив сообщений' })
112
+ messages?: string[];
113
+
114
+ @ApiPropertyOptional({ example: 'Unauthorized', description: 'Сообщение ошибки' })
115
+ error?: string;
116
+ }
117
+
118
+ export class ForbiddenDto {
119
+ @ApiProperty({ example: '403', description: 'Статус код' })
120
+ statusCode: 403;
121
+
122
+ @ApiPropertyOptional({ example: 'Ошибка', description: 'Сообщение' })
123
+ message?: string;
124
+
125
+ @ApiPropertyOptional({ example: '["Ошибка в поле name"]', description: 'Массив сообщений' })
126
+ messages?: string[];
127
+
128
+ @ApiPropertyOptional({ example: 'Forbidden', description: 'Сообщение ошибки' })
129
+ error?: string;
130
+ }
131
+
132
+ export class NotFoundDto {
133
+ @ApiProperty({ example: '404', description: 'Статус код' })
134
+ statusCode: 404;
135
+
136
+ @ApiPropertyOptional({ example: 'Ошибка', description: 'Сообщение' })
137
+ message?: string;
138
+
139
+ @ApiPropertyOptional({ example: '["Ошибка в поле name"]', description: 'Массив сообщений' })
140
+ messages?: string[];
141
+
142
+ @ApiPropertyOptional({ example: 'Not Found', description: 'Сообщение ошибки' })
143
+ error?: string;
144
+ }
145
+
146
+ export class MethodNotAllowedDto {
147
+ @ApiProperty({ example: '405', description: 'Статус код' })
148
+ statusCode: 405;
149
+
150
+ @ApiPropertyOptional({ example: 'Ошибка', description: 'Сообщение' })
151
+ message?: string;
152
+
153
+ @ApiPropertyOptional({ example: '["Ошибка в поле name"]', description: 'Массив сообщений' })
154
+ messages?: string[];
155
+
156
+ @ApiPropertyOptional({ example: 'Method not allowed', description: 'Сообщение ошибки' })
157
+ error?: string;
158
+ }
159
+
160
+ export class BadGatewayDto {
161
+ @ApiProperty({ example: '502', description: 'Статус код' })
162
+ statusCode: 502;
163
+
164
+ @ApiPropertyOptional({ example: 'Ошибка', description: 'Сообщение' })
165
+ message?: string;
166
+
167
+ @ApiPropertyOptional({ example: '["Ошибка в поле name"]', description: 'Массив сообщений' })
168
+ messages?: string[];
169
+
170
+ @ApiPropertyOptional({ example: 'Method not allowed', description: 'Сообщение ошибки' })
171
+ error?: string;
172
+ }
package/src/lib.ts ADDED
@@ -0,0 +1,77 @@
1
+ import parsePhoneNumber from 'libphonenumber-js';
2
+
3
+ import { IPaginate } from './types';
4
+
5
+ /**
6
+ * @description Отлов ошибки
7
+ * @param options - Объект параметров
8
+ * @param options.error - Объект ошибки
9
+ * @param options.message - Сообщение ошибки
10
+ */
11
+ export const getError = ({ error, message }: { error?: unknown; message?: string }): void => {
12
+ if (error) console.error(error);
13
+ if (message) console.error(message);
14
+ };
15
+
16
+ /**
17
+ * @description Создает объект с информацией о пагинации
18
+ * @param {number} page - Текущая страница
19
+ * @param {number} count - Общее количество записей
20
+ * @param {number} limit - Сколько записей на одной странице,
21
+ * @param {number} modelsNumber - Количество участвующих таблиц
22
+ * @return Возвращает объект пагинации
23
+ */
24
+ export const createPaginate = (
25
+ page: number,
26
+ count: number,
27
+ limit: number,
28
+ modelsNumber = 1,
29
+ ): IPaginate => {
30
+ const pages = Math.ceil(count / (limit * modelsNumber));
31
+
32
+ const paginate: IPaginate = {
33
+ page,
34
+ pages,
35
+ previous: page > 1 ? page - 1 : undefined,
36
+ next: page <= pages - 1 ? page + 1 : undefined,
37
+ count,
38
+ limit,
39
+ };
40
+
41
+ return paginate;
42
+ };
43
+
44
+ /**
45
+ * @description Превращает страницу и лимит в то, сколько пропустить записей в orm
46
+ * @param {number} page - Текущая страница
47
+ * @param {number} limit - Сколько записей на одной странице,
48
+ * @return Возвращает orm skip
49
+ */
50
+ export const getPaginateSkip = (page: number, limit: number): number => {
51
+ return (page - 1) * limit;
52
+ };
53
+
54
+ /**
55
+ * @description Валидация номера телефона
56
+ * @param {string} phone - Номер телефона
57
+ * @return {string} Возвращает либо пустую строку, либо отформатированный номер телефона
58
+ * @example
59
+ * const phone = formatPhone(+79885054219)
60
+ * phone === "+7 988 5054219"
61
+ */
62
+ export const formatPhone = (phone: string): string => {
63
+ const parsedPhone = parsePhoneNumber(phone);
64
+ if (!parsedPhone?.isValid()) return '';
65
+
66
+ return parsedPhone.formatInternational();
67
+ };
68
+
69
+ /**
70
+ * @description Объединяет два 32 битных числа
71
+ * @param {number} low - младшие 32 бита числа
72
+ * @param {number} hight - старшие 32 бита числа
73
+ * @return {number} Готовое число
74
+ */
75
+ export const concatenateTwo32BitNumbers = (low: number, high: number): number => {
76
+ return (high >>> 0) * Math.pow(2, 32) + (low >>> 0);
77
+ };
package/src/main.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './dto';
2
+ export * from './lib';
3
+ export * from './types';
package/src/types.ts ADDED
@@ -0,0 +1,22 @@
1
+ export interface IResponse<StatusCode, Data = object> {
2
+ statusCode: StatusCode;
3
+ messages?: string[];
4
+ message?: string;
5
+ error?: string;
6
+ data?: Data;
7
+ }
8
+ export interface IPaginate {
9
+ page: number;
10
+ pages: number;
11
+ previous?: number;
12
+ next?: number;
13
+ count: number;
14
+ limit: number;
15
+ }
16
+
17
+ export type OkNotFoundType = IResponse<404 | 200>;
18
+ export type OkNotFoundBadRequestType = IResponse<404 | 400 | 200>;
19
+ export type OkNotFoundForbiddenType = IResponse<404 | 403 | 200>;
20
+
21
+ export type FilterType = 'and' | 'or';
22
+ export type SortOrderType = 'asc' | 'desc' | 'ASC' | 'DESC';
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "declaration": true,
5
+ "removeComments": true,
6
+ "emitDecoratorMetadata": true,
7
+ "experimentalDecorators": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "target": "ESNext",
10
+ "sourceMap": true,
11
+ "outDir": "./dist",
12
+ "baseUrl": "./",
13
+ "incremental": true,
14
+ "paths": {
15
+ "@src/*": ["src/*"],
16
+ },
17
+ /* Linting */
18
+ "strict": true,
19
+ "strictNullChecks": true,
20
+ "strictFunctionTypes": true,
21
+ "strictBindCallApply": true,
22
+ "strictPropertyInitialization": false,
23
+ "noUnusedLocals": true,
24
+ "noUnusedParameters": true,
25
+ "noImplicitThis": true,
26
+ "noImplicitAny": true,
27
+ "alwaysStrict": true,
28
+ "forceConsistentCasingInFileNames": false,
29
+ "noImplicitReturns": true,
30
+ "noFallthroughCasesInSwitch": true
31
+ }
32
+ }