@fecommunity/reactpress-toolkit 1.0.0-beta.1 → 1.0.0-beta.2

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.
Files changed (124) hide show
  1. package/README.md +190 -0
  2. package/dist/Article.d.ts +121 -0
  3. package/dist/Article.js +189 -0
  4. package/dist/Auth.d.ts +30 -0
  5. package/dist/Auth.js +59 -0
  6. package/dist/Category.d.ts +49 -0
  7. package/dist/Category.js +85 -0
  8. package/dist/Comment.d.ts +58 -0
  9. package/dist/Comment.js +98 -0
  10. package/dist/File.d.ts +40 -0
  11. package/dist/File.js +72 -0
  12. package/dist/HttpClient.d.ts +41 -0
  13. package/dist/HttpClient.js +94 -0
  14. package/dist/Knowledge.d.ts +76 -0
  15. package/dist/Knowledge.js +124 -0
  16. package/dist/Page.d.ts +58 -0
  17. package/dist/Page.js +98 -0
  18. package/dist/Search.d.ts +30 -0
  19. package/dist/Search.js +59 -0
  20. package/dist/Setting.d.ts +22 -0
  21. package/dist/Setting.js +46 -0
  22. package/dist/Smtp.d.ts +31 -0
  23. package/dist/Smtp.js +59 -0
  24. package/dist/Tag.d.ts +58 -0
  25. package/dist/Tag.js +98 -0
  26. package/dist/User.d.ts +41 -0
  27. package/dist/User.js +73 -0
  28. package/dist/View.d.ts +49 -0
  29. package/dist/View.js +85 -0
  30. package/dist/api/ApiInstance.d.ts +39 -0
  31. package/dist/api/ApiInstance.js +46 -0
  32. package/dist/api/Article.d.ts +11 -11
  33. package/dist/api/Article.js +12 -12
  34. package/dist/api/ArticleRoute.d.ts +201 -0
  35. package/dist/api/ArticleRoute.js +12 -0
  36. package/dist/api/Auth.d.ts +5 -5
  37. package/dist/api/Auth.js +6 -6
  38. package/dist/api/AuthRoute.d.ts +44 -0
  39. package/dist/api/AuthRoute.js +12 -0
  40. package/dist/api/Category.d.ts +1 -1
  41. package/dist/api/Category.js +2 -2
  42. package/dist/api/CategoryRoute.d.ts +79 -0
  43. package/dist/api/CategoryRoute.js +12 -0
  44. package/dist/api/Comment.d.ts +3 -3
  45. package/dist/api/Comment.js +4 -4
  46. package/dist/api/CommentRoute.d.ts +95 -0
  47. package/dist/api/CommentRoute.js +12 -0
  48. package/dist/api/File.d.ts +3 -3
  49. package/dist/api/File.js +4 -4
  50. package/dist/api/FileRoute.d.ts +63 -0
  51. package/dist/api/FileRoute.js +12 -0
  52. package/dist/api/Knowledge.d.ts +5 -5
  53. package/dist/api/Knowledge.js +6 -6
  54. package/dist/api/KnowledgeRoute.d.ts +125 -0
  55. package/dist/api/KnowledgeRoute.js +12 -0
  56. package/dist/api/Page.d.ts +3 -3
  57. package/dist/api/Page.js +4 -4
  58. package/dist/api/PageRoute.d.ts +95 -0
  59. package/dist/api/PageRoute.js +12 -0
  60. package/dist/api/Search.d.ts +3 -3
  61. package/dist/api/Search.js +4 -4
  62. package/dist/api/SearchRoute.d.ts +46 -0
  63. package/dist/api/SearchRoute.js +12 -0
  64. package/dist/api/Setting.d.ts +3 -3
  65. package/dist/api/Setting.js +4 -4
  66. package/dist/api/SettingRoute.d.ts +31 -0
  67. package/dist/api/SettingRoute.js +12 -0
  68. package/dist/api/Smtp.d.ts +1 -1
  69. package/dist/api/Smtp.js +2 -2
  70. package/dist/api/SmtpRoute.d.ts +47 -0
  71. package/dist/api/SmtpRoute.js +12 -0
  72. package/dist/api/Tag.d.ts +3 -3
  73. package/dist/api/Tag.js +4 -4
  74. package/dist/api/TagRoute.d.ts +95 -0
  75. package/dist/api/TagRoute.js +12 -0
  76. package/dist/api/User.d.ts +7 -7
  77. package/dist/api/User.js +8 -8
  78. package/dist/api/UserRoute.d.ts +60 -0
  79. package/dist/api/UserRoute.js +12 -0
  80. package/dist/api/View.d.ts +3 -3
  81. package/dist/api/View.js +4 -4
  82. package/dist/api/ViewRoute.d.ts +77 -0
  83. package/dist/api/ViewRoute.js +12 -0
  84. package/dist/api/httpClient.d.ts +1 -0
  85. package/dist/api/httpClient.js +1 -0
  86. package/dist/api/index.d.ts +5 -5
  87. package/dist/api/index.js +6 -7
  88. package/dist/data-contracts.d.ts +245 -0
  89. package/dist/data-contracts.js +12 -0
  90. package/dist/index.d.ts +5 -9
  91. package/dist/index.js +14 -15
  92. package/dist/utils/common.d.ts +1 -3
  93. package/dist/utils/common.js +5 -31
  94. package/dist/utils/http.js +4 -6
  95. package/package.json +5 -3
  96. package/demos/simple.ts +0 -65
  97. package/scripts/generate-api-types.js +0 -571
  98. package/scripts/generate-swagger.js +0 -47
  99. package/scripts/organize-api.js +0 -136
  100. package/src/api/Article.ts +0 -186
  101. package/src/api/Auth.ts +0 -54
  102. package/src/api/Category.ts +0 -82
  103. package/src/api/Comment.ts +0 -95
  104. package/src/api/File.ts +0 -69
  105. package/src/api/Knowledge.ts +0 -122
  106. package/src/api/Page.ts +0 -95
  107. package/src/api/Search.ts +0 -54
  108. package/src/api/Setting.ts +0 -43
  109. package/src/api/Smtp.ts +0 -56
  110. package/src/api/Tag.ts +0 -95
  111. package/src/api/User.ts +0 -72
  112. package/src/api/View.ts +0 -82
  113. package/src/api/api-instance.ts +0 -69
  114. package/src/api/httpClient.ts +0 -140
  115. package/src/api/index.ts +0 -25
  116. package/src/index.ts +0 -18
  117. package/src/types/data-contracts.ts +0 -198
  118. package/src/types/index.ts +0 -4
  119. package/src/utils/common.ts +0 -108
  120. package/src/utils/http.ts +0 -52
  121. package/src/utils/index.ts +0 -5
  122. package/test-final.js +0 -56
  123. package/test-import.js +0 -34
  124. package/tsconfig.json +0 -20
