Browse Source

refactor: http BaseModel

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 2 months ago
parent
commit
2b01a07f04

+ 64 - 55
client/src/http/BaseModel.ts

@@ -1,84 +1,93 @@
 import http from './Axios';
 import http from './Axios';
-import { Method } from 'axios';
+import { Method, AxiosRequestConfig } from 'axios';
 
 
-type findParamsType = {
+type RequestParams = {
   method?: Method;
   method?: Method;
   path: string;
   path: string;
-  params: { [x: string]: any };
-  timeout?: number;
-};
-
-type updateParamsType = {
-  path: string;
+  params?: Record<string, any>;
   data?: any;
   data?: any;
+  timeout?: number;
+  config?: AxiosRequestConfig;
 };
 };
 
 
 export default class BaseModel {
 export default class BaseModel {
-  static async findAll<T>(data: findParamsType) {
-    const { params = {}, path = '', method = 'get' } = data;
-    const type = method === 'post' ? 'data' : 'params';
-    const httpConfig = {
+  /**
+   * Fetch multiple items
+   */
+  static async find<T>(options: RequestParams): Promise<T> {
+    const { method = 'get', path, params = {}, config } = options;
+    const requestConfig: AxiosRequestConfig = {
       method,
       method,
       url: path,
       url: path,
-      [type]: { ...params },
+      ...config,
     };
     };
 
 
-    const res = await http(httpConfig);
-    let list = res.data.data || [];
-    if (!Array.isArray(list)) {
-      return list as T;
+    if (method.toLowerCase() === 'get') {
+      requestConfig.params = params;
+    } else {
+      requestConfig.data = params;
     }
     }
 
 
-    return Object.assign(list, {
-      _total: res.data.data.total_count || list.length,
-    } as T);
+    const response = await http(requestConfig);
+    return response.data?.data as T;
   }
   }
 
 
-  static async search<T>(data: findParamsType) {
-    const { method = 'get', params = {}, path = '', timeout } = data;
-    const httpConfig = {
-      method,
-      url: path,
-      params,
-    } as any;
-    if (timeout) httpConfig.timeout = timeout;
-    const res = await http(httpConfig);
-    return (res.data.data || {}) as T;
+  /**
+   * Search with flexible parameters
+   */
+  static async search<T>(options: RequestParams): Promise<T> {
+    return this.find<T>(options);
   }
   }
 
 
   /**
   /**
-   * Create instance in database
+   * Create a new resource
    */
    */
-  static async create<T>(options: updateParamsType) {
-    const { path, data } = options;
-    const res = await http.post(path, data);
-    return (res.data.data || {}) as T;
+  static async create<T>(options: RequestParams): Promise<T> {
+    const { path, data = {}, config } = options;
+    const response = await http.post(path, data, config);
+    return response.data?.data as T;
   }
   }
 
 
-  static async update<T>(options: updateParamsType) {
-    const { path, data } = options;
-    const res = await http.put(path, data);
-
-    return (res.data.data || {}) as T;
+  /**
+   * Update an existing resource
+   */
+  static async update<T>(options: RequestParams): Promise<T> {
+    const { path, data = {}, config } = options;
+    const response = await http.put(path, data, config);
+    return response.data?.data as T;
   }
   }
 
 
-  static async delete<T>(options: updateParamsType) {
-    const { path, data } = options;
-
-    const res = await http.delete(path, { data: data });
-
-    return res.data as T;
+  /**
+   * Delete a resource
+   */
+  static async delete<T>(options: RequestParams): Promise<T> {
+    const { path, params = {}, config } = options;
+    const response = await http.delete(path, { 
+      data: params,
+      ...config 
+    });
+    return response.data?.data as T;
   }
   }
 
 
-  static async batchDelete<T>(options: updateParamsType) {
-    const { path, data } = options;
-    const res = await http.post(path, data);
-    return res.data as T;
+  /**
+   * Batch delete resources
+   */
+  static async batchDelete<T>(options: RequestParams): Promise<T> {
+    return this.create<T>({
+      ...options,
+      method: 'post',
+      path: `${options.path}/batch-delete`,
+    });
   }
   }
 
 
-  static async query(options: updateParamsType) {
-    const { path, data } = options;
-    const res = await http.post(path, data);
-    return res.data.data;
+  /**
+   * Custom query
+   */
+  static async query<T>(options: RequestParams): Promise<T> {
+    return this.find<T>({
+      ...options,
+      params: options.data,
+      method: 'post',
+    });
   }
   }
-}
+}

+ 3 - 3
client/src/http/Collection.service.ts

@@ -22,11 +22,11 @@ export class CollectionService extends BaseModel {
   static getCollections(data?: {
   static getCollections(data?: {
     type: ShowCollectionsType;
     type: ShowCollectionsType;
   }): Promise<CollectionObject[]> {
   }): Promise<CollectionObject[]> {
-    return super.findAll({ path: `/collections`, params: data || {} });
+    return super.find({ path: `/collections`, params: data || {} });
   }
   }
 
 
   static getCollectionsNames(data?: { db_name: string }): Promise<string[]> {
   static getCollectionsNames(data?: { db_name: string }): Promise<string[]> {
-    return super.findAll({ path: `/collections/names`, params: data || {} });
+    return super.find({ path: `/collections/names`, params: data || {} });
   }
   }
 
 
   static describeCollectionUnformatted(collectionName: string) {
   static describeCollectionUnformatted(collectionName: string) {
@@ -121,7 +121,7 @@ export class CollectionService extends BaseModel {
   }
   }
 
 
   static async describeIndex(collectionName: string): Promise<IndexObject[]> {
   static async describeIndex(collectionName: string): Promise<IndexObject[]> {
-    const res = await super.findAll<DescribeIndexRes>({
+    const res = await super.find<DescribeIndexRes>({
       path: `/collections/index`,
       path: `/collections/index`,
       params: { collection_name: collectionName },
       params: { collection_name: collectionName },
     });
     });

+ 1 - 1
client/src/http/Partition.service.ts

@@ -10,7 +10,7 @@ export class PartitionService extends BaseModel {
   static getPartitions(collectionName: string) {
   static getPartitions(collectionName: string) {
     const path = `/partitions`;
     const path = `/partitions`;
 
 
-    return super.findAll<PartitionData[]>({
+    return super.find<PartitionData[]>({
       path,
       path,
       params: { collection_name: collectionName },
       params: { collection_name: collectionName },
     });
     });