From 6467364ec1162ad6d92d78652e385c8c953ecc22 Mon Sep 17 00:00:00 2001 From: azabroflovski Date: Wed, 11 May 2022 09:50:46 +0500 Subject: [PATCH] feat: add MXIKSearch interface --- src/api/index.ts | 23 ++++++++++- src/lib/index.ts | 21 ++++++++++ src/main.ts | 12 +++--- src/typings/index.ts | 92 +++++++++++++++++++++++++++----------------- 4 files changed, 106 insertions(+), 42 deletions(-) create mode 100644 src/lib/index.ts diff --git a/src/api/index.ts b/src/api/index.ts index 21c8bbe..ae232b8 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,26 @@ import { createHttpClient } from '../shared' +import { AxiosResponse } from 'axios' + +import { + MXIKSearchParamsObj, + MXIKSearchSymbolResponseObj +} from '../typings' export const http = createHttpClient({ - baseURL: process.ENV.API_URL // see .env + baseURL: 'https://tasnif.soliq.uz/api' // see .env }) + +/** + * Search items via keyword + * @param keyword {string} + * @param size {number} + * @returns {AxiosResponse} + */ +export function MXIKSearchSymbol({ keyword = '', limit = 20 }: MXIKSearchParamsObj): Promise> { + return http.get('cls-api/mxik/search-symbol', { + params: { + search_text: keyword, + size: limit, + } + }) +} diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..4fa2c2f --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1,21 @@ +import { MXIKSearchSymbol } from '../api' +import { MXIKSearchOptionsObj, MXIKSearchResponseObj } from '../typings' + +export async function MXIKSearch(keyword: string, { limit }: MXIKSearchOptionsObj): Promise { + try { + const { data: response } = await MXIKSearchSymbol({ keyword, limit }) + + return { + items: response.data.content, + pagination: { + totalPages: response.data.totalPages, + totalItems: response.data.totalElements, + perPage: response.data.size, + isFirstPage: response.data.first, + isLastPage: response.data.last, + } + } + } catch (error) { + throw error + } +} diff --git a/src/main.ts b/src/main.ts index f77db7a..d7dace7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,10 @@ import './style.css' +import { MXIKSearch } from './lib' + +// Usage example +const response = await MXIKSearch('футболка', { limit: 10 }) +console.log(response) -const app = document.querySelector('#app')! -app.innerHTML = ` -

Hello Vite!

- Documentation -` +const app = document.querySelector('#app')! +app.innerHTML = `

mxik

` diff --git a/src/typings/index.ts b/src/typings/index.ts index 2ce3ca1..81b07cf 100644 --- a/src/typings/index.ts +++ b/src/typings/index.ts @@ -14,52 +14,72 @@ export interface MXIKPageableObj { } export interface NXIKItemUnitObj { - id: number, - name: string, - nameUz: string, - nameRu: string, - nameEng?: string, - nameLatin?: string, - unit: string, - commonUnitsId: number, - difference?: string, + id: number + name: string + nameUz: string + nameRu: string + nameEng?: string + nameLatin?: string + unit: string + commonUnitsId: number + difference?: string description?: string } export interface MXIKItemObj { - mxikCode: string, - groupName: string, - className: string, - positionName: string, - subPositionName: string, - brandName: string | null, - attributeName: string | null, - unitCode: number, - unitName: string, - commonUnitCode: number, - commonUnitName: string, - internationalCode: number | null, - units: NXIKItemUnitObj[], - myProduct: number, + mxikCode: string + groupName: string + className: string + positionName: string + subPositionName: string + brandName: string | null + attributeName: string | null + unitCode: number + unitName: string + commonUnitCode: number + commonUnitName: string + internationalCode: number | null + units: NXIKItemUnitObj[] + myProduct: number packages?: string | null } -export interface MXIKResponseObj { - success: boolean, - code: number, - reason: 'ok' | 'error', +export interface MXIKSearchSymbolResponseObj { + success: boolean + code: number + reason: 'ok' | 'error' data: { - content: MXIKItemObj[], + content: MXIKItemObj[] pageable: MXIKPageableObj - last: boolean, - totalPages: number, - totalElements: number, + last: boolean + totalPages: number + totalElements: number sort: MXIKSortObj - first: boolean, - numberOfElements: number, - size: number, - number: number, + first: boolean + numberOfElements: number + size: number + number: number empty: boolean - }, + } errors: string | null } + +export interface MXIKSearchParamsObj { + keyword: string + limit?: number +} + +export interface MXIKSearchOptionsObj { + limit?: number +} + +export interface MXIKSearchResponseObj { + items: MXIKItemObj[] + pagination: { + totalPages: number + totalItems: number + perPage: number + isFirstPage: boolean + isLastPage: boolean + } +}