Skip to content

Commit 59a7484

Browse files
mitc-gjugeclaude
andcommitted
fix(dns): paginate the documented Infomaniak products endpoint
listZones used /1/product, which lego uses but the API docs don't: it is the legacy singular route, it returns no pagination metadata, and it 401s for at least some tokens. The documented /1/products returns `total`, `pages`, `items_per_page` and `page`, and defaults to 15 domains per page, so an account with more domains than that would silently lose zones. Switch to the plural endpoint and walk every page. Verified against a live account: forcing per_page=2 collects all 5 domains across 3 pages with no duplicates. Also inline the single-use createdId helper. Both reported by @narcisonunez on Dokploy#5257. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent be4a5da commit 59a7484

2 files changed

Lines changed: 78 additions & 20 deletions

File tree

‎apps/dokploy/__test__/dns/infomaniak.test.ts‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const jsonResponse = (body: unknown, ok = true, status = 200) =>
1414

1515
const ikSuccess = (data: unknown) => jsonResponse({ result: "success", data });
1616

17+
const ikPage = (data: unknown, page: number, pages: number) =>
18+
jsonResponse({ result: "success", data, page, pages });
19+
1720
const ikError = (description: string, status = 400) =>
1821
jsonResponse(
1922
{ result: "error", error: { code: "not_authorized", description } },
@@ -38,10 +41,14 @@ beforeEach(() => {
3841
describe("infomaniakClient.listZones", () => {
3942
it("exposes each domain product as a zone keyed by its name", async () => {
4043
mockFetch.mockResolvedValue(
41-
ikSuccess([
42-
{ id: 1, customer_name: "example.com" },
43-
{ id: 2, customer_name: "example.ch" },
44-
]),
44+
ikPage(
45+
[
46+
{ id: 1, customer_name: "example.com" },
47+
{ id: 2, customer_name: "example.ch" },
48+
],
49+
1,
50+
1,
51+
),
4552
);
4653

4754
const zones = await infomaniakClient.listZones(config);
@@ -51,14 +58,37 @@ describe("infomaniakClient.listZones", () => {
5158
{ id: "example.ch", name: "example.ch" },
5259
]);
5360
const [url, init] = lastCall();
54-
expect(url).toBe(
55-
"https://api.infomaniak.com/1/product?service_name=domain",
56-
);
61+
// The documented endpoint is the plural one; the singular is legacy and
62+
// returns no pagination metadata at all.
63+
expect(url).toContain("/1/products?service_name=domain");
64+
expect(url).toContain("page=1");
5765
expect(init.headers).toMatchObject({
5866
Authorization: "Bearer ik_test_token",
5967
});
6068
});
6169

70+
it("walks every page so accounts with many domains keep all their zones", async () => {
71+
mockFetch
72+
.mockResolvedValueOnce(ikPage([{ id: 1, customer_name: "a.com" }], 1, 3))
73+
.mockResolvedValueOnce(ikPage([{ id: 2, customer_name: "b.com" }], 2, 3))
74+
.mockResolvedValueOnce(ikPage([{ id: 3, customer_name: "c.com" }], 3, 3));
75+
76+
const zones = await infomaniakClient.listZones(config);
77+
78+
expect(zones.map((zone) => zone.name)).toEqual(["a.com", "b.com", "c.com"]);
79+
expect(mockFetch).toHaveBeenCalledTimes(3);
80+
expect((mockFetch.mock.calls[2] as [string])[0]).toContain("page=3");
81+
});
82+
83+
it("stops after a single page when the response has no pagination", async () => {
84+
mockFetch.mockResolvedValue(ikSuccess([{ id: 1, customer_name: "a.com" }]));
85+
86+
const zones = await infomaniakClient.listZones(config);
87+
88+
expect(zones).toEqual([{ id: "a.com", name: "a.com" }]);
89+
expect(mockFetch).toHaveBeenCalledTimes(1);
90+
});
91+
6292
it("propagates the API error description", async () => {
6393
mockFetch.mockResolvedValue(ikError("Authorization required", 401));
6494

@@ -376,7 +406,7 @@ describe("infomaniakClient.testConnection", () => {
376406
mockFetch.mockResolvedValue(ikError("Authorization required", 401));
377407

378408
await expect(infomaniakClient.testConnection(config)).rejects.toThrow(
379-
"Infomaniak: request to /1/product?service_name=domain failed: Authorization required",
409+
"Infomaniak: request to /1/products?service_name=domain&per_page=1 failed: Authorization required",
380410
);
381411
});
382412
});

‎packages/server/src/utils/dns/infomaniak.ts‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ interface InfomaniakResponse<T> {
88
result: "success" | "error";
99
data?: T;
1010
error?: { code?: string; description?: string };
11+
page?: number;
12+
pages?: number;
13+
total?: number;
1114
}
1215

1316
interface InfomaniakRecord {
@@ -28,11 +31,11 @@ const INFOMANIAK_API = "https://api.infomaniak.com";
2831
// Infomaniak requires a TTL on every record, within a 60..86400 range.
2932
const DEFAULT_TTL = 300;
3033

31-
const ikFetch = async <T>(
34+
const ikRequest = async <T>(
3235
config: InfomaniakConfig,
3336
path: string,
3437
init: RequestInit = {},
35-
): Promise<T> => {
38+
): Promise<InfomaniakResponse<T>> => {
3639
const response = await dnsFetch(`${INFOMANIAK_API}${path}`, {
3740
...init,
3841
headers: {
@@ -51,9 +54,15 @@ const ikFetch = async <T>(
5154
}`,
5255
);
5356
}
54-
return body.data as T;
57+
return body;
5558
};
5659

60+
const ikFetch = async <T>(
61+
config: InfomaniakConfig,
62+
path: string,
63+
init: RequestInit = {},
64+
): Promise<T> => (await ikRequest<T>(config, path, init)).data as T;
65+
5766
// Infomaniak's "source" holds the subdomain only, relative to the zone. The apex
5867
// is a bare root dot; "" and "@" are accepted too so a hand-written record still
5968
// round-trips.
@@ -113,9 +122,25 @@ const recordPayload = (
113122
ttl: record.ttl ?? DEFAULT_TTL,
114123
});
115124

116-
// The API returns the created record, but older responses only carry its id.
117-
const createdId = (data: InfomaniakRecord | string | number) =>
118-
typeof data === "object" && data !== null ? String(data.id) : String(data);
125+
const PRODUCTS_PER_PAGE = 100;
126+
127+
// The products endpoint paginates — 15 per page by default — so an account with
128+
// more domains than fit on one page would otherwise silently lose zones.
129+
const listDomainProducts = async (config: InfomaniakConfig) => {
130+
const domains: InfomaniakDomain[] = [];
131+
let page = 1;
132+
while (true) {
133+
const body = await ikRequest<InfomaniakDomain[]>(
134+
config,
135+
`/1/products?service_name=domain&page=${page}&per_page=${PRODUCTS_PER_PAGE}`,
136+
);
137+
domains.push(...(body.data ?? []));
138+
if (page >= (body.pages ?? 1)) {
139+
return domains;
140+
}
141+
page += 1;
142+
}
143+
};
119144

120145
const listZoneRecords = async (config: InfomaniakConfig, zoneId: string) =>
121146
await ikFetch<InfomaniakRecord[]>(
@@ -125,10 +150,7 @@ const listZoneRecords = async (config: InfomaniakConfig, zoneId: string) =>
125150

126151
export const infomaniakClient: DnsClient<InfomaniakConfig> = {
127152
async listZones(config) {
128-
const domains = await ikFetch<InfomaniakDomain[]>(
129-
config,
130-
"/1/product?service_name=domain",
131-
);
153+
const domains = await listDomainProducts(config);
132154
// The v2 record endpoints are keyed by zone name, not by product id.
133155
return domains.map((domain) => ({
134156
id: domain.customer_name,
@@ -172,7 +194,13 @@ export const infomaniakClient: DnsClient<InfomaniakConfig> = {
172194
`/2/zones/${zone}/records`,
173195
{ method: "POST", body },
174196
);
175-
return { id: createdId(created) };
197+
// The API returns the created record, but older responses only carry its id.
198+
return {
199+
id:
200+
typeof created === "object" && created !== null
201+
? String(created.id)
202+
: String(created),
203+
};
176204
},
177205

178206
async updateRecord(config, zoneId, recordId, record) {
@@ -193,6 +221,6 @@ export const infomaniakClient: DnsClient<InfomaniakConfig> = {
193221
},
194222

195223
async testConnection(config) {
196-
await ikFetch(config, "/1/product?service_name=domain");
224+
await ikFetch(config, "/1/products?service_name=domain&per_page=1");
197225
},
198226
};

0 commit comments

Comments
 (0)