Skip to content

Commit

Permalink
🎉 chore: project init
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Apr 23, 2022
0 parents commit 5275f22
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
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
24 changes: 24 additions & 0 deletions .gitignore
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1 align="center">
Pansy Request
</h1>

25 changes: 25 additions & 0 deletions package.json
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"
}
}
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/config.ts
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,
}
13 changes: 13 additions & 0 deletions src/index.ts
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';
60 changes: 60 additions & 0 deletions src/request.ts
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);
}
});
});
}
54 changes: 54 additions & 0 deletions src/types.ts
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>>;
}

0 comments on commit 5275f22

Please sign in to comment.