Skip to content

Commit 58ecb81

Browse files
committed
[client] improve default HTTP client
1 parent 8e36892 commit 58ecb81

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/client.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ describe('Client', () => {
347347
describe('Client with JWT Authentication', () => {
348348
let client: Client;
349349
let mockHttpClient: HttpClient;
350-
const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ';
350+
const jwtToken = 'fake-token-123';
351351

352352
beforeEach(() => {
353353
mockHttpClient = {
@@ -391,7 +391,7 @@ describe('Client', () => {
391391
{
392392
"Content-Type": "application/json",
393393
"User-Agent": "android-sms-gateway/3.0 (client; js)",
394-
Authorization: `Bearer ${jwtToken}`,
394+
Authorization: `Bearer fake-token-123`,
395395
},
396396
);
397397
expect(result).toBe(expectedState);
@@ -418,7 +418,7 @@ describe('Client', () => {
418418
`${BASE_URL}/message/${messageId}`,
419419
{
420420
"User-Agent": "android-sms-gateway/3.0 (client; js)",
421-
Authorization: `Bearer ${jwtToken}`,
421+
Authorization: `Bearer fake-token-123`,
422422
},
423423
);
424424
expect(result).toBe(expectedState);
@@ -533,7 +533,7 @@ describe('Client', () => {
533533
ttl: 3600,
534534
};
535535
const expectedResponse: TokenResponse = {
536-
access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
536+
access_token: 'fake-token-123',
537537
token_type: 'Bearer',
538538
id: 'token-id-123',
539539
expires_at: '2024-12-31T23:59:59Z',
@@ -560,7 +560,7 @@ describe('Client', () => {
560560
scopes: ['read'],
561561
};
562562
const expectedResponse: TokenResponse = {
563-
access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
563+
access_token: 'fake-token-123',
564564
token_type: 'Bearer',
565565
id: 'token-id-456',
566566
expires_at: '2024-12-31T23:59:59Z',

src/client.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,56 @@ export class Client {
5555
* Gets the default HTTP client implementation
5656
*/
5757
private getDefaultHttpClient(): HttpClient {
58-
// This would typically be implemented elsewhere, but we'll provide a basic implementation
58+
const handleResponse = async (response: Response): Promise<any> => {
59+
if (response.status === 204) {
60+
return null;
61+
}
62+
63+
if (!response.ok) {
64+
const text = await response.text();
65+
throw new Error(`HTTP error ${response.status}: ${text}`);
66+
}
67+
68+
const contentType = response.headers.get("Content-Type");
69+
if (contentType && contentType.includes("application/json")) {
70+
return await response.json();
71+
} else {
72+
return await response.text();
73+
}
74+
};
75+
5976
return {
6077
get: async <T>(url: string, headers?: Record<string, string>): Promise<T> => {
6178
const response = await fetch(url, { method: 'GET', headers });
62-
return response.json();
79+
return handleResponse(response);
6380
},
6481
post: async <T>(url: string, body: any, headers?: Record<string, string>): Promise<T> => {
6582
const response = await fetch(url, {
6683
method: 'POST',
6784
headers,
6885
body: JSON.stringify(body)
6986
});
70-
return response.json();
87+
return handleResponse(response);
7188
},
7289
put: async <T>(url: string, body: any, headers?: Record<string, string>): Promise<T> => {
7390
const response = await fetch(url, {
7491
method: 'PUT',
7592
headers,
7693
body: JSON.stringify(body)
7794
});
78-
return response.json();
95+
return handleResponse(response);
7996
},
8097
patch: async <T>(url: string, body: any, headers?: Record<string, string>): Promise<T> => {
8198
const response = await fetch(url, {
8299
method: 'PATCH',
83100
headers,
84101
body: JSON.stringify(body)
85102
});
86-
return response.json();
103+
return handleResponse(response);
87104
},
88105
delete: async <T>(url: string, headers?: Record<string, string>): Promise<T> => {
89106
const response = await fetch(url, { method: 'DELETE', headers });
90-
return response.json();
107+
return handleResponse(response);
91108
},
92109
};
93110
}

0 commit comments

Comments
 (0)