This commit is contained in:
AntoXa PRO 2023-10-05 17:01:45 +03:00
parent 5cc8f45f90
commit a68f3964e1
9 changed files with 72 additions and 45 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "axp-ts", "name": "axp-ts",
"version": "1.9.11", "version": "1.9.10",
"description": "TypeScript helper library", "description": "TypeScript helper library",
"author": "AntoXa PRO <info@antoxa.pro>", "author": "AntoXa PRO <info@antoxa.pro>",
"homepage": "https://antoxahub.ru/antoxa/axp-ts", "homepage": "https://antoxahub.ru/antoxa/axp-ts",
@ -8,20 +8,31 @@
"type": "git", "type": "git",
"url": "https://antoxahub.ru/antoxa/axp-ts.git" "url": "https://antoxahub.ru/antoxa/axp-ts.git"
}, },
"main": "dist/index.js", "module": "./dist/index.mjs",
"main": "./dist/index.umd.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"requier": "./dist/index.umd.js"
},
"./tsconfig.json": "./tsconfig.json"
},
"files": [ "files": [
"dist", "dist",
"tsconfig.json" "tsconfig.json"
], ],
"scripts": { "scripts": {
"build": "tsc", "build": "vite build",
"prepare": "npm run build" "prepare": "npm run build"
}, },
"dependencies": { "peerDependencies": {
"zod": "^3.22.2" "zod": "^3.22.4"
}, },
"devDependencies": { "devDependencies": {
"prettier": "^2.8.8", "prettier": "^2.8.8",
"typescript": "^4.9.5" "typescript": "^4.9.5",
"vite": "^4.4.11",
"vite-plugin-dts": "^3.6.0"
} }
} }

View File

