Something went wrong. Try again.
Self-hosted file management and sharing system, supports multiple storage providers. forked from tdvorak.tngl.sh/cloudreve
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294import axios, { AxiosRequestConfig } from "axios";import { enqueueSnackbar, SnackbarAction } from "notistack";import { DefaultCloseAction, ErrorListDetailAction } from "../component/Common/Snackbar/snackbar.tsx";import i18n from "../i18n.ts";import { AppThunk } from "../redux/store.ts";import { openLockConflictDialog } from "../redux/thunks/dialog.ts";import { router } from "../router";import SessionManager from "../session";import { ErrNames } from "../session/errors.ts";import { sendRefreshToken } from "./api.ts";
export interface requestOpts { errorSnackbarMsg: (e: Error) => string; bypassSnackbar?: (e: Error) => boolean; noCredential: boolean; skipBatchError?: boolean; skipLockConflict?: boolean; withHost?: boolean; acceptBatchPartialSuccess?: boolean;}
export const defaultOpts: requestOpts = { errorSnackbarMsg: (e) => e.message, noCredential: false, skipBatchError: false,};
export const ApiPrefix = "/api/v4";
const instance = axios.create({ baseURL: ApiPrefix,});export interface AggregatedError<T> { [key: string]: Response<T>;}
export interface Response<T> { data: T; code: number; msg: string; error?: string; correlation_id?: string; aggregated_error?: AggregatedError<T>;}
export class AppError extends Error { public code: any; public cid: string | undefined = undefined; public aggregatedError: AggregatedError<any> | undefined = undefined; public rawMessage: string = ""; public error?: string = undefined; public response: Response<any>;
constructor(resp: Response<any>) { const message = resp.msg; const code = resp.code; super(message);
this.response = resp; this.code = code; const error = resp.error; this.cid = resp.correlation_id; this.aggregatedError = resp.aggregated_error; this.rawMessage = message; this.error = error;
if (i18n.exists(`errors.${code}`, { ns: "common" })) { this.message = i18n.t(`errors.${code}`, { ns: "common", message, }); } else if (i18n.exists(`errors.${code}`, { ns: "dashboard" })) { this.message = i18n.t(`errors.${code}`, { ns: "dashboard", message, }); } else { this.message = message || i18n.t("unknownError", { ns: "common" }); }
this.message += error && !this.message.includes(error) ? ` (${error})` : ""; this.stack = new Error().stack; }
ErrorResponse = (): Response<any> => { return this.response; };}//// instance.interceptors.response.use(// function (response: any) {// response.rawData = response.data;// response.data = response.data.data;// if (// response.rawData.code !== undefined &&// response.rawData.code !== 0 &&// response.rawData.code !== 203// ) {// // Login expired// if (response.rawData.code === 401) {// Auth.signout();// window.location.href =// "/login?redirect=" +// encodeURIComponent(// window.location.pathname + window.location.search// );// }//// // Non-admin// if (response.rawData.code === 40008) {// window.location.href = "/home";// }//// // Not binding mobile phone// if (response.rawData.code === 40010) {// window.location.href = "/setting?modal=phone";// }// throw new AppError(// response.rawData.msg,// response.rawData.code,// response.rawData.error// );// }// return response;// },// function (error) {// return Promise.reject(error);// }// );
export type ThunkResponse<T = any> = AppThunk<Promise<T>>;
export const Code = { Success: 0, Continue: 203, CredentialInvalid: 40020, IncorrectPassword: 40069, LockConflict: 40073, StaleVersion: 40076, ObjectExist: 40004, BatchOperationNotFullyCompleted: 40081, DomainNotLicensed: 40087, AnonymouseAccessDenied: 40088, VaultLocked: 40093, EntityNotExist: 40077, CodeLoginRequired: 401, PermissionDenied: 403, NodeFound: 404,};
export const CrHeaderPrefix = "X-Cr-";export const CrHeaders = { context_hint: CrHeaderPrefix + "Context-Hint",};
function getAccessToken(): AppThunk<Promise<string | undefined>> { return async (dispatch, _getState) => { try { const accessToken = await SessionManager.getAccessToken(); return `Bearer ${accessToken}`; } catch (e) { if (!(e instanceof Error)) { throw e; } switch (e.name) { case ErrNames.ErrNoAvailableSession: case ErrNames.ErrRefreshTokenExpired: case ErrNames.ErrNoSesssionSelected: return undefined; case ErrNames.ErrAccessTokenExpired: // try to refresh token console.log("refresh access token"); const newToken = await dispatch(refreshToken()); return `Bearer ${newToken}`; }
throw e; } };}
function refreshToken(): AppThunk<Promise<string>> { return async (dispatch, _getState) => { const user = SessionManager.currentLogin(); const token = await dispatch(sendRefreshToken({ refresh_token: user.token.refresh_token })); SessionManager.refreshToken(user.user.id, token); return token.access_token; };}
let signOutLock = false;
export function send<T = any>( url: string, config?: AxiosRequestConfig, opts: requestOpts = defaultOpts,): ThunkResponse<T> { return async (dispatch, _getState) => { try { let axiosConf: AxiosRequestConfig = { ...config, headers: { ...config?.headers, }, url, };
if (!opts.noCredential) { const token = await dispatch(getAccessToken()); if (token && axiosConf.headers) { axiosConf.headers["Authorization"] = token; } }
const resp = await instance.request<Response<T>>(axiosConf);
if (resp.data.code !== undefined && resp.data.code !== Code.Success) { switch (resp.data.code) { case Code.CredentialInvalid: case Code.CodeLoginRequired: if (!signOutLock) { SessionManager.signOutCurrent(); router.navigate( "/session?redirect=" + encodeURIComponent(window.location.pathname + window.location.search), ); } signOutLock = true; }
throw new AppError(resp.data); } return resp.data.data; } catch (e) { let partialSuccessResponse: any = undefined; if (e instanceof Error) { // Handle lock conflict error if (e instanceof AppError && e.code == Code.LockConflict && !opts.skipLockConflict) { let rejected = false; try { await dispatch(openLockConflictDialog(e.response)); } catch (e) { rejected = true; } if (!rejected) { return await dispatch(send(url, config, opts)); } }
if (opts.bypassSnackbar && opts.bypassSnackbar(e)) { throw e; }
let action: SnackbarAction = DefaultCloseAction; // Handle aggregated error if (e instanceof AppError && e.code == Code.BatchOperationNotFullyCompleted) { if (!opts.skipBatchError) { action = ErrorListDetailAction(e.ErrorResponse()); if (opts.acceptBatchPartialSuccess) { partialSuccessResponse = e.response.data; } } else { const inner = e.aggregatedError?.[Object.keys(e.aggregatedError)[0]]; e = inner?.code ? new AppError(inner) : e; } }
if (e instanceof Error) { // Module-level enqueueSnackbar is only bound after SnackbarProvider // mounts; requests failing during early boot would otherwise throw // a TypeError here and mask the real error. if (typeof enqueueSnackbar === "function") { enqueueSnackbar({ message: opts.errorSnackbarMsg(e), variant: "error", action, }); } else { console.error(opts.errorSnackbarMsg(e), e); } } }
if (partialSuccessResponse) { return partialSuccessResponse; } throw e; } };}
export const isRequestAbortedError = (e: Error) => { return e.message == "Request aborted";};