This repository has been archived on 2025-10-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FPGA_WebLab/src/utils/Common.ts
SikongJueluo 49cbdc51d9 fix: 修复多个外设无法认证的问题
refactor: 同时使用更加优雅的方式处理injection
2025-07-15 11:30:13 +08:00

51 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { type FileParameter } from "@/APIClient";
import { isNull, isUndefined } from "lodash";
export function toFileParameter(object: File): FileParameter {
if (isNull(object) || isUndefined(object))
throw new Error("File is Null or Undefined");
return {
data: object,
fileName: object.name,
};
}
export function toFileParameterOrNull(
object?: File | null,
): FileParameter | null {
if (isNull(object) || isUndefined(object)) return null;
else
return {
data: object,
fileName: object.name,
};
}
export function toFileParameterOrUndefined(
object?: File | undefined,
): FileParameter | undefined {
if (isNull(object) || isUndefined(object)) return undefined;
else
return {
data: object,
fileName: object.name,
};
}
// 自定义 Hook检查依赖注入值是否为空
export function useRequiredInjection<T>(useFn: () => T | undefined): T {
const value = useFn();
if (value === undefined) {
throw new Error("Missing required injection");
}
return value;
}
export function useOptionalInjection<T>(
useFn: () => T | undefined,
defaultValue: T,
): T {
const value = useFn();
return value ?? defaultValue;
}