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
22 changes: 22 additions & 0 deletions packages/social/x/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ describe('social-x API posting', () => {
});
});

it('normalizes custom API base URLs before composing tweet paths', async () => {
const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
status: 201,
json: async () => ({ data: { id: '1445880548472328194', text: 'Custom base' } }),
} as Response);

const ctx = {
...fakeConnectContext({ X_BEARER_TOKEN: 'x-token' }),
dryRun: false,
};

await adapter.post(ctx as any, {
body: 'Custom base',
}, {
mode: 'api',
apiBaseUrl: 'https://api.x.example/',
});

expect(fetchMock.mock.calls[0]?.[0]).toBe('https://api.x.example/2/tweets');
});

it('rejects media attachments without pre-uploaded media ids', async () => {
const ctx = {
...fakeConnectContext({ X_BEARER_TOKEN: 'x-token' }),
Expand Down
3 changes: 2 additions & 1 deletion packages/social/x/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export default defineSocial<Config>({
throw new Error('X media posts require pre-uploaded media IDs in config.mediaIds');
}

const res = await fetch(`${config.apiBaseUrl ?? 'https://api.x.com'}/2/tweets`, {
const apiBaseUrl = (config.apiBaseUrl ?? 'https://api.x.com').replace(/\/+$/, '');
const res = await fetch(`${apiBaseUrl}/2/tweets`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
Expand Down
Loading