Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@figma-vars/hooks",
"version": "4.0.0",
"version": "4.1.0",
"description": "Typed React hooks for managing Figma Variables, modes, tokens, and bindings via API.",
"author": "Mark Learst",
"license": "MIT",
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useInvalidateVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import { getInvalidationKeys } from 'utils/swrKeys'
*/
export const useInvalidateVariables = () => {
const { mutate } = useSWRConfig()
const { token, fileKey, fallbackFile, providerId } = useFigmaTokenContext()
const { token, fileKey, parsedFallbackFile, providerId } =
useFigmaTokenContext()

const hasFallback = Boolean(fallbackFile)
const hasFallback = Boolean(parsedFallbackFile)

/**
* Invalidates all variable-related SWR cache entries.
Expand Down
18 changes: 3 additions & 15 deletions src/hooks/usePublishedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,10 @@ import { getPublishedVariablesKey } from 'utils/swrKeys'
* ```
*/
export const usePublishedVariables = () => {
const {
token,
fileKey,
fallbackFile,
parsedFallbackFile,
providerId,
swrConfig,
} = useFigmaTokenContext()
const { token, fileKey, parsedFallbackFile, providerId, swrConfig } =
useFigmaTokenContext()

const hasFallback = Boolean(fallbackFile || parsedFallbackFile)
const hasFallback = Boolean(parsedFallbackFile)

const key = getPublishedVariablesKey({
fileKey,
Expand All @@ -70,12 +64,6 @@ export const usePublishedVariables = () => {
return parsedFallbackFile as PublishedVariablesResponse
}

// Legacy support: if fallbackFile is an object but parsedFallbackFile wasn't set
// This can happen if the provider didn't validate the structure correctly
if (fallbackFile && typeof fallbackFile === 'object') {
return fallbackFile as PublishedVariablesResponse
}

const [u, t] = Array.isArray(args[0])
? args[0]
: ([args[0], args[1]] as const)
Expand Down
18 changes: 3 additions & 15 deletions src/hooks/useVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ import { getVariablesKey } from 'utils/swrKeys'
* @public
*/
export const useVariables = () => {
const {
token,
fileKey,
fallbackFile,
parsedFallbackFile,
providerId,
swrConfig,
} = useFigmaTokenContext()
const { token, fileKey, parsedFallbackFile, providerId, swrConfig } =
useFigmaTokenContext()

const hasFallback = Boolean(fallbackFile || parsedFallbackFile)
const hasFallback = Boolean(parsedFallbackFile)

const key = getVariablesKey({
fileKey,
Expand All @@ -44,12 +38,6 @@ export const useVariables = () => {
return parsedFallbackFile as LocalVariablesResponse
}

// Legacy support: if fallbackFile is an object but parsedFallbackFile wasn't set
// This can happen if the provider didn't validate the structure correctly
if (fallbackFile && typeof fallbackFile === 'object') {
return fallbackFile as LocalVariablesResponse
}

// At this point we expect live credentials; guard just in case
const [u, t] = Array.isArray(args[0])
? args[0]
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,17 @@ export {
*/
export {
filterVariables,
withRetry,
redactToken,
isFigmaApiError,
getErrorStatus,
getErrorMessage,
hasErrorStatus,
isRateLimited,
getRetryAfter,
isLocalVariablesResponse,
isPublishedVariablesResponse,
validateFallbackData,
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validateFallbackData function is marked as @internal in its JSDoc comment, but it's being exported from the main index.ts (line 98), making it part of the public API. The @internal tag should be changed to @public to reflect its actual visibility, or if it should remain internal, it should not be exported from the main index.ts file.

Suggested change
validateFallbackData,

Copilot uses AI. Check for mistakes.
} from 'utils'

/**
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
export {
isLocalVariablesResponse,
isPublishedVariablesResponse,
validateFallbackData,
} from 'utils/typeGuards'
export { redactToken } from 'utils/redactToken'
export type { RedactTokenOptions } from 'utils/redactToken'
Expand Down
2 changes: 1 addition & 1 deletion src/utils/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function isPublishedVariablesResponse(
* @param data - The data to validate
* @returns The validated data or undefined
*
* @internal
* @public
*/
export function validateFallbackData(
data: unknown
Expand Down
4 changes: 2 additions & 2 deletions tests/hooks/useInvalidateVariables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('useInvalidateVariables', () => {
token: 'test-token',
fileKey: 'test-file-key',
providerId: 'test-provider-id',
fallbackFile: '{}',
parsedFallbackFile: {},
})

const { result } = renderHook(() => useInvalidateVariables())
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('useInvalidateVariables', () => {
token: 'test-token',
fileKey: 'test-file-key',
providerId: 'test-provider-id',
fallbackFile: '{}',
parsedFallbackFile: {},
})

const { result } = renderHook(() => useInvalidateVariables())
Expand Down
33 changes: 13 additions & 20 deletions tests/hooks/usePublishedVariables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ describe('usePublishedVariables', () => {
token: null,
fileKey: null,
fallbackFile: mockPublishedVariablesResponse,
parsedFallbackFile: mockPublishedVariablesResponse,
} as ReturnType<typeof useFigmaTokenContextModule.useFigmaTokenContext>)

mockedUseSWR.mockReturnValue({
Expand Down Expand Up @@ -482,42 +483,34 @@ describe('usePublishedVariables', () => {
).toBeDefined()
})

it('should use legacy fallbackFile object when parsedFallbackFile is not set', async () => {
// Mock context to return fallbackFile but no parsedFallbackFile
it('should use live key when fallbackFile exists but parsedFallbackFile is invalid', () => {
const spy = vi
.spyOn(useFigmaTokenContextModule, 'useFigmaTokenContext')
.mockReturnValue({
token: 'test-token',
fileKey: 'test-key',
fallbackFile: mockPublishedVariablesResponse,
parsedFallbackFile: undefined, // Not set
fallbackFile: '{invalid-json}',
parsedFallbackFile: undefined,
providerId: 'test-provider',
} as ReturnType<typeof useFigmaTokenContextModule.useFigmaTokenContext>)

mockedUseSWR.mockReturnValue({
data: undefined,
error: null,
error: undefined,
isLoading: false,
isValidating: false,
})

renderHook(() => usePublishedVariables())

const useSWRCalls = mockedUseSWR.mock.calls
expect(useSWRCalls.length).toBeGreaterThan(0)

const call = useSWRCalls[0]
expect(call).toBeDefined()
const [, fetcher] = call as [
unknown,
(
...args: [readonly [string, string]] | [string, string]
) => Promise<unknown>,
]

// Call fetcher to test legacy fallbackFile path
const resultData = await fetcher('url', 'token')
expect(resultData).toEqual(mockPublishedVariablesResponse)
expect(mockedUseSWR).toHaveBeenCalledWith(
[
'https://api.figma.com/v1/files/test-key/variables/published',
'test-token',
],
expect.any(Function),
undefined
)

spy.mockRestore()
})
Expand Down
30 changes: 10 additions & 20 deletions tests/hooks/useVariables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ describe('useVariables', () => {
token: null,
fileKey: null,
fallbackFile: mockLocalVariablesResponse,
parsedFallbackFile: mockLocalVariablesResponse,
} as ReturnType<typeof useFigmaTokenContextModule.useFigmaTokenContext>)

mockedUseSWR.mockReturnValue({
Expand All @@ -466,42 +467,31 @@ describe('useVariables', () => {
spy.mockRestore()
})

it('should use legacy fallbackFile object when parsedFallbackFile is not set', async () => {
// Mock context to return fallbackFile but no parsedFallbackFile
it('should use live key when fallbackFile exists but parsedFallbackFile is invalid', () => {
const spy = vi
.spyOn(useFigmaTokenContextModule, 'useFigmaTokenContext')
.mockReturnValue({
token: 'test-token',
fileKey: 'test-key',
fallbackFile: mockLocalVariablesResponse,
parsedFallbackFile: undefined, // Not set
fallbackFile: '{invalid-json}',
parsedFallbackFile: undefined,
providerId: 'test-provider',
} as ReturnType<typeof useFigmaTokenContextModule.useFigmaTokenContext>)

mockedUseSWR.mockReturnValue({
data: undefined,
error: null,
error: undefined,
isLoading: false,
isValidating: false,
})

renderHook(() => useVariables())

const useSWRCalls = mockedUseSWR.mock.calls
expect(useSWRCalls.length).toBeGreaterThan(0)

const call = useSWRCalls[0]
expect(call).toBeDefined()
const [, fetcher] = call as [
unknown,
(
...args: [readonly [string, string]] | [string, string]
) => Promise<unknown>,
]

// Call fetcher to test legacy fallbackFile path
const resultData = await fetcher('url', 'token')
expect(resultData).toEqual(mockLocalVariablesResponse)
expect(mockedUseSWR).toHaveBeenCalledWith(
['https://api.figma.com/v1/files/test-key/variables/local', 'test-token'],
expect.any(Function),
undefined
)

spy.mockRestore()
})
Expand Down
7 changes: 7 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('main index barrel file', () => {
// Check utils export
expect(indexModule.filterVariables).toBeDefined()
expect(typeof indexModule.filterVariables).toBe('function')
expect(indexModule.withRetry).toBeDefined()
expect(indexModule.redactToken).toBeDefined()
expect(indexModule.isRateLimited).toBeDefined()
expect(indexModule.getRetryAfter).toBeDefined()
expect(indexModule.isLocalVariablesResponse).toBeDefined()
expect(indexModule.isPublishedVariablesResponse).toBeDefined()
expect(indexModule.validateFallbackData).toBeDefined()

// Check that types are exported (they should be available as type exports)
// We can't directly test type exports, but we can check the module structure
Expand Down