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
8 changes: 7 additions & 1 deletion src/api/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface FetcherOptions {
* Optional fetch implementation override (useful for testing or custom fetch implementations).
*/
fetch?: typeof fetch
/**
* Optional base URL override. Defaults to 'https://api.figma.com'.
* Useful for testing with mocks or proxies.
*/
baseUrl?: string
}

/**
Expand Down Expand Up @@ -81,6 +86,7 @@ export async function fetcher<TResponse = unknown>(
signal: providedSignal,
timeout,
fetch: customFetch = fetch,
baseUrl = FIGMA_API_BASE_URL,
} = options ?? {}

// Create timeout signal if timeout is provided and no signal is provided
Expand All @@ -102,7 +108,7 @@ export async function fetcher<TResponse = unknown>(
const requestUrl =
url.startsWith('http://') || url.startsWith('https://')
? url
: `${FIGMA_API_BASE_URL}${url.startsWith('/') ? '' : '/'}${url}`
: `${baseUrl}${url.startsWith('/') ? '' : '/'}${url}`

const response = await customFetch(requestUrl, {
method: 'GET',
Expand Down
8 changes: 7 additions & 1 deletion src/api/mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface MutatorOptions {
* Optional fetch implementation override (useful for testing or custom fetch implementations).
*/
fetch?: typeof fetch
/**
* Optional base URL override. Defaults to 'https://api.figma.com'.
* Useful for testing with mocks or proxies.
*/
baseUrl?: string
}

/**
Expand Down Expand Up @@ -81,6 +86,7 @@ export async function mutator<TResponse = unknown>(
signal: providedSignal,
timeout,
fetch: customFetch = fetch,
baseUrl = FIGMA_API_BASE_URL,
} = options ?? {}

// Create timeout signal if timeout is provided and no signal is provided
Expand Down Expand Up @@ -121,7 +127,7 @@ export async function mutator<TResponse = unknown>(
const requestUrl =
url.startsWith('http://') || url.startsWith('https://')
? url
: `${FIGMA_API_BASE_URL}${url.startsWith('/') ? '' : '/'}${url}`
: `${baseUrl}${url.startsWith('/') ? '' : '/'}${url}`

const response = await customFetch(requestUrl, init)

Expand Down
52 changes: 52 additions & 0 deletions tests/api/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,56 @@ describe('fetcher', () => {
expect(result).toEqual({ data: 'custom' })
})
})

describe('baseUrl override', () => {
it('should use custom baseUrl when provided', async () => {
const customFetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: 'custom' }),
})

await fetcher('/v1/files/abc/variables/local', DUMMY_TOKEN, {
fetch: customFetch as typeof fetch,
baseUrl: 'https://proxy.example.com',
})

expect(customFetch).toHaveBeenCalledWith(
'https://proxy.example.com/v1/files/abc/variables/local',
expect.any(Object)
)
})

it('should use default baseUrl when not provided', async () => {
const customFetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: 'custom' }),
})

await fetcher('/v1/files/abc/variables/local', DUMMY_TOKEN, {
fetch: customFetch as typeof fetch,
})

expect(customFetch).toHaveBeenCalledWith(
'https://api.figma.com/v1/files/abc/variables/local',
expect.any(Object)
)
})

it('should ignore baseUrl when URL is already absolute', async () => {
const customFetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: 'custom' }),
})

await fetcher('https://other.api.com/endpoint', DUMMY_TOKEN, {
fetch: customFetch as typeof fetch,
baseUrl: 'https://proxy.example.com',
})

expect(customFetch).toHaveBeenCalledWith(
'https://other.api.com/endpoint',
expect.any(Object)
)
})
})
})
58 changes: 58 additions & 0 deletions tests/api/mutator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,62 @@ describe('mutator', () => {
expect(result).toEqual({ id: '123' })
})
})

describe('baseUrl override', () => {
it('should use custom baseUrl when provided', async () => {
const customFetch = vi.fn().mockResolvedValue({
ok: true,
status: 200,
body: 'not-null',
json: () => Promise.resolve({ id: '123' }),
})

await mutator('/v1/files/abc/variables', token, 'CREATE', body, {
fetch: customFetch as typeof fetch,
baseUrl: 'https://proxy.example.com',
})

expect(customFetch).toHaveBeenCalledWith(
'https://proxy.example.com/v1/files/abc/variables',
expect.any(Object)
)
})

it('should use default baseUrl when not provided', async () => {
const customFetch = vi.fn().mockResolvedValue({
ok: true,
status: 200,
body: 'not-null',
json: () => Promise.resolve({ id: '123' }),
})

await mutator('/v1/files/abc/variables', token, 'CREATE', body, {
fetch: customFetch as typeof fetch,
})

expect(customFetch).toHaveBeenCalledWith(
'https://api.figma.com/v1/files/abc/variables',
expect.any(Object)
)
})

it('should ignore baseUrl when URL is already absolute', async () => {
const customFetch = vi.fn().mockResolvedValue({
ok: true,
status: 200,
body: 'not-null',
json: () => Promise.resolve({ id: '123' }),
})

await mutator('https://other.api.com/endpoint', token, 'CREATE', body, {
fetch: customFetch as typeof fetch,
baseUrl: 'https://proxy.example.com',
})

expect(customFetch).toHaveBeenCalledWith(
'https://other.api.com/endpoint',
expect.any(Object)
)
})
})
})