Skip to content

Commit 89a09c8

Browse files
authored
fix: batch requests to get app definitions in upload script [] (#2248)
1 parent 9c382c8 commit 89a09c8

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

packages/contentful--app-scripts/src/definition-api.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
import ora from 'ora';
22
import { selectFromList, throwError } from './utils';
33
import { APP_DEF_ENV_KEY } from './constants';
4-
import { ClientAPI } from 'contentful-management';
4+
import { AppDefinition, ClientAPI } from 'contentful-management';
55

66
export interface Definition {
7-
name: string,
8-
value: string,
7+
name: string;
8+
value: string;
99
}
1010

1111
async function fetchDefinitions(client: ClientAPI, orgId: string): Promise<Definition[]> {
1212
try {
1313
const organization = await client.getOrganization(orgId);
14-
const definitions = await organization.getAppDefinitions();
15-
return definitions.items.map((def) => ({
14+
15+
const batchedAppDefinitions: AppDefinition[] = [];
16+
let skip = 0;
17+
let totalNumOfAppDefinitions = 0;
18+
19+
while (skip === 0 || batchedAppDefinitions.length < totalNumOfAppDefinitions) {
20+
const appDefinitionsResponse = await organization.getAppDefinitions({ skip, limit: 100 });
21+
22+
totalNumOfAppDefinitions = appDefinitionsResponse.total;
23+
batchedAppDefinitions.push(...appDefinitionsResponse.items);
24+
25+
skip += 100;
26+
}
27+
28+
return batchedAppDefinitions.map((def) => ({
1629
name: def.name,
1730
value: def.sys.id,
1831
}));
@@ -32,7 +45,11 @@ export async function selectDefinition(client: ClientAPI, orgId: string): Promis
3245
return await selectFromList(definitions as Definition[], 'Select an app:', APP_DEF_ENV_KEY);
3346
}
3447

35-
export async function getDefinitionById(client: ClientAPI, orgId: string, defId: string): Promise<Definition> {
48+
export async function getDefinitionById(
49+
client: ClientAPI,
50+
orgId: string,
51+
defId: string
52+
): Promise<Definition> {
3653
try {
3754
const organization = await client.getOrganization(orgId);
3855
const definition = await organization.getAppDefinition(defId);

0 commit comments

Comments
 (0)