Files
DailyHotApi/src/utils/getData.ts
2024-05-29 10:11:06 +08:00

101 lines
3.1 KiB
TypeScript

import type { Get, Post } from "../types.ts";
import { config } from "../config.js";
import { getCache, setCache, delCache } from "./cache.js";
import logger from "./logger.js";
import axios from "axios";
// 基础配置
const request = axios.create({
// 请求超时设置
timeout: config.REQUEST_TIMEOUT,
withCredentials: true,
});
// 请求拦截
request.interceptors.request.use(
(request) => {
if (!request.params) request.params = {};
// 发送请求
return request;
},
(error) => {
logger.error("请求失败,请稍后重试");
return Promise.reject(error);
},
);
// 响应拦截
request.interceptors.response.use(
(response) => {
return response;
},
(error) => {
// 继续传递错误
return Promise.reject(error);
},
);
// GET
export const get = async (options: Get) => {
const { url, headers, params, noCache, ttl = config.CACHE_TTL, originaInfo = false } = options;
logger.info("发起 GET 请求", options);
try {
// 检查缓存
if (noCache) delCache(url);
else {
const cachedData = getCache(url);
if (cachedData) {
logger.info("采用缓存", { url });
return { fromCache: true, data: cachedData.data, updateTime: cachedData.updateTime };
}
}
// 缓存不存在时请求接口
logger.info("请求接口", { url });
const response = await request.get(url, { headers, params });
const responseData = response?.data || response;
// 存储新获取的数据到缓存
const updateTime = new Date().toISOString();
const data = originaInfo ? response : responseData;
setCache(url, { data, updateTime }, ttl);
// 返回数据
logger.info("接口调用成功", { status: response?.statusText });
return { fromCache: false, data, updateTime };
} catch (error) {
logger.error("GET 请求出错", error);
throw error;
}
};
// POST
export const post = async (options: Post) => {
const { url, headers, body, noCache, ttl = config.CACHE_TTL, originaInfo = false } = options;
logger.info("发起 POST 请求", options);
try {
// 检查缓存
if (noCache) delCache(url);
else {
const cachedData = getCache(url);
if (cachedData) {
logger.info("采用缓存", { url });
return { fromCache: true, data: cachedData.data, updateTime: cachedData.updateTime };
}
}
// 缓存不存在时请求接口
logger.info("请求接口", { url });
const response = await request.post(url, body, { headers });
const responseData = response?.data || response;
// 存储新获取的数据到缓存
const updateTime = new Date().toISOString();
const data = originaInfo ? response : responseData;
if (!noCache) {
setCache(url, { data, updateTime }, ttl);
}
// 返回数据
logger.info("接口调用成功", { status: response?.statusText });
return { fromCache: false, data, updateTime };
} catch (error) {
logger.error("POST 请求出错", error);
throw error;
}
};