@umituz/react-native-ai-fal-provider 3.2.52 → 3.2.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/package.json +1 -1
  2. package/src/domain/services/ErrorClassificationService.ts +201 -0
  3. package/src/domain/services/ImageProcessingService.ts +89 -0
  4. package/src/domain/services/PricingService.ts +101 -0
  5. package/src/domain/services/ValidationService.ts +132 -0
  6. package/src/domain/services/index.ts +13 -0
  7. package/src/index.ts +24 -22
  8. package/src/infrastructure/utils/fal-error-handler.util.ts +14 -146
  9. package/src/infrastructure/utils/fal-storage.util.ts +2 -3
  10. package/src/infrastructure/utils/helpers/index.ts +2 -4
  11. package/src/infrastructure/utils/image-helpers.util.ts +13 -27
  12. package/src/infrastructure/utils/input-preprocessor.util.ts +6 -12
  13. package/src/infrastructure/utils/input-validator.util.ts +17 -156
  14. package/src/infrastructure/utils/pricing/fal-pricing.util.ts +26 -53
  15. package/src/infrastructure/utils/type-guards/index.ts +2 -2
  16. package/src/shared/helpers.ts +149 -0
  17. package/src/shared/index.ts +8 -0
  18. package/src/shared/type-guards.ts +122 -0
  19. package/src/shared/validators.ts +281 -0
  20. package/src/infrastructure/utils/helpers/calculation-helpers.util.ts +0 -21
  21. package/src/infrastructure/utils/helpers/error-helpers.util.ts +0 -65
  22. package/src/infrastructure/utils/helpers/object-helpers.util.ts +0 -44
  23. package/src/infrastructure/utils/helpers/timing-helpers.util.ts +0 -11
  24. package/src/infrastructure/utils/type-guards/model-type-guards.util.ts +0 -56
  25. package/src/infrastructure/utils/type-guards/validation-guards.util.ts +0 -101
  26. package/src/infrastructure/utils/validators/data-uri-validator.util.ts +0 -91
  27. package/src/infrastructure/utils/validators/string-validator.util.ts +0 -64