@@ -0,0 +1,245 @@
1
+ export interface IUser {
2
+ name: string;
3
+ password: string;
4
+ avatar: string;
5
+ email: string;
6
+ role: string;
7
+ status: string;
8
+ type: string;
9
+ /** @format date-time */
10
+ createAt: string;
11
+ /** @format date-time */
12
+ updateAt: string;
13
+ }
14
+ export interface ISetting {
15
+ id: string;
16
+ i18n: string;
17
+ globalSetting: string;
18
+ systemUrl: string;
19
+ systemTitle: string;
20
+ systemSubTitle: string;
21
+ systemLogo: string;
22
+ systemBg: string;
23
+ systemFavicon: string;
24
+ systemNoticeInfo: string;
25
+ systemFooterInfo: string;
26
+ adminSystemUrl: string;
27
+ baiduAnalyticsId: string;
28
+ googleAnalyticsId: string;
29
+ seoKeyword: string;
30
+ seoDesc: string;
31
+ oss: string;
32
+ smtpHost: string;
33
+ smtpPort: string;
34
+ smtpUser: string;
35
+ smtpPass: string;
36
+ smtpFromUser: string;
37
+ /** @format date-time */
38
+ createAt: string;
39
+ /** @format date-time */
40
+ updateAt: string;
41
+ }
42
+ export interface I_SMTP {
43
+ id: string;
44
+ from: string;
45
+ to: string;
46
+ subject: string;
47
+ text: string;
48
+ html: string;
49
+ /** @format date-time */
50
+ createAt: string;
51
+ }
52
+ export interface IFile {
53
+ id: string;
54
+ originalname: string;
55
+ filename: string;
56
+ type: string;
57
+ size: number;
58
+ url: string;
59
+ /** @format date-time */
60
+ createAt: string;
61
+ }
62
+ export interface ITag {
63
+ label: string;
64
+ value: string;
65
+ articles: string[];
66
+ /** @format date-time */
67
+ createAt: string;
68
+ /** @format date-time */
69
+ updateAt: string;
70
+ }
71
+ export interface ICategory {
72
+ id: string;
73
+ label: string;
74
+ value: string;
75
+ articles: string[];
76
+ /** @format date-time */
77
+ createAt: string;
78
+ /** @format date-time */
79
+ updateAt: string;
80
+ }
81
+ export interface IArticle {
82
+ id: string;
83
+ title: string;
84
+ cover: string;
85
+ summary: string;
86
+ content: string;
87
+ html: string;
88
+ toc: string;
89
+ category: ICategory;
90
+ tags: string[];
91
+ status: string;
92
+ views: number;
93
+ likes: number;
94
+ isRecommended: boolean;
95
+ password: string;
96
+ needPassword: boolean;
97
+ isCommentable: boolean;
98
+ /** @format date-time */
99
+ publishAt: string;
100
+ /** @format date-time */
101
+ createAt: string;
102
+ /** @format date-time */
103
+ updateAt: string;
104
+ }
105
+ export interface IKnowledge {
106
+ id: string;
107
+ parentId: string;
108
+ order: number;
109
+ title: string;
110
+ cover: string;
111
+ summary: string;
112
+ content: string;
113
+ html: string;
114
+ toc: string;
115
+ status: string;
116
+ views: number;
117
+ likes: number;
118
+ isCommentable: boolean;
119
+ /** @format date-time */
120
+ publishAt: string;
121
+ /** @format date-time */
122
+ createAt: string;
123
+ /** @format date-time */
124
+ updateAt: string;
125
+ }
126
+ export interface IComment {
127
+ id: string;
128
+ name: string;
129
+ email: string;
130
+ avatar: string;
131
+ content: string;
132
+ html: string;
133
+ pass: boolean;
134
+ userAgent: string;
135
+ hostId: string;
136
+ url: string;
137
+ parentCommentId: string;
138
+ replyUserName: string;
139
+ replyUserEmail: string;
140
+ /** @format date-time */
141
+ createAt: string;
142
+ /** @format date-time */
143
+ updateAt: string;
144
+ }
145
+ export interface IPage {
146
+ id: string;
147
+ cover: string;
148
+ name: string;
149
+ path: string;
150
+ order: number;
151
+ content: string;
152
+ html: string;
153
+ toc: string;
154
+ status: string;
155
+ /** @format date-time */
156
+ publishAt: string;
157
+ views: number;
158
+ /** @format date-time */
159
+ createAt: string;
160
+ /** @format date-time */
161
+ updateAt: string;
162
+ }
163
+ export interface IView {
164
+ ip: string;
165
+ userAgent: string;
166
+ url: string;
167
+ count: number;
168
+ address: string;
169
+ browser: string;
170
+ engine: string;
171
+ os: string;
172
+ device: string;
173
+ /** @format date-time */
174
+ createAt: string;
175
+ /** @format date-time */
176
+ updateAt: string;
177
+ }
178
+ export declare type IUserControllerFindAllData = IUser[];
179
+ export declare type IUserControllerRegisterData = IUser[];
180
+ export declare type IUserControllerUpdateData = IUser[];
181
+ export declare type IUserControllerUpdatePasswordData = IUser[];
182
+ export declare type IAuthControllerLoginData = any;
183
+ export declare type IAuthControllerCreateBookData = any;
184
+ export declare type IAuthControllerLoginWithGithubData = any;
185
+ export declare type ISettingControllerUpdateData = ISetting[];
186
+ export declare type ISettingControllerFindAllData = any;
187
+ export declare type ISmtpControllerCreateData = I_SMTP[];
188
+ export declare type ISmtpControllerFindAllData = any;
189
+ export declare type ISmtpControllerDeleteByIdData = any;
190
+ export declare type IFileControllerUploadFileData = IFile[];
191
+ export declare type IFileControllerFindAllData = any;
192
+ export declare type IFileControllerFindByIdData = any;
193
+ export declare type IFileControllerDeleteByIdData = any;
194
+ export declare type ITagControllerCreateData = ITag[];
195
+ export declare type ITagControllerFindAllData = any;
196
+ export declare type ITagControllerFindByIdData = any;
197
+ export declare type ITagControllerUpdateByIdData = any;
198
+ export declare type ITagControllerDeleteByIdData = any;
199
+ export declare type ITagControllerGetArticleByIdData = any;
200
+ export declare type IArticleControllerCreateData = IArticle[];
201
+ export declare type IArticleControllerFindAllData = any;
202
+ export declare type IArticleControllerFindArticlesByCategoryData = any;
203
+ export declare type IArticleControllerFindArticlesByTagData = any;
204
+ export declare type IArticleControllerGetRecommendArticlesData = any;
205
+ export declare type IArticleControllerGetArchivesData = any;
206
+ export declare type IArticleControllerRecommendData = any;
207
+ export declare type IArticleControllerFindByIdData = any;
208
+ export declare type IArticleControllerUpdateByIdData = any;
209
+ export declare type IArticleControllerDeleteByIdData = any;
210
+ export declare type IArticleControllerCheckPasswordData = any;
211
+ export declare type IArticleControllerUpdateViewsByIdData = any;
212
+ export declare type IArticleControllerUpdateLikesByIdData = any;
213
+ export declare type ICategoryControllerCreateData = ICategory[];
214
+ export declare type ICategoryControllerFindAllData = any;
215
+ export declare type ICategoryControllerFindByIdData = any;
216
+ export declare type ICategoryControllerUpdateByIdData = any;
217
+ export declare type ICategoryControllerDeleteByIdData = any;
218
+ export declare type IKnowledgeControllerCreateBookData = IKnowledge[];
219
+ export declare type IKnowledgeControllerCreateChapterData = IKnowledge[];
220
+ export declare type IKnowledgeControllerDeleteByIdData = any;
221
+ export declare type IKnowledgeControllerUpdateByIdData = any;
222
+ export declare type IKnowledgeControllerFindByIdData = any;
223
+ export declare type IKnowledgeControllerFindAllData = any;
224
+ export declare type IKnowledgeControllerUpdateViewsByIdData = any;
225
+ export declare type IKnowledgeControllerUpdateLikesByIdData = any;
226
+ export declare type ICommentControllerCreateData = IComment[];
227
+ export declare type ICommentControllerFindAllData = any;
228
+ export declare type ICommentControllerFindByIdData = any;
229
+ export declare type ICommentControllerUpdateByIdData = any;
230
+ export declare type ICommentControllerDeleteByIdData = any;
231
+ export declare type ICommentControllerGetArticleCommentsData = any;
232
+ export declare type IPageControllerCreateData = IPage[];
233
+ export declare type IPageControllerFindAllData = any;
234
+ export declare type IPageControllerFindByIdData = any;
235
+ export declare type IPageControllerUpdateByIdData = any;
236
+ export declare type IPageControllerDeleteByIdData = any;
237
+ export declare type IPageControllerUpdateViewsByIdData = any;
238
+ export declare type IViewControllerCreateData = IView[];
239
+ export declare type IViewControllerFindAllData = any;
240
+ export declare type IViewControllerFindByUrlData = any;
241
+ export declare type IViewControllerFindByIdData = any;
242
+ export declare type IViewControllerDeleteByIdData = any;
243
+ export declare type ISearchControllerSearchArticleData = any;
244
+ export declare type ISearchControllerFindAllData = any;
245
+ export declare type ISearchControllerDeleteByIdData = any;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ /* eslint-disable */
3
+ /* tslint:disable */
4
+ /*
5
+ * ---------------------------------------------------------------
6
+ * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
7
+ * ## ##
8
+ * ## AUTHOR: acacode ##
9
+ * ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
10
+ * ---------------------------------------------------------------
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- import * as api from './api';
2
- import * as types from './types';
3
- import * as utils from './utils';
4
- import apiInstance from './api/api-instance';
5
- export { api, types, utils };
6
- export { apiInstance as default };
7
- export * from './api';
8
- export * from './types';
9
- export * from './utils';
1
+ export { default as api } from './api/ApiInstance';
2
+ export * from './api/ApiInstance';
3
+ export * as types from './types';
4
+ export * as utils from './utils';
5
+ export { default } from './api/ApiInstance';
package/dist/index.js CHANGED
@@ -17,6 +17,9 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
17
17
  }) : function(o, v) {
18
18
  o["default"] = v;
19
19
  });
20
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
21
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
22
+ };
20
23
  var __importStar = (this && this.__importStar) || function (mod) {
21
24
  if (mod && mod.__esModule) return mod;
22
25
  var result = {};
@@ -24,23 +27,19 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
27
  __setModuleDefault(result, mod);
25
28
  return result;
26
29
  };
27
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
28
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
29
- };
30
30
  var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
34
  exports.default = exports.utils = exports.types = exports.api = void 0;
35
- const api = __importStar(require("./api"));
36
- exports.api = api;
37
- const types = __importStar(require("./types"));
38
- exports.types = types;
39
- const utils = __importStar(require("./utils"));
40
- exports.utils = utils;
41
- const api_instance_1 = __importDefault(require("./api/api-instance"));
42
- exports.default = api_instance_1.default;
43
- // Also export individual items for backward compatibility
44
- __exportStar(require("./api"), exports);
45
- __exportStar(require("./types"), exports);
46
- __exportStar(require("./utils"), exports);
35
+ // 导出 API 实例
36
+ var ApiInstance_1 = require("./api/ApiInstance");
37
+ Object.defineProperty(exports, "api", { enumerable: true, get: function () { return __importDefault(ApiInstance_1).default; } });
38
+ __exportStar(require("./api/ApiInstance"), exports);
39
+ // 导出类型定义
40
+ exports.types = __importStar(require("./types"));
41
+ // 导出工具函数
42
+ exports.utils = __importStar(require("./utils"));
43
+ // 默认导出 API 实例
44
+ var ApiInstance_2 = require("./api/ApiInstance");
45
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(ApiInstance_2).default; } });
@@ -1,8 +1,5 @@
1
1
  export declare const formatDate: (date: Date, format?: string) => string;
2
2
  export declare const deepClone: <T>(obj: T) => T;
3
- export declare const debounce: <T extends (...args: any[]) => any>(func: T, wait: number) => (...args: Parameters<T>) => void;
4
- export declare const throttle: <T extends (...args: any[]) => any>(func: T, limit: number) => (...args: Parameters<T>) => void;
5
- export declare const isApiError: (error: any) => error is ApiError;
6
3
  export interface ApiResponse<T = any> {
7
4
  code: number;
8
5
  message: string;
@@ -20,4 +17,5 @@ export declare class ApiError extends Error {
20
17
  code: number;
21
18
  details?: any;
22
19
  constructor(code: number, message: string, details?: any);
20
+ static isInstance(error: any): error is ApiError;
23
21
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // 通用工具函数
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ApiError = exports.isApiError = exports.throttle = exports.debounce = exports.deepClone = exports.formatDate = void 0;
4
+ exports.ApiError = exports.deepClone = exports.formatDate = void 0;
5
5
  // 日期格式化
6
6
  const formatDate = (date, format = 'YYYY-MM-DD') => {
7
7
  const year = date.getFullYear();
@@ -36,36 +36,6 @@ const deepClone = (obj) => {
36
36
  return obj;
37
37
  };
38
38
  exports.deepClone = deepClone;
39
- // 防抖函数
40
- const debounce = (func, wait) => {
41
- let timeout = null;
42
- return (...args) => {
43
- if (timeout) {
44
- clearTimeout(timeout);
45
- }
46
- timeout = setTimeout(() => {
47
- func(...args);
48
- }, wait);
49
- };
50
- };
51
- exports.debounce = debounce;
52
- // 节流函数
53
- const throttle = (func, limit) => {
54
- let inThrottle = false;
55
- return (...args) => {
56
- if (!inThrottle) {
57
- func(...args);
58
- inThrottle = true;
59
- setTimeout(() => (inThrottle = false), limit);
60
- }
61
- };
62
- };
63
- exports.throttle = throttle;
64
- // 检查是否为ApiError
65
- const isApiError = (error) => {
66
- return error instanceof ApiError || (error && typeof error === 'object' && 'code' in error && 'message' in error);
67
- };
68
- exports.isApiError = isApiError;
69
39
  // 错误处理
70
40
  class ApiError extends Error {
71
41
  constructor(code, message, details) {
@@ -74,5 +44,9 @@ class ApiError extends Error {
74
44
  this.details = details;
75
45
  this.name = 'ApiError';
76
46
  }
47
+ // 检查是否为 ApiError 实例
48
+ static isInstance(error) {
49
+ return error instanceof ApiError;
50
+ }
77
51
  }
78
52
  exports.ApiError = ApiError;
@@ -14,10 +14,10 @@ const createHttpClient = (baseURL) => {
14
14
  'Content-Type': 'application/json',
15
15
  },
16
16
  });
17
- // 请求拦截器
17
+ // 请求拦截器 - 修复类型错误
18
18
  instance.interceptors.request.use((config) => {
19
19
  // 添加认证令牌
20
- const token = typeof window !== 'undefined' ? localStorage.getItem('auth_token') : null;
20
+ const token = localStorage.getItem('auth_token');
21
21
  if (token && config.headers) {
22
22
  config.headers.Authorization = `Bearer ${token}`;
23
23
  }
@@ -32,10 +32,8 @@ const createHttpClient = (baseURL) => {
32
32
  // 统一错误处理
33
33
  if (error.response?.status === 401) {
34
34
  // 未授权处理
35
- if (typeof window !== 'undefined') {
36
- localStorage.removeItem('auth_token');
37
- window.location.href = '/login';
38
- }
35
+ localStorage.removeItem('auth_token');
36
+ window.location.href = '/login';
39
37
  }
40
38
  return Promise.reject(error.response?.data || error);
41
39
  });
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@fecommunity/reactpress-toolkit",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.2",
4
4
  "main": "index.js",
5
+ "files": [
6
+ "dist"
7
+ ],
5
8
  "keywords": [],
6
9
  "author": "",
7
10
  "license": "ISC",
@@ -13,7 +16,6 @@
13
16
  },
14
17
  "scripts": {
15
18
  "generate": "node scripts/generate-swagger.js",
16
- "build": "tsc",
17
- "test": "node test-toolkit.js"
19
+ "build": "tsc"
18
20
  }
19
21
  }
package/demos/simple.ts DELETED
@@ -1,65 +0,0 @@
1
- // src/services/articleService.ts
2
- import api, { types, utils } from '@/api';
3
-
4
- // 文章服务层
5
- export class ArticleService {
6
- // 获取所有文章
7
- static async getAllArticles() {
8
- try {
9
- const articles = await api.article.findAll();
10
-
11
- // 格式化数据
12
- return articles.map(article => ({
13
- ...article,
14
- formattedDate: utils.formatDate(new Date(article.createdAt || Date.now())),
15
- }));
16
- } catch (error) {
17
- throw this.handleError(error, '获取文章列表失败');
18
- }
19
- }
20
-
21
- // 获取单篇文章
22
- static async getArticleById(id: string) {
23
- try {
24
- return await api.article.findById(id);
25
- } catch (error) {
26
- throw this.handleError(error, '获取文章失败');
27
- }
28
- }
29
-
30
- // 创建文章
31
- static async createArticle(articleData: Partial<types.IArticle>) {
32
- try {
33
- return await api.article.create(articleData);
34
- } catch (error) {
35
- throw this.handleError(error, '创建文章失败');
36
- }
37
- }
38
-
39
- // 更新文章
40
- static async updateArticle(id: string, updates: Partial<types.IArticle>) {
41
- try {
42
- return await api.article.updateById(id, updates);
43
- } catch (error) {
44
- throw this.handleError(error, '更新文章失败');
45
- }
46
- }
47
-
48
- // 删除文章
49
- static async deleteArticle(id: string) {
50
- try {
51
- return await api.article.deleteById(id);
52
- } catch (error) {
53
- throw this.handleError(error, '删除文章失败');
54
- }
55
- }
56
-
57
- // 统一错误处理
58
- private static handleError(error: unknown, defaultMessage: string) {
59
- if (utils.ApiError.isInstance(error)) {
60
- return error;
61
- }
62
-
63
- return new utils.ApiError(500, defaultMessage, error);
64
- }
65
- }