···
13
13
14
14
// Helper function modified to handle direct fetch or agent calls
15
15
// Now accepts an optional 'useDirectFetch' flag and the direct URL if needed
16
16
-
async function fetchAllPaginated(agentInstance, apiMethod, initialParams, useDirectFetch = false, directUrl = null) {
16
16
+
// And accepts apiContext and methodName for correct 'this' binding
17
17
+
async function fetchAllPaginated(apiContext, methodName, initialParams, useDirectFetch = false, directUrl = null) {
17
18
let results = [];
18
19
let cursor = initialParams.cursor;
19
20
const params = { ...initialParams }; // Copy initial params
20
21
// Determine operation name
21
21
-
const operationName = apiMethod ? (apiMethod.name.includes('bound ') ? apiMethod.name.split('bound ')[1].trim() : apiMethod.name) : (directUrl || 'directFetch');
22
22
+
const operationName = methodName || (directUrl || 'directFetch');
22
23
console.log(`fetchAllPaginated: Starting ${operationName} with initialParams:`, initialParams);
23
24
24
25
let currentUrl = directUrl; // Use direct URL if provided
···
42
43
const response = await fetch(url.toString());
43
44
if (!response.ok) throw new Error(`HTTP error ${response.status}`);
44
45
responseData = await response.json();
45
45
-
} else if (apiMethod) {
46
46
-
// Use agent method
46
46
+
} else if (apiContext && methodName) {
47
47
+
// Use agent method with correct context
47
48
if (cursor) {
48
49
params.cursor = cursor;
49
50
}
50
50
-
const response = await apiMethod(params);
51
51
+
// Call the method using the provided context
52
52
+
const response = await apiContext[methodName](params);
51
53
if (!response || !response.data) {
52
54
console.warn(`fetchAllPaginated: Invalid agent response for ${operationName}`, response);
53
55
break;
54
56
}
55
57
responseData = response.data;
56
58
} else {
57
57
-
console.error("fetchAllPaginated: Called without agent method or direct URL");
59
59
+
console.error("fetchAllPaginated: Called without apiContext/methodName or direct URL");
58
60
break;
59
61
}
60
62
···
432
434
setBulkVerifyStatus('Fetching your lists...'); // Use bulk status for list fetching message
433
435
try {
434
436
const lists = await fetchAllPaginated(
435
435
-
agent, // Pass the agent instance
436
436
-
agent.api.app.bsky.graph.getLists, // The method to call
437
437
+
agent.api.app.bsky.graph, // The context object
438
438
+
'getLists', // The method name as a string
437
439
{ actor: session.did, limit: 100 }, // Initial parameters
438
440
false // Not using direct fetch here
439
441
);
···
698
700
try {
699
701
// Fetch all items from the selected list
700
702
const listItems = await fetchAllPaginated(
701
701
-
agent,
702
702
-
agent.api.app.bsky.graph.getList,
703
703
+
agent.api.app.bsky.graph, // The context object
704
704
+
'getList', // The method name as a string
703
705
{ list: selectedListUri, limit: 100 },
704
706
false // Use agent method
705
707
);