@@ -1,56 +0,0 @@
1
- /**
2
- * Model Type Guards
3
- * Runtime type checking for model types
4
- */
5
-
6
- import type { FalModelType } from "../../../domain/entities/fal.types";
7
- import type { ModelType } from "../../../domain/types/model-selection.types";
8
- import { FalErrorType } from "../../../domain/entities/error.types";
9
-
10
- /**
11
- * Check if a string is a valid FalModelType
12
- */
13
- export function isFalModelType(value: unknown): value is FalModelType {
14
- const validTypes: ReadonlyArray<FalModelType> = [
15
- "text-to-image",
16
- "text-to-video",
17
- "text-to-voice",
18
- "image-to-video",
19
- "image-to-image",
20
- "text-to-text",
21
- ];
22
- return typeof value === "string" && validTypes.includes(value as FalModelType);
23
- }
24
-
25
- /**
26
- * Check if a string is a valid ModelType
27
- */
28
- export function isModelType(value: unknown): value is ModelType {
29
- const validTypes: ReadonlyArray<ModelType> = [
30
- "text-to-image",
31
- "text-to-video",
32
- "image-to-video",
33
- "text-to-voice",
34
- ];
35
- return typeof value === "string" && validTypes.includes(value as ModelType);
36
- }
37
-
38
- /**
39
- * Check if error is a FalErrorType
40
- */
41
- export function isFalErrorType(value: unknown): value is FalErrorType {
42
- const validTypes: ReadonlyArray<FalErrorType> = [
43
- FalErrorType.NETWORK,
44
- FalErrorType.TIMEOUT,
45
- FalErrorType.API_ERROR,
46
- FalErrorType.VALIDATION,
47
- FalErrorType.IMAGE_TOO_SMALL,
48
- FalErrorType.CONTENT_POLICY,
49
- FalErrorType.RATE_LIMIT,
50
- FalErrorType.AUTHENTICATION,
51
- FalErrorType.QUOTA_EXCEEDED,
52
- FalErrorType.MODEL_NOT_FOUND,
53
- FalErrorType.UNKNOWN,
54
- ];
55
- return typeof value === "string" && validTypes.includes(value as FalErrorType);
56
- }
@@ -1,101 +0,0 @@
1
- /**
2
- * Validation Guards
3
- * Runtime validation for values and formats
4
- */
5
-
6
- import {
7
- MIN_BASE64_IMAGE_LENGTH,
8
- MIN_MODEL_ID_LENGTH,
9
- MAX_PROMPT_LENGTH,
10
- MAX_TIMEOUT_MS,
11
- MAX_RETRY_COUNT,
12
- } from './constants';
13
- import { isNonEmptyString } from '../validators/string-validator.util';
14
-
15
- /**
16
- * Validate base64 image string
17
- */
18
- export function isValidBase64Image(value: unknown): boolean {
19
- if (typeof value !== "string") {
20
- return false;
21
- }
22
-
23
- // Check data URI prefix - use direct check instead of type guard to avoid type narrowing issues
24
- if (value.startsWith("data:image/")) {
25
- const parts = value.split("base64,");
26
- // Ensure split produced at least 2 parts and the second part exists
27
- if (parts.length < 2) return false;
28
- const base64Part = parts[1];
29
- if (!base64Part) return false;
30
- return base64Part.length >= MIN_BASE64_IMAGE_LENGTH;
31
- }
32
-
33
- // Check if it's a valid base64 string with minimum length
34
- const base64Pattern = /^[A-Za-z0-9+/]+=*$/;
35
- return base64Pattern.test(value) && value.length >= MIN_BASE64_IMAGE_LENGTH;
36
- }
37
-
38
- /**
39
- * Validate API key format
40
- */
41
- export function isValidApiKey(value: unknown): boolean {
42
- return typeof value === "string" && value.length > 0;
43
- }
44
-
45
- /**
46
- * Validate model ID format
47
- * Pattern: org/model or org/model/sub1/sub2/... (multiple path segments)
48
- * Allows dots for versions (e.g., v1.5) but prevents path traversal (..)
49
- * Examples:
50
- * - xai/grok-imagine-video/text-to-video
51
- * - fal-ai/minimax/hailuo-02/standard/image-to-video
52
- */
53
- const MODEL_ID_PATTERN = /^[a-zA-Z0-9-_]+\/[a-zA-Z0-9-_.]+(\/[a-zA-Z0-9-_.]+)*$/;
54
-
55
- export function isValidModelId(value: unknown): boolean {
56
- if (typeof value !== "string") {
57
- return false;
58
- }
59
-
60
- // Prevent path traversal attacks
61
- if (value.includes('..')) {
62
- return false;
63
- }
64
-
65
- // Ensure it doesn't start or end with dots
66
- if (value.startsWith('.') || value.endsWith('.')) {
67
- return false;
68
- }
69
-
70
- return MODEL_ID_PATTERN.test(value) && value.length >= MIN_MODEL_ID_LENGTH;
71
- }
72
-
73
- /**
74
- * Validate prompt string
75
- */
76
- export function isValidPrompt(value: unknown): boolean {
77
- // Use type guard first, then check length
78
- if (!isNonEmptyString(value)) return false;
79
- return value.length <= MAX_PROMPT_LENGTH;
80
- }
81
-
82
- /**
83
- * Validate timeout value
84
- */
85
- export function isValidTimeout(value: unknown): boolean {
86
- return typeof value === "number" && !isNaN(value) && isFinite(value) && value > 0 && value <= MAX_TIMEOUT_MS;
87
- }
88
-
89
- /**
90
- * Validate retry count
91
- */
92
- export function isValidRetryCount(value: unknown): boolean {
93
- return (
94
- typeof value === "number" &&
95
- !isNaN(value) &&
96
- isFinite(value) &&
97
- Number.isInteger(value) &&
98
- value >= 0 &&
99
- value <= MAX_RETRY_COUNT
100
- );
101
- }
@@ -1,91 +0,0 @@
1
- /**
2
- * Data URI Validation Utilities
3
- * Centralized data URI format checking and validation
4
- *
5
- * Eliminates the duplicated pattern:
6
- * `value.startsWith("data:image/")`
7
- * which appeared in 4 different files.
8
- */
9
-
10
- /**
11
- * Check if value is a data URI (any type)
12
- *
13
- * @param value - Value to check
14
- * @returns Type guard indicating if the value is a data URI string
15
- *
16
- * @example
17
- * ```typescript
18
- * isDataUri("data:text/plain;base64,SGVsbG8="); // true
19
- * isDataUri("https://example.com/image.png"); // false
20
- * ```
21
- */
22
- export function isDataUri(value: unknown): value is string {
23
- return typeof value === "string" && value.startsWith("data:");
24
- }
25
-
26
- /**
27
- * Check if value is an image data URI
28
- *
29
- * @param value - Value to check
30
- * @returns Type guard indicating if the value is an image data URI string
31
- *
32
- * @example
33
- * ```typescript
34
- * isImageDataUri("data:image/png;base64,iVBOR..."); // true
35
- * isImageDataUri("data:text/plain;base64,SGVsbG8="); // false
36
- * isImageDataUri("https://example.com/image.png"); // false
37
- * ```
38
- */
39
- export function isImageDataUri(value: unknown): value is string {
40
- return typeof value === "string" && value.startsWith("data:image/");
41
- }
42
-
43
- /**
44
- * Check if value is a base64-encoded data URI
45
- *
46
- * @param value - Value to check
47
- * @returns Type guard indicating if the value contains base64 encoding
48
- *
49
- * @example
50
- * ```typescript
51
- * isBase64DataUri("data:image/png;base64,iVBOR..."); // true
52
- * isBase64DataUri("data:image/svg+xml,<svg>...</svg>"); // false
53
- * ```
54
- */
55
- export function isBase64DataUri(value: unknown): value is string {
56
- return isDataUri(value) && value.includes("base64,");
57
- }
58
-
59
- /**
60
- * Extract MIME type from data URI
61
- *
62
- * @param dataUri - Data URI string
63
- * @returns MIME type or null if not found
64
- *
65
- * @example
66
- * ```typescript
67
- * extractMimeType("data:image/png;base64,iVBOR..."); // "image/png"
68
- * extractMimeType("data:text/plain;charset=utf-8,Hello"); // "text/plain"
69
- * ```
70
- */
71
- export function extractMimeType(dataUri: string): string | null {
72
- const match = dataUri.match(/^data:([^;,]+)/);
73
- return match ? match[1] : null;
74
- }
75
-
76
- /**
77
- * Extract base64 content from data URI
78
- *
79
- * @param dataUri - Data URI string
80
- * @returns Base64 content or null if not base64-encoded
81
- *
82
- * @example
83
- * ```typescript
84
- * extractBase64Content("data:image/png;base64,iVBOR..."); // "iVBOR..."
85
- * extractBase64Content("data:text/plain,Hello"); // null
86
- * ```
87
- */
88
- export function extractBase64Content(dataUri: string): string | null {
89
- const parts = dataUri.split("base64,");
90
- return parts.length === 2 ? parts[1] : null;
91
- }
@@ -1,64 +0,0 @@
1
- /**
2
- * String Validation Utilities
3
- * Common string validation patterns
4
- *
5
- * Eliminates the duplicated pattern:
6
- * `value.trim().length === 0`
7
- * which appeared in 3+ files.
8
- */
9
-
10
- /**
11
- * Check if string is empty or whitespace-only
12
- *
13
- * @param value - Value to check
14
- * @returns True if the value is an empty string or contains only whitespace
15
- *
16
- * @example
17
- * ```typescript
18
- * isEmptyString(""); // true
19
- * isEmptyString(" "); // true
20
- * isEmptyString("Hello"); // false
21
- * isEmptyString(null); // false
22
- * ```
23
- */
24
- export function isEmptyString(value: unknown): boolean {
25
- return typeof value === "string" && value.trim().length === 0;
26
- }
27
-
28
- /**
29
- * Check if value is a non-empty string
30
- * Type guard version for better TypeScript inference
31
- *
32
- * @param value - Value to check
33
- * @returns Type guard indicating if the value is a non-empty string
34
- *
35
- * @example
36
- * ```typescript
37
- * if (isNonEmptyString(input)) {
38
- * // TypeScript knows input is a string here
39
- * console.log(input.toUpperCase());
40
- * }
41
- * ```
42
- */
43
- export function isNonEmptyString(value: unknown): value is string {
44
- return typeof value === "string" && value.trim().length > 0;
45
- }
46
-
47
- /**
48
- * Check if value is a string (empty or non-empty)
49
- * Basic type guard for string validation
50
- *
51
- * @param value - Value to check
52
- * @returns Type guard indicating if the value is a string
53
- *
54
- * @example
55
- * ```typescript
56
- * if (isString(value)) {
57
- * // TypeScript knows value is a string here
58
- * console.log(value.length);
59
- * }
60
- * ```
61
- */
62
- export function isString(value: unknown): value is string {
63
- return typeof value === "string";
64
- }