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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function createInstance(defaults?: Partial<Config>): Instance {
...merged,
url: fullUrl,
});
return request(instanceConfig.url!, instanceConfig, instanceChain);
const { baseURL: _baseURL, url: _url, ...resolvedInstanceConfig } = instanceConfig;
return request(instanceConfig.url!, resolvedInstanceConfig, instanceChain);
};

const methodFn = (method: HttpMethod): RequestFn => {
Expand Down
10 changes: 10 additions & 0 deletions test/axios/compatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const server = setupServer(
await new Promise((resolve) => setTimeout(resolve, 1000));
return HttpResponse.json([{ id: 1 }]);
}),
http.post('https://api.example.com/api/auth/login', async ({ request: req }) => {
const body = await req.json();
return HttpResponse.json({ token: 'abc', ...body });
}),
);

beforeAll(() => server.listen());
Expand Down Expand Up @@ -74,4 +78,10 @@ describe('axios compatibility', () => {
]);
expect(results).toHaveLength(2);
});

it('axios.create() with baseURL containing a path prepends full baseURL when path starts with /', async () => {
const api = axios.create({ baseURL: 'https://api.example.com/api' });
const res = await api.post('/auth/login', { email: 'test@test.com', password: 'test' });
expect(res.data).toMatchObject({ token: 'abc', email: 'test@test.com' });
});
});
46 changes: 46 additions & 0 deletions test/unit/instance-baseurl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { createInstance } from '../../src/instance';

describe('baseURL + leading slash path (relative baseURL)', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('does not double-prefix baseURL when path starts with /', async () => {
const capturedUrls: string[] = [];

// Spy on global fetch to capture the URL actually used
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
capturedUrls.push(url);
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}));

const instance = createInstance({ baseURL: '/api' });
await instance.post('/auth/login', { body: { email: 'test@test.com' } });

expect(capturedUrls).toHaveLength(1);
// Should be /api/auth/login, NOT /api/api/auth/login
expect(capturedUrls[0]).toBe('/api/auth/login');
});

it('does not double-prefix baseURL when path does not start with /', async () => {
const capturedUrls: string[] = [];

vi.stubGlobal('fetch', vi.fn(async (url: string) => {
capturedUrls.push(url);
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}));

const instance = createInstance({ baseURL: '/api' });
await instance.get('users');

expect(capturedUrls).toHaveLength(1);
expect(capturedUrls[0]).toBe('/api/users');
});
});
Loading