fal-js/libs/client/src/utils.ts
Daniel Rochetti 6ad41e1bfa
feat(client): realtime state machine impl (#32)
* fix: connection state handling

* chore: reset token expiration

* feat: state machine experiment

* feat: new realtime state machine impl

* chore: update client to 0.7.0 before release

* fix: error handling x-fal-error

* chore(client): release v0.7.0

* fix(client): strict type check error
2023-12-04 14:44:56 -08:00

68 lines
1.8 KiB
TypeScript

export function isUUIDv4(id: string): boolean {
return (
typeof id === 'string' &&
id.length === 36 &&
id[14] === '4' &&
['8', '9', 'a', 'b'].includes(id[19])
);
}
export function isValidUrl(url: string) {
try {
const parsedUrl = new URL(url);
return parsedUrl.hostname.endsWith('fal.ai');
} catch (_) {
return false;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number,
leading = false
): (...funcArgs: Parameters<T>) => ReturnType<T> | void {
let lastFunc: NodeJS.Timeout | null;
let lastRan: number;
return (...args: Parameters<T>): ReturnType<T> | void => {
if (!lastRan && leading) {
func(...args);
lastRan = Date.now();
} else {
if (lastFunc) {
clearTimeout(lastFunc);
}
lastFunc = setTimeout(() => {
if (Date.now() - lastRan >= limit) {
func(...args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
};
}
let isRunningInReact: boolean | undefined;
/**
* Not really the most optimal way to detect if we're running in React,
* but the idea here is that we can support multiple rendering engines
* (starting with React), with all their peculiarities, without having
* to add a dependency or creating custom integrations (e.g. custom hooks).
*
* Yes, a bit of magic to make things works out-of-the-box.
* @returns `true` if running in React, `false` otherwise.
*/
export function isReact() {
if (isRunningInReact === undefined) {
const stack = new Error().stack;
isRunningInReact =
!!stack &&
(stack.includes('node_modules/react-dom/') ||
stack.includes('node_modules/next/'));
}
return isRunningInReact;
}