Skip to content

Commit 0c3e069

Browse files
committed
feat(js-api-client): Add server timings to profiling.
1 parent 6f850f2 commit 0c3e069

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crystallize/js-api-client",
33
"license": "MIT",
4-
"version": "1.11.0",
4+
"version": "1.12.0",
55
"author": "Crystallize <[email protected]> (https://crystallize.com)",
66
"contributors": [
77
"Sébastien Morel <[email protected]>"

src/core/client.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ export type ClientConfiguration = {
1111
};
1212

1313
type ProfilingOptions = {
14-
onRequest: (query: string, variables?: VariablesType) => void;
15-
onRequestResolved: (processingTimeMs: number, query: string, variables?: VariablesType) => void;
14+
onRequest?: (query: string, variables?: VariablesType) => void;
15+
onRequestResolved: (
16+
{
17+
resolutionTimeMs,
18+
serverTimeMs,
19+
}: {
20+
resolutionTimeMs: number;
21+
serverTimeMs: number;
22+
},
23+
query: string,
24+
variables?: VariablesType,
25+
) => void;
1626
};
1727

1828
export type CreateClientOptions = {
@@ -69,7 +79,9 @@ async function post<T>(
6979
let start: number = 0;
7080
if (profiling) {
7181
start = Date.now();
72-
profiling.onRequest(query, variables);
82+
if (profiling.onRequest) {
83+
profiling.onRequest(query, variables);
84+
}
7385
}
7486

7587
const response = await fetch(path, {
@@ -81,7 +93,16 @@ async function post<T>(
8193

8294
if (profiling) {
8395
const ms = Date.now() - start;
84-
profiling.onRequestResolved(ms, query, variables);
96+
const serverTiming = response.headers.get('server-timing') ?? undefined;
97+
const duration = serverTiming?.split(';')[1]?.split('=')[1] ?? -1;
98+
profiling.onRequestResolved(
99+
{
100+
resolutionTimeMs: ms,
101+
serverTimeMs: Number(duration),
102+
},
103+
query,
104+
variables,
105+
);
85106
}
86107
if (response.ok && 204 === response.status) {
87108
return <T>{};

tests/catalogue-profiled.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ test('Test with Profiling 1 ', async () => {
2626
},
2727
};
2828
},
29-
onRequestResolved: (processingTimeMs, query, variables) => {
29+
onRequestResolved: ({ resolutionTimeMs, serverTimeMs }, query, variables) => {
3030
profiledQuery = {
3131
...profiledQuery,
3232
onRequestResolved: {
33-
processingTimeMs,
33+
resolutionTimeMs,
34+
serverTimeMs,
3435
query,
3536
variables,
3637
},
@@ -67,5 +68,6 @@ test('Test with Profiling 1 ', async () => {
6768

6869
expect(profiledQuery.onRequestResolved.query).toBe(expectedQuery);
6970
expect(profiledQuery.onRequestResolved.variables).toBe(variables);
70-
expect(profiledQuery.onRequestResolved.processingTimeMs).toBeGreaterThan(0);
71+
expect(profiledQuery.onRequestResolved.resolutionTimeMs).toBeGreaterThan(0);
72+
expect(profiledQuery.onRequestResolved.serverTimeMs).toBeGreaterThan(0);
7173
});

0 commit comments

Comments
 (0)