diff --git a/package.json b/package.json index 861eba5..89ce0b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axp-ts", - "version": "1.9.11", + "version": "1.9.10", "description": "TypeScript helper library", "author": "AntoXa PRO ", "homepage": "https://antoxahub.ru/antoxa/axp-ts", @@ -8,20 +8,31 @@ "type": "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": [ "dist", "tsconfig.json" ], "scripts": { - "build": "tsc", + "build": "vite build", "prepare": "npm run build" }, - "dependencies": { - "zod": "^3.22.2" + "peerDependencies": { + "zod": "^3.22.4" }, "devDependencies": { "prettier": "^2.8.8", - "typescript": "^4.9.5" + "typescript": "^4.9.5", + "vite": "^4.4.11", + "vite-plugin-dts": "^3.6.0" } } diff --git a/src/forms/fields.ts b/src/forms/fields.ts index 3c70f32..0591434 100644 --- a/src/forms/fields.ts +++ b/src/forms/fields.ts @@ -22,7 +22,7 @@ export type TUiFieldSelectOption = { */ export const fieldSchema = ( base: T, - args?: TFormSchemaCtrlArgs + args: TFormSchemaCtrlArgs = {} ) => base.describe(FormSchemaCtrl.toString(args, base.description)) /** diff --git a/src/forms/model.ts b/src/forms/model.ts index f22940e..113e19d 100644 --- a/src/forms/model.ts +++ b/src/forms/model.ts @@ -73,7 +73,7 @@ export class BaseFormModel implements IFormModel { let items: TNotificationItem[] = [] for (const code in this._errors) { const text = this._errors[code] - items.push({ code, text }) + if (text) items.push({ code, text }) } return items } @@ -94,7 +94,9 @@ export class BaseFormModel implements IFormModel { } 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) { diff --git a/src/requests/data-result.ts b/src/requests/data-result.ts index 801516d..e2f1e96 100644 --- a/src/requests/data-result.ts +++ b/src/requests/data-result.ts @@ -30,7 +30,7 @@ export class DataResultEntity implements IDataResult { this.setData() } - setData(data: T = null, pagination?: TPagination): void { + setData(data: T | null = null, pagination?: TPagination): void { if (data !== null) { this.data = data } diff --git a/src/requests/find-filter.ts b/src/requests/find-filter.ts index cde4e9b..85e49bf 100644 --- a/src/requests/find-filter.ts +++ b/src/requests/find-filter.ts @@ -5,7 +5,6 @@ import { isEqual } from '../utils' import { Pagination, paginationQuerySchema } from './pagination' import { bFieldsSchema, cFieldsSchema, fieldSchema } from '../forms' - export const querySchema = cFieldsSchema .pick({ q: true }) .extend(paginationQuerySchema.shape) @@ -14,6 +13,8 @@ export const querySchema = cFieldsSchema label: 'Сортировка' }) }) + .partial() + .describe('Параметры базового запроса') export type TQuery = z.infer @@ -32,39 +33,32 @@ export class FindFilter implements TFindFilter { sort?: string constructor(query?: T) { - let queryCopy = Object.assign({}, query) + let queryCopy: T = Object.assign({}, query) // Pagination. this.setPagination(queryCopy) - for (const key of Object.keys(this.pagination)) { - if (queryCopy[key]) delete queryCopy[key] + if (this.pagination) { + // Delete pagination props. + const paginationKeys = Object.keys(this.pagination) as [keyof T] + for (const key of paginationKeys) { + if (queryCopy[key]) delete queryCopy[key] + } } // Sort. if (queryCopy.sort) { this.sort = queryCopy.sort - delete queryCopy.sort + queryCopy.sort = undefined } // Obj. this.obj = queryCopy } - setPagination(pagination?: TPagination) { + setPagination(pagination?: Partial) { this.pagination = new Pagination(pagination).toObject() } - static getQuery (filter: TFindFilter): 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 { return { obj: this.obj, diff --git a/src/requests/pagination.ts b/src/requests/pagination.ts index 0933aa9..6303498 100644 --- a/src/requests/pagination.ts +++ b/src/requests/pagination.ts @@ -21,9 +21,13 @@ export const paginationSchema = cFieldsSchema export type TPagination = z.infer -export const paginationQuerySchema = paginationSchema.pick({ - page: true, limit: true -}) +export const paginationQuerySchema = paginationSchema + .pick({ + page: true, + limit: true + }) + .partial() + .describe('Параметры разбиения на страницы') export type TPaginationQuery = z.infer @@ -113,7 +117,6 @@ export class Pagination implements TPagination { page: this.page, limit: this.limit, total: this.total, - skip: this.skip, pages: this.pages } diff --git a/src/utils/objects.ts b/src/utils/objects.ts index 983fe2b..c2bd9e2 100644 --- a/src/utils/objects.ts +++ b/src/utils/objects.ts @@ -1,10 +1,12 @@ export const isEqual = (objects: T[]) => { 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) { - const keys1 = Object.keys(obj1) - const keys2 = Object.keys(obj2) + const keys1 = Object.keys(obj1) as [keyof T] + const keys2 = Object.keys(obj2) as [keyof T] if (keys1.length !== keys2.length) { return false @@ -16,7 +18,7 @@ export const isEqual = (objects: T[]) => { return false } } else { - if (!isEqual([obj1[key], obj2[key]])) { + if (!isEqual([obj1[key], obj2[key]] as T[])) { return false } } diff --git a/tsconfig.json b/tsconfig.json index 8bf7fb5..9576b77 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,15 @@ { "compilerOptions": { - "rootDir": "src", - "outDir": "dist", - "target": "ES6", - "module": "CommonJS", - "moduleResolution": "node", - "lib": ["es2017"], - "declaration": true, - "sourceMap": true + "target": "ESNext", + "module": "ESNext", + + "strict": true, + "moduleResolution": "Node", + "isolatedModules": true, + "esModuleInterop": true, + "useDefineForClassFields": true, + + "declaration": true }, - "exclude": ["node_modules", "dist"] + "include": ["src/**/*.ts", "src/**/*.d.ts"] } diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..2c92571 --- /dev/null +++ b/vite.config.ts @@ -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' + } + } +})