Files
Artveris/apps/client/src/api.ts
T

58 lines
2.2 KiB
TypeScript

import axios from 'axios';
import { i18n } from './locales';
const instance = axios.create({ baseURL: '/api', timeout: 20000 });
instance.interceptors.request.use((config) => {
config.headers = config.headers ?? {};
config.headers['Accept-Language'] = String(i18n.global.locale.value || 'zh-CN');
return config;
});
instance.interceptors.response.use(
(res) => {
const body = res.data;
if (body && typeof body === 'object' && 'code' in body) {
return body.code === 0 ? body.data : Promise.reject(body);
}
return body;
},
(err) => Promise.reject(err),
);
export function fetchTrace(shortCode: string): Promise<any> {
return instance.get(`/public/trace/${shortCode}`) as unknown as Promise<any>;
}
export function fetchTraceOutboundImages(
shortCode: string,
page: number,
pageSize = 20,
preview?: { token?: string; state?: 'inactive' | 'active' },
): Promise<any> {
return instance.get(`/public/trace/${shortCode}/outbound-images`, {
params: { page, pageSize, ...preview },
}) as unknown as Promise<any>;
}
// 后台预览:凭时间令牌访问,零副作用(不激活/不计数/不写日志);state 控制未激活/已激活形态
export function fetchTracePreview(shortCode: string, token: string, state: 'inactive' | 'active'): Promise<any> {
return instance.get(`/public/trace/${shortCode}/preview`, { params: { token, state } }) as unknown as Promise<any>;
}
export function fetchAppInfo(): Promise<any> {
return instance.get('/settings/public') as unknown as Promise<any>;
}
export interface TraceCertificateResult {
status: 'NONE' | 'PENDING' | 'SUBMITTING' | 'CHECKING' | 'WAITING' | 'READY' | 'UNKNOWN' | 'FAILED';
url: string | null;
generatedAt: string | null;
}
export function requestTraceCertificate(shortCode: string): Promise<TraceCertificateResult> {
return instance.post(`/public/trace/${encodeURIComponent(shortCode)}/certificate`, {}) as unknown as Promise<TraceCertificateResult>;
}
export function fetchTraceCertificate(shortCode: string): Promise<TraceCertificateResult> {
return instance.get(`/public/trace/${encodeURIComponent(shortCode)}/certificate`) as unknown as Promise<TraceCertificateResult>;
}