react-toolkits 2.11.0 → 2.11.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # react-toolkits
2
2
 
3
+ ## 2.11.1
4
+
5
+ ### Patch Changes
6
+
7
+ - d685357: feat: export axios instance
8
+
3
9
  ## 2.11.0
4
10
 
5
11
  ### Minor Changes
package/lib/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import { ParagraphProps } from 'antd/es/typography/Paragraph';
4
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
5
  import { FormInstance, MenuProps, ButtonProps, ModalProps, FormProps } from 'antd';
6
6
  import { TableProps } from 'antd/es/table';
7
+ import * as axios from 'axios';
7
8
  import { AxiosHeaderValue, InternalAxiosRequestConfig, AxiosResponse, AxiosInterceptorOptions, AxiosRequestConfig } from 'axios';
8
9
  import { ItemType, SubMenuType, MenuItemGroupType, MenuItemType } from 'antd/es/menu/interface';
9
10
  import { Merge } from 'ts-essentials';
@@ -454,4 +455,14 @@ declare const SignIn: FC<SignInProps>;
454
455
 
455
456
  declare const mixedStorage: StateStorage;
456
457
 
457
- export { APP_ID_HEADER, DynamicTags, type DynamicTagsProps, ExpandableParagraph, type ExpandableParagraphProps, FilterFormWrapper, type FilterFormWrapperProps, type Game, Highlight, type HighlightProps, InfiniteList, type InfiniteListProps, Layout, type LayoutState, NavMenu, type NavMenuItem, NotFound, OperationLogList, PermissionButton, type PermissionButtonProps, QueryList, QueryListAction, type QueryListProps, type QueryListRef, RequireGame, RequirePermission, type RequirePermissionProps, SSO_URL, SignIn, ToolkitsProvider, type ToolkitsProviderProps, type UseFormModalProps, type UseModalOperation, type UseModalProps, UserWidget, type UserWidgetProps, mixedStorage, _default as permissionRoutes, toolkitsStore, useFormModal, useLayoutStore, useModal, useModalStore, usePermission, usePermissions, useQueryListStore, useToolkitsStore };
458
+ declare const instance: axios.AxiosInstance;
459
+ declare function registerInterceptors(): void;
460
+ type JsonData<T = any> = {
461
+ code?: number;
462
+ status?: number;
463
+ errno?: number;
464
+ data: T;
465
+ msg: string;
466
+ };
467
+
468
+ export { APP_ID_HEADER, DynamicTags, type DynamicTagsProps, ExpandableParagraph, type ExpandableParagraphProps, FilterFormWrapper, type FilterFormWrapperProps, type Game, Highlight, type HighlightProps, InfiniteList, type InfiniteListProps, type JsonData, Layout, type LayoutState, NavMenu, type NavMenuItem, NotFound, OperationLogList, PermissionButton, type PermissionButtonProps, QueryList, QueryListAction, type QueryListProps, type QueryListRef, RequireGame, RequirePermission, type RequirePermissionProps, SSO_URL, SignIn, ToolkitsProvider, type ToolkitsProviderProps, type UseFormModalProps, type UseModalOperation, type UseModalProps, UserWidget, type UserWidgetProps, instance, mixedStorage, _default as permissionRoutes, registerInterceptors, toolkitsStore, useFormModal, useLayoutStore, useModal, useModalStore, usePermission, usePermissions, useQueryListStore, useToolkitsStore };
package/lib/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import axios2, { AxiosHeaders } from 'axios';
1
+ import axios, { AxiosHeaders } from 'axios';
2
2
  import { jwtDecode } from 'jwt-decode';
3
3
  import { create, createStore } from 'zustand';
4
4
  import { persist, createJSONStorage } from 'zustand/middleware';
5
5
  import * as Antd2 from 'antd';
