forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-http-client.ts
More file actions
52 lines (47 loc) · 1.53 KB
/
Copy pathcustom-http-client.ts
File metadata and controls
52 lines (47 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Example: logging custom HttpClient injection.
* Bounty #105 — $50
*/
import { LilySdk } from '../src';
import type { HttpClient, HttpRequest, HttpResponse } from '../src';
// A logging HttpClient wrapper that logs every request/response
function createLoggingHttpClient(inner: HttpClient): HttpClient {
return {
async request<TResponse, TRequest = unknown>(
request: HttpRequest<TRequest>,
): Promise<HttpResponse<TResponse>> {
console.log(`[HTTP] ${request.method} ${request.path}`);
const start = performance.now();
try {
const response = await inner.request<TResponse, TRequest>(request);
console.log(`[HTTP] ${request.method} ${request.path} → ${response.status} (${Math.round(performance.now() - start)}ms)`);
return response;
} catch (error) {
console.error(`[HTTP] ${request.method} ${request.path} → ERROR (${Math.round(performance.now() - start)}ms)`);
throw error;
}
},
};
}
// A mock inner client
const mockClient: HttpClient = {
async request<TResponse>(): Promise<HttpResponse<TResponse>> {
return {
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
data: { status: 'ok' } as TResponse,
};
},
};
const sdk = new LilySdk(
{
baseUrl: 'https://api.lily.example',
authToken: 'demo-token',
},
createLoggingHttpClient(mockClient),
);
async function main(): Promise<void> {
const health = await sdk.system.health();
console.log('Health:', health.status);
}
await main();