@softimist/api 1.0.0
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/README.md +67 -0
- package/dist/index.cjs +665 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +298 -0
- package/dist/index.d.ts +298 -0
- package/dist/index.js +617 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/client.ts","../src/hooks.ts","../src/tanstack-query.tsx","../src/utils.ts"],"sourcesContent":["/**\n * @softimist/api - Shared API Types, Interfaces, and Hooks\n *\n * Main exports for the API package\n */\n\n// Server Response Types (Backend API)\nexport type { ServerResponse, ServerPaginatedResponse, PaginationMeta } from './types';\n\n// Hook Return Types (Client-side React Hooks)\nexport type { UsePaginatedApiReturn } from './types';\n\n// HTTP Client Abstraction\nexport type { HttpClient, HttpResponse, PaginationOptions, UsePaginatedApiHook } from './types';\n\n// Filter Types\nexport type {\n\tFilter,\n\tSelectFilter,\n\tMultiSelectFilter,\n\tDateFilter,\n\tBooleanFilter,\n\tNumberFilter,\n\tRangeFilter,\n\tStringFilter,\n} from './filter';\n\n// Base API Client\nexport { BaseApiClient, type BaseApiClientConfig, type RequestConfig } from './client';\n\n// React Hooks\nexport { useApi, useApiAction, usePaginatedApi, useMockPaginatedApi } from './hooks';\n\n// Utility Functions\nexport { getErrorMessage, getSuccessMessage } from './utils';\n\n// TanStack Query\nexport { QueryProvider, makeQueryClient, getQueryClient } from './tanstack-query';\n\nexport { useInfiniteQuery, useQuery } from '@tanstack/react-query';\n","/**\n * @softimist/api - Base API Client\n * \n * Base class for API clients that can be extended by module-specific implementations.\n * Uses axios for HTTP requests and provides common functionality like authentication,\n * error handling, and request/response interceptors.\n * \n * All HTTP methods return unwrapped server response data directly (e.g., ServerResponse<T>).\n */\n\nimport axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios';\nimport type { HttpClient } from './types';\n\n/**\n * Configuration options for BaseApiClient\n */\nexport interface BaseApiClientConfig {\n baseURL?: string;\n getAuthToken?: () => string | null;\n getRefreshToken?: () => string | null;\n setAuthToken?: (token: string) => void;\n setRefreshToken?: (token: string) => void;\n clearAuthTokens?: () => void;\n refreshTokenUrl?: string;\n timeout?: number;\n headers?: Record<string, string>;\n}\n\n/**\n * Request configuration passed to HTTP methods\n */\nexport interface RequestConfig {\n headers?: Record<string, string>;\n params?: Record<string, any>;\n [key: string]: any;\n}\n\n/**\n * Base API Client class\n * Provides common HTTP functionality using axios that can be extended by module-specific clients\n * \n * @example\n * ```ts\n * class WebsiteApiClient extends BaseApiClient {\n * async getPages(): Promise<ServerPaginatedResponse<Page>> {\n * return this.get<ServerPaginatedResponse<Page>>('/api/v1/website/pages');\n * }\n * }\n * ```\n */\nexport class BaseApiClient implements HttpClient {\n protected axiosInstance: AxiosInstance;\n protected getAuthToken?: () => string | null;\n protected getRefreshToken?: () => string | null;\n protected setAuthToken?: (token: string) => void;\n protected setRefreshToken?: (token: string) => void;\n protected clearAuthTokens?: () => void;\n protected refreshTokenUrl?: string;\n private isRefreshing = false;\n private failedQueue: Array<{\n resolve: (value?: any) => void;\n reject: (error?: any) => void;\n }> = [];\n\n constructor(config: BaseApiClientConfig = {}) {\n const defaultHeaders = {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n ...config.headers,\n };\n\n this.axiosInstance = axios.create({\n baseURL: config.baseURL || '',\n timeout: config.timeout || 30000,\n headers: defaultHeaders,\n });\n\n this.getAuthToken = config.getAuthToken;\n this.getRefreshToken = config.getRefreshToken;\n this.setAuthToken = config.setAuthToken;\n this.setRefreshToken = config.setRefreshToken;\n this.clearAuthTokens = config.clearAuthTokens;\n this.refreshTokenUrl = config.refreshTokenUrl || `${config.baseURL || ''}/v1/auth/refresh`;\n\n // Set up request interceptor for authentication\n this.axiosInstance.interceptors.request.use(\n (requestConfig) => {\n if (this.getAuthToken) {\n const token = this.getAuthToken();\n if (token) {\n requestConfig.headers.Authorization = `Bearer ${token}`;\n }\n }\n return requestConfig;\n },\n (error) => {\n return Promise.reject(error);\n }\n );\n\n // Set up response interceptor for error handling and token refresh\n this.axiosInstance.interceptors.response.use(\n (response) => response,\n async (error) => {\n const originalRequest = error.config;\n\n // Handle 401 errors with token refresh\n if (error.response?.status === 401 && !originalRequest._retry && this.getRefreshToken) {\n if (this.isRefreshing) {\n // If we're already refreshing, queue this request\n return new Promise((resolve, reject) => {\n this.failedQueue.push({ resolve, reject });\n })\n .then((token) => {\n originalRequest.headers.Authorization = `Bearer ${token}`;\n return this.axiosInstance(originalRequest);\n })\n .catch((err) => {\n return Promise.reject(err);\n });\n }\n\n originalRequest._retry = true;\n this.isRefreshing = true;\n\n try {\n const accessToken = await this.refreshAuthToken();\n\n // Update the original request with new token\n originalRequest.headers.Authorization = `Bearer ${accessToken}`;\n\n // Process queued requests\n this.processQueue(null, accessToken);\n this.isRefreshing = false;\n\n // Retry the original request\n return this.axiosInstance(originalRequest);\n } catch (refreshError) {\n // Refresh token is invalid, clear all tokens\n this.processQueue(refreshError, null);\n this.isRefreshing = false;\n return Promise.reject(this.handleError(refreshError));\n }\n }\n\n return Promise.reject(this.handleError(error));\n }\n );\n }\n\n /**\n * Process queued requests after token refresh\n */\n private processQueue(error: any, token: string | null = null) {\n this.failedQueue.forEach(({ resolve, reject }) => {\n if (error) {\n reject(error);\n } else {\n resolve(token);\n }\n });\n this.failedQueue = [];\n }\n\n /**\n * Refresh authentication token\n */\n private async refreshAuthToken(): Promise<string> {\n if (!this.getRefreshToken) {\n throw new Error('No refresh token function provided');\n }\n\n const refreshToken = this.getRefreshToken();\n if (!refreshToken) {\n if (this.clearAuthTokens) {\n this.clearAuthTokens();\n }\n throw new Error('No refresh token available');\n }\n\n try {\n // Use a fresh axios instance for refresh token request to avoid circular interceptor issues\n const response = await axios.post(this.refreshTokenUrl!, {\n refresh_token: refreshToken,\n }, {\n baseURL: '', // Use absolute URL or empty to avoid baseURL duplication\n });\n\n const { access_token, refresh_token } = response.data;\n \n if (this.setAuthToken) {\n this.setAuthToken(access_token);\n }\n if (refresh_token && this.setRefreshToken) {\n this.setRefreshToken(refresh_token);\n }\n\n return access_token;\n } catch (error) {\n // Refresh token is invalid, clear all tokens\n if (this.clearAuthTokens) {\n this.clearAuthTokens();\n }\n throw error;\n }\n }\n\n /**\n * Handle request errors\n */\n protected handleError(error: any): Error {\n if (error.response) {\n // Server responded with error status\n const message =\n error.response.data?.message ||\n error.response.data?.error ||\n error.message ||\n 'An unknown error occurred';\n return new Error(message);\n } else if (error.request) {\n // Request was made but no response received\n return new Error('Network error: No response from server');\n } else {\n // Something else happened\n return error instanceof Error ? error : new Error(String(error));\n }\n }\n\n /**\n * Make HTTP GET request\n * Returns unwrapped server response data\n */\n async get<T = any>(url: string, config?: RequestConfig): Promise<T> {\n const axiosConfig: AxiosRequestConfig = {\n params: config?.params,\n headers: config?.headers,\n ...config,\n };\n delete axiosConfig.params;\n delete axiosConfig.headers;\n\n const response = await this.axiosInstance.get<T>(url, {\n ...axiosConfig,\n params: config?.params,\n headers: config?.headers,\n });\n \n return response.data;\n }\n\n /**\n * Make HTTP POST request\n * Returns unwrapped server response data\n */\n async post<T = any>(\n url: string,\n data?: any,\n config?: RequestConfig\n ): Promise<T> {\n const axiosConfig: AxiosRequestConfig = {\n headers: config?.headers,\n ...config,\n };\n delete axiosConfig.headers;\n\n const response = await this.axiosInstance.post<T>(url, data, {\n ...axiosConfig,\n headers: config?.headers,\n });\n \n return response.data;\n }\n\n /**\n * Make HTTP PATCH request\n * Returns unwrapped server response data\n */\n async patch<T = any>(\n url: string,\n data?: any,\n config?: RequestConfig\n ): Promise<T> {\n const axiosConfig: AxiosRequestConfig = {\n headers: config?.headers,\n ...config,\n };\n delete axiosConfig.headers;\n\n const response = await this.axiosInstance.patch<T>(url, data, {\n ...axiosConfig,\n headers: config?.headers,\n });\n \n return response.data;\n }\n\n /**\n * Make HTTP PUT request\n * Returns unwrapped server response data\n */\n async put<T = any>(\n url: string,\n data?: any,\n config?: RequestConfig\n ): Promise<T> {\n const axiosConfig: AxiosRequestConfig = {\n headers: config?.headers,\n ...config,\n };\n delete axiosConfig.headers;\n\n const response = await this.axiosInstance.put<T>(url, data, {\n ...axiosConfig,\n headers: config?.headers,\n });\n \n return response.data;\n }\n\n /**\n * Make HTTP DELETE request\n * Returns unwrapped server response data\n */\n async delete<T = any>(url: string, config?: RequestConfig): Promise<T> {\n const axiosConfig: AxiosRequestConfig = {\n headers: config?.headers,\n ...config,\n };\n delete axiosConfig.headers;\n\n const response = await this.axiosInstance.delete<T>(url, {\n ...axiosConfig,\n headers: config?.headers,\n });\n \n return response.data;\n }\n\n /**\n * Get the underlying axios instance for advanced usage\n */\n get axios(): AxiosInstance {\n return this.axiosInstance;\n }\n\n /**\n * Create a new client instance with a different baseURL\n * Useful for server-side requests or different API endpoints\n */\n withBaseURL(baseURL: string): this {\n const ConfigClass = this.constructor as new (config: BaseApiClientConfig) => this;\n \n // Create a fresh copy of headers to avoid reference issues\n const headers = { ...this.axiosInstance.defaults.headers } as Record<string, string>;\n \n return new ConfigClass({\n baseURL,\n getAuthToken: this.getAuthToken,\n getRefreshToken: this.getRefreshToken,\n setAuthToken: this.setAuthToken,\n setRefreshToken: this.setRefreshToken,\n clearAuthTokens: this.clearAuthTokens,\n refreshTokenUrl: this.refreshTokenUrl,\n timeout: this.axiosInstance.defaults.timeout,\n headers,\n });\n }\n}\n","/**\n * @softimist/api - React Hooks for API interactions\n *\n * Generic hooks that work with any HttpClient implementation\n */\n'use client';\n\nimport { useQuery, useMutation, type QueryKey, type UseQueryOptions } from '@tanstack/react-query';\nimport { createParser, parseAsInteger, parseAsString, useQueryState } from 'nuqs';\nimport { useState } from 'react';\nimport { toast } from 'sonner';\nimport type {\n\tHttpClient,\n\tPaginationOptions,\n\tServerPaginatedResponse,\n\tServerResponse,\n\tUsePaginatedApiReturn,\n} from './types';\nimport type { Filter } from './filter';\nimport { getQueryClient } from './tanstack-query';\n\n/**\n * Hook for making simple API GET requests\n */\nexport function useApi<T>(\n\thttpClient: HttpClient,\n\turl: string | null,\n\toptions: Omit<UseQueryOptions<ServerResponse<T>>, 'queryKey' | 'queryFn' | 'enabled'> & {\n\t\tqueryKey?: QueryKey;\n\t\tenabled?: boolean;\n\t} = {},\n) {\n\tconst { queryKey, enabled = true, ...restOptions } = options;\n\n\treturn useQuery<ServerResponse<T>>({\n\t\tqueryKey: queryKey || [url],\n\t\tqueryFn: async () => {\n\t\t\tif (!url) throw new Error('URL is required');\n\t\t\treturn httpClient.get<ServerResponse<T>>(url);\n\t\t},\n\t\tenabled: url !== null && enabled,\n\t\t...restOptions,\n\t});\n}\n\n/**\n * Hook for making API mutations (POST, PATCH, PUT, DELETE) with toast notifications\n */\nexport function useApiAction<T>(\n\thttpClient: HttpClient,\n\toptions: {\n\t\taction: () => Promise<ServerResponse<T>>;\n\t\tshowToast?: boolean;\n\t\tinvalidateQueries?: QueryKey[];\n\t\tonSuccess?: (data: T) => void;\n\t\tonError?: (error: Error) => void;\n\t},\n) {\n\tconst { action, showToast = true, invalidateQueries, onSuccess, onError } = options;\n\tconst [isExecuted, setIsExecuted] = useState(false);\n\n\tconst mutation = useMutation({\n\t\tmutationFn: action,\n\n\t\tonSuccess: (res) => {\n\t\t\tonSuccess?.(res.data);\n\t\t\tsetIsExecuted(true);\n\t\t\tif (showToast) {\n\t\t\t\ttoast.success(res.message || 'Success');\n\t\t\t}\n\t\t\tif (invalidateQueries && invalidateQueries.length > 0) {\n\t\t\t\tconst queryClient = getQueryClient();\n\t\t\t\tinvalidateQueries.forEach((queryKey) => {\n\t\t\t\t\tqueryClient.invalidateQueries({ queryKey });\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tonError: (err: Error) => {\n\t\t\tonError?.(err);\n\t\t\tif (showToast) {\n\t\t\t\ttoast.error(err.message || 'An error occurred');\n\t\t\t}\n\t\t},\n\t});\n\n\tconst execute = () => {\n\t\tmutation.mutate();\n\t};\n\n\treturn {\n\t\tisLoading: mutation.isPending,\n\t\terror: mutation.error ?? undefined,\n\t\tdata: mutation.data?.data,\n\t\texecute,\n\t\tisExecuted,\n\t};\n}\n\nconst parseAsFilterArray = createParser<Filter[]>({\n\tparse: (value) => {\n\t\treturn JSON.parse(value);\n\t},\n\tserialize: (value) => {\n\t\treturn JSON.stringify(value);\n\t},\n});\n\nfunction usePaginationState(options: PaginationOptions) {\n\tconst { persist = true, params: defaultParams } = options;\n\tconst defaultPerPage = (defaultParams?.per_page as number) || 20;\n\tconst defaultPage = (defaultParams?.page as number) || 1;\n\tconst [perPageQuery, setPerPageQuery] = useQueryState(\n\t\t'per_page',\n\t\tparseAsInteger.withDefault(defaultPerPage),\n\t);\n\tconst [pageQuery, setPageQuery] = useQueryState('page', parseAsInteger.withDefault(defaultPage));\n\tconst [searchQuery, setSearchQuery] = useQueryState('search', parseAsString.withDefault(''));\n\tconst [filtersQuery, setFiltersQuery] = useQueryState(\n\t\t'filters',\n\t\tparseAsFilterArray.withDefault([]),\n\t);\n\tconst [sortQuery, setSortQuery] = useQueryState('sort', parseAsString.withDefault(''));\n\n\tconst [perPageState, setPerPageState] = useState<number>(defaultPerPage);\n\tconst [pageState, setPageState] = useState<number>(defaultPage);\n\tconst [searchState, setSearchState] = useState<string>('');\n\tconst [filtersState, setFiltersState] = useState<Filter[]>([]);\n\tconst [sortState, setSortState] = useState<string>();\n\n\tconst perPage = persist ? perPageQuery : perPageState;\n\tconst page = persist ? pageQuery : pageState;\n\tconst search = persist ? searchQuery : searchState;\n\tconst filters = persist ? filtersQuery : filtersState;\n\tconst sort = persist ? sortQuery : sortState;\n\n\tconst setPerPage = (value: number | ((prev: number) => number)) => {\n\t\tif (persist) {\n\t\t\tsetPerPageQuery(value);\n\t\t} else {\n\t\t\tsetPerPageState(value);\n\t\t}\n\t};\n\n\tconst setPage = (value: number | ((prev: number) => number)) => {\n\t\tif (persist) {\n\t\t\tsetPageQuery(value);\n\t\t} else {\n\t\t\tsetPageState(value);\n\t\t}\n\t};\n\n\tconst setSearch = (value: string | ((prev: string) => string)) => {\n\t\tif (persist) {\n\t\t\tsetSearchQuery(value);\n\t\t} else {\n\t\t\tsetSearchState(value);\n\t\t}\n\t};\n\n\tconst setFilters = (value: Filter[] | ((prev: Filter[]) => Filter[])) => {\n\t\tif (persist) {\n\t\t\tsetFiltersQuery(value);\n\t\t} else {\n\t\t\tsetFiltersState(value);\n\t\t}\n\t};\n\n\tconst setSort = (value: string | ((prev: string) => string)) => {\n\t\tif (persist) {\n\t\t\tsetSortQuery(value);\n\t\t} else {\n\t\t\tsetSortState(typeof value === 'function' ? value(sortState || '') : value);\n\t\t}\n\t};\n\n\tconst getParams = () => {\n\t\tconst params = new URLSearchParams();\n\t\tif (defaultParams) {\n\t\t\tObject.entries(defaultParams).forEach(([key, value]) => {\n\t\t\t\tif (value !== undefined) {\n\t\t\t\t\tparams.set(key, value.toString());\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t\tif (perPage) params.set('per_page', perPage.toString());\n\t\tif (page) params.set('page', page.toString());\n\t\tif (search) params.set('search', search);\n\t\tif (sort) {\n\t\t\tconst [sortBy, sortDirection] = sort.split(':');\n\t\t\tif (sortBy) params.set('sort_by', sortBy);\n\t\t\tif (sortDirection) params.set('sort_dir', sortDirection);\n\t\t}\n\t\tfilters?.forEach((filter) => {\n\t\t\tif (Array.isArray(filter.value)) {\n\t\t\t\tfilter.value.forEach((v) => {\n\t\t\t\t\tparams.append(filter.key, v.toString());\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tparams.set(filter.key, filter.value.toString());\n\t\t\t}\n\t\t});\n\t\treturn params;\n\t};\n\n\treturn {\n\t\tperPage,\n\t\tpage,\n\t\tsearch,\n\t\tfilters,\n\t\tsort,\n\t\tsetPerPage,\n\t\tsetPage,\n\t\tsetSearch,\n\t\tsetFilters,\n\t\tsetSort,\n\t\tgetParams,\n\t};\n}\n\n/**\n * Hook for making paginated API requests with URL state management\n */\nexport function usePaginatedApi<T>(\n\thttpClient: HttpClient,\n\tpath: string | null,\n\toptions: PaginationOptions = {},\n): UsePaginatedApiReturn<T> {\n\tconst {\n\t\tperPage,\n\t\tpage,\n\t\tsearch,\n\t\tfilters,\n\t\tsort,\n\t\tsetPerPage,\n\t\tsetPage,\n\t\tsetSearch,\n\t\tsetFilters,\n\t\tsetSort,\n\t\tgetParams,\n\t} = usePaginationState(options);\n\n\tconst getUrl = (): string | null => {\n\t\tif (!path) return null;\n\t\tconst params = getParams();\n\t\treturn params.size === 0 ? path : `${path}?${params.toString()}`;\n\t};\n\n\tconst url = getUrl();\n\n\tconst { data, isLoading, error, refetch, isSuccess, isFetching } = useQuery({\n\t\tqueryKey: [path, options, page, perPage, search, filters, sort],\n\t\tqueryFn: async () => {\n\t\t\tif (!url) throw new Error('URL is required');\n\t\t\treturn httpClient.get<ServerPaginatedResponse<T>>(url);\n\t\t},\n\t\tenabled: !!url,\n\t});\n\n\tfunction getBulkActionUrl(\n\t\taction: string,\n\t\tadditionalFilters?: Record<string, string | string[]>,\n\t): string {\n\t\tconst params = getParams();\n\t\tparams.delete('page');\n\t\tparams.delete('per_page');\n\t\tObject.entries(additionalFilters || {}).forEach(([key, value]) => {\n\t\t\tif (value === undefined) {\n\t\t\t\tparams.delete(key);\n\t\t\t} else if (Array.isArray(value)) {\n\t\t\t\tvalue.forEach((v) => {\n\t\t\t\t\tparams.append(key, v);\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tparams.set(key, value);\n\t\t\t}\n\t\t});\n\t\tif (params.size === 0) return `${path}/${action}`;\n\t\treturn `${path}/${action}?${params.toString()}`;\n\t}\n\n\tfunction setFilter(key: string, value: any) {\n\t\tif (value === undefined) {\n\t\t\tremoveFilter(key);\n\t\t\treturn;\n\t\t}\n\t\tconst index = filters.findIndex((filter) => filter.key === key);\n\t\tif (index === -1) {\n\t\t\tsetFilters([...filters, { key, value }]);\n\t\t} else {\n\t\t\tsetFilters([...filters.slice(0, index), { key, value }, ...filters.slice(index + 1)]);\n\t\t}\n\t}\n\n\tfunction removeFilter(key: string) {\n\t\tsetFilters(filters.filter((filter) => filter.key !== key));\n\t}\n\n\tfunction getFilterValue(key: string) {\n\t\treturn filters.find((filter) => filter.key === key)?.value;\n\t}\n\n\tfunction resetFilters() {\n\t\tsetFilters([]);\n\t}\n\n\treturn {\n\t\tmessage: data?.message || '',\n\t\tdata: data?.data || [],\n\t\tpagination: data?.pagination,\n\t\tisLoading,\n\t\tisSuccess,\n\t\tisFetching,\n\t\terror: error as Error | null,\n\t\tmutate: refetch,\n\t\tpage,\n\t\tsetPage,\n\t\tperPage,\n\t\tsetPerPage,\n\t\tsearch,\n\t\tsetSearch,\n\t\tfilters,\n\t\tsetFilters,\n\t\tsetFilter,\n\t\tremoveFilter,\n\t\tgetFilterValue,\n\t\tresetFilters,\n\t\tgetBulkActionUrl,\n\t\tsort,\n\t\tsetSort,\n\t};\n}\n\n/**\n * Mock hook for testing/development\n * Returns a mock paginated response with the provided data\n */\nexport function useMockPaginatedApi<T>(data: T[]): UsePaginatedApiReturn<T> {\n\treturn {\n\t\tdata,\n\t\tpagination: {\n\t\t\ttotal: data.length,\n\t\t\tper_page: 10,\n\t\t\tcurrent_page: 1,\n\t\t\ttotal_pages: Math.ceil(data.length / 10),\n\t\t\tfrom: 1,\n\t\t\tto: data.length,\n\t\t},\n\t\tmessage: 'Data fetched successfully',\n\t\tisLoading: false,\n\t\tisSuccess: true,\n\t\tisFetching: false,\n\t\tmutate: () => {},\n\t\tpage: 1,\n\t\tsetPage: () => {},\n\t\tperPage: 10,\n\t\tsetPerPage: () => {},\n\t\tsearch: '',\n\t\tsetSearch: () => {},\n\t\tfilters: [],\n\t\tsetFilters: () => {},\n\t\tgetFilterValue: () => undefined,\n\t\tresetFilters: () => {},\n\t\tsetFilter: () => {},\n\t\tremoveFilter: () => {},\n\t\tsort: '',\n\t\tsetSort: () => {},\n\t\tgetBulkActionUrl: () => '',\n\t};\n}\n","'use client';\n\nimport { QueryClient, QueryClientProvider } from '@tanstack/react-query';\nimport { ReactNode } from 'react';\n\nexport function makeQueryClient() {\n return new QueryClient({\n defaultOptions: {\n queries: {\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n },\n },\n });\n}\n\nlet browserQueryClient: QueryClient | undefined = undefined;\n\nexport function getQueryClient() {\n if (typeof window === 'undefined') {\n return makeQueryClient();\n } else {\n if (!browserQueryClient) browserQueryClient = makeQueryClient();\n return browserQueryClient;\n }\n}\n\n\nexport function QueryProvider({ children }: { children: ReactNode }) {\n const queryClient = getQueryClient();\n\n return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;\n}\n","/**\n * @softimist/api - Utility Functions\n * \n * Common utility functions for API error and response handling\n */\n\ntype ApiErrorLike = {\n message?: unknown;\n response?: {\n data?: {\n message?: unknown;\n error?: unknown;\n };\n };\n};\n\n/**\n * Extract error message from various error types\n */\nexport function getErrorMessage(error: unknown): string {\n const err = error as ApiErrorLike | null | undefined;\n \n // Try to get message from API response\n const apiMessage = err?.response?.data?.message;\n if (typeof apiMessage === 'string' && apiMessage.trim() !== '') {\n return apiMessage;\n }\n\n // Try to get error from API response\n const apiError = err?.response?.data?.error;\n if (typeof apiError === 'string' && apiError.trim() !== '') {\n return apiError;\n }\n\n // Try to get message from error object\n const message = err?.message;\n if (typeof message === 'string' && message.trim() !== '') {\n return message;\n }\n\n // Try Error instance message\n if (error instanceof Error && error.message.trim() !== '') {\n return error.message;\n }\n\n return 'Something went wrong';\n}\n\n/**\n * Extract success message from API response\n */\nexport function getSuccessMessage(response: any, defaultMessage?: string): string {\n return response?.data?.message || defaultMessage || 'Success';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACUA,mBAAmE;AAwC5D,IAAM,gBAAN,MAA0C;AAAA,EACrC;AAAA,EACA;AAAA,EACA;A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,YAAY;AAAA,IACZ,QAAQ,MAAM;AAAA,IAAC;AAAA,IACf,MAAM;AAAA,IACN,SAAS,MAAM;AAAA,IAAC;AAAA,IAChB,SAAS;AAAA,IACT,YAAY,MAAM;AAAA,IAAC;AAAA,IACnB,QAAQ;AAAA,IACR,WAAW,MAAM;AAAA,IAAC;AAAA,IAClB,SAAS,CAAC;AAAA,IACV,YAAY,MAAM;AAAA,IAAC;AAAA,IACnB,gBAAgB,MAAM;AAAA,IACtB,cAAc,MAAM;AAAA,IAAC;AAAA,IACrB,WAAW,MAAM;AAAA,IAAC;AAAA,IAClB,cAAc,MAAM;AAAA,IAAC;AAAA,IACrB,MAAM;AAAA,IACN,SAAS,MAAM;AAAA,IAAC;AAAA,IAChB,kBAAkB,MAAM;AAAA,EACzB;AACD;;;AE7VO,SAAS,gBAAgB,OAAwB;AACtD,QAAM,MAAM;AAGZ,QAAM,aAAa,KAAK,UAAU,MAAM;AACxC,MAAI,OAAO,eAAe,YAAY,WAAW,KAAK,MAAM,IAAI;AAC9D,WAAO;AAAA,EACT;AAGA,QAAM,WAAW,KAAK,UAAU,MAAM;AACtC,MAAI,OAAO,aAAa,YAAY,SAAS,KAAK,MAAM,IAAI;AAC1D,WAAO;AAAA,EACT;AAGA,QAAM,UAAU,KAAK;AACrB,MAAI,OAAO,YAAY,YAAY,QAAQ,KAAK,MAAM,IAAI;AACxD,WAAO;AAAA,EACT;AAGA,MAAI,iBAAiB,SAAS,MAAM,QAAQ,KAAK,MAAM,IAAI;AACzD,WAAO,MAAM;AAAA,EACf;AAEA,SAAO;AACT;AAKO,SAAS,kBAAkB,UAAe,gBAAiC;AAChF,SAAO,UAAU,MAAM,WAAW,kBAAkB;AACtD;;;AJdA,IAAAC,sBAA2C;","names":["axios","import_react_query","import_react_query"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
3
|
+
import { UseQueryOptions, QueryKey, QueryClient } from '@tanstack/react-query';
|
|
4
|
+
export { useInfiniteQuery, useQuery } from '@tanstack/react-query';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
import { ReactNode } from 'react';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @softimist/api - Shared API Types and Interfaces
|
|
10
|
+
*
|
|
11
|
+
* Common types and interfaces for API clients, responses, and pagination
|
|
12
|
+
* that work across different frontend frameworks and API implementations.
|
|
13
|
+
*
|
|
14
|
+
* Type naming convention:
|
|
15
|
+
* - Server* = Types matching what the backend API actually returns
|
|
16
|
+
* - Use* = Types returned by React hooks (includes UI state)
|
|
17
|
+
* - Http* = Types for HTTP client abstraction
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Pagination metadata as returned by the backend API
|
|
21
|
+
*/
|
|
22
|
+
interface PaginationMeta {
|
|
23
|
+
total: number;
|
|
24
|
+
per_page: number;
|
|
25
|
+
current_page: number;
|
|
26
|
+
total_pages: number;
|
|
27
|
+
from: number;
|
|
28
|
+
to: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Standard API response from the backend server
|
|
32
|
+
* This matches the actual JSON structure returned by the API
|
|
33
|
+
*/
|
|
34
|
+
interface ServerResponse<T> {
|
|
35
|
+
data: T;
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Paginated API response from the backend server
|
|
40
|
+
*/
|
|
41
|
+
interface ServerPaginatedResponse<T> {
|
|
42
|
+
data: T[];
|
|
43
|
+
message: string;
|
|
44
|
+
pagination: PaginationMeta;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Response type returned by paginated API hooks (e.g., usePaginatedApi)
|
|
48
|
+
* Includes both data and UI state management functions
|
|
49
|
+
*/
|
|
50
|
+
interface UsePaginatedApiReturn<T> {
|
|
51
|
+
message: string;
|
|
52
|
+
data: T[];
|
|
53
|
+
pagination?: PaginationMeta;
|
|
54
|
+
isLoading: boolean;
|
|
55
|
+
isSuccess: boolean;
|
|
56
|
+
isFetching: boolean;
|
|
57
|
+
error?: Error | null;
|
|
58
|
+
mutate: () => void;
|
|
59
|
+
page: number;
|
|
60
|
+
setPage: (page: number) => void;
|
|
61
|
+
perPage: number;
|
|
62
|
+
setPerPage: (perPage: number) => void;
|
|
63
|
+
search: string;
|
|
64
|
+
setSearch: (search: string) => void;
|
|
65
|
+
filters: any[];
|
|
66
|
+
setFilters: (filters: any[]) => void;
|
|
67
|
+
setFilter: (key: string, value: any) => void;
|
|
68
|
+
removeFilter: (key: string) => void;
|
|
69
|
+
getFilterValue: (key: string) => any;
|
|
70
|
+
resetFilters: () => void;
|
|
71
|
+
getBulkActionUrl: (action: string, additionalFilters?: Record<string, string | string[]>) => string;
|
|
72
|
+
sort?: string;
|
|
73
|
+
setSort: (sort: string) => void;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Simple response wrapper for HTTP client abstraction
|
|
77
|
+
* Used internally by HttpClient interface
|
|
78
|
+
*/
|
|
79
|
+
interface HttpResponse<T> {
|
|
80
|
+
data: T;
|
|
81
|
+
message?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* HTTP Client interface for dependency injection
|
|
85
|
+
* Compatible with axios-like HTTP clients
|
|
86
|
+
* Returns unwrapped server response data (ServerResponse<T>)
|
|
87
|
+
*/
|
|
88
|
+
interface HttpClient {
|
|
89
|
+
get: <T = any>(url: string, config?: any) => Promise<T>;
|
|
90
|
+
post: <T = any>(url: string, data?: any, config?: any) => Promise<T>;
|
|
91
|
+
patch: <T = any>(url: string, data?: any, config?: any) => Promise<T>;
|
|
92
|
+
put: <T = any>(url: string, data?: any, config?: any) => Promise<T>;
|
|
93
|
+
delete: <T = any>(url: string, config?: any) => Promise<T>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Options for paginated API hooks
|
|
97
|
+
*/
|
|
98
|
+
interface PaginationOptions {
|
|
99
|
+
persist?: boolean;
|
|
100
|
+
params?: Record<string, number | boolean | string | string[]>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Function type for paginated API hooks
|
|
104
|
+
* Compatible with usePaginatedApi hooks from both repos
|
|
105
|
+
*/
|
|
106
|
+
type UsePaginatedApiHook = <T>(path: string | null, options?: PaginationOptions) => UsePaginatedApiReturn<T>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Filter types for API queries
|
|
110
|
+
*/
|
|
111
|
+
type SelectFilter = {
|
|
112
|
+
key: string;
|
|
113
|
+
value: string;
|
|
114
|
+
};
|
|
115
|
+
type MultiSelectFilter = {
|
|
116
|
+
key: string;
|
|
117
|
+
value: string[];
|
|
118
|
+
};
|
|
119
|
+
type DateFilter = {
|
|
120
|
+
key: string;
|
|
121
|
+
value: string;
|
|
122
|
+
};
|
|
123
|
+
type BooleanFilter = {
|
|
124
|
+
key: string;
|
|
125
|
+
value: boolean;
|
|
126
|
+
};
|
|
127
|
+
type NumberFilter = {
|
|
128
|
+
key: string;
|
|
129
|
+
value: number;
|
|
130
|
+
};
|
|
131
|
+
type RangeFilter = {
|
|
132
|
+
key: string;
|
|
133
|
+
value: [number, number];
|
|
134
|
+
};
|
|
135
|
+
type StringFilter = {
|
|
136
|
+
key: string;
|
|
137
|
+
value: string;
|
|
138
|
+
};
|
|
139
|
+
type Filter = SelectFilter | MultiSelectFilter | DateFilter | BooleanFilter | NumberFilter | RangeFilter | StringFilter;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @softimist/api - Base API Client
|
|
143
|
+
*
|
|
144
|
+
* Base class for API clients that can be extended by module-specific implementations.
|
|
145
|
+
* Uses axios for HTTP requests and provides common functionality like authentication,
|
|
146
|
+
* error handling, and request/response interceptors.
|
|
147
|
+
*
|
|
148
|
+
* All HTTP methods return unwrapped server response data directly (e.g., ServerResponse<T>).
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Configuration options for BaseApiClient
|
|
153
|
+
*/
|
|
154
|
+
interface BaseApiClientConfig {
|
|
155
|
+
baseURL?: string;
|
|
156
|
+
getAuthToken?: () => string | null;
|
|
157
|
+
getRefreshToken?: () => string | null;
|
|
158
|
+
setAuthToken?: (token: string) => void;
|
|
159
|
+
setRefreshToken?: (token: string) => void;
|
|
160
|
+
clearAuthTokens?: () => void;
|
|
161
|
+
refreshTokenUrl?: string;
|
|
162
|
+
timeout?: number;
|
|
163
|
+
headers?: Record<string, string>;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Request configuration passed to HTTP methods
|
|
167
|
+
*/
|
|
168
|
+
interface RequestConfig {
|
|
169
|
+
headers?: Record<string, string>;
|
|
170
|
+
params?: Record<string, any>;
|
|
171
|
+
[key: string]: any;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Base API Client class
|
|
175
|
+
* Provides common HTTP functionality using axios that can be extended by module-specific clients
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* class WebsiteApiClient extends BaseApiClient {
|
|
180
|
+
* async getPages(): Promise<ServerPaginatedResponse<Page>> {
|
|
181
|
+
* return this.get<ServerPaginatedResponse<Page>>('/api/v1/website/pages');
|
|
182
|
+
* }
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
declare class BaseApiClient implements HttpClient {
|
|
187
|
+
protected axiosInstance: AxiosInstance;
|
|
188
|
+
protected getAuthToken?: () => string | null;
|
|
189
|
+
protected getRefreshToken?: () => string | null;
|
|
190
|
+
protected setAuthToken?: (token: string) => void;
|
|
191
|
+
protected setRefreshToken?: (token: string) => void;
|
|
192
|
+
protected clearAuthTokens?: () => void;
|
|
193
|
+
protected refreshTokenUrl?: string;
|
|
194
|
+
private isRefreshing;
|
|
195
|
+
private failedQueue;
|
|
196
|
+
constructor(config?: BaseApiClientConfig);
|
|
197
|
+
/**
|
|
198
|
+
* Process queued requests after token refresh
|
|
199
|
+
*/
|
|
200
|
+
private processQueue;
|
|
201
|
+
/**
|
|
202
|
+
* Refresh authentication token
|
|
203
|
+
*/
|
|
204
|
+
private refreshAuthToken;
|
|
205
|
+
/**
|
|
206
|
+
* Handle request errors
|
|
207
|
+
*/
|
|
208
|
+
protected handleError(error: any): Error;
|
|
209
|
+
/**
|
|
210
|
+
* Make HTTP GET request
|
|
211
|
+
* Returns unwrapped server response data
|
|
212
|
+
*/
|
|
213
|
+
get<T = any>(url: string, config?: RequestConfig): Promise<T>;
|
|
214
|
+
/**
|
|
215
|
+
* Make HTTP POST request
|
|
216
|
+
* Returns unwrapped server response data
|
|
217
|
+
*/
|
|
218
|
+
post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<T>;
|
|
219
|
+
/**
|
|
220
|
+
* Make HTTP PATCH request
|
|
221
|
+
* Returns unwrapped server response data
|
|
222
|
+
*/
|
|
223
|
+
patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<T>;
|
|
224
|
+
/**
|
|
225
|
+
* Make HTTP PUT request
|
|
226
|
+
* Returns unwrapped server response data
|
|
227
|
+
*/
|
|
228
|
+
put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<T>;
|
|
229
|
+
/**
|
|
230
|
+
* Make HTTP DELETE request
|
|
231
|
+
* Returns unwrapped server response data
|
|
232
|
+
*/
|
|
233
|
+
delete<T = any>(url: string, config?: RequestConfig): Promise<T>;
|
|
234
|
+
/**
|
|
235
|
+
* Get the underlying axios instance for advanced usage
|
|
236
|
+
*/
|
|
237
|
+
get axios(): AxiosInstance;
|
|
238
|
+
/**
|
|
239
|
+
* Create a new client instance with a different baseURL
|
|
240
|
+
* Useful for server-side requests or different API endpoints
|
|
241
|
+
*/
|
|
242
|
+
withBaseURL(baseURL: string): this;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Hook for making simple API GET requests
|
|
247
|
+
*/
|
|
248
|
+
declare function useApi<T>(httpClient: HttpClient, url: string | null, options?: Omit<UseQueryOptions<ServerResponse<T>>, 'queryKey' | 'queryFn' | 'enabled'> & {
|
|
249
|
+
queryKey?: QueryKey;
|
|
250
|
+
enabled?: boolean;
|
|
251
|
+
}): _tanstack_react_query.UseQueryResult<ServerResponse<T>, Error>;
|
|
252
|
+
/**
|
|
253
|
+
* Hook for making API mutations (POST, PATCH, PUT, DELETE) with toast notifications
|
|
254
|
+
*/
|
|
255
|
+
declare function useApiAction<T>(httpClient: HttpClient, options: {
|
|
256
|
+
action: () => Promise<ServerResponse<T>>;
|
|
257
|
+
showToast?: boolean;
|
|
258
|
+
invalidateQueries?: QueryKey[];
|
|
259
|
+
onSuccess?: (data: T) => void;
|
|
260
|
+
onError?: (error: Error) => void;
|
|
261
|
+
}): {
|
|
262
|
+
isLoading: boolean;
|
|
263
|
+
error: Error | undefined;
|
|
264
|
+
data: T | undefined;
|
|
265
|
+
execute: () => void;
|
|
266
|
+
isExecuted: boolean;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Hook for making paginated API requests with URL state management
|
|
270
|
+
*/
|
|
271
|
+
declare function usePaginatedApi<T>(httpClient: HttpClient, path: string | null, options?: PaginationOptions): UsePaginatedApiReturn<T>;
|
|
272
|
+
/**
|
|
273
|
+
* Mock hook for testing/development
|
|
274
|
+
* Returns a mock paginated response with the provided data
|
|
275
|
+
*/
|
|
276
|
+
declare function useMockPaginatedApi<T>(data: T[]): UsePaginatedApiReturn<T>;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @softimist/api - Utility Functions
|
|
280
|
+
*
|
|
281
|
+
* Common utility functions for API error and response handling
|
|
282
|
+
*/
|
|
283
|
+
/**
|
|
284
|
+
* Extract error message from various error types
|
|
285
|
+
*/
|
|
286
|
+
declare function getErrorMessage(error: unknown): string;
|
|
287
|
+
/**
|
|
288
|
+
* Extract success message from API response
|
|
289
|
+
*/
|
|
290
|
+
declare function getSuccessMessage(response: any, defaultMessage?: string): string;
|
|
291
|
+
|
|
292
|
+
declare function makeQueryClient(): QueryClient;
|
|
293
|
+
declare function getQueryClient(): QueryClient;
|
|
294
|
+
declare function QueryProvider({ children }: {
|
|
295
|
+
children: ReactNode;
|
|
296
|
+
}): react_jsx_runtime.JSX.Element;
|
|
297
|
+
|
|
298
|
+
export { BaseApiClient, type BaseApiClientConfig, type BooleanFilter, type DateFilter, type Filter, type HttpClient, type HttpResponse, type MultiSelectFilter, type NumberFilter, type PaginationMeta, type PaginationOptions, QueryProvider, type RangeFilter, type RequestConfig, type SelectFilter, type ServerPaginatedResponse, type ServerResponse, type StringFilter, type UsePaginatedApiHook, type UsePaginatedApiReturn, getErrorMessage, getQueryClient, getSuccessMessage, makeQueryClient, useApi, useApiAction, useMockPaginatedApi, usePaginatedApi };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
3
|
+
import { UseQueryOptions, QueryKey, QueryClient } from '@tanstack/react-query';
|
|
4
|
+
export { useInfiniteQuery, useQuery } from '@tanstack/react-query';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
import { ReactNode } from 'react';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @softimist/api - Shared API Types and Interfaces
|
|
10
|
+
*
|
|
11
|
+
* Common types and interfaces for API clients, responses, and pagination
|
|
12
|
+
* that work across different frontend frameworks and API implementations.
|
|
13
|
+
*
|
|
14
|
+
* Type naming convention:
|
|
15
|
+
* - Server* = Types matching what the backend API actually returns
|
|
16
|
+
* - Use* = Types returned by React hooks (includes UI state)
|
|
17
|
+
* - Http* = Types for HTTP client abstraction
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Pagination metadata as returned by the backend API
|
|
21
|
+
*/
|
|
22
|
+
interface PaginationMeta {
|
|
23
|
+
total: number;
|
|
24
|
+
per_page: number;
|
|
25
|
+
current_page: number;
|
|
26
|
+
total_pages: number;
|
|
27
|
+
from: number;
|
|
28
|
+
to: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Standard API response from the backend server
|
|
32
|
+
* This matches the actual JSON structure returned by the API
|
|
33
|
+
*/
|
|
34
|
+
interface ServerResponse<T> {
|
|
35
|
+
data: T;
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Paginated API response from the backend server
|
|
40
|
+
*/
|
|
41
|
+
interface ServerPaginatedResponse<T> {
|
|
42
|
+
data: T[];
|
|
43
|
+
message: string;
|
|
44
|
+
pagination: PaginationMeta;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Response type returned by paginated API hooks (e.g., usePaginatedApi)
|
|
48
|
+
* Includes both data and UI state management functions
|
|
49
|
+
*/
|
|
50
|
+
interface UsePaginatedApiReturn<T> {
|
|
51
|
+
message: string;
|
|
52
|
+
data: T[];
|
|
53
|
+
pagination?: PaginationMeta;
|
|
54
|
+
isLoading: boolean;
|
|
55
|
+
isSuccess: boolean;
|
|
56
|
+
isFetching: boolean;
|
|
57
|
+
error?: Error | null;
|
|
58
|
+
mutate: () => void;
|
|
59
|
+
page: number;
|
|
60
|
+
setPage: (page: number) => void;
|
|
61
|
+
perPage: number;
|
|
62
|
+
setPerPage: (perPage: number) => void;
|
|
63
|
+
search: string;
|
|
64
|
+
setSearch: (search: string) => void;
|
|
65
|
+
filters: any[];
|
|
66
|
+
setFilters: (filters: any[]) => void;
|
|
67
|
+
setFilter: (key: string, value: any) => void;
|
|
68
|
+
removeFilter: (key: string) => void;
|
|
69
|
+
getFilterValue: (key: string) => any;
|
|
70
|
+
resetFilters: () => void;
|
|
71
|
+
getBulkActionUrl: (action: string, additionalFilters?: Record<string, string | string[]>) => string;
|
|
72
|
+
sort?: string;
|
|
73
|
+
setSort: (sort: string) => void;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Simple response wrapper for HTTP client abstraction
|
|
77
|
+
* Used internally by HttpClient interface
|
|
78
|
+
*/
|
|
79
|
+
interface HttpResponse<T> {
|
|
80
|
+
data: T;
|
|
81
|
+
message?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* HTTP Client interface for dependency injection
|
|
85
|
+
* Compatible with axios-like HTTP clients
|
|
86
|
+
* Returns unwrapped server response data (ServerResponse<T>)
|
|
87
|
+
*/
|
|
88
|
+
interface HttpClient {
|
|
89
|
+
get: <T = any>(url: string, config?: any) => Promise<T>;
|
|
90
|
+
post: <T = any>(url: string, data?: any, config?: any) => Promise<T>;
|
|
91
|
+
patch: <T = any>(url: string, data?: any, config?: any) => Promise<T>;
|
|
92
|
+
put: <T = any>(url: string, data?: any, config?: any) => Promise<T>;
|
|
93
|
+
delete: <T = any>(url: string, config?: any) => Promise<T>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Options for paginated API hooks
|
|
97
|
+
*/
|
|
98
|
+
interface PaginationOptions {
|
|
99
|
+
persist?: boolean;
|
|
100
|
+
params?: Record<string, number | boolean | string | string[]>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Function type for paginated API hooks
|
|
104
|
+
* Compatible with usePaginatedApi hooks from both repos
|
|
105
|
+
*/
|
|
106
|
+
type UsePaginatedApiHook = <T>(path: string | null, options?: PaginationOptions) => UsePaginatedApiReturn<T>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Filter types for API queries
|
|
110
|
+
*/
|
|
111
|
+
type SelectFilter = {
|
|
112
|
+
key: string;
|
|
113
|
+
value: string;
|
|
114
|
+
};
|
|
115
|
+
type MultiSelectFilter = {
|
|
116
|
+
key: string;
|
|
117
|
+
value: string[];
|
|
118
|
+
};
|
|
119
|
+
type DateFilter = {
|
|
120
|
+
key: string;
|
|
121
|
+
value: string;
|
|
122
|
+
};
|
|
123
|
+
type BooleanFilter = {
|
|
124
|
+
key: string;
|
|
125
|
+
value: boolean;
|
|
126
|
+
};
|
|
127
|
+
type NumberFilter = {
|
|
128
|
+
key: string;
|
|
129
|
+
value: number;
|
|
130
|
+
};
|
|
131
|
+
type RangeFilter = {
|
|
132
|
+
key: string;
|
|
133
|
+
value: [number, number];
|
|
134
|
+
};
|
|
135
|
+
type StringFilter = {
|
|
136
|
+
key: string;
|
|
137
|
+
value: string;
|
|
138
|
+
};
|
|
139
|
+
type Filter = SelectFilter | MultiSelectFilter | DateFilter | BooleanFilter | NumberFilter | RangeFilter | StringFilter;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @softimist/api - Base API Client
|
|
143
|
+
*
|
|
144
|
+
* Base class for API clients that can be extended by module-specific implementations.
|
|
145
|
+
* Uses axios for HTTP requests and provides common functionality like authentication,
|
|
146
|
+
* error handling, and request/response interceptors.
|
|
147
|
+
*
|
|
148
|
+
* All HTTP methods return unwrapped server response data directly (e.g., ServerResponse<T>).
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Configuration options for BaseApiClient
|
|
153
|
+
*/
|
|
154
|
+
interface BaseApiClientConfig {
|
|
155
|
+
baseURL?: string;
|
|
156
|
+
getAuthToken?: () => string | null;
|
|
157
|
+
getRefreshToken?: () => string | null;
|
|
158
|
+
setAuthToken?: (token: string) => void;
|
|
159
|
+
setRefreshToken?: (token: string) => void;
|
|
160
|
+
clearAuthTokens?: () => void;
|
|
161
|
+
refreshTokenUrl?: string;
|
|
162
|
+
timeout?: number;
|
|
163
|
+
headers?: Record<string, string>;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Request configuration passed to HTTP methods
|
|
167
|
+
*/
|
|
168
|
+
interface RequestConfig {
|
|
169
|
+
headers?: Record<string, string>;
|
|
170
|
+
params?: Record<string, any>;
|
|
171
|
+
[key: string]: any;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Base API Client class
|
|
175
|
+
* Provides common HTTP functionality using axios that can be extended by module-specific clients
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* class WebsiteApiClient extends BaseApiClient {
|
|
180
|
+
* async getPages(): Promise<ServerPaginatedResponse<Page>> {
|
|
181
|
+
* return this.get<ServerPaginatedResponse<Page>>('/api/v1/website/pages');
|
|
182
|
+
* }
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
declare class BaseApiClient implements HttpClient {
|
|
187
|
+
protected axiosInstance: AxiosInstance;
|
|
188
|
+
protected getAuthToken?: () => string | null;
|
|
189
|
+
protected getRefreshToken?: () => string | null;
|
|
190
|
+
protected setAuthToken?: (token: string) => void;
|
|
191
|
+
protected setRefreshToken?: (token: string) => void;
|
|
192
|
+
protected clearAuthTokens?: () => void;
|
|
193
|
+
protected refreshTokenUrl?: string;
|
|
194
|
+
private isRefreshing;
|
|
195
|
+
private failedQueue;
|
|
196
|
+
constructor(config?: BaseApiClientConfig);
|
|
197
|
+
/**
|
|
198
|
+
* Process queued requests after token refresh
|
|
199
|
+
*/
|
|
200
|
+
private processQueue;
|
|
201
|
+
/**
|
|
202
|
+
* Refresh authentication token
|
|
203
|
+
*/
|
|
204
|
+
private refreshAuthToken;
|
|
205
|
+
/**
|
|
206
|
+
* Handle request errors
|
|
207
|
+
*/
|
|
208
|
+
protected handleError(error: any): Error;
|
|
209
|
+
/**
|
|
210
|
+
* Make HTTP GET request
|
|
211
|
+
* Returns unwrapped server response data
|
|
212
|
+
*/
|
|
213
|
+
get<T = any>(url: string, config?: RequestConfig): Promise<T>;
|
|
214
|
+
/**
|
|
215
|
+
* Make HTTP POST request
|
|
216
|
+
* Returns unwrapped server response data
|
|
217
|
+
*/
|
|
218
|
+
post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<T>;
|
|
219
|
+
/**
|
|
220
|
+
* Make HTTP PATCH request
|
|
221
|
+
* Returns unwrapped server response data
|
|
222
|
+
*/
|
|
223
|
+
patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<T>;
|
|
224
|
+
/**
|
|
225
|
+
* Make HTTP PUT request
|
|
226
|
+
* Returns unwrapped server response data
|
|
227
|
+
*/
|
|
228
|
+
put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<T>;
|
|
229
|
+
/**
|
|
230
|
+
* Make HTTP DELETE request
|
|
231
|
+
* Returns unwrapped server response data
|
|
232
|
+
*/
|
|
233
|
+
delete<T = any>(url: string, config?: RequestConfig): Promise<T>;
|
|
234
|
+
/**
|
|
235
|
+
* Get the underlying axios instance for advanced usage
|
|
236
|
+
*/
|
|
237
|
+
get axios(): AxiosInstance;
|
|
238
|
+
/**
|
|
239
|
+
* Create a new client instance with a different baseURL
|
|
240
|
+
* Useful for server-side requests or different API endpoints
|
|
241
|
+
*/
|
|
242
|
+
withBaseURL(baseURL: string): this;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Hook for making simple API GET requests
|
|
247
|
+
*/
|
|
248
|
+
declare function useApi<T>(httpClient: HttpClient, url: string | null, options?: Omit<UseQueryOptions<ServerResponse<T>>, 'queryKey' | 'queryFn' | 'enabled'> & {
|
|
249
|
+
queryKey?: QueryKey;
|
|
250
|
+
enabled?: boolean;
|
|
251
|
+
}): _tanstack_react_query.UseQueryResult<ServerResponse<T>, Error>;
|
|
252
|
+
/**
|
|
253
|
+
* Hook for making API mutations (POST, PATCH, PUT, DELETE) with toast notifications
|
|
254
|
+
*/
|
|
255
|
+
declare function useApiAction<T>(httpClient: HttpClient, options: {
|
|
256
|
+
action: () => Promise<ServerResponse<T>>;
|
|
257
|
+
showToast?: boolean;
|
|
258
|
+
invalidateQueries?: QueryKey[];
|
|
259
|
+
onSuccess?: (data: T) => void;
|
|
260
|
+
onError?: (error: Error) => void;
|
|
261
|
+
}): {
|
|
262
|
+
isLoading: boolean;
|
|
263
|
+
error: Error | undefined;
|
|
264
|
+
data: T | undefined;
|
|
265
|
+
execute: () => void;
|
|
266
|
+
isExecuted: boolean;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Hook for making paginated API requests with URL state management
|
|
270
|
+
*/
|
|
271
|
+
declare function usePaginatedApi<T>(httpClient: HttpClient, path: string | null, options?: PaginationOptions): UsePaginatedApiReturn<T>;
|
|
272
|
+
/**
|
|
273
|
+
* Mock hook for testing/development
|
|
274
|
+
* Returns a mock paginated response with the provided data
|
|
275
|
+
*/
|
|
276
|
+
declare function useMockPaginatedApi<T>(data: T[]): UsePaginatedApiReturn<T>;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @softimist/api - Utility Functions
|
|
280
|
+
*
|
|
281
|
+
* Common utility functions for API error and response handling
|
|
282
|
+
*/
|
|
283
|
+
/**
|
|
284
|
+
* Extract error message from various error types
|
|
285
|
+
*/
|
|
286
|
+
declare function getErrorMessage(error: unknown): string;
|
|
287
|
+
/**
|
|
288
|
+
* Extract success message from API response
|
|
289
|
+
*/
|
|
290
|
+
declare function getSuccessMessage(response: any, defaultMessage?: string): string;
|
|
291
|
+
|
|
292
|
+
declare function makeQueryClient(): QueryClient;
|
|
293
|
+
declare function getQueryClient(): QueryClient;
|
|
294
|
+
declare function QueryProvider({ children }: {
|
|
295
|
+
children: ReactNode;
|
|
296
|
+
}): react_jsx_runtime.JSX.Element;
|
|
297
|
+
|
|
298
|
+
export { BaseApiClient, type BaseApiClientConfig, type BooleanFilter, type DateFilter, type Filter, type HttpClient, type HttpResponse, type MultiSelectFilter, type NumberFilter, type PaginationMeta, type PaginationOptions, QueryProvider, type RangeFilter, type RequestConfig, type SelectFilter, type ServerPaginatedResponse, type ServerResponse, type StringFilter, type UsePaginatedApiHook, type UsePaginatedApiReturn, getErrorMessage, getQueryClient, getSuccessMessage, makeQueryClient, useApi, useApiAction, useMockPaginatedApi, usePaginatedApi };
|