Skip to content

Commit 1a54295

Browse files
committed
fix: handle paginated API responses and ensure array return in fetchAllResources
1 parent d33df8b commit 1a54295

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/pull.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ export async function fetchAllResources(resourceType: ResourceType): Promise<Vap
4848
throw new Error(`API GET ${endpoint} failed (${response.status}): ${errorText}`);
4949
}
5050

51-
return response.json() as Promise<VapiResource[]>;
51+
const data = await response.json();
52+
53+
// Handle paginated response format (e.g., structured-output returns { results: [], metadata: {} })
54+
if (data && typeof data === "object" && "results" in data && Array.isArray(data.results)) {
55+
return data.results as VapiResource[];
56+
}
57+
58+
return data as VapiResource[];
5259
}
5360

5461
// ─────────────────────────────────────────────────────────────────────────────
@@ -210,7 +217,13 @@ export async function pullResourceType(
210217
): Promise<PullStats> {
211218
console.log(`\n📥 Pulling ${resourceType}...`);
212219

213-
const resources = await fetchAllResources(resourceType);
220+
const resources = await fetchAllResources(resourceType) ?? [];
221+
222+
if (!Array.isArray(resources)) {
223+
console.log(` ⚠️ No ${resourceType} found (API returned non-array)`);
224+
return { created: 0, updated: 0 };
225+
}
226+
214227
console.log(` Found ${resources.length} ${resourceType} in Vapi`);
215228

216229
const reverseMap = buildReverseMap(state, resourceType);

0 commit comments

Comments
 (0)