-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5275f22
Showing
9 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[Makefile] | ||
indent_style = tab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/npm-debug.log* | ||
/yarn-error.log | ||
/yarn.lock | ||
/package-lock.json | ||
|
||
# production | ||
/dist | ||
/lib | ||
/es | ||
|
||
/report | ||
|
||
# misc | ||
.DS_Store | ||
|
||
# umi | ||
/src/.umi | ||
/src/.umi-production | ||
/src/.umi-test | ||
/.env.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<h1 align="center"> | ||
Pansy Request | ||
</h1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@pansy/request", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/pansyjs/request.git" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/pansyjs/request/issues" | ||
}, | ||
"homepage": "https://github.com/pansyjs/request#readme", | ||
"dependencies": { | ||
"axios": "^0.26.1" | ||
}, | ||
"devDependencies": { | ||
"husky": "^7.0.4" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export enum ErrorShowType { | ||
/** 不做处理 */ | ||
SILENT = 0, | ||
/** 全局提示 - 警告 */ | ||
WARN_MESSAGE = 1, | ||
/** 全局提示 - 异常 */ | ||
ERROR_MESSAGE = 2, | ||
/** 通知提醒 */ | ||
NOTIFICATION = 3, | ||
/** 重定向 */ | ||
REDIRECT = 9, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export { request, setConfig } from './request'; | ||
|
||
export { ErrorShowType } from './config'; | ||
|
||
export type { | ||
RequestConfig | ||
} from './types'; | ||
|
||
export type { | ||
AxiosInstance, | ||
AxiosRequestConfig, | ||
AxiosResponse, | ||
} from 'axios'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import axios from 'axios'; | ||
|
||
import type { AxiosInstance, } from 'axios'; | ||
import type { | ||
Request, | ||
RequestError, | ||
RequestConfig, | ||
} from './types'; | ||
|
||
let requestInstance: AxiosInstance; | ||
let config: RequestConfig = {}; | ||
|
||
const getRequestInstance = (): AxiosInstance => { | ||
if (requestInstance) return requestInstance; | ||
|
||
requestInstance = axios.create(config); | ||
|
||
requestInstance.interceptors.response.use((response)=>{ | ||
const {data} = response; | ||
const adaptor = config?.errorConfig?.adaptor || ((resData) => resData); | ||
const errorInfo = adaptor(data,response); | ||
if(errorInfo.success === false){ | ||
const error: RequestError = new Error(errorInfo.errorMessage); | ||
error.name = 'BizError'; | ||
error.data = data; | ||
error.info = errorInfo; | ||
throw error; | ||
} | ||
return response; | ||
}); | ||
|
||
return requestInstance; | ||
} | ||
|
||
export const setConfig = (data: RequestConfig = {}) => { | ||
config = data; | ||
} | ||
|
||
export const request: Request = (url, opts) => { | ||
const requestInstance = getRequestInstance(); | ||
|
||
return new Promise((resolve, reject) => { | ||
requestInstance | ||
.request({ ...opts, url }) | ||
.then((res) => { | ||
const formatResultAdaptor = | ||
config?.formatResultAdaptor || ((res) => res.data); | ||
resolve(formatResultAdaptor(res)); | ||
}) | ||
.catch((error) => { | ||
try { | ||
const handler = config.errorConfig?.errorHandler; | ||
|
||
handler?.(error, opts, config); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ErrorShowType } from './config'; | ||
|
||
import type { | ||
AxiosResponse, | ||
AxiosRequestConfig, | ||
} from 'axios'; | ||
|
||
export interface RequestData<D = any> { | ||
/** 状态码 */ | ||
code: number; | ||
/** 数据 */ | ||
data: D, | ||
/** 报错信息 */ | ||
message: string; | ||
/** 错误信息类型 */ | ||
showType?: ErrorShowType; | ||
[key: string]: any; | ||
} | ||
|
||
interface Adaptor { | ||
(resData: any, response: AxiosResponse): RequestData; | ||
} | ||
|
||
export interface FormatResultAdaptor { | ||
(res: AxiosResponse): any; | ||
} | ||
|
||
export interface RequestError extends Error { | ||
data?: any; | ||
info?: RequestData; | ||
} | ||
|
||
interface ErrorHandler { | ||
(error: RequestError, opts: AxiosRequestConfig & { skipErrorHandler?: boolean }, config: RequestConfig): void; | ||
} | ||
|
||
export interface RequestConfig extends AxiosRequestConfig { | ||
errorConfig?: { | ||
errorPage?: string; | ||
// adaptor 用以用户将不满足接口的后端数据修改成 errorInfo | ||
adaptor?: Adaptor; | ||
errorHandler?: ErrorHandler; | ||
defaultNoneResponseErrorMessage?: string; | ||
defaultRequestErrorMessage?: string; | ||
}; | ||
formatResultAdaptor?: FormatResultAdaptor; | ||
} | ||
|
||
export interface Request { | ||
( | ||
url: string, | ||
opts: AxiosRequestConfig & { skipErrorHandler?: boolean }, | ||
): Promise<AxiosResponse<any, any>>; | ||
} |