From 3bbd2a1cb5f59ee47a5796488dd367fa5604d560 Mon Sep 17 00:00:00 2001 From: colin Date: Mon, 21 Apr 2025 17:47:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(vben5):=20=E5=A2=9E=E5=8A=A0http=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=BB=A3=E7=A0=81hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../@abp/request/src/constants/httpStatus.ts | 44 +++++++++++ .../@abp/request/src/constants/index.ts | 1 + .../packages/@abp/request/src/hooks/index.ts | 1 + .../request/src/hooks/useHttpStatusCodeMap.ts | 76 +++++++++++++++++++ apps/vben5/packages/@abp/request/src/index.ts | 1 + 5 files changed, 123 insertions(+) create mode 100644 apps/vben5/packages/@abp/request/src/constants/httpStatus.ts create mode 100644 apps/vben5/packages/@abp/request/src/constants/index.ts create mode 100644 apps/vben5/packages/@abp/request/src/hooks/useHttpStatusCodeMap.ts diff --git a/apps/vben5/packages/@abp/request/src/constants/httpStatus.ts b/apps/vben5/packages/@abp/request/src/constants/httpStatus.ts new file mode 100644 index 000000000..4256fe315 --- /dev/null +++ b/apps/vben5/packages/@abp/request/src/constants/httpStatus.ts @@ -0,0 +1,44 @@ +export enum HttpStatusCode { + Accepted = 202, + Ambiguous = 300, + BadGateway = 502, + BadRequest = 400, + Conflict = 409, + Continue = 100, + Created = 201, + ExpectationFailed = 417, + Forbidden = 403, + GatewayTimeout = 504, + Gone = 410, + HttpVersionNotSupported = 505, + InternalServerError = 500, + LengthRequired = 411, + MethodNotAllowed = 405, + Moved = 301, + NoContent = 204, + NonAuthoritativeInformation = 203, + NotAcceptable = 406, + NotFound = 404, + NotImplemented = 501, + NotModified = 304, + OK = 200, + PartialContent = 206, + PaymentRequired = 402, + PreconditionFailed = 412, + ProxyAuthenticationRequired = 407, + Redirect = 302, + RedirectKeepVerb = 307, + RedirectMethod = 303, + RequestedRangeNotSatisfiable = 416, + RequestEntityTooLarge = 413, + RequestTimeout = 408, + RequestUriTooLong = 414, + ResetContent = 205, + ServiceUnavailable = 503, + SwitchingProtocols = 101, + Unauthorized = 401, + UnsupportedMediaType = 415, + Unused = 306, + UpgradeRequired = 426, + UseProxy = 305, +} diff --git a/apps/vben5/packages/@abp/request/src/constants/index.ts b/apps/vben5/packages/@abp/request/src/constants/index.ts new file mode 100644 index 000000000..6b6cc383f --- /dev/null +++ b/apps/vben5/packages/@abp/request/src/constants/index.ts @@ -0,0 +1 @@ +export * from './httpStatus'; diff --git a/apps/vben5/packages/@abp/request/src/hooks/index.ts b/apps/vben5/packages/@abp/request/src/hooks/index.ts index 0c4767a5f..229c41551 100644 --- a/apps/vben5/packages/@abp/request/src/hooks/index.ts +++ b/apps/vben5/packages/@abp/request/src/hooks/index.ts @@ -1,3 +1,4 @@ export * from './useErrorFormat'; +export * from './useHttpStatusCodeMap'; export * from './useRequest'; export * from './useWrapperResult'; diff --git a/apps/vben5/packages/@abp/request/src/hooks/useHttpStatusCodeMap.ts b/apps/vben5/packages/@abp/request/src/hooks/useHttpStatusCodeMap.ts new file mode 100644 index 000000000..ac442b7d1 --- /dev/null +++ b/apps/vben5/packages/@abp/request/src/hooks/useHttpStatusCodeMap.ts @@ -0,0 +1,76 @@ +import { HttpStatusCode } from '../constants/httpStatus'; + +export function useHttpStatusCodeMap() { + const httpStatusCodeMap: { [key: number]: string } = { + [HttpStatusCode.Accepted]: '202 - Accepted', + [HttpStatusCode.Ambiguous]: '300 - Ambiguous/Multiple Choices', + [HttpStatusCode.BadGateway]: '502 - Bad Gateway', + [HttpStatusCode.BadRequest]: '400 - Bad Request', + [HttpStatusCode.Conflict]: '409 - Conflict', + [HttpStatusCode.Continue]: '100 - Continue', + [HttpStatusCode.Created]: '201 - Created', + [HttpStatusCode.ExpectationFailed]: '417 - Expectation Failed', + [HttpStatusCode.Forbidden]: '403 - Forbidden', + [HttpStatusCode.GatewayTimeout]: '504 - Gateway Timeout', + [HttpStatusCode.Gone]: '410 - Gone', + [HttpStatusCode.HttpVersionNotSupported]: + '505 - Http Version Not Supported', + [HttpStatusCode.InternalServerError]: '500 - Internal Server Error', + [HttpStatusCode.LengthRequired]: '411 - Length Required', + [HttpStatusCode.MethodNotAllowed]: '405 - Method Not Allowed', + [HttpStatusCode.Moved]: '301 - Moved/Moved Permanently', + [HttpStatusCode.NoContent]: '204 - No Content', + [HttpStatusCode.NonAuthoritativeInformation]: + '203 - Non Authoritative Information', + [HttpStatusCode.NotAcceptable]: '406 - Not Acceptable', + [HttpStatusCode.NotFound]: '404 - Not Found', + [HttpStatusCode.NotImplemented]: '501 - Not Implemented', + [HttpStatusCode.NotModified]: '304 - Not Modified', + [HttpStatusCode.OK]: '200 - OK', + [HttpStatusCode.PartialContent]: '206 - Partial Content', + [HttpStatusCode.PaymentRequired]: '402 - Payment Required', + [HttpStatusCode.PreconditionFailed]: '412 - Precondition Failed', + [HttpStatusCode.ProxyAuthenticationRequired]: + '407 - Proxy Authentication Required', + [HttpStatusCode.Redirect]: '302 - Found/Redirect', + [HttpStatusCode.RedirectKeepVerb]: + '307 - Redirect Keep Verb/Temporary Redirect', + [HttpStatusCode.RedirectMethod]: '303 - Redirect Method/See Other', + [HttpStatusCode.RequestedRangeNotSatisfiable]: + '416 - Requested Range Not Satisfiable', + [HttpStatusCode.RequestEntityTooLarge]: '413 - Request Entity Too Large', + [HttpStatusCode.RequestTimeout]: '408 - Request Timeout', + [HttpStatusCode.RequestUriTooLong]: '414 - Request Uri Too Long', + [HttpStatusCode.ResetContent]: '205 - Reset Content', + [HttpStatusCode.ServiceUnavailable]: '503 - Service Unavailable', + [HttpStatusCode.SwitchingProtocols]: '101 - Switching Protocols', + [HttpStatusCode.Unauthorized]: '401 - Unauthorized', + [HttpStatusCode.UnsupportedMediaType]: '415 - Unsupported Media Type', + [HttpStatusCode.Unused]: '306 - Unused', + [HttpStatusCode.UpgradeRequired]: '426 - Upgrade Required', + [HttpStatusCode.UseProxy]: '305 - Use Proxy', + }; + + function getHttpStatusColor(statusCode: HttpStatusCode) { + if (statusCode < 200) { + return 'default'; + } + if (statusCode >= 200 && statusCode < 300) { + return 'success'; + } + if (statusCode >= 300 && statusCode < 400) { + return 'processing'; + } + if (statusCode >= 400 && statusCode < 500) { + return 'warning'; + } + if (statusCode >= 500) { + return 'error'; + } + } + + return { + getHttpStatusColor, + httpStatusCodeMap, + }; +} diff --git a/apps/vben5/packages/@abp/request/src/index.ts b/apps/vben5/packages/@abp/request/src/index.ts index 5c13205f7..af59094a0 100644 --- a/apps/vben5/packages/@abp/request/src/index.ts +++ b/apps/vben5/packages/@abp/request/src/index.ts @@ -6,6 +6,7 @@ import type { RequestClientOptions } from '@vben/request'; import { useAppConfig } from '@vben/hooks'; import { RequestClient } from '@vben/request'; +export * from './constants'; export * from './hooks'; export * from './types';