kmod-cli 1.4.7 → 1.4.9

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.
@@ -92,7 +92,7 @@ export interface DeleteManyParams {
92
92
  }
93
93
 
94
94
  export interface CustomParams {
95
- url: string;
95
+ url?: string;
96
96
  method?: 'get' | 'post' | 'put' | 'patch' | 'delete';
97
97
  payload?: any;
98
98
  query?: Record<string, any>;
@@ -160,6 +160,8 @@ export interface UseMutationOptions<TData = any> {
160
160
  onError?: (error: DataProviderError) => void;
161
161
  }
162
162
 
163
+ export type Payload = any;
164
+
163
165
  // ============ UTILITY FUNCTIONS ============
164
166
 
165
167
  /**
@@ -215,16 +217,23 @@ class DataProvider {
215
217
  private options: Required<DataProviderOptions>;
216
218
 
217
219
  constructor(
218
- apiUrl: string,
219
- httpClient: AxiosInstance = axios,
220
+ httpClient: AxiosInstance = axios.create(),
220
221
  options: DataProviderOptions = {}
221
222
  ) {
222
- this.apiUrl = apiUrl.endsWith('/') ? apiUrl.slice(0, -1) : apiUrl;
223
223
  this.httpClient = httpClient;
224
+
225
+ // Lấy baseURL từ httpClient
226
+ const baseURL = httpClient.defaults.baseURL || '';
227
+ this.apiUrl = baseURL.endsWith('/') ? baseURL.slice(0, -1) : baseURL;
228
+
229
+ if (!this.apiUrl) {
230
+ console.warn('[DataProvider] No baseURL found in httpClient. Please set baseURL when creating httpClient.');
231
+ }
232
+
224
233
  this.cache = new Map();
225
234
  this.options = {
226
235
  cacheTime: 5 * 60 * 1000,
227
- retryCount: 3,
236
+ retryCount: 1,
228
237
  retryDelay: 1000,
229
238
  debug: false,
230
239
  ...options
@@ -646,15 +655,17 @@ class DataProvider {
646
655
  }
647
656
 
648
657
  async custom<T = any>(params: CustomParams): Promise<CustomResponse<T>> {
649
- const { url, method = 'get', payload, query, headers } = params;
650
-
658
+ const { url , method = 'get', payload, query, headers } = params;
659
+
660
+ if (!url) throw this.handleError("No url provided");
651
661
  try {
652
662
  return await this.retryRequest(async () => {
653
- const fullUrl = url.startsWith('http') ? url : `${this.apiUrl}${url}`;
654
- this.log(`${method.toUpperCase()} ${fullUrl}`, { payload, query });
655
-
663
+ // if url is not absolute, assume it's a relative path
664
+ const requestUrl = url.startsWith('http') ? url : url;
665
+ this.log(`${method.toUpperCase()} ${requestUrl}`, { payload, query });
666
+
656
667
  const response = await this.httpClient<T>({
657
- url: fullUrl,
668
+ url: requestUrl,
658
669
  method,
659
670
  data: payload,
660
671
  params: query,
@@ -793,7 +804,7 @@ export function useOne<T = any>(
793
804
 
794
805
  export function useCreate<T = any, V = any>(
795
806
  resource: string,
796
- options: UseMutationOptions<T> = {}
807
+ options: UseMutationOptions<T>
797
808
  ) {
798
809
  const [loading, setLoading] = useState<boolean>(false);
799
810
  const [error, setError] = useState<DataProviderError | null>(null);
@@ -807,7 +818,9 @@ export function useCreate<T = any, V = any>(
807
818
  setError(null);
808
819
 
809
820
  try {
810
- const result = await dataProvider.create<T, V>(resource, { variables });
821
+ const result = await dataProvider.create<T, V>(resource, {
822
+ variables
823
+ });
811
824
  if (onSuccess) {
812
825
  onSuccess(result.data);
813
826
  }
@@ -829,7 +842,7 @@ export function useCreate<T = any, V = any>(
829
842
 
830
843
  export function useUpdate<T = any, V = any>(
831
844
  resource: string,
832
- options: UseMutationOptions<T> = {}
845
+ options: UseMutationOptions<T>
833
846
  ) {
834
847
  const [loading, setLoading] = useState<boolean>(false);
835
848
  const [error, setError] = useState<DataProviderError | null>(null);
@@ -868,7 +881,7 @@ export function useUpdate<T = any, V = any>(
868
881
 
869
882
  export function useDelete<T = any>(
870
883
  resource: string,
871
- options: UseMutationOptions<T> = {}
884
+ options: UseMutationOptions<T>
872
885
  ) {
873
886
  const [loading, setLoading] = useState<boolean>(false);
874
887
  const [error, setError] = useState<DataProviderError | null>(null);
@@ -907,23 +920,29 @@ export function useDelete<T = any>(
907
920
 
908
921
  export function useCustom<T = any>(
909
922
  resource: string,
910
- options: UseMutationOptions<T> = {}
923
+ options: UseMutationOptions<T> & CustomParams
911
924
  ) {
912
925
  const [loading, setLoading] = useState<boolean>(false);
913
926
  const [error, setError] = useState<DataProviderError | null>(null);
914
927
  const [customData, setCustomData] = useState<T | null>(null);
915
928
 
916
929
  const dataProvider = useDataProvider();
917
-
930
+
918
931
  const { onSuccess, onError } = options;
919
-
932
+
920
933
  const mutate = useCallback(
921
- async (variables: any): Promise<T> => {
934
+ async (variables?: Payload): Promise<T> => {
922
935
  setLoading(true);
923
936
  setError(null);
924
-
937
+
925
938
  try {
926
- const result = await dataProvider.custom<T>({ url: resource, ...variables });
939
+ const result = await dataProvider.custom<T>({
940
+ url: resource,
941
+ payload: variables || options.payload || {},
942
+ method: options.method,
943
+ headers: options.headers,
944
+ query: options.query,
945
+ });
927
946
  if (onSuccess) {
928
947
  onSuccess(result.data);
929
948
  }
@@ -939,10 +958,10 @@ export function useCustom<T = any>(
939
958
  } finally {
940
959
  setLoading(false);
941
960
  }
942
- },
961
+ },
943
962
  [dataProvider, resource, onSuccess, onError]
944
963
  );
945
-
964
+
946
965
  return { mutate, loading, error, data: customData };
947
966
  }
948
967
 
@@ -1188,7 +1207,7 @@ export const cookiesProvider = {
1188
1207
  // });
1189
1208
 
1190
1209
  // const { data, isLoading, error } = useCustom<DataResponse>({
1191
- // url: '/route_name',
1210
+ // url: '/route_name or api_url/route/...',
1192
1211
  // method: 'post',
1193
1212
  // payload: {},
1194
1213
  // });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kmod-cli",
3
- "version": "1.4.7",
3
+ "version": "1.4.9",
4
4
  "description": "Stack components utilities fast setup in projects",
5
5
  "author": "kumo_d",
6
6
  "license": "MIT",