diff --git a/package-lock.json b/package-lock.json index 090f597..e079ff5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,11 +11,13 @@ "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" }, "devDependencies": { + "@types/async": "^3.2.24", "@types/jest": "^29.5.14", "@types/lodash": "^4.17.15", "@types/node": "^20.17.17", @@ -1815,6 +1817,12 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@types/async": { + "version": "3.2.24", + "resolved": "https://registry.npmjs.org/@types/async/-/async-3.2.24.tgz", + "integrity": "sha512-8iHVLHsCCOBKjCF2KwFe0p9Z3rfM9mL+sSP8btyR5vTjJRAqpBYD28/ZLgXPf0pjG1VxOvtCV/BgXkQbpSe8Hw==", + "dev": true + }, "node_modules/@types/babel__core": { "version": "7.20.5", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", @@ -2039,7 +2047,6 @@ "version": "3.2.6", "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==", - "dev": true, "license": "MIT" }, "node_modules/asynckit": { diff --git a/package.json b/package.json index 339c4d6..0bee2c2 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/generateTS/index.ts b/src/generateTS/index.ts index b3dfbe2..6515505 100644 --- a/src/generateTS/index.ts +++ b/src/generateTS/index.ts @@ -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"; @@ -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; @@ -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; + } +};