6
- import { Spin, Menu, Modal, Form, theme, Space, Input, Tag, Typography, Result, Table, Button, Empty, Dropdown, Divider, Card, Select, Alert, App, Row, Col, Breadcrumb, Skeleton, Descriptions, Tooltip, notification, Collapse, Checkbox } from 'antd';
6
+ import { Spin, Menu, notification, Modal, Form, theme, Space, Input, Tag, Typography, Result, Table, Button, Empty, Dropdown, Divider, Card, Select, Alert, App, Row, Col, Breadcrumb, Skeleton, Descriptions, Tooltip, Collapse, Checkbox } from 'antd';
7
7
  import { memo, useMemo, useCallback, useEffect, lazy, useContext, useState, useRef, Fragment as Fragment$1, Suspense, createContext, forwardRef, useImperativeHandle, cloneElement } from 'react';
8
8
  import { useStoreWithEqualityFn } from 'zustand/traditional';
9
9
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
@@ -43,7 +43,7 @@ var init_storage = __esm({
43
43
  sessionStorage.setItem(name, value);
44
44
  localStorage.setItem(name, value);
45
45
  },
46
- removeItem: async (name) => {
46
+ removeItem(name) {
47
47
  sessionStorage.removeItem(name);
48
48
  localStorage.removeItem(name);
49
49
  }
@@ -177,10 +177,10 @@ var init_game = __esm({
177
177
  set({ isLoading: true });
178
178
  let games;
179
179
  if (apiV2) {
180
- const response = await axios2.get("/api/game/list");
180
+ const response = await axios.get("/api/game/list");
181
181
  games = response.data.data.list ?? [];
182
182
  } else {
183
- const response = await axios2.get("/api/usystem/game/all");
183
+ const response = await axios.get("/api/usystem/game/all");
184
184
  games = response.data.data ?? [];
185
185
  }
186
186
  set({ games });
@@ -204,7 +204,7 @@ var init_token = __esm({
204
204
  if (token) {
205
205
  try {
206
206
  return jwtDecode(token);
207
- } catch (_) {
207
+ } catch (error) {
208
208
  return null;
209
209
  }
210
210
  }
@@ -262,101 +262,100 @@ var init_constants = __esm({
262
262
  APP_ID_HEADER = "App-ID";
263
263
  }
264
264
  });
265
- var instance, requestInterceptorId, responseInterceptorId, registerInterceptors, axios_default;
265
+ function registerInterceptors() {
266
+ const state = toolkitsStore?.getState() ?? {};
267
+ if (requestInterceptorId) {
268
+ instance.interceptors.request.eject(requestInterceptorId);
269
+ }
270
+ if (responseInterceptorId) {
271
+ instance.interceptors.response.eject(responseInterceptorId);
272
+ }
273
+ const interceptors = state.interceptors;
274
+ if (interceptors?.request) {
275
+ requestInterceptorId = instance.interceptors.request.use(...interceptors.request);
276
+ } else {
277
+ requestInterceptorId = instance.interceptors.request.use((config) => {
278
+ const headers = new AxiosHeaders(config.headers);
279
+ config.responseType = config.responseType || "json";
280
+ if (state.token) {
281
+ headers.set("Authorization", `Bearer ${state.token}`);
282
+ }
283
+ if (!headers.has(APP_ID_HEADER) && state.usePermissionApiV2 && state.game) {
284
+ if (state.gameApiV2) {
285
+ headers.set(APP_ID_HEADER, state.game.game_id);
286
+ } else {
287
+ headers.set(APP_ID_HEADER, state.game.id);
288
+ }
289
+ }
290
+ headers.set("Accept", config.responseType === "blob" ? "application/octet-stream" : "application/json");
291
+ config.headers = headers;
292
+ return config;
293
+ });
294
+ }
295
+ if (interceptors?.response) {
296
+ responseInterceptorId = instance.interceptors.response.use(...interceptors.response);
297
+ } else {
298
+ responseInterceptorId = instance.interceptors.response.use(
299
+ (response) => {
300
+ const responseType = response.request.responseType || "json";
301
+ if (responseType === "json") {
302
+ const data = response.data;
303
+ if (data.code === 0 || data.code === 200 || data.status === 0 || data.errno === 0) {
304
+ return response;
305
+ } else {
306
+ notification.error({
307
+ message: "Request failed",
308
+ description: data.msg
309
+ });
310
+ throw new Error(data.msg);
311
+ }
312
+ }
313
+ return response;
314
+ },
315
+ (error) => {
316
+ if (axios.isAxiosError(error)) {
317
+ const response = error.response;
318
+ if (!response) {
319
+ throw error;
320
+ }
321
+ if (response.status === 401 || response.status === 412) {
322
+ state?.clearToken();
323
+ if (state.signInPath) {
324
+ if (response.status === 412) ;
325
+ window.location.replace(state?.signInPath);
326
+ } else {
327
+ throw new Error("\u8BF7\u5728 ContextProvider \u4E2D\u914D\u7F6E signInPath");
328
+ }
329
+ } else if (response.status === 403) {
330
+ notification.error({
331
+ message: "Forbidden",
332
+ description: "You do not have permission to access this resource."
333
+ });
334
+ } else {
335
+ notification.error({
336
+ message: "Request failed",
337
+ description: response.data.msg
338
+ });
339
+ throw new Error(response.data.msg);
340
+ }
341
+ }
342
+ throw error;
343
+ }
344
+ );
345
+ }
346
+ }
347
+ var instance, requestInterceptorId, responseInterceptorId;
266
348
  var init_axios = __esm({
267
349
  "src/utils/axios.ts"() {
268
350
  init_toolkitsProvider();
269
351
  init_constants();
270
- instance = axios2.create({
352
+ instance = axios.create({
271
353
  baseURL: "/",
272
354
  headers: {
273
355
  "Content-Type": "application/json; charset=utf-8"
274
356
  }
275
357
  });
276
- registerInterceptors = () => {
277
- const state = toolkitsStore?.getState() ?? {};
278
- if (requestInterceptorId) {
279
- instance.interceptors.request.eject(requestInterceptorId);
280
- }
281
- if (responseInterceptorId) {
282
- instance.interceptors.response.eject(responseInterceptorId);
283
- }
284
- const interceptors = state.interceptors;
285
- if (interceptors?.request) {
286
- requestInterceptorId = instance.interceptors.request.use(...interceptors.request);
287
- } else {
288
- requestInterceptorId = instance.interceptors.request.use((config) => {
289
- const headers = new AxiosHeaders(config.headers);
290
- config.responseType = config.responseType || "json";
291
- if (state.token) {
292
- headers.set("Authorization", `Bearer ${state.token}`);
293
- }
294
- if (!headers.has(APP_ID_HEADER) && state.usePermissionApiV2 && state.game) {
295
- if (state.gameApiV2) {
296
- headers.set(APP_ID_HEADER, state.game.game_id);
297
- } else {
298
- headers.set(APP_ID_HEADER, state.game.id);
299
- }
300
- }
301
- headers.set("Accept", config.responseType === "blob" ? "application/octet-stream" : "application/json");
302
- config.headers = headers;
303
- return config;
304
- });
305
- }
306
- if (interceptors?.response) {
307
- responseInterceptorId = instance.interceptors.response.use(...interceptors.response);
308
- } else {
309
- responseInterceptorId = instance.interceptors.response.use(
310
- (response) => {
311
- const responseType = response.request.responseType || "json";
312
- if (responseType === "json") {
313
- const data = response.data;
314
- if (data.code === 0 || data.code === 200 || data.status === 0) {
315
- return response;
316
- } else {
317
- notification.error({
318
- message: "Request failed",
319
- description: data.msg
320
- });
321
- throw new Error(data.msg);
322
- }
323
- }
324
- return response;
325
- },
326
- (error) => {
327
- console.log(axios2.isAxiosError(error));
328
- if (axios2.isAxiosError(error)) {
329
- const response = error.response;
330
- if (!response) {
331
- throw error;
332
- }
333
- if (response.status === 401 || response.status === 412) {
334
- state?.clearToken();
335
- if (state.signInPath) {
336
- if (response.status === 412) ;
337
- window.location.replace(state?.signInPath);
338
- } else {
339
- throw new Error("\u8BF7\u5728 ContextProvider \u4E2D\u914D\u7F6E signInPath");
340
- }
341
- } else if (response.status === 403) {
342
- notification.error({
343
- message: "Forbidden",
344
- description: "You do not have permission to access this resource."
345
- });
346
- } else {
347
- notification.error({
348
- message: "Request failed",
349
- description: response.data.msg
350
- });
351
- throw new Error(response.data.msg);
352
- }
353
- }
354
- throw error;
355
- }
356
- );
357
- }
358
- };
359
- axios_default = instance;
358
+ registerInterceptors();
360
359
  }
361
360
  });
362
361
  function useToolkitsStore(selector) {
@@ -502,12 +501,12 @@ var init_axios2 = __esm({
502
501
  "src/hooks/axios.ts"() {
503
502
  init_axios();
504
503
  useSWRAxios = (key, swrConfig) => {
505
- return useSWR(key, (config) => axios_default.request(config).then((response) => response.data.data), swrConfig);
504
+ return useSWR(key, (config) => instance.request(config).then((response) => response.data.data), swrConfig);
506
505
  };
507
506
  useSWRImmutableAxios = (axiosConfig, swrConfig) => {
508
507
  return useSWRImmutable(
509
508
  axiosConfig,
510
- (config) => axios_default.request(config).then((response) => response.data.data),
509
+ (config) => instance.request(config).then((response) => response.data.data),
511
510
  swrConfig
512
511
  );
513
512
  };
@@ -693,7 +692,7 @@ var init_QueryList = __esm({
693
692
  const { data, isValidating } = useSWR(
694
693
  key,
695
694
  async () => {
696
- const response = await axios_default.request(requestConfig);
695
+ const response = await instance.request(requestConfig);
697
696
  return response.data.data;
698
697
  },
699
698
  {
@@ -966,7 +965,7 @@ function useCreateRole() {
966
965
  usePermissionApiV2 ? "/api/usystem/role/createV2" : "/api/usystem/role/create",
967
966
  (url3, {
968
967
  arg
969
- }) => axios_default.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
968
+ }) => instance.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
970
969
  );
971
970
  }
972
971
  function useUpdateRole() {
@@ -975,7 +974,7 @@ function useUpdateRole() {
975
974
  usePermissionApiV2 ? "/api/usystem/role/updateV2" : "/api/usystem/role/update",
976
975
  (url3, {
977
976
  arg
978
- }) => axios_default.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
977
+ }) => instance.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
979
978
  );
980
979
  }
981
980
  function useRemoveRole() {
@@ -983,7 +982,7 @@ function useRemoveRole() {
983
982
  "/api/usystem/role/delete",
984
983
  (url3, {
985
984
  arg
986
- }) => axios_default.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
985
+ }) => instance.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
987
986
  );
988
987
  }
989
988
  function useCreateUser() {
@@ -991,7 +990,7 @@ function useCreateUser() {
991
990
  "/api/usystem/user/create",
992
991
  (url3, {
993
992
  arg
994
- }) => axios_default.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
993
+ }) => instance.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
995
994
  );
996
995
  }
997
996
  function useUpdateUser() {
@@ -999,7 +998,7 @@ function useUpdateUser() {
999
998
  "/api/usystem/user/update",
1000
999
  (url3, {
1001
1000
  arg
1002
- }) => axios_default.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
1001
+ }) => instance.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
1003
1002
  );
1004
1003
  }
1005
1004
  function useRemoveUser() {
@@ -1007,7 +1006,7 @@ function useRemoveUser() {
1007
1006
  "/api/usystem/user/delete",
1008
1007
  (url3, {
1009
1008
  arg
1010
- }) => axios_default.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
1009
+ }) => instance.post(url3, arg, { headers: { [APP_ID_HEADER]: "global" } })
1011
1010
  );
1012
1011
  }
1013
1012
  var init_hooks2 = __esm({
@@ -1561,7 +1560,7 @@ var init_roleList = __esm({
1561
1560
  const { show: showUpdateModal, modal: updateModal } = useModal3();
1562
1561
  const remove = useRemoveRole();
1563
1562
  const handleUpdateBtnClick = async (record) => {
1564
- const response = await axios_default.get(
1563
+ const response = await instance.get(
1565
1564
  `/api/usystem/role/info${usePermissionApiV2 ? "V2" : ""}?name=${record.name}`,
1566
1565
  {
1567
1566
  headers: { [APP_ID_HEADER]: "global" }
@@ -1924,7 +1923,7 @@ var InfiniteList = (props) => {
1924
1923
  } = useSWRInfinite(
1925
1924
  getKey,
1926
1925
  async (arg) => {
1927
- const response = await axios_default({ url: arg, headers: _headers });
1926
+ const response = await instance({ url: arg, headers: _headers });
1928
1927
  return response.data.data;
1929
1928
  },
1930
1929
  {
@@ -1957,7 +1956,7 @@ var InfiniteList = (props) => {
1957
1956
  form.resetFields();
1958
1957
  await form.validateFields({ validateOnly: true });
1959
1958
  setSize(1);
1960
- } catch (_) {
1959
+ } catch (error) {
1961
1960
  setSize(0);
1962
1961
  }
1963
1962
  };
@@ -2593,7 +2592,8 @@ var signIn_default = SignIn;
2593
2592
 
2594
2593
  // src/index.ts
2595
2594
  init_storage();
2595
+ init_axios();
2596
2596
 
2597
- export { APP_ID_HEADER, DynamicTags_default as DynamicTags, ExpandableParagraph_default as ExpandableParagraph, FilterFormWrapper_default as FilterFormWrapper, Highlight_default as Highlight, InfiniteList_default as InfiniteList, Layout_default as Layout, NavMenu_default as NavMenu, notFound_default as NotFound, operationLogList_default as OperationLogList, PermissionButton_default as PermissionButton, QueryList_default as QueryList, QueryListAction, RequireGame_default as RequireGame, RequirePermission_default as RequirePermission, SSO_URL, signIn_default as SignIn, Provider_default as ToolkitsProvider, UserWidget_default as UserWidget, mixedStorage, permission_default as permissionRoutes, toolkitsStore, useFormModal, useLayoutStore, useModal, useModalStore, usePermission, usePermissions, useQueryListStore, useToolkitsStore };
2597
+ export { APP_ID_HEADER, DynamicTags_default as DynamicTags, ExpandableParagraph_default as ExpandableParagraph, FilterFormWrapper_default as FilterFormWrapper, Highlight_default as Highlight, InfiniteList_default as InfiniteList, Layout_default as Layout, NavMenu_default as NavMenu, notFound_default as NotFound, operationLogList_default as OperationLogList, PermissionButton_default as PermissionButton, QueryList_default as QueryList, QueryListAction, RequireGame_default as RequireGame, RequirePermission_default as RequirePermission, SSO_URL, signIn_default as SignIn, Provider_default as ToolkitsProvider, UserWidget_default as UserWidget, instance, mixedStorage, permission_default as permissionRoutes, registerInterceptors, toolkitsStore, useFormModal, useLayoutStore, useModal, useModalStore, usePermission, usePermissions, useQueryListStore, useToolkitsStore };
2598
2598
  //# sourceMappingURL=index.js.map
2599
2599
  //# sourceMappingURL=index.js.map