@ -22,7 +22,7 @@ export type TUiFieldSelectOption = {
*/ */
export const fieldSchema = <T extends ZodTypeAny>( export const fieldSchema = <T extends ZodTypeAny>(
base: T, base: T,
args?: TFormSchemaCtrlArgs args: TFormSchemaCtrlArgs = {}
) => base.describe(FormSchemaCtrl.toString(args, base.description)) ) => base.describe(FormSchemaCtrl.toString(args, base.description))
/** /**

View File

@ -73,7 +73,7 @@ export class BaseFormModel<T extends object = {}> implements IFormModel<T> {
let items: TNotificationItem[] = [] let items: TNotificationItem[] = []
for (const code in this._errors) { for (const code in this._errors) {
const text = this._errors[code] const text = this._errors[code]
items.push({ code, text }) if (text) items.push({ code, text })
} }
return items return items
} }
@ -94,7 +94,9 @@ export class BaseFormModel<T extends object = {}> implements IFormModel<T> {
} }
setValidError(code: string, text: string) { setValidError(code: string, text: string) {
this._errors[code] = code + ' - ' + text let obj:any = {}
obj[code] = code + ' - ' + text
this._errors = Object.assign(this._errors, obj)
} }
mergeObj(obj: any) { mergeObj(obj: any) {

View File

@ -30,7 +30,7 @@ export class DataResultEntity<T> implements IDataResult<T> {
this.setData() this.setData()
} }
setData(data: T = null, pagination?: TPagination): void { setData(data: T | null = null, pagination?: TPagination): void {
if (data !== null) { if (data !== null) {
this.data = data this.data = data
} }

View File

@ -5,7 +5,6 @@ import { isEqual } from '../utils'
import { Pagination, paginationQuerySchema } from './pagination' import { Pagination, paginationQuerySchema } from './pagination'
import { bFieldsSchema, cFieldsSchema, fieldSchema } from '../forms' import { bFieldsSchema, cFieldsSchema, fieldSchema } from '../forms'
export const querySchema = cFieldsSchema export const querySchema = cFieldsSchema
.pick({ q: true }) .pick({ q: true })
.extend(paginationQuerySchema.shape) .extend(paginationQuerySchema.shape)
@ -14,6 +13,8 @@ export const querySchema = cFieldsSchema
label: 'Сортировка' label: 'Сортировка'
}) })
}) })
.partial()
.describe('Параметры базового запроса')
export type TQuery = z.infer<typeof querySchema> export type TQuery = z.infer<typeof querySchema>
@ -32,39 +33,32 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
sort?: string sort?: string
constructor(query?: T) { constructor(query?: T) {
let queryCopy = Object.assign({}, query) let queryCopy: T = Object.assign({}, query)
// Pagination. // Pagination.
this.setPagination(queryCopy) this.setPagination(queryCopy)
for (const key of Object.keys(this.pagination)) { if (this.pagination) {
if (queryCopy[key]) delete queryCopy[key] // Delete pagination props.
const paginationKeys = Object.keys(this.pagination) as [keyof T]
for (const key of paginationKeys) {
if (queryCopy[key]) delete queryCopy[key]
}
} }
// Sort. // Sort.
if (queryCopy.sort) { if (queryCopy.sort) {
this.sort = queryCopy.sort this.sort = queryCopy.sort
delete queryCopy.sort queryCopy.sort = undefined
} }
// Obj. // Obj.
this.obj = queryCopy this.obj = queryCopy
} }
setPagination(pagination?: TPagination) { setPagination(pagination?: Partial<TPagination>) {
this.pagination = new Pagination(pagination).toObject() this.pagination = new Pagination(pagination).toObject()
} }
static getQuery <T extends TQuery = {}>(filter: TFindFilter<T>): T {
let query: any = {}
for(const key of Object.keys(filter.obj)) {
query[key] = filter.obj[key]
}
if (filter.pagination?.page) query.page = filter.pagination.page
if (filter.pagination?.limit) query.limit = filter.pagination.limit
if (filter.sort) query.sort = filter.sort
return query
}
toObject(): TFindFilter<T> { toObject(): TFindFilter<T> {
return { return {
obj: this.obj, obj: this.obj,

View File

@ -21,9 +21,13 @@ export const paginationSchema = cFieldsSchema
export type TPagination = z.infer<typeof paginationSchema> export type TPagination = z.infer<typeof paginationSchema>
export const paginationQuerySchema = paginationSchema.pick({ export const paginationQuerySchema = paginationSchema
page: true, limit: true .pick({
}) page: true,
limit: true
})
.partial()
.describe('Параметры разбиения на страницы')
export type TPaginationQuery = z.infer<typeof paginationQuerySchema> export type TPaginationQuery = z.infer<typeof paginationQuerySchema>
@ -113,7 +117,6 @@ export class Pagination implements TPagination {
page: this.page, page: this.page,
limit: this.limit, limit: this.limit,
total: this.total, total: this.total,
skip: this.skip, skip: this.skip,
pages: this.pages pages: this.pages
} }

View File

@ -1,10 +1,12 @@
export const isEqual = <T extends object>(objects: T[]) => { export const isEqual = <T extends object>(objects: T[]) => {
for (let i = 0; i < objects.length; i++) { for (let i = 0; i < objects.length; i++) {
const obj1 = objects[i]
const obj2 = objects[i + 1] const obj1: T = objects[i]
const obj2: T = objects[i + 1]
if (obj2) { if (obj2) {
const keys1 = Object.keys(obj1) const keys1 = Object.keys(obj1) as [keyof T]
const keys2 = Object.keys(obj2) const keys2 = Object.keys(obj2) as [keyof T]
if (keys1.length !== keys2.length) { if (keys1.length !== keys2.length) {
return false return false
@ -16,7 +18,7 @@ export const isEqual = <T extends object>(objects: T[]) => {
return false return false
} }
} else { } else {
if (!isEqual([obj1[key], obj2[key]])) { if (!isEqual([obj1[key], obj2[key]] as T[])) {
return false return false
} }
} }

View File

@ -1,13 +1,15 @@
{ {
"compilerOptions": { "compilerOptions": {
"rootDir": "src", "target": "ESNext",
"outDir": "dist", "module": "ESNext",
"target": "ES6",
"module": "CommonJS", "strict": true,
"moduleResolution": "node", "moduleResolution": "Node",
"lib": ["es2017"], "isolatedModules": true,
"declaration": true, "esModuleInterop": true,
"sourceMap": true "useDefineForClassFields": true,
"declaration": true
}, },
"exclude": ["node_modules", "dist"] "include": ["src/**/*.ts", "src/**/*.d.ts"]
} }

13
vite.config.ts Normal file
View File

@ -0,0 +1,13 @@
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
export default defineConfig({
plugins: [dts()],
build: {
lib: {
entry: 'src/index.ts',
name: 'axp-ts',
fileName: 'index'
}
}
})