listpage-next 0.0.2 → 0.0.4

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.
@@ -0,0 +1,3 @@
1
+ import { ClientOptions } from './types';
2
+ export declare function setupBaseUrl(HttpClient: typeof import('./index').HttpClient): void;
3
+ export declare function mergeServerConfig(server: ClientOptions['server']): ClientOptions['server'];
@@ -0,0 +1,16 @@
1
+ function setupBaseUrl(HttpClient) {
2
+ HttpClient.prototype.getBaseURL = function() {
3
+ const { server } = this.options;
4
+ return `${server?.protocol}//${server?.host}:${server?.port}${server?.publicPath}`;
5
+ };
6
+ }
7
+ function mergeServerConfig(server) {
8
+ if (server?.protocol && !server?.protocol?.endsWith(':')) server.protocol += ':';
9
+ return {
10
+ protocol: server?.protocol || location.protocol,
11
+ host: server?.host || location.hostname,
12
+ port: server?.port || location.port,
13
+ publicPath: server?.publicPath || '/api/v1'
14
+ };
15
+ }
16
+ export { mergeServerConfig, setupBaseUrl };
@@ -0,0 +1 @@
1
+ export declare function setupClient(HttpClient: typeof import('./index').HttpClient): void;
@@ -0,0 +1,107 @@
1
+ import axios from "axios";
2
+ import { notification } from "antd";
3
+ function setupClient(HttpClient) {
4
+ HttpClient.prototype.createAxiosInstance = function() {
5
+ const { config } = this.options;
6
+ const baseURL = config?.baseURL || this.getBaseURL();
7
+ const headers = config?.headers || this.getHeaders();
8
+ return axios.create({
9
+ ...config,
10
+ baseURL: baseURL,
11
+ headers: headers
12
+ });
13
+ };
14
+ HttpClient.prototype.setup = function() {
15
+ const successCodes = this.options.successCodes || [
16
+ 10000,
17
+ 200
18
+ ];
19
+ const apiClient = this.createAxiosInstance();
20
+ apiClient.interceptors.request.use((config)=>{
21
+ config.headers.Authorization = `Bearer ${this.getToken()}`;
22
+ return config;
23
+ }, (error)=>{
24
+ console.error('Request Error:', error);
25
+ return Promise.reject(error);
26
+ });
27
+ apiClient.interceptors.response.use((response)=>{
28
+ if ('blob' === response.config.responseType) return response;
29
+ if (!successCodes.includes(response.data.code)) {
30
+ const error = {
31
+ code: response.data.code,
32
+ message: response.data.message,
33
+ data: response.data.data
34
+ };
35
+ notification.error({
36
+ message: error.code,
37
+ description: error.message
38
+ });
39
+ return Promise.reject(error);
40
+ }
41
+ return response;
42
+ }, (error)=>{
43
+ console.error('Response Error:', error);
44
+ if (error.response) {
45
+ const apiError = {
46
+ code: error.response.status,
47
+ message: error.response.data?.message || error.message,
48
+ data: error.response.data
49
+ };
50
+ return Promise.reject(apiError);
51
+ }
52
+ const networkError = {
53
+ code: 0,
54
+ message: '网络连接失败,请检查网络设置'
55
+ };
56
+ return Promise.reject(networkError);
57
+ });
58
+ this.client = apiClient;
59
+ };
60
+ HttpClient.prototype.post = function(url, data, config) {
61
+ const client = this.client;
62
+ return client.post(url, data, config);
63
+ };
64
+ HttpClient.prototype.get = function(url, config) {
65
+ const client = this.client;
66
+ return client.get(url, config);
67
+ };
68
+ HttpClient.prototype.patch = function(url, data, config) {
69
+ const client = this.client;
70
+ return client.patch(url, data, config);
71
+ };
72
+ HttpClient.prototype.delete = function(url, config) {
73
+ const client = this.client;
74
+ return client.delete(url, config);
75
+ };
76
+ HttpClient.prototype.download = async function(url, params, downloadOptions, config) {
77
+ let { filename, type } = downloadOptions || {};
78
+ const client = this.client;
79
+ const response = await client.post(url, params, {
80
+ ...config,
81
+ headers: {
82
+ Accept: type,
83
+ ...config?.headers
84
+ },
85
+ responseType: 'blob'
86
+ });
87
+ const blob = new Blob([
88
+ response.data
89
+ ], {
90
+ type
91
+ });
92
+ const downloadUrl = window.URL.createObjectURL(blob);
93
+ const link = document.createElement('a');
94
+ link.href = downloadUrl;
95
+ const contentDisposition = response.headers['content-disposition'];
96
+ if (contentDisposition) {
97
+ const filenameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
98
+ if (filenameMatch && filenameMatch[1]) filename = decodeURIComponent(filenameMatch[1].replace(/['"]/g, ''));
99
+ }
100
+ link.download = filename;
101
+ document.body.appendChild(link);
102
+ link.click();
103
+ document.body.removeChild(link);
104
+ window.URL.revokeObjectURL(url);
105
+ };
106
+ }
107
+ export { setupClient };
@@ -0,0 +1 @@
1
+ export declare function setupHeaders(HttpClient: typeof import('./index').HttpClient): void;
@@ -0,0 +1,8 @@
1
+ function setupHeaders(HttpClient) {
2
+ HttpClient.prototype.getHeaders = function() {
3
+ return {
4
+ 'Content-Type': 'application/json'
5
+ };
6
+ };
7
+ }
8
+ export { setupHeaders };
@@ -0,0 +1,22 @@
1
+ import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2
+ import { ClientOptions, IHttpClient } from './types';
3
+ import { ApiResponse } from '../types';
4
+ export declare class HttpClient implements IHttpClient {
5
+ options: ClientOptions;
6
+ client?: AxiosInstance;
7
+ constructor(options: ClientOptions);
8
+ getToken(): string;
9
+ setToken(token: string): void;
10
+ getBaseURL(): string;
11
+ getHeaders(): Record<string, string>;
12
+ createAxiosInstance(): AxiosInstance;
13
+ setup(): void;
14
+ get<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
15
+ post<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
16
+ patch<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
17
+ delete<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
18
+ download<D = any>(url: string, params?: D, downloadOptions?: {
19
+ filename?: string;
20
+ type?: string;
21
+ }, config?: AxiosRequestConfig<D>): Promise<void>;
22
+ }
@@ -0,0 +1,55 @@
1
+ import { setupToken } from "./token.js";
2
+ import { mergeServerConfig, setupBaseUrl } from "./base_url.js";
3
+ import { setupHeaders } from "./headers.js";
4
+ import { setupClient } from "./client.js";
5
+ class HttpClient {
6
+ options;
7
+ client;
8
+ constructor(options){
9
+ const mergedOptions = {
10
+ ...options,
11
+ tokenKey: options.tokenKey || '__REQUEST_TOKEN__',
12
+ server: mergeServerConfig(options.server),
13
+ config: options.config || {}
14
+ };
15
+ this.options = mergedOptions;
16
+ setupToken(HttpClient);
17
+ setupBaseUrl(HttpClient);
18
+ setupHeaders(HttpClient);
19
+ setupClient(HttpClient);
20
+ }
21
+ getToken() {
22
+ throw new Error('Method should be implemented by setupToken');
23
+ }
24
+ setToken(token) {
25
+ throw new Error('Method should be implemented by setupToken');
26
+ }
27
+ getBaseURL() {
28
+ throw new Error('Method should be implemented by setupBaseUrl');
29
+ }
30
+ getHeaders() {
31
+ throw new Error('Method should be implemented by setupHeaders');
32
+ }
33
+ createAxiosInstance() {
34
+ throw new Error('Method should be implemented by setupClient');
35
+ }
36
+ setup() {
37
+ throw new Error('Method should be implemented by setupClient');
38
+ }
39
+ get(url, config) {
40
+ throw new Error('Method should be implemented by setupClient');
41
+ }
42
+ post(url, data, config) {
43
+ throw new Error('Method should be implemented by setupClient');
44
+ }
45
+ patch(url, data, config) {
46
+ throw new Error('Method should be implemented by setupClient');
47
+ }
48
+ delete(url, config) {
49
+ throw new Error('Method should be implemented by setupClient');
50
+ }
51
+ download(url, params, downloadOptions, config) {
52
+ throw new Error('Method should be implemented by setupClient');
53
+ }
54
+ }
55
+ export { HttpClient };
@@ -0,0 +1 @@
1
+ export declare function setupToken(HttpClient: typeof import('./index').HttpClient): void;
@@ -0,0 +1,10 @@
1
+ function setupToken(HttpClient) {
2
+ HttpClient.prototype.getToken = function() {
3
+ const API_TOKEN = localStorage.getItem(this.options.tokenKey);
4
+ return API_TOKEN || '';
5
+ };
6
+ HttpClient.prototype.setToken = function(token) {
7
+ localStorage.setItem(this.options.tokenKey, token);
8
+ };
9
+ }
10
+ export { setupToken };
@@ -0,0 +1,31 @@
1
+ import { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
2
+ import { ApiResponse } from '../types';
3
+ export interface ClientOptions {
4
+ tokenKey?: string;
5
+ successCodes?: number[];
6
+ server?: {
7
+ protocol?: string;
8
+ host?: string;
9
+ port?: string;
10
+ publicPath?: string;
11
+ };
12
+ config?: AxiosRequestConfig;
13
+ }
14
+ export interface IHttpClient {
15
+ options: ClientOptions;
16
+ client?: AxiosInstance;
17
+ getToken(): string;
18
+ setToken(token: string): void;
19
+ getBaseURL(): string;
20
+ getHeaders(): Record<string, string>;
21
+ createAxiosInstance(): AxiosInstance;
22
+ setup(): void;
23
+ get<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
24
+ post<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
25
+ patch<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
26
+ delete<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<ApiResponse<T>>>;
27
+ download<D = any>(url: string, params?: D, downloadOptions?: {
28
+ filename?: string;
29
+ type?: string;
30
+ }, config?: AxiosRequestConfig<D>): Promise<void>;
31
+ }
File without changes
@@ -0,0 +1,3 @@
1
+ export { HttpClient } from './client';
2
+ export type { ClientOptions } from './client/types';
3
+ export type { ApiResponse, ApiError, PaginationData, PaginatedResponse, BaseQueryParams, } from './types';
@@ -0,0 +1,2 @@
1
+ import { HttpClient } from "./client/index.js";
2
+ export { HttpClient };
@@ -0,0 +1,23 @@
1
+ export interface ApiResponse<T = any> {
2
+ code: number;
3
+ message: string;
4
+ data?: T;
5
+ }
6
+ export interface ApiError {
7
+ code: number;
8
+ message: string;
9
+ data?: any;
10
+ }
11
+ export interface PaginationData<T> {
12
+ total: number;
13
+ pageSize: number;
14
+ current: number;
15
+ list: T[];
16
+ }
17
+ export interface PaginatedResponse<T> extends ApiResponse<PaginationData<T>> {
18
+ }
19
+ export interface BaseQueryParams {
20
+ current?: number;
21
+ pageSize?: number;
22
+ sort?: string;
23
+ }
File without changes
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import '@ant-design/v5-patch-for-react-19';
2
- export { FilterForm } from './components/FilterGroup';
2
+ export * from './components/FilterGroup';
3
+ export * from './http-client';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import "@ant-design/v5-patch-for-react-19";
2
- import { FilterForm } from "./components/FilterGroup/index.js";
3
- export { FilterForm };
2
+ export * from "./components/FilterGroup/index.js";
3
+ export * from "./http-client/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "listpage-next",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "A React component library for creating filter forms with Ant Design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -31,10 +31,12 @@
31
31
  "prepublishOnly": "npm run build"
32
32
  },
33
33
  "devDependencies": {
34
+ "@ant-design/v5-patch-for-react-19": "^1.0.3",
34
35
  "@rsbuild/core": "^1.5.7",
35
36
  "@rsbuild/plugin-react": "^1.4.0",
36
37
  "@rslib/core": "^0.13.3",
37
38
  "@storybook/addon-essentials": "^8.6.14",
39
+ "@storybook/test": "^8.6.14",
38
40
  "@types/lodash": "^4.17.20",
39
41
  "@types/react": "^19.1.13",
40
42
  "@types/styled-components": "^5.1.34",
@@ -43,8 +45,6 @@
43
45
  "storybook": "^8.6.14",
44
46
  "storybook-addon-rslib": "^1.0.3",
45
47
  "storybook-react-rsbuild": "^1.0.3",
46
- "@storybook/test": "^8.6.14",
47
- "@ant-design/v5-patch-for-react-19": "^1.0.3",
48
48
  "typescript": "^5.9.2"
49
49
  },
50
50
  "peerDependencies": {
@@ -54,6 +54,7 @@
54
54
  "dependencies": {
55
55
  "ahooks": "^3.9.5",
56
56
  "antd": "^5.27.3",
57
+ "axios": "^1.12.2",
57
58
  "dayjs": "^1.11.18",
58
59
  "lodash": "^4.17.21",
59
60
  "styled-components": "^6.1.19"