-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeGuards.ts
More file actions
133 lines (118 loc) · 3.34 KB
/
typeGuards.ts
File metadata and controls
133 lines (118 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type {
LocalVariablesResponse,
PublishedVariablesResponse,
} from 'types/figma'
/**
* Runtime type guard to check if data matches LocalVariablesResponse structure.
*
* @remarks
* Use this to validate fallback files or API responses at runtime before casting.
* Validates the essential structure: meta object with variableCollections and variables.
*
* @param data - The data to validate
* @returns `true` if data matches LocalVariablesResponse structure
*
* @example
* ```ts
* import { isLocalVariablesResponse } from '@figma-vars/hooks';
*
* if (isLocalVariablesResponse(fallbackData)) {
* // Safe to use as LocalVariablesResponse
* } else {
* console.error('Invalid fallback file structure');
* }
* ```
*
* @public
*/
export function isLocalVariablesResponse(
data: unknown
): data is LocalVariablesResponse {
if (typeof data !== 'object' || data === null) {
return false
}
const obj = data as Record<string, unknown>
if (typeof obj.meta !== 'object' || obj.meta === null) {
return false
}
const meta = obj.meta as Record<string, unknown>
// Check for required properties
if (
typeof meta.variableCollections !== 'object' ||
meta.variableCollections === null
) {
return false
}
if (typeof meta.variables !== 'object' || meta.variables === null) {
return false
}
return true
}
/**
* Runtime type guard to check if data matches PublishedVariablesResponse structure.
*
* @remarks
* Use this to validate fallback files or API responses at runtime before casting.
* Validates the essential structure: meta object with variableCollections and variables.
*
* @param data - The data to validate
* @returns `true` if data matches PublishedVariablesResponse structure
*
* @example
* ```ts
* import { isPublishedVariablesResponse } from '@figma-vars/hooks';
*
* if (isPublishedVariablesResponse(fallbackData)) {
* // Safe to use as PublishedVariablesResponse
* } else {
* console.error('Invalid fallback file structure');
* }
* ```
*
* @public
*/
export function isPublishedVariablesResponse(
data: unknown
): data is PublishedVariablesResponse {
if (typeof data !== 'object' || data === null) {
return false
}
const obj = data as Record<string, unknown>
if (typeof obj.meta !== 'object' || obj.meta === null) {
return false
}
const meta = obj.meta as Record<string, unknown>
// Check for required properties
if (
typeof meta.variableCollections !== 'object' ||
meta.variableCollections === null
) {
return false
}
if (typeof meta.variables !== 'object' || meta.variables === null) {
return false
}
return true
}
/**
* Validates and returns typed fallback data, or undefined if invalid.
*
* @remarks
* Attempts to validate data as either LocalVariablesResponse or PublishedVariablesResponse.
* Returns the typed data if valid, undefined otherwise.
*
* @param data - The data to validate
* @returns The validated data or undefined
*
* @public
*/
export function validateFallbackData(
data: unknown
): LocalVariablesResponse | PublishedVariablesResponse | undefined {
// Both isLocalVariablesResponse and isPublishedVariablesResponse check the same structure,
// so we only need to check one. Using isLocalVariablesResponse as the canonical check.
if (isLocalVariablesResponse(data)) {
return data
}
return undefined
}