From af71c348f47fe60f58dcf9abf730e3ca9ae665ea Mon Sep 17 00:00:00 2001 From: lik Date: Thu, 12 Nov 2020 17:23:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86@ehason/utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .vscode/launch.json | 17 +++++ utils/impl/httpstatus.js | 139 +++++++++++++++++++++++++++++++++++++++ utils/impl/koaresp.js | 6 +- utils/index.js | 3 +- utils/package.json | 2 +- 6 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 utils/impl/httpstatus.js diff --git a/.gitignore b/.gitignore index e55cbd2..35c3181 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ /auth/.idea/ /utils/.idea/ /utils/node_modules/ +/test/node_modules/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8fd6852 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "test-debug", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/test/test.js" + } + ] +} \ No newline at end of file diff --git a/utils/impl/httpstatus.js b/utils/impl/httpstatus.js new file mode 100644 index 0000000..cbc2968 --- /dev/null +++ b/utils/impl/httpstatus.js @@ -0,0 +1,139 @@ +const Status = { + Continue: 100, + SwitchingProtocols: 101, + Processing: 102, + EarlyHints: 103, + + OK: 200, + Created: 201, + Accepted: 202, + NonAuthoritativeInfo: 203, + NoContent: 204, + ResetContent: 205, + PartialContent: 206, + Multi: 207, + AlreadyReported: 208, + IMUsed: 226, + + MultipleChoices: 300, + MovedPermanently: 301, + Found: 302, + SeeOther: 303, + NotModified: 304, + UseProxy: 305, + TemporaryRedirect: 307, + PermanentRedirect: 308, + + BadRequest: 400, + Unauthorized: 401, + PaymentRequired: 402, + Forbidden: 403, + NotFound: 404, + MethodNotAllowed: 405, + NotAcceptable: 406, + ProxyAuthRequired: 407, + RequestTimeout: 408, + Conflict: 409, + Gone: 410, + LengthRequired: 411, + PreconditionFailed: 412, + RequestEntityTooLarge: 413, + RequestURITooLong: 414, + UnsupportedMediaType: 415, + RequestedRangeNotSatisfiable: 416, + ExpectationFailed: 417, + Teapot: 418, + MisdirectedRequest: 421, + UnprocessableEntity: 422, + Locked: 423, + FailedDependency: 424, + TooEarly: 425, + UpgradeRequired: 426, + PreconditionRequired: 428, + TooManyRequests: 429, + RequestHeaderFieldsTooLarge: 431, + UnavailableForLegalReasons: 451, + + InternalServerError: 500, + NotImplemented: 501, + BadGateway: 502, + ServiceUnavailable: 503, + GatewayTimeout: 504, + HTTPVersionNotSupported: 505, + VariantAlsoNegotiates: 506, + InsufficientStorage: 507, + LoopDetected: 508, + NotExtended: 510, + NetworkAuthenticationRequired: 511 +} + +const StatusStr = { + Continue: "Continue", + SwitchingProtocols: "Switching Protocols", + Processing: "Processing", + EarlyHints: "Early Hints", + + OK: "OK", + Created: "Created", + Accepted: "Accepted", + NonAuthoritativeInfo: "Non-Authoritative Information", + NoContent: "No Content", + ResetContent: "Reset Content", + PartialContent: "Partial Content", + Multi: "Multi-", + AlreadyReported: "Already Reported", + IMUsed: "IM Used", + + MultipleChoices: "Multiple Choices", + MovedPermanently: "Moved Permanently", + Found: "Found", + SeeOther: "See Other", + NotModified: "Not Modified", + UseProxy: "Use Proxy", + TemporaryRedirect: "Temporary Redirect", + PermanentRedirect: "Permanent Redirect", + + BadRequest: "Bad Request", + Unauthorized: "Unauthorized", + PaymentRequired: "Payment Required", + Forbidden: "Forbidden", + NotFound: "Not Found", + MethodNotAllowed: "Method Not Allowed", + NotAcceptable: "Not Acceptable", + ProxyAuthRequired: "Proxy Authentication Required", + RequestTimeout: "Request Timeout", + Conflict: "Conflict", + Gone: "Gone", + LengthRequired: "Length Required", + PreconditionFailed: "Precondition Failed", + RequestEntityTooLarge: "Request Entity Too Large", + RequestURITooLong: "Request URI Too Long", + UnsupportedMediaType: "Unsupported Media Type", + RequestedRangeNotSatisfiable: "Requested Range Not Satisfiable", + ExpectationFailed: "Expectation Failed", + Teapot: "I'm a teapot", + MisdirectedRequest: "Misdirected Request", + UnprocessableEntity: "Unprocessable Entity", + Locked: "Locked", + FailedDependency: "Failed Dependency", + TooEarly: "Too Early", + UpgradeRequired: "Upgrade Required", + PreconditionRequired: "Precondition Required", + TooManyRequests: "Too Many Requests", + RequestHeaderFieldsTooLarge: "Request Header Fields Too Large", + UnavailableForLegalReasons: "Unavailable For Legal Reasons", + + InternalServerError: "Internal Server Error", + NotImplemented: "Not Implemented", + BadGateway: "Bad Gateway", + ServiceUnavailable: "Service Unavailable", + GatewayTimeout: "Gateway Timeout", + HTTPVersionNotSupported: "HTTP Version Not Supported", + VariantAlsoNegotiates: "Variant Also Negotiates", + InsufficientStorage: "Insufficient Storage", + LoopDetected: "Loop Detected", + NotExtended: "Not Extended", + NetworkAuthenticationRequired: "Network Authentication Required", +} + +module.exports = { Status, StatusStr } diff --git a/utils/impl/koaresp.js b/utils/impl/koaresp.js index 4bdac45..0d242da 100644 --- a/utils/impl/koaresp.js +++ b/utils/impl/koaresp.js @@ -8,10 +8,10 @@ function ctxresp(ctx, data, errno, errmsg) { if (data === null || data === undefined) data = {}; ctx.body = { - result: errno === 0 ? 'ok' : 'fail', - error: { code: errno, msg: errmsg }, + code: errno, + msg: errmsg, data: data, - stime: Moment().format('YYYY-MM-DD HH:mm:ss') + stime: Moment().format('YYYY-MM-DD HH:mm:ss SSS') }; } diff --git a/utils/index.js b/utils/index.js index 13bafca..3c458a1 100644 --- a/utils/index.js +++ b/utils/index.js @@ -2,5 +2,6 @@ module.exports = { prxrmq: require('./impl/prxrmq'), ctxresp: require('./impl/koaresp'), - crc: require('./impl/crc') + crc: require('./impl/crc'), + httpstatus: require('./impl/httpstatus') }; diff --git a/utils/package.json b/utils/package.json index 8a3c8e3..84c223a 100644 --- a/utils/package.json +++ b/utils/package.json @@ -1,6 +1,6 @@ { "name": "@ehason/utils", - "version": "1.1.2", + "version": "1.1.5", "description": "Utils for ehason develope", "main": "index.js", "scripts": {