Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'umi-request' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
description: '您的网络发生异常,无法连接服务器',
message: '网络异常',
});
}
} catch (ie) {
notification.error({
description: '响应数据异常',
message: '服务器异常',
});
}
return response;
};
/** 配置 request 请求时的默认参数 */
export let umiRequest = extend({
errorHandler, // 默认错误处理
credentials: 'same-origin', // 默认请求是否带上cookie
});
/** 动态设置请求头 */
export function setRequestHeader(headers: Record) {
umiRequest = extend({
errorHandler, // 默认错误处理
headers,
credentials: 'same-origin', // 默认请求是否带上cookie
});
}
description: errorText,
});
router.push('/exception/403');
} else {
notification.error({
message: `请求错误 ${status}: ${url}`,
description: errorText,
});
}
}
};
/**
* 配置request请求时的默认参数
*/
const request = extend({
errorHandler, // 默认错误处理
credentials: 'include', // 默认请求是否带上cookie
});
request.interceptors.request.use((url, options) => {
options.headers = {
...options.headers,
'Authorization': `Bearer ${sessionStorage.getItem('token')}`
}
return (
{
url: `${url}`,
options: {
...options,
interceptors: true,
},
const { response } = error;
if (response && response.status) {
const errorText = codeMessage[response.status] || response.statusText;
const { status, url } = response;
notification.error({
message: `请求错误 ${status}: ${url}`,
description: errorText,
});
}
};
/**
* 配置request请求时的默认参数
*/
const request = extend({
errorHandler, // 默认错误处理
credentials: 'include', // 默认请求是否带上cookie
});
export default request;
const errorHandlerRe = response => {
if (response && response.status) {
const errorText = codeMessage[response.status] || response.statusText;
const { status, url } = response;
notification.error({
message: `请求错误 ${status}: ${url}`,
description: errorText,
});
}
};
/**
* 配置request请求时的默认参数
*/
const request = extend({
errorHandler,
// 默认错误处理
credentials: 'include', // 默认请求是否带上cookie
});
// 返回值统一处理
request.interceptors.response.use(async response => {
if (response.status >= 400) {
errorHandlerRe(response);
}
const data = await response.clone().json();
if (data && data.code === 10001) {
message.config({
top: '15%',
});
message.error(data.msg);
});
// environment should not be used
if (status === 403) {
router.push('/exception/403');
return;
}
if (status <= 504 && status >= 500) {
router.push('/exception/500');
return;
}
if (status >= 404 && status < 422) {
router.push('/exception/404');
}
};
const request = extend({
errorHandler,
credentials: 'include',
headers: {
Authorization: `JWT ${localStorage.getItem('cello-token')}`,
},
});
export default request;
*/
const errorHandler = (error: ResponseError) => {
const { response = {} as Response } = error;
const errortext = codeMessage[response.status] || response.statusText;
const { status, url } = response;
notification.error({
message: `请求错误 ${status}: ${url}`,
description: errortext,
});
};
/**
* 配置request请求时的默认参数
*/
const request = extend({
errorHandler, // 默认错误处理
credentials: 'include', // 默认请求是否带上cookie
});
export default request;
function statusHandler(status: number, message: string) {
if (status !== 200) {
notification.error({
message: status + ': ' + message,
});
return true;
}
return false;
}
* 异常处理程序
*/
export const errorHandler = (error: ResponseError) => {
const { response = {} as Response } = error;
const errorText = codeMessage[response.status] || response.statusText;
const { status } = response;
notification.error({
message: `请求错误 ${status}:`,
description: errorText,
});
};
/**
* 配置request请求时的默认参数
*/
const request = extend({
errorHandler, // 默认错误处理
credentials: 'include', // 默认请求是否带上cookie
});
export default request;
const errorHandler = async (error) => {
const { response } = error;
if (response && response.status) {
const errorText = codeMessage[response.status] || response.statusText;
const { status, url } = response;
message.error(`请求错误 ${status}:${url} ${errorText}`);
} else if (!response) {
message.error('网络异常: 您的网络发生异常,无法连接服务器');
}
};
/**
* 配置request请求时的默认参数
*/
const request = extend({
errorHandler, // 默认错误处理
credentials: 'include', // 默认请求是否带上cookie
});
/*
* 1) code == 500
* data 为报错字符串
* 2) code == 200
* 如果 method = Get,data 类型 = list/json
* 否则,data 类型 = string,存储的是操作成功的信息
*/
request.interceptors.response.use(async (response) => {
if (response.status !== 200 && response.status !== 500) {
errorHandler({ response });
return;
}
load = async () => {
try {
if (process.env.NODE_ENV !== 'development') {
this.config = await request.get(`${config.resourceHost}/config.json`);
}
runInAction(() => {
this.isLatestVersion = !hasUpdate(this.config.chromeWebStoreVersion, this.localVersion);
});
const iconsFile = await request.get(this.config.iconfont);
const matchResult: string[] = iconsFile.match(/id="([A-Za-z]+)"/g) || [];
const remoteIcons = matchResult.map(o => o.match(/id="([A-Za-z]+)"/)![1]);
runInAction(() => {
remoteIcons.forEach(icon => {
this.remoteIconSet.add(icon);
});
});
} catch (_error) {
console.log('Load Config Error');
}
};
load = async () => {
try {
if (process.env.NODE_ENV !== 'development') {
this.config = await request.get(`${config.resourceHost}/config.json`);
}
runInAction(() => {
this.isLatestVersion = !hasUpdate(this.config.chromeWebStoreVersion, this.localVersion);
});
const iconsFile = await request.get(this.config.iconfont);
const matchResult: string[] = iconsFile.match(/id="([A-Za-z]+)"/g) || [];
const remoteIcons = matchResult.map(o => o.match(/id="([A-Za-z]+)"/)![1]);
runInAction(() => {
remoteIcons.forEach(icon => {
this.remoteIconSet.add(icon);
});
});
} catch (_error) {
console.log('Load Config Error');
}
};