Skip to content

Commit

Permalink
chore: fix mock api
Browse files Browse the repository at this point in the history
  • Loading branch information
dpyzo0o committed Dec 20, 2019
1 parent 2f2532e commit 5934df8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion config/dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
env: {
NODE_ENV: 'development',
NODE_ENV: '"development"',
},
defineConstants: {},
weapp: {},
Expand Down
2 changes: 1 addition & 1 deletion config/prod.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
env: {
NODE_ENV: 'production',
NODE_ENV: '"production"',
},
defineConstants: {},
weapp: {},
Expand Down
3 changes: 2 additions & 1 deletion src/service/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import apiDef from '~/constants/api';
import http from './http';
// import http from './http';
import http from './mock/http';

export const login = () => http.get({ url: apiDef.login });

Expand Down
24 changes: 12 additions & 12 deletions src/service/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Taro from '@tarojs/taro';

export interface FetchOption {
export interface IFetchOption {
/**
* 请求资源地址
*/
Expand Down Expand Up @@ -28,16 +28,16 @@ export interface FetchOption {
showErrorToast?: boolean;
}

export interface ResponseData {
export interface IResponse {
/**
* 后台定义的状态码
*/
errorCode: number;
code: number;

/**
* 后台定义的 message
*/
errorMessage: string;
message: string;

/**
* 实际返回数据
Expand All @@ -53,7 +53,7 @@ const CODE_SUCCESS = 200;

Taro.addInterceptor(Taro.interceptors.logInterceptor);

async function fetch(option: FetchOption) {
async function fetch(option: IFetchOption) {
const { url, data, header, method, showErrorToast = true } = option;

// 这里可以根据业务需求对 header 进行改造
Expand All @@ -62,27 +62,27 @@ async function fetch(option: FetchOption) {
* 根据后台实现做相应的修改, 小程序只要成功接收到服务器返回, 无论 statusCode 是多少
* 都会成功返回, 不会抛出错误, 所以需要根据后台实际返回的状态码来判断请求是否成功
*/
return Taro.request<ResponseData>({
return Taro.request<IResponse>({
url,
method,
header,
data,
})
.then(res => {
const { errorCode } = res.data;
const { code } = res.data;

if (errorCode !== CODE_SUCCESS) {
if (code !== CODE_SUCCESS) {
return Promise.reject(res.data);
}

return res.data;
})
.catch(err => {
console.error(err.error || err.errorMessage || '请求异常');
console.error(err.error || err.message || '请求异常');

if (showErrorToast) {
Taro.showToast({
title: err.error || err.errorMessage || '请求异常',
title: err.error || err.message || '请求异常',
icon: 'none',
duration: 2000,
});
Expand All @@ -94,10 +94,10 @@ async function fetch(option: FetchOption) {
}

export default {
get(option: FetchOption) {
get(option: IFetchOption) {
return fetch({ ...option, method: 'GET' });
},
post(option: FetchOption) {
post(option: IFetchOption) {
return fetch({ ...option, method: 'POST' });
},
};
14 changes: 7 additions & 7 deletions src/service/mock/data.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import apiDef from '~/constants/api';
import { ResponseData } from '../http';
import { IResponse } from '../http';

export interface ResponseFunc {
(payload: any): ResponseData;
(payload: any): IResponse;
}

type MockData = Record<string, ResponseData | ResponseFunc>;
type MockData = Record<string, IResponse | ResponseFunc>;

const mockData: MockData = {
[apiDef.login]: {
errorCode: 200,
errorMessage: 'success',
code: 200,
message: 'success',
data: {
name: 'dpyzo0o',
age: 100,
},
},
[apiDef.test]: () => ({
errorCode: 200,
errorMessage: 'success',
code: 200,
message: 'success',
data: {
greeting: 'hello taro-toolkit',
},
Expand Down
23 changes: 11 additions & 12 deletions src/service/mock/http.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import apiDef from '~/constants/api';
import data, { ResponseFunc } from './data';
import { FetchOption, ResponseData } from '../http';
import data from './data';
import { IFetchOption } from '../http';

function fetch(option: FetchOption) {
function fetch(option: IFetchOption) {
const { url, data: payload } = option;
const delay = Math.floor(Math.random() * 1000);

Expand All @@ -13,34 +13,33 @@ function fetch(option: FetchOption) {

return new Promise((resolve, reject) => {
const key = Object.keys(apiDef).find(k => apiDef[k] === url) as string;
const result = data[apiDef[key]];

setTimeout(() => {
const res =
typeof data[key] === 'function'
? (data[key] as ResponseFunc)(payload)
: (data[key] as ResponseData);
const res = typeof result === 'function' ? result(payload) : result;

console.group('%c RECEIVE', 'color: #428df5');
console.log('Url ->', url);
console.log('Results -> ', res);
console.groupEnd();

if (res.errorCode === 200) {
if (res.code === 200) {
resolve(res);
} else {
reject({
errorCode: res.errorCode,
errorMessage: res.errorMessage,
code: res.code,
message: res.message,
});
}
}, delay);
});
}

export default {
get(option: FetchOption) {
get(option: IFetchOption) {
return fetch(option);
},
post(option: FetchOption) {
post(option: IFetchOption) {
return fetch(option);
},
};

0 comments on commit 5934df8

Please sign in to comment.