Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"homepage": "https://github.com/contentstack/types-generator",
"devDependencies": {
"@types/async": "^3.2.24",
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.15",
"@types/node": "^20.17.17",
Expand All @@ -45,6 +46,7 @@
"dependencies": {
"@contentstack/delivery-sdk": "^4.5.0",
"@gql2ts/from-schema": "^2.0.0-4",
"async": "^3.2.6",
"axios": "^1.8.2",
"lodash": "^4.17.21",
"prettier": "^3.4.2"
Expand Down
33 changes: 32 additions & 1 deletion src/generateTS/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import async from "async";
import {flatMap, flatten} from 'lodash';
import { TOKEN_TYPE } from "../constants";
import { initializeContentstackSdk } from "../sdk/utils";
import { GenerateTS, GenerateTSFromContentTypes } from "../types";
Expand Down Expand Up @@ -41,8 +43,9 @@ export const generateTS = async ({
});

const contentTypeQuery = Stack.contentType();
contentTypeQuery._queryParams['include_count'] = 'true';
const globalFieldsQuery = Stack.globalField();
const contentTypes = await contentTypeQuery.find();
const contentTypes = await getContentTypes(contentTypeQuery);
const globalFields = await globalFieldsQuery.find();

const { content_types }: any = contentTypes;
Expand Down Expand Up @@ -163,3 +166,31 @@ export const generateTSFromContentTypes = async ({
};
}
};

const getContentTypes = async (contentTypeQuery: any) => {
try {
const limit = 100;

const results: any = await contentTypeQuery.find();

if (results?.count > limit) {
const additionalQueries = Array.from(
{ length: Math.ceil(results.count / limit) - 1 },
(_, i) => {
return async.reflect(async () => {
contentTypeQuery._queryParams['skip'] = (i + 1) * limit;
contentTypeQuery._queryParams['limit'] = limit;
return contentTypeQuery.find();
});
}
);
const additionalResults: any = (await async.parallel(additionalQueries));
const flattenedResult = additionalResults.flatMap((res: any) => res?.value?.content_types);
results.content_types = flatten([flattenedResult, results.content_types]);
}

return results;
} catch (error) {
throw error;
}
};