Skip to content

Commit 770ef00

Browse files
committed
refactor: Enhance response normalization in API client to handle serialized types
1 parent dafa0cc commit 770ef00

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

MobileApp/src/api/client.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@ import {
1313
type StoredTokens,
1414
} from "../storage/keychain";
1515

16+
/**
17+
* Recursively normalizes OneUptime API serialized types in response data.
18+
* Converts { _type: "ObjectID", value: "uuid" } → "uuid"
19+
* Converts { _type: "DateTime", value: "iso-string" } → "iso-string"
20+
*/
21+
function normalizeResponseData(data: unknown): unknown {
22+
if (data === null || data === undefined) {
23+
return data;
24+
}
25+
26+
if (Array.isArray(data)) {
27+
return data.map(normalizeResponseData);
28+
}
29+
30+
if (typeof data === "object") {
31+
const obj: Record<string, unknown> = data as Record<string, unknown>;
32+
33+
// Check for serialized OneUptime types
34+
if (
35+
typeof obj["_type"] === "string" &&
36+
typeof obj["value"] === "string" &&
37+
(obj["_type"] === "ObjectID" || obj["_type"] === "DateTime")
38+
) {
39+
return obj["value"];
40+
}
41+
42+
const normalized: Record<string, unknown> = {};
43+
for (const key in obj) {
44+
normalized[key] = normalizeResponseData(obj[key]);
45+
}
46+
return normalized;
47+
}
48+
49+
return data;
50+
}
51+
1652
let isRefreshing: boolean = false;
1753
let refreshSubscribers: Array<(token: string) => void> = [];
1854
let onAuthFailure: (() => void) | null = null;
@@ -55,9 +91,10 @@ apiClient.interceptors.request.use(
5591
},
5692
);
5793

58-
// Response interceptor: handle 401 with token refresh queue
94+
// Response interceptor: normalize OneUptime serialized types then handle 401
5995
apiClient.interceptors.response.use(
6096
(response: AxiosResponse) => {
97+
response.data = normalizeResponseData(response.data);
6198
return response;
6299
},
63100
async (error: AxiosError) => {

0 commit comments

Comments
 (0)