51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
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;
|
||
}
|