labelCategory.ts 1.54 KB
import type { LabelCategoryListItemDto } from '../types/platformCategories'
import { usAppApiRequest } from '../utils/usAppApiRequest'
import { extractPagedItems } from '../utils/pagedList'
import { fetchWithOfflineCache } from '../utils/sqliteSync'

export type LabelCategoryListQuery = {
  skipCount?: number
  maxResultCount?: number
  sorting?: string
  keyword?: string
  state?: boolean
}

function buildQuery(q: LabelCategoryListQuery): string {
  const p = new URLSearchParams()
  /** 第一页为 1(页码语义,非 offset) */
  p.set('skipCount', String(q.skipCount ?? 1))
  p.set('maxResultCount', String(q.maxResultCount ?? 50))
  if (q.sorting != null && String(q.sorting).trim() !== '') {
    p.set('sorting', String(q.sorting).trim())
  }
  if (q.keyword != null && String(q.keyword).trim() !== '') {
    p.set('keyword', String(q.keyword).trim())
  }
  if (q.state === true) {
    p.set('state', 'true')
  }
  return p.toString()
}

/** GET /api/app/label-category — 《标签模块接口对接说明(6).md》接口 1.1 */
export async function fetchLabelCategoryPage(
  query: LabelCategoryListQuery = {}
): Promise<{ items: LabelCategoryListItemDto[]; totalCount: number }> {
  const qs = buildQuery(query)
  return fetchWithOfflineCache('categories', `label:${qs}`, async () => {
    const body = await usAppApiRequest<unknown>({
      path: `/api/app/label-category?${qs}`,
      method: 'GET',
      auth: true,
    })
    const { items, totalCount } = extractPagedItems<LabelCategoryListItemDto>(body)
    return { items, totalCount }
  })
}